Skip to content

Commit

Permalink
Shell: Local servers menu
Browse files Browse the repository at this point in the history
Main menu has a submenu for the currently running local servers.

If garbage is not recycled regularly, popups will not be disposed and the menu will refuse to open a new menu. The application has responsibility to do recycling.
  • Loading branch information
skyjake committed Sep 22, 2019
1 parent 6c848f6 commit e95dc44
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 21 deletions.
76 changes: 60 additions & 16 deletions doomsday/tools/shell/src/guishellapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
#include <de/Config>
#include <de/EscapeParser>
#include <de/FileSystem>
#include <de/Garbage>
#include <de/Id>
#include <de/PackageLoader>
#include <de/ServerFinder>
#include <de/TextValue>
#include <de/Timer>
#include <de/WindowSystem>
#include <doomsday/network/LocalServer>
Expand All @@ -40,13 +42,14 @@ using namespace network;

DE_PIMPL(GuiShellApp)
, DE_OBSERVES(ServerFinder, Update)
, DE_OBSERVES(GuiLoop, Iteration)
{
ServerFinder finder;

ImageBank imageBank;
// QMenuBar *menuBar;
// QMenu *localMenu;
PopupMenuWidget *localMenu;
// PopupMenuWidget *localMenu;
#ifdef MACOSX
// QAction *stopAction;
// QAction *disconnectAction;
Expand All @@ -64,26 +67,62 @@ DE_PIMPL(GuiShellApp)
localCheckTimer.setSingleShot(false);

finder.audienceForUpdate() += this;

GuiLoop::get().audienceForIteration() += this;
}

~Impl() override
{
self().glDeinit();
}

void loopIteration() override
{
Garbage_Recycle();
}

void foundServersUpdated() override
{
DE_ASSERT(inMainThread());

const auto found = finder.foundServers();
const auto found =
map<StringList>(finder.foundServers(), [](const Address &a) { return a.asText(); });

// Add new servers.
for (const auto &sv : found)
{

if (localServerMenuItems.findData(TextValue(sv)) == ui::Data::InvalidPos)
{
const String address = sv;

auto *item = new ui::ActionItem(
ui::Item::ShownAsButton | ui::Item::ActivationClosesPopup |
ui::Item::ClosesParentPopup,
sv,
[address]() {
if (auto *win = GuiShellApp::app().newOrReusedConnectionWindow())
{
win->openConnection(address);
}
});

item->setData(TextValue(sv));
localServerMenuItems << item;
}
}

// Remove servers that are not present.
for (auto i = localServerMenuItems.begin(); i != localServerMenuItems.end(); )
{
if (found.indexOf((*i)->data().asText()) < 0)
{
auto *item = *i;
i = localServerMenuItems.erase(i);
delete item;
continue;
}
i++;
}
}

void loadAllShaders()
Expand Down Expand Up @@ -237,10 +276,10 @@ GuiShellApp &GuiShellApp::app()
return *static_cast<GuiShellApp *>(DE_BASE_GUI_APP);
}

PopupMenuWidget &GuiShellApp::localServersMenu()
{
return *d->localMenu;
}
//PopupMenuWidget &GuiShellApp::localServersMenu()
//{
// return *d->localMenu;
//}

//QMenu *GuiShellApp::makeHelpMenu()
//{
Expand Down Expand Up @@ -343,8 +382,8 @@ void GuiShellApp::startLocalServer()
}
}

void GuiShellApp::updateLocalServerMenu()
{
//void GuiShellApp::updateLocalServerMenu()
//{
// d->localMenu->setDisabled(d->finder.foundServers().isEmpty());
// d->localMenu->clear();

Expand All @@ -359,7 +398,7 @@ void GuiShellApp::updateLocalServerMenu()
// QAction *act = d->localMenu->addAction(label, this, SLOT(connectToLocalServer()));
// act->setData(QVariant::fromValue(host));
// }
}
//}

void GuiShellApp::aboutShell()
{
Expand Down Expand Up @@ -403,15 +442,15 @@ void GuiShellApp::showPreferences()
// }
}

void GuiShellApp::updateMenu()
{
#ifdef MACOSX
//void GuiShellApp::updateMenu()
//{
//#ifdef MACOSX
// LinkWindow *win = dynamic_cast<LinkWindow *>(activeWindow());
// d->stopAction->setEnabled(win && win->isConnected());
// d->disconnectAction->setEnabled(win && win->isConnected());
#endif
updateLocalServerMenu();
}
//#endif
// updateLocalServerMenu();
//}

void GuiShellApp::checkLocalServers()
{
Expand Down Expand Up @@ -443,3 +482,8 @@ ImageBank &GuiShellApp::imageBank()
{
return app().d->imageBank;
}

const GuiShellApp::MenuItems &GuiShellApp::localServerMenuItems() const
{
return d->localServerMenuItems;
}
11 changes: 8 additions & 3 deletions doomsday/tools/shell/src/guishellapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@
#include <de/ImageBank>
#include <de/PopupMenuWidget>
#include <de/ServerFinder>
#include <de/ui/ListData>
#include <de/ui/ActionItem>

class LinkWindow;

class GuiShellApp : public de::BaseGuiApp
{
public:
using MenuItems = de::ui::ListDataT<de::ui::ActionItem>;

public:
GuiShellApp(const de::StringList &args);

Expand All @@ -40,20 +45,20 @@ class GuiShellApp : public de::BaseGuiApp

static GuiShellApp &app();
static de::ImageBank &imageBank();
de::PopupMenuWidget &localServersMenu();
// de::PopupMenuWidget &localServersMenu();
// de::PopupMenuWidget *makeHelpMenu();
const MenuItems &localServerMenuItems() const;

void connectToServer();
void connectToLocalServer();
void disconnectFromServer();
void closeActiveWindow();
void startLocalServer();
void updateLocalServerMenu();
void aboutShell();
void showHelp();
void openWebAddress(const de::String &address);
void showPreferences();
void updateMenu();
// void updateMenu();

DE_AUDIENCE(LocalServerStop, void localServerStopped(int port))

Expand Down
10 changes: 8 additions & 2 deletions doomsday/tools/shell/src/linkwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <de/PopupMenuWidget>
#include <de/SequentialLayout>
#include <de/StyledLogSinkFormatter>
#include <de/ui/SubwidgetItem>
#include <de/TabWidget>
#include <de/Timer>
#include <de/term/CommandLineWidget>
Expand Down Expand Up @@ -263,6 +264,11 @@ DE_PIMPL(LinkWindow)
<< new ui::Item(ui::Item::Separator)
<< new ui::ActionItem("New Local Server...", []() { GuiShellApp::app().startLocalServer(); })
<< new ui::ActionItem("Stop Server", [this]() { self().stopServer(); })
<< new ui::SubwidgetItem("Local Servers", ui::Left, []() -> PopupWidget * {
auto *pop = new PopupMenuWidget;
pop->menu().setItems(GuiShellApp::app().localServerMenuItems());
return pop;
})
<< new ui::Item(ui::Item::Separator)
<< new ui::ActionItem("Preferences...", []() { GuiShellApp::app().showPreferences(); })
<< new ui::ActionItem("Help...", [](){ GuiShellApp::app().showHelp(); })
Expand Down Expand Up @@ -812,8 +818,8 @@ void LinkWindow::openConnection(const String &address)
debug("Opening connection to %s", address.c_str());

// Keep trying to connect to 30 seconds.
const String addr{address};
openConnection(new network::Link(addr, 30.0_s), addr);
// const String addr{address};
openConnection(new network::Link(address, 30.0_s), address);
}

void LinkWindow::closeConnection()
Expand Down

0 comments on commit e95dc44

Please sign in to comment.