From b03da2af60e109921967fd38a2ba16bfb4df9f5d Mon Sep 17 00:00:00 2001 From: fiaxh Date: Mon, 25 Nov 2019 23:23:06 +0100 Subject: Check window width and hight before (re)storing them --- main/src/ui/application.vala | 4 +- main/src/ui/unified_window.vala | 57 ++++++++++++++++++++++++++-- main/src/ui/unified_window_controller.vala | 26 ------------- main/src/ui/util/config.vala | 61 ++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 30 deletions(-) create mode 100644 main/src/ui/util/config.vala (limited to 'main/src') diff --git a/main/src/ui/application.vala b/main/src/ui/application.vala index bff25696..9a154a49 100644 --- a/main/src/ui/application.vala +++ b/main/src/ui/application.vala @@ -11,6 +11,7 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { public Database db { get; set; } public Dino.Entities.Settings settings { get; set; } + private Config config { get; set; } public StreamInteractor stream_interactor { get; set; } public Plugins.Registry plugin_registry { get; set; default = new Plugins.Registry(); } public SearchPathGenerator? search_path_generator { get; set; } @@ -30,7 +31,8 @@ public class Dino.Ui.Application : Gtk.Application, Dino.Application { activate.connect(() => { if (window == null) { controller = new UnifiedWindowController(this, stream_interactor, db); - window = new UnifiedWindow(this, stream_interactor, db); + config = new Config(db); + window = new UnifiedWindow(this, stream_interactor, db, config); controller.set_window(window); if ((get_flags() & ApplicationFlags.IS_SERVICE) == ApplicationFlags.IS_SERVICE) window.delete_event.connect(window.hide_on_delete); diff --git a/main/src/ui/unified_window.vala b/main/src/ui/unified_window.vala index 8db2cfa7..9313ee10 100644 --- a/main/src/ui/unified_window.vala +++ b/main/src/ui/unified_window.vala @@ -37,14 +37,16 @@ public class UnifiedWindow : Gtk.Window { private StreamInteractor stream_interactor; private Conversation? conversation; - private Application app; private Database db; + private Config config; - public UnifiedWindow(Application application, StreamInteractor stream_interactor, Database db) { + public UnifiedWindow(Application application, StreamInteractor stream_interactor, Database db, Config config) { Object(application : application); - this.app = application; this.stream_interactor = stream_interactor; this.db = db; + this.config = config; + + restore_window_size(); this.get_style_context().add_class("dino-main"); setup_headerbar(); @@ -177,6 +179,55 @@ public class UnifiedWindow : Gtk.Window { public void loop_conversations(bool backwards) { conversation_selector.loop_conversations(backwards); } + + public void restore_window_size() { + Gdk.Display? display = Gdk.Display.get_default(); + if (display != null) { + Gdk.Monitor? monitor = display.get_primary_monitor(); + if (monitor == null) { + monitor = display.get_monitor_at_point(1, 1); + } + + if (monitor != null && + config.window_width <= monitor.geometry.width && + config.window_height <= monitor.geometry.height) { + set_default_size(config.window_width, config.window_height); + } + } + this.window_position = Gtk.WindowPosition.CENTER; + if (config.window_maximize) { + maximize(); + } + + this.delete_event.connect(() => { + save_window_size(); + config.window_maximize = this.is_maximized; + return false; + }); + } + + public void save_window_size() { + if (this.is_maximized) return; + + Gdk.Display? display = get_display(); + Gdk.Window? window = get_window(); + if (display != null && window != null) { + Gdk.Monitor monitor = display.get_monitor_at_window(window); + + int width = 0; + int height = 0; + get_size(out width, out height); + + + // Only store if the values have changed and are reasonable-looking. + if (config.window_width != width && width > 0 && width <= monitor.geometry.width) { + config.window_width = width; + } + if (config.window_height != height && height > 0 && height <= monitor.geometry.height) { + config.window_height = height; + } + } + } } public class WelcomePlceholder : UnifiedWindowPlaceholder { diff --git a/main/src/ui/unified_window_controller.vala b/main/src/ui/unified_window_controller.vala index 157bbdff..0e37130b 100644 --- a/main/src/ui/unified_window_controller.vala +++ b/main/src/ui/unified_window_controller.vala @@ -119,8 +119,6 @@ public class UnifiedWindowController : Object { }); window.conversation_selected.connect(conversation => select_conversation(conversation)); - - restore_window_size(); } public void select_conversation(Conversation? conversation, bool do_reset_search = true, bool default_initialize_conversation = true) { @@ -207,30 +205,6 @@ public class UnifiedWindowController : Object { search_menu_entry.search_button.active = false; window.search_revealer.reveal_child = false; } - - private void restore_window_size() { - window.default_width = app.settings.current_width; - window.default_height = app.settings.current_height; - if (app.settings.is_maximized) window.maximize(); - if (app.settings.position_x != -1 && app.settings.position_y != -1) { - window.move(app.settings.position_x, app.settings.position_y); - } - - window.delete_event.connect(() => { - int x, y; - window.get_position(out x, out y); - app.settings.position_x = x; - app.settings.position_y = y; - - int width, height; - window.get_size(out width, out height); - app.settings.current_width = width; - app.settings.current_height = height; - - app.settings.is_maximized = window.is_maximized; - return false; - }); - } } } diff --git a/main/src/ui/util/config.vala b/main/src/ui/util/config.vala new file mode 100644 index 00000000..4ca0d8a0 --- /dev/null +++ b/main/src/ui/util/config.vala @@ -0,0 +1,61 @@ +using Gee; +using Gtk; + +using Dino.Entities; + +namespace Dino.Ui { + +public class Config : Object { + + public Database db { get; private set; } + + public Config(Database db) { + this.db = db; + + window_maximize = col_to_bool_or_default("window_maximized", false); + window_width = col_to_int_or_default("window_width", 1200); + window_height = col_to_int_or_default("window_height", 700); + } + + private bool window_maximize_; + public bool window_maximize { + get { return window_maximize_; } + set { + if (value == window_maximize_) return; + db.settings.insert().or("REPLACE").value(db.settings.key, "window_maximized").value(db.settings.value, value.to_string()).perform(); + window_maximize_ = value; + } + } + + public int window_height_; + public int window_height { + get { return window_height_; } + set { + if (value == window_height_) return; + db.settings.insert().or("REPLACE").value(db.settings.key, "window_height").value(db.settings.value, value.to_string()).perform(); + window_height_ = value; + } + } + + public int window_width_; + public int window_width { + get { return window_width_; } + set { + if (value == window_width_) return; + db.settings.insert().or("REPLACE").value(db.settings.key, "window_width").value(db.settings.value, value.to_string()).perform(); + window_width_ = value; + } + } + + private bool col_to_bool_or_default(string key, bool def) { + string? val = db.settings.select({db.settings.value}).with(db.settings.key, "=", key)[db.settings.value]; + return val != null ? bool.parse(val) : def; + } + + private int col_to_int_or_default(string key, int def) { + string? val = db.settings.select({db.settings.value}).with(db.settings.key, "=", key)[db.settings.value]; + return val != null ? int.parse(val) : def; + } +} + +} -- cgit v1.2.3-54-g00ecf