Skip to content

Commit

Permalink
Application: Add storing of window size into GSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
birros committed Aug 3, 2018
1 parent 5704729 commit e0d0a72
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
10 changes: 10 additions & 0 deletions data/gsettings/com.github.birros.WebArchives.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@
<summary>Night mode</summary>
<description>Night mode state</description>
</key>
<key name="window-maximized" type="b">
<default>true</default>
<summary>Window maximized</summary>
<description>Window maximized state.</description>
</key>
<key name="window-size" type="(ii)">
<default>(768, 600)</default>
<summary>Window size</summary>
<description>Window size (width and height).</description>
</key>
</schema>
</schemalist>
57 changes: 53 additions & 4 deletions src/app/application.vala
Original file line number Diff line number Diff line change
@@ -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<Gtk.Window, Size?> sizes;
private const OptionEntry [] option_entries = {
{
"version", 'v', 0,
Expand All @@ -20,6 +26,7 @@ public class WebArchives.Application : Gtk.Application {
flags: ApplicationFlags.HANDLES_OPEN
);

sizes = new HashTable<Gtk.Window, Size?> (direct_hash, direct_equal);
add_main_option_entries (option_entries);
}

Expand Down Expand Up @@ -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",
Expand All @@ -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 () {
Expand All @@ -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 ();
}
Expand Down
2 changes: 0 additions & 2 deletions src/app/ui/window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e0d0a72

Please sign in to comment.