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
This commit is contained in:
Robert Griesel 2019-02-25 11:01:33 +00:00 committed by Daniel Garcia Moreno
parent d7d7ae3004
commit b4b38868ad
3 changed files with 102 additions and 0 deletions

View file

@ -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>

View file

@ -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

View file

@ -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);
}
}