[gnome-text-editor] window: inhibit logout while documents are modified



commit 5969eb03e9d94a6b9e3b3ade89571b59ef705cbe
Author: Christian Hergert <chergert redhat com>
Date:   Fri May 20 12:54:52 2022 -0700

    window: inhibit logout while documents are modified
    
    Fixes #378

 src/editor-window-private.h |  2 ++
 src/editor-window.c         | 73 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
---
diff --git a/src/editor-window-private.h b/src/editor-window-private.h
index 5ddd055..9e7077e 100644
--- a/src/editor-window-private.h
+++ b/src/editor-window-private.h
@@ -70,6 +70,8 @@ struct _EditorWindow
   /* Used to update "Document Type: Markdown" */
   GMenuModel           *doc_type_menu;
   guint                 doc_type_index;
+
+  guint                 inhibit_cookie;
 };
 
 
diff --git a/src/editor-window.c b/src/editor-window.c
index 3e8f5cb..f7f6202 100644
--- a/src/editor-window.c
+++ b/src/editor-window.c
@@ -778,6 +778,13 @@ editor_window_dispose (GObject *object)
   editor_signal_group_set_target (self->page_signals, NULL);
   editor_signal_group_set_target (self->document_signals, NULL);
 
+  if (self->inhibit_cookie != 0)
+    {
+      gtk_application_uninhibit (GTK_APPLICATION (EDITOR_APPLICATION_DEFAULT),
+                                 self->inhibit_cookie);
+      self->inhibit_cookie = 0;
+    }
+
   G_OBJECT_CLASS (editor_window_parent_class)->dispose (object);
 }
 
@@ -1089,6 +1096,58 @@ modified_to_icon (GBinding     *binding,
   return TRUE;
 }
 
+static void
+editor_window_update_inhibit (EditorWindow *self)
+{
+  gboolean inhibit = FALSE;
+  guint n_pages;
+
+  g_assert (EDITOR_IS_WINDOW (self));
+
+  n_pages = editor_window_get_n_pages (self);
+
+  for (guint i = 0; i < n_pages; i++)
+    {
+      EditorPage *page = editor_window_get_nth_page (self, i);
+
+      if (editor_page_get_is_modified (page))
+        {
+          inhibit = TRUE;
+          break;
+        }
+    }
+
+  if ((inhibit && self->inhibit_cookie != 0) ||
+      (!inhibit && self->inhibit_cookie == 0))
+    return;
+
+  if (inhibit)
+    {
+      self->inhibit_cookie =
+        gtk_application_inhibit (GTK_APPLICATION (EDITOR_APPLICATION_DEFAULT),
+                                 GTK_WINDOW (self),
+                                 GTK_APPLICATION_INHIBIT_LOGOUT,
+                                 _("There are unsaved documents"));
+    }
+  else
+    {
+      gtk_application_uninhibit (GTK_APPLICATION (EDITOR_APPLICATION_DEFAULT),
+                                 self->inhibit_cookie);
+      self->inhibit_cookie = 0;
+    }
+}
+
+static void
+page_notify_is_modified_cb (EditorWindow *self,
+                            GParamSpec   *pspec,
+                            EditorPage   *page)
+{
+  g_assert (EDITOR_IS_WINDOW (self));
+  g_assert (EDITOR_IS_PAGE (page));
+
+  editor_window_update_inhibit (self);
+}
+
 void
 _editor_window_add_page (EditorWindow *self,
                          EditorPage   *page)
@@ -1109,7 +1168,15 @@ _editor_window_add_page (EditorWindow *self,
                                modified_to_icon, NULL,
                                NULL, NULL);
 
+  g_signal_connect_object (page,
+                           "notify::is-modified",
+                           G_CALLBACK (page_notify_is_modified_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   adw_tab_view_set_selected_page (self->tab_view, tab_page);
+
+  editor_window_update_inhibit (self);
 }
 
 void
@@ -1121,6 +1188,10 @@ _editor_window_remove_page (EditorWindow *self,
   g_return_if_fail (EDITOR_IS_WINDOW (self));
   g_return_if_fail (EDITOR_IS_PAGE (page));
 
+  g_signal_handlers_disconnect_by_func (page,
+                                        G_CALLBACK (page_notify_is_modified_cb),
+                                        self);
+
   tab_page = adw_tab_view_get_page (self->tab_view, GTK_WIDGET (page));
   adw_tab_view_close_page (self->tab_view, tab_page);
 
@@ -1131,6 +1202,8 @@ _editor_window_remove_page (EditorWindow *self,
       if (self->visible_page != NULL)
         editor_page_grab_focus (self->visible_page);
     }
+
+  editor_window_update_inhibit (self);
 }
 
 /**


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