[fractal] Save and restore window state



commit b4b38868adc3ed98501c3d8def2101824831f544
Author: Robert Griesel <r griesel gmail com>
Date:   Mon Feb 25 11:01:33 2019 +0000

    Save and restore window state
    
    Size, position and window maximize state are saved in gsettings
    when closing Fractal. They are then restored on startup.
    
    Window position is not working on all windowing systems.
    
    Fixes #123

 fractal-gtk/res/org.gnome.Fractal.gschema.xml | 25 ++++++++++++
 fractal-gtk/src/app/mod.rs                    | 22 +++++++++++
 fractal-gtk/src/app/windowstate.rs            | 55 +++++++++++++++++++++++++++
 3 files changed, 102 insertions(+)
---
diff --git a/fractal-gtk/res/org.gnome.Fractal.gschema.xml b/fractal-gtk/res/org.gnome.Fractal.gschema.xml
index d9da71fc..7ecdf633 100644
--- a/fractal-gtk/res/org.gnome.Fractal.gschema.xml
+++ b/fractal-gtk/res/org.gnome.Fractal.gschema.xml
@@ -24,6 +24,31 @@
       </description>
     </key>
 
+    <key name="main-window-state-x" type="i">
+      <default>-1</default>
+      <summary>X position of the main window on startup</summary>
+    </key>
+
+    <key name="main-window-state-y" type="i">
+      <default>-1</default>
+      <summary>Y position of the main window on startup</summary>
+    </key>
+
+    <key name="main-window-state-width" type="i">
+      <default>860</default>
+      <summary>Width of the main window on startup</summary>
+    </key>
+
+    <key name="main-window-state-height" type="i">
+      <default>640</default>
+      <summary>Height of the main window on startup</summary>
+    </key>
+
+    <key name="main-window-state-maximized" type="b">
+      <default>false</default>
+      <summary>Whether the main window is maximized on startup</summary>
+    </key>
+
   </schema>
 
 </schemalist>
diff --git a/fractal-gtk/src/app/mod.rs b/fractal-gtk/src/app/mod.rs
index ea0ae9e2..d77175ee 100644
--- a/fractal-gtk/src/app/mod.rs
+++ b/fractal-gtk/src/app/mod.rs
@@ -18,6 +18,9 @@ use crate::globals;
 use crate::uibuilder;
 
 mod connect;
+mod windowstate;
+
+use windowstate::WindowState;
 
 static mut OP: Option<SyncWeak<Mutex<AppOp>>> = None;
 #[macro_export]
@@ -104,6 +107,15 @@ impl App {
         window.set_application(gtk_app);
 
         window.set_title("Fractal");
+
+        let settings: gio::Settings = gio::Settings::new("org.gnome.Fractal");
+        let window_state = WindowState::load_from_gsettings(&settings);
+        window.set_default_size(window_state.width, window_state.height);
+        if window_state.is_maximized {
+            window.maximize();
+        } else if window_state.x > 0 && window_state.y > 0 {
+            window.move_(window_state.x, window_state.y);
+        }
         window.show_all();
 
         if gtk_app
@@ -177,6 +189,16 @@ impl App {
                 app.op.lock().unwrap().mark_active_room_messages();
             });
 
+        let app_weak = app.downgrade();
+        app.main_window.connect_delete_event(move |window, _| {
+            let app = upgrade_weak!(app_weak, Inhibit(false));
+            let settings: gio::Settings = gio::Settings::new("org.gnome.Fractal");
+            let window_state = WindowState::from_window(window);
+            window_state.save_in_gsettings(&settings);
+            app.op.lock().unwrap().quit();
+            Inhibit(false)
+        });
+
         app.op.lock().unwrap().init();
 
         // When the application is shut down we drop our app struct
diff --git a/fractal-gtk/src/app/windowstate.rs b/fractal-gtk/src/app/windowstate.rs
new file mode 100644
index 00000000..485ffb93
--- /dev/null
+++ b/fractal-gtk/src/app/windowstate.rs
@@ -0,0 +1,55 @@
+use gio::SettingsExt;
+use gtk;
+use gtk::prelude::*;
+
+pub struct WindowState {
+    pub x: i32,
+    pub y: i32,
+    pub width: i32,
+    pub height: i32,
+    pub is_maximized: bool,
+}
+
+impl WindowState {
+    pub fn load_from_gsettings(settings: &gio::Settings) -> WindowState {
+        let x = settings.get_int("main-window-state-x");
+        let y = settings.get_int("main-window-state-y");
+        let width = settings.get_int("main-window-state-width");
+        let height = settings.get_int("main-window-state-height");
+        let is_maximized = settings.get_boolean("main-window-state-maximized");
+
+        WindowState {
+            x,
+            y,
+            width,
+            height,
+            is_maximized,
+        }
+    }
+
+    pub fn from_window(window: &gtk::ApplicationWindow) -> WindowState {
+        let position = window.get_position();
+        let size = window.get_size();
+        let x = position.0;
+        let y = position.1;
+        let width = size.0;
+        let height = size.1;
+        let is_maximized = window.is_maximized();
+
+        WindowState {
+            x,
+            y,
+            width,
+            height,
+            is_maximized,
+        }
+    }
+
+    pub fn save_in_gsettings(&self, settings: &gio::Settings) {
+        settings.set_int("main-window-state-x", self.x);
+        settings.set_int("main-window-state-y", self.y);
+        settings.set_int("main-window-state-width", self.width);
+        settings.set_int("main-window-state-height", self.height);
+        settings.set_boolean("main-window-state-maximized", self.is_maximized);
+    }
+}


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