Jelajahi Sumber

Remember application window size

Matthias Vogelgesang 7 tahun lalu
induk
melakukan
4c11ca92ba
4 mengubah file dengan 61 tambahan dan 9 penghapusan
  1. 19 0
      data/net.bloerg.Iridium.gschema.xml
  2. 38 0
      src/iridium-window.c
  3. 2 2
      src/iridium-window.ui
  4. 2 7
      src/main.c

+ 19 - 0
data/net.bloerg.Iridium.gschema.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <schemalist gettext-domain="iridium">
+
   <schema id="net.bloerg.iridium.preferences" path="/net/bloerg/iridium/preferences/">
     <key name="server" type="s">
       <default>""</default>
@@ -16,4 +17,22 @@
       </description>
     </key>
   </schema>
+
+  <schema id="net.bloerg.iridium.window-state" path="/net/bloerg/iridium/window-state/">
+    <key name="width" type="i">
+      <default>890</default>
+      <summary>Width of the window</summary>
+      <description>
+        The initial width of the application window.
+      </description>
+    </key>
+    <key name="height" type="i">
+      <default>550</default>
+      <summary>Height of the window</summary>
+      <description>
+        The initial height of the application window.
+      </description>
+    </key>
+  </schema>
+
 </schemalist>

+ 38 - 0
src/iridium-window.c

@@ -33,7 +33,11 @@ struct _IridiumWindow
 {
   GtkApplicationWindow  parent_instance;
 
+  gint               width;
+  gint               height;
+
   GSettings         *preferences;
+  GSettings         *window_state;
 
   GtkHeaderBar      *header_bar;
   GtkListBox        *tag_list;
@@ -331,6 +335,22 @@ on_show (IridiumWindow *self, gpointer user_data)
   g_free (email);
 }
 
+static void
+on_size_allocate (IridiumWindow *self,
+                  GtkAllocation *allocation,
+                  gpointer user_data)
+{
+  gtk_window_get_size (GTK_WINDOW (self), &self->width, &self->height);
+}
+
+static void
+on_destroy (IridiumWindow *self,
+            gpointer user_data)
+{
+  g_settings_set_int (self->window_state, "width", self->width);
+  g_settings_set_int (self->window_state, "height", self->height);
+}
+
 static void
 on_notification_close_clicked (IridiumWindow *self,
                                GtkButton *button)
@@ -338,6 +358,17 @@ on_notification_close_clicked (IridiumWindow *self,
   gtk_revealer_set_reveal_child (self->notification_revealer, FALSE);
 }
 
+static void
+iridium_window_constructed (GObject *object)
+{
+  IridiumWindow *self;
+
+  self = IRIDIUM_WINDOW (object);
+  gtk_window_set_default_size (GTK_WINDOW (self), self->width, self->height);
+
+  G_OBJECT_CLASS (iridium_window_parent_class)->constructed (object);
+}
+
 static void
 iridium_window_dispose (GObject *object)
 {
@@ -361,6 +392,7 @@ iridium_window_class_init (IridiumWindowClass *klass)
   oclass = G_OBJECT_CLASS (klass);
   widget_class = GTK_WIDGET_CLASS (klass);
 
+  oclass->constructed = iridium_window_constructed;
   oclass->dispose = iridium_window_dispose;
 
   gtk_widget_class_set_template_from_resource (widget_class, "/net/bloerg/Iridium/iridium-window.ui");
@@ -380,6 +412,8 @@ iridium_window_class_init (IridiumWindowClass *klass)
   gtk_widget_class_bind_template_callback (widget_class, on_search_changed);
   gtk_widget_class_bind_template_callback (widget_class, on_notification_close_clicked);
   gtk_widget_class_bind_template_callback (widget_class, on_show);
+  gtk_widget_class_bind_template_callback (widget_class, on_size_allocate);
+  gtk_widget_class_bind_template_callback (widget_class, on_destroy);
 }
 
 static void
@@ -401,6 +435,10 @@ iridium_window_init (IridiumWindow *self)
   gtk_widget_init_template (GTK_WIDGET (self));
 
   self->preferences = g_settings_new ("net.bloerg.iridium.preferences");
+  self->window_state = g_settings_new ("net.bloerg.iridium.window-state");
+  self->width = g_settings_get_int (self->window_state, "width");
+  self->height = g_settings_get_int (self->window_state, "height");
+
   self->title_binding = NULL;
   self->content_binding = NULL;
   self->markdown = iridium_markdown_new ();

+ 2 - 2
src/iridium-window.ui

@@ -1,9 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <template class="IridiumWindow" parent="GtkApplicationWindow">
-    <property name="width-request">800</property>
-    <property name="height-request">600</property>
     <signal name="show" handler="on_show"/>
+    <signal name="size-allocate" handler="on_size_allocate"/>
+    <signal name="destroy" handler="on_destroy"/>
     <child type="titlebar">
       <object class="GtkHeaderBar" id="header_bar">
         <property name="visible">True</property>

+ 2 - 7
src/main.c

@@ -58,13 +58,8 @@ on_activate (GtkApplication *app)
 
   window = gtk_application_get_active_window (app);
 
-  if (window == NULL) {
-    window = g_object_new (IRIDIUM_TYPE_WINDOW,
-        "application", app,
-        "default-width", 600,
-        "default-height", 300,
-        NULL);
-  }
+  if (window == NULL)
+    window = g_object_new (IRIDIUM_TYPE_WINDOW, "application", app, NULL);
 
   gtk_window_present (window);
 }