ENH: restore single instance (#4810)

This commit is contained in:
noobydp 2024-04-20 20:05:15 +08:00 committed by GitHub
parent 1101ed9955
commit 2b4520b2df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 57 additions and 46 deletions

View file

@ -399,8 +399,8 @@ set(SLIC3R_GUI_SOURCES
GUI/ObjectDataViewModel.hpp GUI/ObjectDataViewModel.hpp
GUI/AuxiliaryDataViewModel.cpp GUI/AuxiliaryDataViewModel.cpp
GUI/AuxiliaryDataViewModel.hpp GUI/AuxiliaryDataViewModel.hpp
#GUI/InstanceCheck.cpp GUI/InstanceCheck.cpp
#GUI/InstanceCheck.hpp GUI/InstanceCheck.hpp
GUI/Search.cpp GUI/Search.cpp
GUI/Search.hpp GUI/Search.hpp
GUI/NotificationManager.cpp GUI/NotificationManager.cpp
@ -558,8 +558,8 @@ if (APPLE)
GUI/RemovableDriveManagerMM.mm GUI/RemovableDriveManagerMM.mm
GUI/RemovableDriveManagerMM.h GUI/RemovableDriveManagerMM.h
GUI/Mouse3DHandlerMac.mm GUI/Mouse3DHandlerMac.mm
#GUI/InstanceCheckMac.mm GUI/InstanceCheckMac.mm
#GUI/InstanceCheckMac.h GUI/InstanceCheckMac.h
GUI/wxMediaCtrl2.mm GUI/wxMediaCtrl2.mm
GUI/wxMediaCtrl2.h GUI/wxMediaCtrl2.h
) )

View file

@ -689,15 +689,15 @@ static void register_win32_device_notification_event()
return false; return false;
}); });
//wxWindow::MSWRegisterMessageHandler(WM_COPYDATA, [](wxWindow* win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) { wxWindow::MSWRegisterMessageHandler(WM_COPYDATA, [](wxWindow* win, WXUINT /* nMsg */, WXWPARAM wParam, WXLPARAM lParam) {
// COPYDATASTRUCT* copy_data_structure = { 0 }; COPYDATASTRUCT* copy_data_structure = { 0 };
// copy_data_structure = (COPYDATASTRUCT*)lParam; copy_data_structure = (COPYDATASTRUCT*)lParam;
// if (copy_data_structure->dwData == 1) { if (copy_data_structure->dwData == 1) {
// LPCWSTR arguments = (LPCWSTR)copy_data_structure->lpData; LPCWSTR arguments = (LPCWSTR)copy_data_structure->lpData;
// Slic3r::GUI::wxGetApp().other_instance_message_handler()->handle_message(boost::nowide::narrow(arguments)); Slic3r::GUI::wxGetApp().other_instance_message_handler()->handle_message(boost::nowide::narrow(arguments));
// } }
// return true; return true;
// }); });
} }
#endif // WIN32 #endif // WIN32
@ -1065,10 +1065,10 @@ void GUI_App::post_init()
} }
BOOST_LOG_TRIVIAL(info) << "finished post_init"; BOOST_LOG_TRIVIAL(info) << "finished post_init";
//BBS: remove the single instance currently //BBS: remove the single instance currently
/*#ifdef _WIN32 #ifdef _WIN32
// Sets window property to mainframe so other instances can indentify it. // Sets window property to mainframe so other instances can indentify it.
OtherInstanceMessageHandler::init_windows_properties(mainframe, m_instance_hash_int); OtherInstanceMessageHandler::init_windows_properties(mainframe, m_instance_hash_int);
#endif //WIN32*/ #endif //WIN32
} }
wxDEFINE_EVENT(EVT_ENTER_FORCE_UPGRADE, wxCommandEvent); wxDEFINE_EVENT(EVT_ENTER_FORCE_UPGRADE, wxCommandEvent);
@ -1086,7 +1086,7 @@ GUI_App::GUI_App()
, m_em_unit(10) , m_em_unit(10)
, m_imgui(new ImGuiWrapper()) , m_imgui(new ImGuiWrapper())
, m_removable_drive_manager(std::make_unique<RemovableDriveManager>()) , m_removable_drive_manager(std::make_unique<RemovableDriveManager>())
//, m_other_instance_message_handler(std::make_unique<OtherInstanceMessageHandler>()) , m_other_instance_message_handler(std::make_unique<OtherInstanceMessageHandler>())
{ {
//app config initializes early becasuse it is used in instance checking in OrcaSlicer.cpp //app config initializes early becasuse it is used in instance checking in OrcaSlicer.cpp
this->init_app_config(); this->init_app_config();
@ -2029,11 +2029,11 @@ std::string GUI_App::get_local_models_path()
return local_path; return local_path;
} }
/*void GUI_App::init_single_instance_checker(const std::string &name, const std::string &path) void GUI_App::init_single_instance_checker(const std::string &name, const std::string &path)
{ {
BOOST_LOG_TRIVIAL(debug) << "init wx instance checker " << name << " "<< path; BOOST_LOG_TRIVIAL(debug) << "init wx instance checker " << name << " "<< path;
m_single_instance_checker = std::make_unique<wxSingleInstanceChecker>(boost::nowide::widen(name), boost::nowide::widen(path)); m_single_instance_checker = std::make_unique<wxSingleInstanceChecker>(boost::nowide::widen(name), boost::nowide::widen(path));
}*/ }
bool GUI_App::OnInit() bool GUI_App::OnInit()
{ {
@ -2510,9 +2510,9 @@ bool GUI_App::on_init_inner()
update_mode(); // update view mode after fix of the object_list size update_mode(); // update view mode after fix of the object_list size
//#ifdef __APPLE__ #ifdef __APPLE__
// other_instance_message_handler()->bring_instance_forward(); other_instance_message_handler()->bring_instance_forward();
//#endif //__APPLE__ #endif //__APPLE__
Bind(EVT_HTTP_ERROR, &GUI_App::on_http_error, this); Bind(EVT_HTTP_ERROR, &GUI_App::on_http_error, this);
@ -5737,7 +5737,8 @@ void GUI_App::MacOpenURL(const wxString& url)
// wxWidgets override to get an event on open files. // wxWidgets override to get an event on open files.
void GUI_App::MacOpenFiles(const wxArrayString &fileNames) void GUI_App::MacOpenFiles(const wxArrayString &fileNames)
{ {
if (m_post_initialized) { bool single_instance = app_config->get("app", "single_instance") == "true";
if (m_post_initialized && !single_instance) {
bool has3mf = false; bool has3mf = false;
std::vector<wxString> names; std::vector<wxString> names;
for (auto & n : fileNames) { for (auto & n : fileNames) {

View file

@ -265,10 +265,10 @@ private:
std::unique_ptr<ImGuiWrapper> m_imgui; std::unique_ptr<ImGuiWrapper> m_imgui;
std::unique_ptr<PrintHostJobQueue> m_printhost_job_queue; std::unique_ptr<PrintHostJobQueue> m_printhost_job_queue;
//std::unique_ptr <OtherInstanceMessageHandler> m_other_instance_message_handler; std::unique_ptr <OtherInstanceMessageHandler> m_other_instance_message_handler;
//std::unique_ptr <wxSingleInstanceChecker> m_single_instance_checker; std::unique_ptr <wxSingleInstanceChecker> m_single_instance_checker;
//std::string m_instance_hash_string; std::string m_instance_hash_string;
//size_t m_instance_hash_int; size_t m_instance_hash_int;
//BBS //BBS
bool m_is_closing {false}; bool m_is_closing {false};
@ -594,13 +594,13 @@ private:
Tab* plate_tab; Tab* plate_tab;
RemovableDriveManager* removable_drive_manager() { return m_removable_drive_manager.get(); } RemovableDriveManager* removable_drive_manager() { return m_removable_drive_manager.get(); }
//OtherInstanceMessageHandler* other_instance_message_handler() { return m_other_instance_message_handler.get(); } OtherInstanceMessageHandler* other_instance_message_handler() { return m_other_instance_message_handler.get(); }
//wxSingleInstanceChecker* single_instance_checker() {return m_single_instance_checker.get();} wxSingleInstanceChecker* single_instance_checker() {return m_single_instance_checker.get();}
//void init_single_instance_checker(const std::string &name, const std::string &path); void init_single_instance_checker(const std::string &name, const std::string &path);
//void set_instance_hash (const size_t hash) { m_instance_hash_int = hash; m_instance_hash_string = std::to_string(hash); } void set_instance_hash (const size_t hash) { m_instance_hash_int = hash; m_instance_hash_string = std::to_string(hash); }
//std::string get_instance_hash_string () { return m_instance_hash_string; } std::string get_instance_hash_string () { return m_instance_hash_string; }
//size_t get_instance_hash_int () { return m_instance_hash_int; } size_t get_instance_hash_int () { return m_instance_hash_int; }
ImGuiWrapper* imgui() { return m_imgui.get(); } ImGuiWrapper* imgui() { return m_imgui.get(); }
@ -625,7 +625,7 @@ private:
int GetSingleChoiceIndex(const wxString& message, const wxString& caption, const wxArrayString& choices, int initialSelection); int GetSingleChoiceIndex(const wxString& message, const wxString& caption, const wxArrayString& choices, int initialSelection);
#ifdef __WXMSW__ #ifdef __WXMSW__
// extend is stl/3mf/gcode/step etc // extend is stl/3mf/gcode/step etc
void associate_files(std::wstring extend); void associate_files(std::wstring extend);
void disassociate_files(std::wstring extend); void disassociate_files(std::wstring extend);
#endif // __WXMSW__ #endif // __WXMSW__

View file

@ -40,14 +40,14 @@ int GUI_Run(GUI_InitParams &params)
try { try {
//GUI::GUI_App* gui = new GUI::GUI_App(params.start_as_gcodeviewer ? GUI::GUI_App::EAppMode::GCodeViewer : GUI::GUI_App::EAppMode::Editor); //GUI::GUI_App* gui = new GUI::GUI_App(params.start_as_gcodeviewer ? GUI::GUI_App::EAppMode::GCodeViewer : GUI::GUI_App::EAppMode::Editor);
GUI::GUI_App* gui = new GUI::GUI_App(); GUI::GUI_App* gui = new GUI::GUI_App();
/*if (gui->get_app_mode() != GUI::GUI_App::EAppMode::GCodeViewer) { //if (gui->get_app_mode() != GUI::GUI_App::EAppMode::GCodeViewer) {
// G-code viewer is currently not performing instance check, a new G-code viewer is started every time. // G-code viewer is currently not performing instance check, a new G-code viewer is started every time.
bool gui_single_instance_setting = gui->app_config->get("single_instance") == "1"; bool gui_single_instance_setting = gui->app_config->get("app", "single_instance") == "true";
if (Slic3r::instance_check(params.argc, params.argv, gui_single_instance_setting)) { if (Slic3r::instance_check(params.argc, params.argv, gui_single_instance_setting)) {
//TODO: do we have delete gui and other stuff? //TODO: do we have delete gui and other stuff?
return -1; return -1;
} }
//}*/ //}
// gui->autosave = m_config.opt_string("autosave"); // gui->autosave = m_config.opt_string("autosave");
GUI::GUI_App::SetInstance(gui); GUI::GUI_App::SetInstance(gui);

View file

@ -114,12 +114,12 @@ public:
OrcaSlicerTaskBarIcon(wxTaskBarIconType iconType = wxTBI_DEFAULT_TYPE) : wxTaskBarIcon(iconType) {} OrcaSlicerTaskBarIcon(wxTaskBarIconType iconType = wxTBI_DEFAULT_TYPE) : wxTaskBarIcon(iconType) {}
wxMenu *CreatePopupMenu() override { wxMenu *CreatePopupMenu() override {
wxMenu *menu = new wxMenu; wxMenu *menu = new wxMenu;
//if (wxGetApp().app_config->get("single_instance") == "false") { if (wxGetApp().app_config->get("single_instance") == "false") {
// Only allow opening a new PrusaSlicer instance on OSX if "single_instance" is disabled, // Only allow opening a new PrusaSlicer instance on OSX if "single_instance" is disabled,
// as starting new instances would interfere with the locking mechanism of "single_instance" support. // as starting new instances would interfere with the locking mechanism of "single_instance" support.
append_menu_item(menu, wxID_ANY, _L("New Window"), _L("Open a new window"), append_menu_item(menu, wxID_ANY, _L("New Window"), _L("Open a new window"),
[](wxCommandEvent&) { start_new_slicer(); }, "", nullptr); [](wxCommandEvent&) { start_new_slicer(); }, "", nullptr);
//} }
// append_menu_item(menu, wxID_ANY, _L("G-code Viewer") + dots, _L("Open G-code Viewer"), // append_menu_item(menu, wxID_ANY, _L("G-code Viewer") + dots, _L("Open G-code Viewer"),
// [](wxCommandEvent&) { start_new_gcodeviewer_open_file(); }, "", nullptr); // [](wxCommandEvent&) { start_new_gcodeviewer_open_file(); }, "", nullptr);
return menu; return menu;
@ -885,7 +885,7 @@ void MainFrame::shutdown()
// Stop the background thread of the removable drive manager, so that no new updates will be sent to the Plater. // Stop the background thread of the removable drive manager, so that no new updates will be sent to the Plater.
//wxGetApp().removable_drive_manager()->shutdown(); //wxGetApp().removable_drive_manager()->shutdown();
//stop listening for messages from other instances //stop listening for messages from other instances
//wxGetApp().other_instance_message_handler()->shutdown(this); wxGetApp().other_instance_message_handler()->shutdown(this);
// Save the slic3r.ini.Usually the ini file is saved from "on idle" callback, // Save the slic3r.ini.Usually the ini file is saved from "on idle" callback,
// but in rare cases it may not have been called yet. // but in rare cases it may not have been called yet.
if(wxGetApp().app_config->dirty()) if(wxGetApp().app_config->dirty())
@ -2180,7 +2180,7 @@ void MainFrame::init_menubar_as_editor()
// New Window // New Window
append_menu_item(fileMenu, wxID_ANY, _L("New Window"), _L("Start a new window"), append_menu_item(fileMenu, wxID_ANY, _L("New Window"), _L("Start a new window"),
[](wxCommandEvent&) { start_new_slicer(); }, "", nullptr, [](wxCommandEvent&) { start_new_slicer(); }, "", nullptr,
[]{ return true; }, this); [this] { return m_plater != nullptr && wxGetApp().app_config->get("app", "single_instance") == "false"; }, this);
#endif #endif
// New Project // New Project
append_menu_item(fileMenu, wxID_ANY, _L("New Project") + "\t" + ctrl + "N", _L("Start a new project"), append_menu_item(fileMenu, wxID_ANY, _L("New Project") + "\t" + ctrl + "N", _L("Start a new project"),

View file

@ -3092,7 +3092,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
//wxPostEvent(this->q, wxCommandEvent{EVT_RESTORE_PROJECT}); //wxPostEvent(this->q, wxCommandEvent{EVT_RESTORE_PROJECT});
} }
/*this->q->Bind(EVT_LOAD_MODEL_OTHER_INSTANCE, [this](LoadFromOtherInstanceEvent& evt) { this->q->Bind(EVT_LOAD_MODEL_OTHER_INSTANCE, [this](LoadFromOtherInstanceEvent& evt) {
BOOST_LOG_TRIVIAL(trace) << "Received load from other instance event."; BOOST_LOG_TRIVIAL(trace) << "Received load from other instance event.";
wxArrayString input_files; wxArrayString input_files;
for (size_t i = 0; i < evt.data.size(); ++i) { for (size_t i = 0; i < evt.data.size(); ++i) {
@ -3103,8 +3103,8 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
}); });
this->q->Bind(EVT_INSTANCE_GO_TO_FRONT, [this](InstanceGoToFrontEvent &) { this->q->Bind(EVT_INSTANCE_GO_TO_FRONT, [this](InstanceGoToFrontEvent &) {
bring_instance_forward(); bring_instance_forward();
});*/ });
//wxGetApp().other_instance_message_handler()->init(this->q); wxGetApp().other_instance_message_handler()->init(this->q);
// collapse sidebar according to saved value // collapse sidebar according to saved value
//if (wxGetApp().is_editor()) { //if (wxGetApp().is_editor()) {
@ -7502,10 +7502,11 @@ void Plater::priv::set_project_name(const wxString& project_name)
BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << __LINE__ << " project is:" << project_name; BOOST_LOG_TRIVIAL(trace) << __FUNCTION__ << __LINE__ << " project is:" << project_name;
m_project_name = project_name; m_project_name = project_name;
//update topbar title //update topbar title
wxGetApp().mainframe->SetTitle(m_project_name);
#ifdef __WINDOWS__ #ifdef __WINDOWS__
wxGetApp().mainframe->SetTitle(m_project_name + " - OrcaSlicer");
wxGetApp().mainframe->topbar()->SetTitle(m_project_name); wxGetApp().mainframe->topbar()->SetTitle(m_project_name);
#else #else
wxGetApp().mainframe->SetTitle(m_project_name);
if (!m_project_name.IsEmpty()) if (!m_project_name.IsEmpty())
wxGetApp().mainframe->update_title_colour_after_set_title(); wxGetApp().mainframe->update_title_colour_after_set_title();
#endif #endif
@ -8409,10 +8410,10 @@ void Plater::priv::update_after_undo_redo(const UndoRedo::Snapshot& snapshot, bo
void Plater::priv::bring_instance_forward() const void Plater::priv::bring_instance_forward() const
{ {
/*#ifdef __APPLE__ #ifdef __APPLE__
wxGetApp().other_instance_message_handler()->bring_instance_forward(); wxGetApp().other_instance_message_handler()->bring_instance_forward();
return; return;
#endif //__APPLE__*/ #endif //__APPLE__
if (main_frame == nullptr) { if (main_frame == nullptr) {
BOOST_LOG_TRIVIAL(debug) << "Couldnt bring instance forward - mainframe is null"; BOOST_LOG_TRIVIAL(debug) << "Couldnt bring instance forward - mainframe is null";
return; return;

View file

@ -1021,6 +1021,14 @@ wxWindow* PreferencesDialog::create_general_page()
std::vector<wxString> Units = {_L("Metric") + " (mm, g)", _L("Imperial") + " (in, oz)"}; std::vector<wxString> Units = {_L("Metric") + " (mm, g)", _L("Imperial") + " (in, oz)"};
auto item_currency = create_item_combobox(_L("Units"), page, _L("Units"), "use_inches", Units); auto item_currency = create_item_combobox(_L("Units"), page, _L("Units"), "use_inches", Units);
auto item_single_instance = create_item_checkbox(_L("Allow only one OrcaSlicer instance"), page,
#if __APPLE__
_L("On OSX there is always only one instance of app running by default. However it is allowed to run multiple instances "
"of same app from the command line. In such case this settings will allow only one instance."),
#else
_L("If this is enabled, when starting OrcaSlicer and another instance of the same OrcaSlicer is already running, that instance will be reactivated instead."),
#endif
50, "single_instance");
std::vector<wxString> DefaultPage = {_L("Home"), _L("Prepare")}; std::vector<wxString> DefaultPage = {_L("Home"), _L("Prepare")};
auto item_default_page = create_item_combobox(_L("Default Page"), page, _L("Set the page opened on startup."), "default_page", DefaultPage); auto item_default_page = create_item_combobox(_L("Default Page"), page, _L("Set the page opened on startup."), "default_page", DefaultPage);
@ -1096,6 +1104,7 @@ wxWindow* PreferencesDialog::create_general_page()
sizer_page->Add(item_currency, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_currency, 0, wxTOP, FromDIP(3));
sizer_page->Add(item_default_page, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_default_page, 0, wxTOP, FromDIP(3));
sizer_page->Add(item_camera_navigation_style, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_camera_navigation_style, 0, wxTOP, FromDIP(3));
sizer_page->Add(item_single_instance, 0, wxTOP, FromDIP(3));
sizer_page->Add(item_mouse_zoom_settings, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_mouse_zoom_settings, 0, wxTOP, FromDIP(3));
sizer_page->Add(item_use_free_camera_settings, 0, wxTOP, FromDIP(3)); sizer_page->Add(item_use_free_camera_settings, 0, wxTOP, FromDIP(3));
sizer_page->Add(reverse_mouse_zoom, 0, wxTOP, FromDIP(3)); sizer_page->Add(reverse_mouse_zoom, 0, wxTOP, FromDIP(3));