1/*
2 * Copyright 2020 Andrey Kutejko <andy128k@gmail.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20using Gtk;
21
22public interface WindowSize : Object {
23    public abstract int width { get; set; }
24    public abstract int height { get; set; }
25    public abstract bool is_maximized { get; set; }
26}
27
28public class WindowSizeSettings : Object, WindowSize {
29    public override int width {
30        get { return settings.get_int ("window-width"); }
31        set { settings.set_int ("window-width", value); }
32    }
33
34    public override int height {
35        get { return settings.get_int ("window-height"); }
36        set { settings.set_int ("window-height", value); }
37    }
38
39    public override bool is_maximized {
40        get { return settings.get_boolean ("window-is-maximized"); }
41        set { settings.set_boolean ("window-is-maximized", value); }
42    }
43
44    private GLib.Settings settings;
45
46    public WindowSizeSettings (string schema_id) {
47        this.settings = new GLib.Settings (schema_id);
48    }
49}
50
51private bool window_configure_event_cb (Gtk.Window window,
52                                        WindowSize size
53) {
54    if (!size.is_maximized) {
55        int width, height;
56        window.get_size (out width, out height);
57        size.width = width;
58        size.height = height;
59    }
60    return false;
61}
62
63private bool window_state_event_cb (Gtk.Window window,
64                                    WindowSize size,
65                                    Gdk.EventWindowState event
66) {
67    if ((event.changed_mask & Gdk.WindowState.MAXIMIZED) != 0) {
68        size.is_maximized = (event.new_window_state & Gdk.WindowState.MAXIMIZED) != 0;
69    }
70    return false;
71}
72
73public void remember_window_size (Gtk.Window window, WindowSize size) {
74    window.configure_event.connect (() => window_configure_event_cb (window, size));
75    window.window_state_event.connect (event => window_state_event_cb (window, size, event));
76    window.set_default_size (size.width, size.height);
77    if (size.is_maximized) {
78        window.maximize ();
79    }
80}
81
82