[gtkmm: 1/2] Gtk::Application: Only create window on first activate




commit fa92de9e8e0d565d47a8d8203a5924bc155e9691
Author: Andrew Potter <agpotter gmail com>
Date:   Sun Jun 12 10:55:52 2022 -0700

    Gtk::Application: Only create window on first activate
    
    Activate signals can arrive more than once, but creating an additional window is
    every time is not normally desired. Instead, check if the window already exists
    and simply raise() it.

 gtk/src/application.hg | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)
---
diff --git a/gtk/src/application.hg b/gtk/src/application.hg
index c9464c55..e11d5a11 100644
--- a/gtk/src/application.hg
+++ b/gtk/src/application.hg
@@ -192,14 +192,17 @@ public:
    */
   int run();
 
-  /** Starts the application, creates and shows a window.
+  /** Starts the application, creates and presents a window.
    *
    * A window of type T_Window is constructed and added to the application
    * in a signal_activate() handler. The window is deleted when it is hidden
    * or removed from the application. The method returns when the window is hidden,
    * unless other windows have been added but not removed.
    *
-   * @tparam T_Window The type of window to show. Must be Gtk::Window or a class type
+   * The window is only constructed on the first activate signal. Any activations
+   * thereafter only cause the window to be raised.
+   *
+   * @tparam T_Window The type of window to present. Must be Gtk::Window or a class type
    *                  that inherits from Gtk::Window.
    * @param argc The argc from main() (or 0 if @a argv is <tt>nullptr</tt>).
    * @param argv The argv from main(), or <tt>nullptr</tt>.
@@ -295,15 +298,20 @@ template <typename T_Window, typename... T_Args>
 int Application::make_window_and_run(int argc, char** argv, T_Args&&... args)
 {
   static_assert(std::is_base_of<Window, T_Window>::value);
-  
+
   signal_activate().connect([this, &args...] () {
-    // The created window is managed. Thus, the C++ wrapper is deleted
-    // by Gtk::Object::destroy_notify_() when the C window is destroyed.
-    // https://gitlab.gnome.org/GNOME/gtkmm/-/issues/114
-    auto window = make_managed<T_Window>(std::forward<T_Args>(args)...);
-    m_run_window = window;
-    add_window(*window);
-    window->show();
+    if (!m_run_window)
+    {
+      // The created window is managed. Thus, the C++ wrapper is deleted
+      // by Gtk::Object::destroy_notify_() when the C window is destroyed.
+      // https://gitlab.gnome.org/GNOME/gtkmm/-/issues/114
+      auto window = make_managed<T_Window>(std::forward<T_Args>(args)...);
+      m_run_window = window;
+      add_window(*window);
+      window->present();
+    } else {
+      m_run_window->present();
+    }
   });
 
   signal_window_removed().connect([this] (Window* window) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]