From b03da2af60e109921967fd38a2ba16bfb4df9f5d Mon Sep 17 00:00:00 2001 From: fiaxh Date: Mon, 25 Nov 2019 23:23:06 +0100 Subject: [PATCH] Check window width and hight before (re)storing them --- libdino/src/entity/settings.vala | 56 -------------------- libdino/src/util.vala | 2 +- main/CMakeLists.txt | 1 + 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 ++++++++++++++++++++++ 7 files changed, 120 insertions(+), 87 deletions(-) create mode 100644 main/src/ui/util/config.vala diff --git a/libdino/src/entity/settings.vala b/libdino/src/entity/settings.vala index f94a92ca..ef25fd23 100644 --- a/libdino/src/entity/settings.vala +++ b/libdino/src/entity/settings.vala @@ -11,12 +11,6 @@ public class Settings : Object { send_marker_ = col_to_bool_or_default("send_marker", true); notifications_ = col_to_bool_or_default("notifications", true); convert_utf8_smileys_ = col_to_bool_or_default("convert_utf8_smileys", true); - - current_width = col_to_int_or_default("window_width", 1200); - current_height = col_to_int_or_default("window_height", 700); - is_maximized = col_to_bool_or_default("window_maximized", false); - position_x = col_to_int_or_default("window_position_x", -1); - position_y = col_to_int_or_default("window_position_y", -1); } private bool col_to_bool_or_default(string key, bool def) { @@ -64,56 +58,6 @@ public class Settings : Object { convert_utf8_smileys_ = value; } } - - private int current_width_; - public int current_width { - get { return current_width_; } - set { - if (value == current_width_) return; - db.settings.insert().or("REPLACE").value(db.settings.key, "window_width").value(db.settings.value, value.to_string()).perform(); - current_width_ = value; - } - } - - private int current_height_; - public int current_height { - get { return current_height_; } - set { - if (value == current_height_) return; - db.settings.insert().or("REPLACE").value(db.settings.key, "window_height").value(db.settings.value, value.to_string()).perform(); - current_height_ = value; - } - } - - private bool is_maximized_; - public bool is_maximized { - get { return is_maximized_; } - set { - if (value == is_maximized_) return; - db.settings.insert().or("REPLACE").value(db.settings.key, "window_maximized").value(db.settings.value, value.to_string()).perform(); - is_maximized_ = value; - } - } - - private int position_x_; - public int position_x { - get { return position_x_; } - set { - if (value == position_x_) return; - db.settings.insert().or("REPLACE").value(db.settings.key, "window_position_x").value(db.settings.value, value.to_string()).perform(); - position_x_ = value; - } - } - - private int position_y_; - public int position_y { - get { return position_y_; } - set { - if (value == position_y_) return; - db.settings.insert().or("REPLACE").value(db.settings.key, "window_position_y").value(db.settings.value, value.to_string()).perform(); - position_y_ = value; - } - } } } diff --git a/libdino/src/util.vala b/libdino/src/util.vala index cc705c10..c691a279 100644 --- a/libdino/src/util.vala +++ b/libdino/src/util.vala @@ -66,4 +66,4 @@ public static void internationalize(string gettext_package, string locales_dir) Intl.bindtextdomain(gettext_package, locales_dir); } -} \ No newline at end of file +} diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 48a349d5..e6387894 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -149,6 +149,7 @@ SOURCES src/ui/occupant_menu/view.vala src/ui/util/accounts_combo_box.vala + src/ui/util/config.vala src/ui/util/data_forms.vala src/ui/util/helper.vala src/ui/util/label_hybrid.vala 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; + } +} + +}