[gtk/state-saving: 14/14] exampleapp9: Add state saving
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/state-saving: 14/14] exampleapp9: Add state saving
- Date: Thu, 3 Jun 2021 14:09:34 +0000 (UTC)
commit a7edd0992e6a888395cc1c543d9d21de9b2de7e5
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Jun 3 10:03:50 2021 -0400
exampleapp9: Add state saving
This tests the GtkApplication state saving support.
examples/application9/exampleapp.c | 10 ++++--
examples/application9/exampleappwin.c | 58 +++++++++++++++++++++++++++++++++++
examples/application9/window.ui | 6 ++++
3 files changed, 71 insertions(+), 3 deletions(-)
---
diff --git a/examples/application9/exampleapp.c b/examples/application9/exampleapp.c
index 62607e40ae..e697d9198f 100644
--- a/examples/application9/exampleapp.c
+++ b/examples/application9/exampleapp.c
@@ -59,12 +59,14 @@ example_app_startup (GApplication *app)
}
static void
-example_app_activate (GApplication *app)
+example_app_create_window (GtkApplication *app,
+ const char *save_id)
{
ExampleAppWindow *win;
win = example_app_window_new (EXAMPLE_APP (app));
- gtk_window_present (GTK_WINDOW (win));
+ gtk_widget_set_save_id (GTK_WIDGET (win), save_id);
+ /* FIXME: differentiate save ids */
}
static void
@@ -93,8 +95,8 @@ static void
example_app_class_init (ExampleAppClass *class)
{
G_APPLICATION_CLASS (class)->startup = example_app_startup;
- G_APPLICATION_CLASS (class)->activate = example_app_activate;
G_APPLICATION_CLASS (class)->open = example_app_open;
+ GTK_APPLICATION_CLASS (class)->create_window = example_app_create_window;
}
ExampleApp *
@@ -103,5 +105,7 @@ example_app_new (void)
return g_object_new (EXAMPLE_APP_TYPE,
"application-id", "org.gtk.exampleapp",
"flags", G_APPLICATION_HANDLES_OPEN,
+ "register-session", TRUE,
+ "save-state", TRUE,
NULL);
}
diff --git a/examples/application9/exampleappwin.c b/examples/application9/exampleappwin.c
index 0edfa2cea1..1283405b21 100644
--- a/examples/application9/exampleappwin.c
+++ b/examples/application9/exampleappwin.c
@@ -17,6 +17,8 @@ struct _ExampleAppWindow
GtkWidget *words;
GtkWidget *lines;
GtkWidget *lines_label;
+
+ GList *files;
};
G_DEFINE_TYPE (ExampleAppWindow, example_app_window, GTK_TYPE_APPLICATION_WINDOW)
@@ -211,14 +213,68 @@ example_app_window_dispose (GObject *object)
g_clear_object (&win->settings);
+ g_list_free_full (win->files, g_object_unref);
+ win->files = NULL;
+
G_OBJECT_CLASS (example_app_window_parent_class)->dispose (object);
}
+static gboolean
+example_app_window_save_state (GtkWidget *widget,
+ GVariantDict *dict,
+ gboolean *save_children)
+{
+ ExampleAppWindow *win = EXAMPLE_APP_WINDOW (widget);
+ GVariantBuilder builder;
+
+ g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
+ for (GList *l = win->files; l; l = l->next)
+ {
+ g_variant_builder_add (&builder, "s", g_file_peek_path (G_FILE (l->data)));
+ }
+
+ g_variant_dict_insert_value (dict, "files", g_variant_builder_end (&builder));
+
+ /* Save window state */
+ GTK_WIDGET_CLASS (example_app_window_parent_class)->save_state (widget, dict, save_children);
+
+ *save_children = TRUE;
+ return TRUE;
+}
+
+static gboolean
+example_app_window_restore_state (GtkWidget *widget,
+ GVariant *state)
+{
+ ExampleAppWindow *win = EXAMPLE_APP_WINDOW (widget);
+ GVariantIter *iter;
+
+ /* Restore window state */
+ GTK_WIDGET_CLASS (example_app_window_parent_class)->restore_state (widget, state);
+
+ if (g_variant_lookup (state, "files", "as", &iter))
+ {
+ const char *path;
+
+ while (g_variant_iter_next (iter, "&s", &path))
+ {
+ GFile *file = g_file_new_for_path (path);
+ example_app_window_open (win, file);
+ g_object_unref (file);
+ }
+ }
+
+ return TRUE;
+}
+
static void
example_app_window_class_init (ExampleAppWindowClass *class)
{
G_OBJECT_CLASS (class)->dispose = example_app_window_dispose;
+ GTK_WIDGET_CLASS (class)->save_state = example_app_window_save_state;
+ GTK_WIDGET_CLASS (class)->restore_state = example_app_window_restore_state;
+
gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
"/org/gtk/exampleapp/window.ui");
@@ -288,4 +344,6 @@ example_app_window_open (ExampleAppWindow *win,
update_words (win);
update_lines (win);
+
+ win->files = g_list_append (win->files, g_object_ref (file));
}
diff --git a/examples/application9/window.ui b/examples/application9/window.ui
index b0f9932b03..1c0d07cc95 100644
--- a/examples/application9/window.ui
+++ b/examples/application9/window.ui
@@ -4,6 +4,7 @@
<property name="title" translatable="yes">Example Application</property>
<property name="default-width">600</property>
<property name="default-height">400</property>
+ <property name="save-id">window</property>
<child type="titlebar">
<object class="GtkHeaderBar" id="header">
<child>
@@ -26,6 +27,7 @@
<object class="GtkToggleButton" id="search">
<property name="sensitive">0</property>
<property name="icon-name">edit-find-symbolic</property>
+ <property name="save-id">search</property>
</object>
</child>
<child type="end">
@@ -38,6 +40,7 @@
<child>
<object class="GtkBox" id="content_box">
<property name="orientation">vertical</property>
+ <property name="save-id">content_box</property>
<child>
<object class="GtkSearchBar" id="searchbar">
<child>
@@ -49,8 +52,10 @@
</child>
<child>
<object class="GtkBox" id="hbox">
+ <property name="save-id">hbox</property>
<child>
<object class="GtkRevealer" id="sidebar">
+ <property name="save-id">sidebar</property>
<property name="transition-type">slide-right</property>
<child>
<object class="GtkScrolledWindow" id="sidebar-sw">
@@ -66,6 +71,7 @@
</child>
<child>
<object class="GtkStack" id="stack">
+ <property name="save-id">stack</property>
<signal name="notify::visible-child" handler="visible_child_changed"/>
</object>
</child>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]