From 5f72563a7439e48c2ed772ff590336a35bbc43b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Sun, 12 Sep 2021 21:12:37 +0700 Subject: [PATCH] Automatically choose the best layout for the screen --- cobang/app.py | 1 + cobang/ui.py | 25 +++++++++++++++++++++++++ data/vn.hoabinh.quan.CoBang.appdata.xml | 5 +++++ meson.build | 2 +- pyproject.toml | 2 +- 5 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cobang/app.py b/cobang/app.py index 3fb17ea..86f2dc6 100644 --- a/cobang/app.py +++ b/cobang/app.py @@ -220,6 +220,7 @@ def do_activate(self): self.discover_webcam() self.window.present() logger.debug("Window {} is shown", self.window) + ui.resize_to_match_screen(self.window) # If no webcam is selected, select the first V4l2 one if not self.webcam_combobox.get_active_iter(): v4l2_idx = next((n for n, r in enumerate(self.webcam_store) if r[2] == 'v4l2src'), 0) diff --git a/cobang/ui.py b/cobang/ui.py index 904124d..c31dd00 100644 --- a/cobang/ui.py +++ b/cobang/ui.py @@ -20,6 +20,9 @@ logger = Logger(__name__) +BEST_HORIZONTAL_WIDTH = 914 +BEST_VERTICAL_HEIGHT = 654 + def build_app_menu_model() -> Gio.Menu: menu = Gio.Menu() @@ -86,3 +89,25 @@ def wifi_connect_done(client: NM.Client, res: Gio.AsyncResult, button: Gtk.Butto if created: button.set_label(_('Saved')) button.set_sensitive(False) + + +def get_monitor_screen(window: Gtk.Window): + display = window.get_display() + logger.debug('Display: {}, {}', display, display.get_name()) + # FIXME: It returns wrong monitor + monitor = display.get_monitor_at_window(window.get_window()) + geo = monitor.get_geometry() + w, h = geo.width, geo.height + logger.debug('Monitor size: {}', (w, h)) + return (w, h) + + +def resize_to_match_screen(window: Gtk.Window): + '''Try to detect desktop or mobile screen, and resize to promote the horizontal or vertical layout.''' + sw, sh = get_monitor_screen(window) + w, h = window.get_size() + logger.debug('Current window size: {}', (w, h)) + if sw > BEST_HORIZONTAL_WIDTH: + window.resize(BEST_HORIZONTAL_WIDTH, h) + elif sh > BEST_VERTICAL_HEIGHT: + window.resize(w, BEST_VERTICAL_HEIGHT) diff --git a/data/vn.hoabinh.quan.CoBang.appdata.xml b/data/vn.hoabinh.quan.CoBang.appdata.xml index 3ca7e7f..b1a0f46 100644 --- a/data/vn.hoabinh.quan.CoBang.appdata.xml +++ b/data/vn.hoabinh.quan.CoBang.appdata.xml @@ -40,6 +40,11 @@ + + +

Automatically choose the best layout for the screen.

+
+

Reduce scrolling on mobile screen.

diff --git a/meson.build b/meson.build index b244b91..3f394fd 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,5 @@ project('cobang', - version: '0.9.2', + version: '0.9.3', meson_version: '>= 0.53', default_options: [ 'warning_level=2', diff --git a/pyproject.toml b/pyproject.toml index 5a13788..2a8b7b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "cobang" -version = "0.9.2" +version = "0.9.3" description = "QR code scanner for Linux desktop" authors = ["Nguyễn Hồng Quân "] license = "GPL-3.0-or-later"