From b4b38868adc3ed98501c3d8def2101824831f544 Mon Sep 17 00:00:00 2001 From: Robert Griesel Date: Mon, 25 Feb 2019 11:01:33 +0000 Subject: [PATCH] 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(+) create mode 100644 fractal-gtk/src/app/windowstate.rs 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 @@ + + -1 + X position of the main window on startup + + + + -1 + Y position of the main window on startup + + + + 860 + Width of the main window on startup + + + + 640 + Height of the main window on startup + + + + false + Whether the main window is maximized on startup + + 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>> = 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: >k::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); + } +}