diff --git a/data/gsettings/com.github.birros.WebArchives.gschema.xml b/data/gsettings/com.github.birros.WebArchives.gschema.xml index 0efd4b9..c2978a0 100644 --- a/data/gsettings/com.github.birros.WebArchives.gschema.xml +++ b/data/gsettings/com.github.birros.WebArchives.gschema.xml @@ -5,5 +5,15 @@ Night mode Night mode state + + true + Window maximized + Window maximized state. + + + (768, 600) + Window size + Window size (width and height). + \ No newline at end of file diff --git a/src/app/application.vala b/src/app/application.vala index 810496c..2301b1e 100644 --- a/src/app/application.vala +++ b/src/app/application.vala @@ -1,7 +1,13 @@ public class WebArchives.Application : Gtk.Application { + private struct Size { + int width; + int height; + } + private Context context; private Persistence persistence; private GLib.Settings settings; + private HashTable sizes; private const OptionEntry [] option_entries = { { "version", 'v', 0, @@ -20,6 +26,7 @@ public class WebArchives.Application : Gtk.Application { flags: ApplicationFlags.HANDLES_OPEN ); + sizes = new HashTable (direct_hash, direct_equal); add_main_option_entries (option_entries); } @@ -65,6 +72,9 @@ public class WebArchives.Application : Gtk.Application { context.night_mode_state.active = settings.get_boolean ("night-mode"); info ("server_url: %s\n", context.server.url); + window_added.connect (on_window_added); + window_removed.connect (on_window_removed); + // styles string[] styles = { "ui/gtk/notebook/notebook.css", @@ -85,10 +95,8 @@ public class WebArchives.Application : Gtk.Application { protected override void activate () { Context window_context = new Context.fork (context, Context.Layer.APP); Window window = new Window (this, window_context); - /** - * FIXME: better support of window size, maximize saving and restoring. - */ - window.maximize (); + + add_window (window); } private void on_night_mode () { @@ -103,6 +111,47 @@ public class WebArchives.Application : Gtk.Application { ); } + private void on_window_added (Gtk.Window window) { + int width, height; + settings.get ("window-size", "(ii)", out width, out height); + bool maximized = settings.get_boolean ("window-maximized"); + + window.resize (width, height); + if (maximized) { + window.maximize (); + } + + window.configure_event.connect ((event) => { + return on_configure_event (event, window); + }); + } + + private bool on_configure_event ( + Gdk.EventConfigure event, Gtk.Window window + ) { + int width, height; + window.get_size (out width, out height); + + Size size = { + width: width, + height: height + }; + sizes.insert (window, size); + + return false; + } + + private void on_window_removed (Gtk.Window window) { + Size? size = sizes.get (window); + sizes.remove (window); + bool maximized = window.is_maximized; + + if (!maximized && size != null) { + settings.set ("window-size", "(ii)", size.width, size.height); + } + settings.set_boolean ("window-maximized", maximized); + } + private void on_quit () { base.quit (); } diff --git a/src/app/ui/window.vala b/src/app/ui/window.vala index 506a354..962805e 100644 --- a/src/app/ui/window.vala +++ b/src/app/ui/window.vala @@ -55,8 +55,6 @@ public class WebArchives.Window : Gtk.ApplicationWindow { this.context = context; - set_default_size (900, 600); - header_bar = new HeaderBar (); header_bar.set_context (context); set_titlebar (header_bar);