From 58fee96e30a3a2d0ec762490739ea696480ec6df Mon Sep 17 00:00:00 2001 From: SoftFever Date: Sat, 31 Oct 2026 00:15:14 +0800 Subject: [PATCH] Keep the Run Script dialog in front after its file picker closes on macOS --- .../orca-wxwidgets/references/windows-dialogs.md | 12 +++++++----- src/slic3r/GUI/PluginsDialog.cpp | 10 ---------- src/slic3r/GUI/PluginsDialog.hpp | 5 ----- src/slic3r/GUI/ScriptRunnerDialog.cpp | 9 ++++++++- src/slic3r/GUI/Widgets/WebViewHostDialog.cpp | 10 ++++++++++ src/slic3r/GUI/Widgets/WebViewHostDialog.hpp | 5 +++++ 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/.claude/skills/orca-wxwidgets/references/windows-dialogs.md b/.claude/skills/orca-wxwidgets/references/windows-dialogs.md index ff8a858e24..25f6505219 100644 --- a/.claude/skills/orca-wxwidgets/references/windows-dialogs.md +++ b/.claude/skills/orca-wxwidgets/references/windows-dialogs.md @@ -496,16 +496,18 @@ name — for any other window call `window_pos_save/restore` with its own name. through `EndModal`. The `Raise()` is deferred to run after the modal has fully torn down, and guarded because the dialog may be destroyed while queued. ```cpp - void PluginsDialog::restore_z_order() + void WebViewHostDialog::restore_z_order() { - wxGetApp().CallAfter([this, alive = m_alive]() { - if (alive->load(std::memory_order_acquire) && IsShown()) + wxGetApp().CallAfter([this, alive = m_host_alive] { + if (alive->load() && IsShown()) Raise(); }); } ``` - Cite: 0a0d59b76b (`PluginsDialog::restore_z_order`; also passed as the `restore` callback of - `detail::run_off_thread_with_progress`). + A `WebViewHostDialog` subclass calls the inherited `restore_z_order()` after each such modal, on every + outcome including Cancel. + Cite: 0a0d59b76b (`PluginsDialog`: also passed as the `restore` callback of + `detail::run_off_thread_with_progress`); `ScriptRunnerDialog::open_file`. - **Rule:** Never position windows by absolute coordinates on Wayland, and never expect `SetIcon` to show on macOS or Wayland. diff --git a/src/slic3r/GUI/PluginsDialog.cpp b/src/slic3r/GUI/PluginsDialog.cpp index dcccfc3cb9..6703ba71d3 100644 --- a/src/slic3r/GUI/PluginsDialog.cpp +++ b/src/slic3r/GUI/PluginsDialog.cpp @@ -1019,16 +1019,6 @@ void PluginsDialog::handle_plugin_menu_action(const std::string& plugin_key, con } } -void PluginsDialog::restore_z_order() -{ - // Deferred so it runs on a clean stack after the modal has fully torn down; the - // alive guard covers the dialog being destroyed while the CallAfter is queued. - wxGetApp().CallAfter([this, alive = m_alive]() { - if (alive->load(std::memory_order_acquire) && IsShown()) - Raise(); - }); -} - void PluginsDialog::install_plugin_from_file() { wxFileDialog dialog(this, _L("Select plugin package"), wxEmptyString, wxEmptyString, _L("Plugin files (*.py;*.whl)|*.py;*.whl"), diff --git a/src/slic3r/GUI/PluginsDialog.hpp b/src/slic3r/GUI/PluginsDialog.hpp index 095334f928..f7eca71fd3 100644 --- a/src/slic3r/GUI/PluginsDialog.hpp +++ b/src/slic3r/GUI/PluginsDialog.hpp @@ -241,11 +241,6 @@ private: nlohmann::json localized_strings() const override; // Runs one web command on a clean main-loop stack; see on_script_message. void handle_web_command(const nlohmann::json& payload); - // Re-raises this dialog after a transient modal it opened (file dialog, message box, - // progress dialog). Native macOS panels end by re-activating the app's main window - // (the mainframe) instead of this webview-hosting dialog, burying it; wx only - // compensates for generic wxDialog modals (wxDialog::EndModal raises the parent). - void restore_z_order(); void send_plugins(); void set_plugin_sort(const std::string& sort_key, const std::string& sort_order); diff --git a/src/slic3r/GUI/ScriptRunnerDialog.cpp b/src/slic3r/GUI/ScriptRunnerDialog.cpp index 9c89041edd..550c2d08cd 100644 --- a/src/slic3r/GUI/ScriptRunnerDialog.cpp +++ b/src/slic3r/GUI/ScriptRunnerDialog.cpp @@ -344,7 +344,11 @@ void ScriptRunnerDialog::open_file() { wxFileDialog dialog(this, _L("Open a script"), wxEmptyString, wxEmptyString, format_wxstr("%1% (*.json)|*.json", _L("Script files")), wxFD_OPEN | wxFD_FILE_MUST_EXIST); - if (dialog.ShowModal() != wxID_OK || !m_alive->load()) + const int result = dialog.ShowModal(); + if (!m_alive->load()) + return; + restore_z_order(); + if (result != wxID_OK) return; const fs::path path = into_path(dialog.GetPath()); boost::nowide::ifstream in(Mcp::path_to_utf8(path).c_str(), std::ios::binary); @@ -519,6 +523,9 @@ void ScriptRunnerDialog::run(const nlohmann::json& payload) return; if (answer == wxID_YES) { if (plater->save_project() != wxID_YES) { + // Saving a new project asks for a file name in a native panel, which leaves the + // main window in front; the run isn't going ahead, so bring this dialog back. + restore_z_order(); refuse({}); return; } diff --git a/src/slic3r/GUI/Widgets/WebViewHostDialog.cpp b/src/slic3r/GUI/Widgets/WebViewHostDialog.cpp index 2582347202..5890aa3f88 100644 --- a/src/slic3r/GUI/Widgets/WebViewHostDialog.cpp +++ b/src/slic3r/GUI/Widgets/WebViewHostDialog.cpp @@ -250,6 +250,16 @@ void WebViewHostDialog::call_web_handler(const nlohmann::json& payload, const wx }); } +void WebViewHostDialog::restore_z_order() +{ + // Deferred so it runs on a clean stack after the modal has fully torn down; the + // alive guard covers the dialog being destroyed while the CallAfter is queued. + wxGetApp().CallAfter([this, alive = m_host_alive] { + if (alive->load() && IsShown()) + Raise(); + }); +} + WebViewHostDialog::~WebViewHostDialog() { m_host_alive->store(false); } bool WebViewHostDialog::handle_common_script_command(const nlohmann::json& payload, int close_return_code) diff --git a/src/slic3r/GUI/Widgets/WebViewHostDialog.hpp b/src/slic3r/GUI/Widgets/WebViewHostDialog.hpp index abee743bb2..27a909b1a5 100644 --- a/src/slic3r/GUI/Widgets/WebViewHostDialog.hpp +++ b/src/slic3r/GUI/Widgets/WebViewHostDialog.hpp @@ -78,6 +78,11 @@ protected: wxString build_resource_url(const std::string& resource_path) const; bool handle_common_script_command(const nlohmann::json& payload, int close_return_code = wxID_CANCEL); + // Re-raises this dialog after a transient modal it opened (file dialog, message box, + // progress dialog). Native macOS panels end by re-activating the app's main window + // (the mainframe) instead of this webview-hosting dialog, burying it; wx only + // compensates for generic wxDialog modals (wxDialog::EndModal raises the parent). + void restore_z_order(); void on_dpi_changed(const wxRect& suggested_rect) override;