diff --git a/pi2-view/include/MapApp.hpp b/pi2-view/include/MapApp.hpp index bc0afad82864a260ea7da9d1a7bcf64683d53109..33df1e3c5012133f19278727ff10554287780785 100644 --- a/pi2-view/include/MapApp.hpp +++ b/pi2-view/include/MapApp.hpp @@ -37,6 +37,8 @@ public: private: static const std::vector zoomLevels; + static Glib::RefPtr app; + static std::thread thread; void addOrReplaceVehicle(const std::string &vehicleName, const std::string &laneName, double positionOnLane, double speed, double remainingFuel, double red, @@ -46,8 +48,6 @@ private: void onVehicleRowSelected(Gtk::ListBoxRow *row); void refreshVehicleLabels(const DrawableVehicle &vehicle); void refreshArea(int x, int y, int width, int height); - Glib::RefPtr app; - std::thread thread; MapArea mapArea; Gtk::Window win; Gtk::ListBox *listBox; diff --git a/pi2-view/src/MapApp.cpp b/pi2-view/src/MapApp.cpp index cc65f7713a98bc78bb03dd5ee24fb7b0869f8c29..017c7d927f490854cdf5cf4d9686bd04b48d0840 100644 --- a/pi2-view/src/MapApp.cpp +++ b/pi2-view/src/MapApp.cpp @@ -27,10 +27,11 @@ // https://developer.gnome.org/glibmm/2.55/namespaceGlib.html#a0c450f82b9e34689e2dda2038ba7834faf2d20696a8afab425c00268d981e9897 const std::vector MapApp::zoomLevels = {"60%", "80%", "100%"}; +Glib::RefPtr MapApp::app = Gtk::Application::create(); +std::thread MapApp::thread; MapApp::MapApp(std::string windowTitle) - : app(Gtk::Application::create()), - mapArea(junctions, roads, nameVehicleMap), listBox(nullptr), + : mapArea(junctions, roads, nameVehicleMap), listBox(nullptr), timeLabel(nullptr), laneLabel(nullptr), lanePositionPercentageLabel(nullptr), speedLabel(nullptr), remainingFuelLabel(nullptr), zoomComboBoxText(nullptr) { @@ -70,11 +71,15 @@ MapApp::MapApp(std::string windowTitle) viewport->add(mapArea); mapArea.show(); - thread = std::thread([this]() { - auto app = Gtk::Application::create(); - app->run(win); - app->remove_window(win); - }); + if (app->get_windows().empty()) { + thread = std::thread([this]() { + app = Gtk::Application::create(); + app->run(win); + }); + } else { + app->add_window(win); + win.show(); + } } void MapApp::onZoomEntryActivate() { @@ -110,8 +115,8 @@ void MapApp::refreshArea(int x, int y, int width, int height) { } MapApp::~MapApp() { - win.close(); - if (thread.joinable()) { + app->remove_window(win); + if (thread.joinable() && app->get_windows().empty()) { thread.join(); } }