[gnome-text-editor] window: unify page closing handling
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-text-editor] window: unify page closing handling
- Date: Sun, 1 Aug 2021 16:50:53 +0000 (UTC)
commit 71a6aff4840f5f617a08e51bb1c5958e1d757888
Author: Christian Hergert <chergert redhat com>
Date: Sun Aug 1 09:50:46 2021 -0700
window: unify page closing handling
This makes sure that ctrl+w and close-page signal via clicking the X result
in the same process flow.
I thought there might be an issue with editor_session_remove_page() not
being called, but it seems to work itself out correctly anyway.
Fixes #118
src/editor-window-actions.c | 49 ++++------------------------
src/editor-window-private.h | 2 ++
src/editor-window.c | 79 +++++++++++++++++++++++++++++++++++++++++++++
src/editor-window.ui | 1 +
4 files changed, 88 insertions(+), 43 deletions(-)
---
diff --git a/src/editor-window-actions.c b/src/editor-window-actions.c
index 6e58c4f..c38ad6c 100644
--- a/src/editor-window-actions.c
+++ b/src/editor-window-actions.c
@@ -46,63 +46,26 @@ editor_window_actions_new_draft_cb (GtkWidget *widget,
editor_session_add_draft (session, self);
}
-static void
-editor_window_actions_close_page_confirm_cb (GObject *object,
- GAsyncResult *result,
- gpointer user_data)
-{
- g_autoptr(GPtrArray) pages = user_data;
- g_autoptr(GError) error = NULL;
- EditorSession *session;
-
- g_assert (pages != NULL);
- g_assert (pages->len > 0);
-
- if (!_editor_save_changes_dialog_run_finish (result, &error))
- {
- g_debug ("Failed to run dialog: %s", error->message);
- return;
- }
-
- session = editor_application_get_session (EDITOR_APPLICATION_DEFAULT);
-
- for (guint i = 0; i < pages->len; i++)
- editor_session_remove_page (session, g_ptr_array_index (pages, i));
-}
-
static void
editor_window_actions_close_page_cb (GtkWidget *widget,
const char *action_name,
GVariant *param)
{
EditorWindow *self = (EditorWindow *)widget;
- EditorSession *session;
EditorPage *page;
g_assert (EDITOR_IS_WINDOW (self));
- session = editor_application_get_session (EDITOR_APPLICATION_DEFAULT);
-
- if ((page = editor_window_get_visible_page (self)))
+ if ((page = editor_window_get_visible_page (self)) &&
+ _editor_window_request_close_page (self, page))
{
- /* If this document has changes, then request the user to save them. */
- if (editor_page_get_is_modified (page))
- {
- g_autoptr(GPtrArray) pages = g_ptr_array_new_with_free_func (g_object_unref);
- g_ptr_array_add (pages, g_object_ref (page));
- _editor_save_changes_dialog_run_async (GTK_WINDOW (self),
- pages,
- NULL,
- editor_window_actions_close_page_confirm_cb,
- g_ptr_array_ref (pages));
- return;
- }
+ EditorSession *session = editor_application_get_session (EDITOR_APPLICATION_DEFAULT);
editor_session_remove_page (session, page);
- }
- if (!(page = editor_window_get_visible_page (self)))
- gtk_window_close (GTK_WINDOW (self));
+ if (editor_window_get_visible_page (self) == NULL)
+ gtk_window_close (GTK_WINDOW (self));
+ }
}
static void
diff --git a/src/editor-window-private.h b/src/editor-window-private.h
index a60a588..5235196 100644
--- a/src/editor-window-private.h
+++ b/src/editor-window-private.h
@@ -77,5 +77,7 @@ void _editor_window_add_page (EditorWindow *self,
void _editor_window_remove_page (EditorWindow *self,
EditorPage *page);
void _editor_window_focus_search (EditorWindow *self);
+gboolean _editor_window_request_close_page (EditorWindow *self,
+ EditorPage *page);
G_END_DECLS
diff --git a/src/editor-window.c b/src/editor-window.c
index 461a147..08c411f 100644
--- a/src/editor-window.c
+++ b/src/editor-window.c
@@ -336,6 +336,82 @@ editor_window_constructed (GObject *object)
}
}
+static void
+editor_window_actions_close_page_confirm_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ g_autoptr(GPtrArray) pages = user_data;
+ g_autoptr(GError) error = NULL;
+ EditorWindow *self;
+ gboolean confirm_close = TRUE;
+
+ g_assert (pages != NULL);
+ g_assert (pages->len > 0);
+
+ if (!_editor_save_changes_dialog_run_finish (result, &error))
+ {
+ g_debug ("Failed to run dialog: %s", error->message);
+ confirm_close = FALSE;
+ }
+
+ self = EDITOR_WINDOW (gtk_window_get_transient_for (GTK_WINDOW (object)));
+
+ for (guint i = 0; i < pages->len; i++)
+ {
+ EditorPage *epage = g_ptr_array_index (pages, i);
+ AdwTabPage *page = adw_tab_view_get_page (self->tab_view, GTK_WIDGET (epage));
+
+ adw_tab_view_close_page_finish (self->tab_view, page, confirm_close);
+ }
+}
+
+gboolean
+_editor_window_request_close_page (EditorWindow *self,
+ EditorPage *page)
+{
+ g_return_val_if_fail (EDITOR_IS_WINDOW (self), FALSE);
+ g_return_val_if_fail (EDITOR_IS_PAGE (page), FALSE);
+
+ /* If this document has changes, then request the user to save them. */
+ if (editor_page_get_is_modified (page))
+ {
+ g_autoptr(GPtrArray) pages = g_ptr_array_new_with_free_func (g_object_unref);
+ g_ptr_array_add (pages, g_object_ref (page));
+ _editor_save_changes_dialog_run_async (GTK_WINDOW (self),
+ pages,
+ NULL,
+ editor_window_actions_close_page_confirm_cb,
+ g_ptr_array_ref (pages));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+on_tab_view_close_page_cb (EditorWindow *self,
+ AdwTabPage *page,
+ AdwTabView *view)
+{
+ EditorPage *epage;
+
+ g_assert (EDITOR_IS_WINDOW (self));
+ g_assert (ADW_IS_TAB_PAGE (page));
+ g_assert (ADW_IS_TAB_VIEW (view));
+
+ if (page != adw_tab_view_get_selected_page (view))
+ adw_tab_view_set_selected_page (view, page);
+
+ if ((epage = EDITOR_PAGE (adw_tab_page_get_child (page))))
+ {
+ if (_editor_window_request_close_page (self, epage))
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
static void
editor_window_dispose (GObject *object)
{
@@ -431,6 +507,7 @@ editor_window_class_init (EditorWindowClass *klass)
g_object_class_install_properties (object_class, N_PROPS, properties);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/TextEditor/ui/editor-window.ui");
+
gtk_widget_class_bind_template_child (widget_class, EditorWindow, empty);
gtk_widget_class_bind_template_child (widget_class, EditorWindow, is_modified);
gtk_widget_class_bind_template_child (widget_class, EditorWindow, open_menu_button);
@@ -448,6 +525,8 @@ editor_window_class_init (EditorWindowClass *klass)
gtk_widget_class_bind_template_child (widget_class, EditorWindow, tab_view);
gtk_widget_class_bind_template_child (widget_class, EditorWindow, title);
+ gtk_widget_class_bind_template_callback (widget_class, on_tab_view_close_page_cb);
+
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_w, GDK_CONTROL_MASK,
"win.close-page-or-window", NULL);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_o, GDK_CONTROL_MASK, "win.open", NULL);
gtk_widget_class_add_binding_action (widget_class, GDK_KEY_k, GDK_CONTROL_MASK, "win.focus-search", NULL);
diff --git a/src/editor-window.ui b/src/editor-window.ui
index 2b8a125..dfe9d0f 100644
--- a/src/editor-window.ui
+++ b/src/editor-window.ui
@@ -131,6 +131,7 @@
<object class="AdwTabView" id="tab_view">
<property name="hexpand">true</property>
<property name="vexpand">true</property>
+ <signal name="close-page" handler="on_tab_view_close_page_cb" swapped="true"/>
</object>
</child>
</object>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]