From a78a538f890ac94b935454059abc95c5873eda79 Mon Sep 17 00:00:00 2001 From: Rafael Mardojai CM Date: Thu, 5 Sep 2024 17:18:35 -0500 Subject: [PATCH] window: Use `Adw.MultiLayoutView` Remove deprecated widgets like Adw.Squeezer --- dialect/window.blp | 741 +++++++++++++++++++++------------------------ dialect/window.py | 12 - 2 files changed, 349 insertions(+), 404 deletions(-) diff --git a/dialect/window.blp b/dialect/window.blp index d7512178..2e61b359 100644 --- a/dialect/window.blp +++ b/dialect/window.blp @@ -47,8 +47,10 @@ template $DialectWindow : Adw.ApplicationWindow { Adw.Breakpoint { condition ("max-width: 680px") setters { - actionbar.revealed: true; - title_stack.visible-child-name: "label"; + multi_layout.layout-name: "mobile"; + error_buttons.orientation: vertical; + key_buttons.orientation: vertical; + mobile_buttons_size.mode: horizontal; translator_box.orientation: vertical; } } @@ -81,63 +83,29 @@ template $DialectWindow : Adw.ApplicationWindow { Adw.StatusPage error_page { vexpand: true; - child: Adw.Squeezer { - homogeneous: false; + child: Box error_buttons { + spacing: 12; + halign: center; - Box { - spacing: 12; - halign: center; - - Button { - label: _("Retry"); - clicked => $_on_retry_load_translator_clicked(); - - styles [ - "pill", - "suggested-action", - ] - } - - Button { - label: _("Open Preferences"); - action-name: "app.preferences"; + Button error_retry_btn { + can-shrink: true; + label: _("Retry"); + clicked => $_on_retry_load_translator_clicked(); - styles [ - "pill", - ] - } + styles [ + "pill", + "suggested-action", + ] } - Box { - spacing: 12; - orientation: vertical; - - Button error_retry_btn { - clicked => $_on_retry_load_translator_clicked(); + Button error_open_prefs_btn { + can-shrink: true; + label: _("Open Preferences"); + action-name: "app.preferences"; - styles [ - "pill", - "suggested-action", - ] - - child: Label { - label: _("Retry"); - ellipsize: end; - }; - } - - Button error_preferences_btn { - action-name: "app.preferences"; - - styles [ - "pill", - ] - - child: Label { - label: _("Open Preferences"); - ellipsize: end; - }; - } + styles [ + "pill", + ] } }; } @@ -155,433 +123,425 @@ template $DialectWindow : Adw.ApplicationWindow { icon-name: "dialog-password-symbolic"; vexpand: true; - child: Adw.Squeezer { - homogeneous: false; + child: Box key_buttons { + spacing: 12; + halign: center; - Box { - spacing: 12; - halign: center; + Button rmv_key_btn { + visible: false; + can-shrink: true; + label: _("Remove Key and Retry"); + clicked => $_on_remove_key_and_reload_clicked(); - Button rmv_key_btn { - visible: false; - label: _("Remove Key and Retry"); - clicked => $_on_remove_key_and_reload_clicked(); - - styles [ - "pill", - "suggested-action", - ] - } - - Button { - label: _("Open Preferences"); - action-name: "app.preferences"; - - styles [ - "pill", - ] - } + styles [ + "pill", + "suggested-action", + ] } - Box { - spacing: 12; - orientation: vertical; - - Button error_api_key_btn { - visible: false; - clicked => $_on_remove_key_and_reload_clicked(); - - styles [ - "pill", - "suggested-action", - ] - - child: Label { - label: _("Remove Key and Retry"); - ellipsize: end; - }; - } - - Button error_preferences2_btn { - action-name: "app.preferences"; - - styles [ - "pill", - ] + Button key_open_prefs_btn { + can-shrink: true; + label: _("Open Preferences"); + action-name: "app.preferences"; - child: Label { - label: _("Open Preferences"); - ellipsize: end; - }; - } + styles [ + "pill", + ] } }; } }; } + /* Translation View */ StackPage { name: "translate"; child: Adw.ToastOverlay toast_overlay { vexpand: true; - child: Adw.ToolbarView { - - [top] - Adw.HeaderBar { - centering-policy: strict; - title-widget: Stack title_stack { - transition-type: crossfade; - hhomogeneous: false; - - StackPage { - name: "selector"; - child: Box langs_button_box { - spacing: 6; - - $LangSelector src_lang_selector { - notify::selected => $_on_src_lang_changed(); - user-selection-changed => $_on_translation(); - tooltip-text: _("Change Source Language"); - } - Button switch_btn { - action-name: "win.switch"; - tooltip-text: _("Switch Languages"); - icon-name: "object-flip-horizontal-symbolic"; - } + child: Adw.MultiLayoutView multi_layout { - $LangSelector dest_lang_selector { - notify::selected => $_on_dest_lang_changed(); - user-selection-changed => $_on_translation(); - tooltip-text: _("Change Destination Language"); - } + /* Desktop Layout */ + Adw.Layout { + name: "desktop"; + content: Adw.ToolbarView { + + [top] + Adw.HeaderBar { + centering-policy: strict; + + title-widget: Adw.LayoutSlot { + id: "langs-selector"; }; + + Adw.LayoutSlot { + id: "back-btn"; + } + + Adw.LayoutSlot { + id: "forward-btn"; + } + + [end] + Adw.LayoutSlot { + id: "menu"; + } } - StackPage { - name: "label"; - child: Adw.WindowTitle { - title: _("Dialect"); - }; + Adw.LayoutSlot { + id: "translation"; + } + }; + } + + /* Mobile Layout */ + Adw.Layout { + name: "mobile"; + content: Adw.ToolbarView { + [top] + Adw.HeaderBar { + + Adw.LayoutSlot { + id: "back-btn"; + } + + Adw.LayoutSlot { + id: "forward-btn"; + } + + [end] + Adw.LayoutSlot { + id: "menu"; + } + } + + Adw.LayoutSlot { + id: "translation"; + } + + [bottom] + Adw.LayoutSlot { + id: "langs-selector"; + + halign: center; + + styles [ + "toolbar", + ] } }; + } + + /* Menu */ + [menu] + MenuButton menu_btn { + menu-model: app-menu; + tooltip-text: _("Main Menu"); + icon-name: "open-menu-symbolic"; + primary: true; + } - Button return_btn { - action-name: "win.back"; - tooltip-text: _("Previous Translation"); - icon-name: "go-previous-symbolic"; + /* Nav back */ + [back-btn] + Button return_btn { + action-name: "win.back"; + tooltip-text: _("Previous Translation"); + icon-name: "go-previous-symbolic"; + } + + /* Nav forward */ + [forward-btn] + Button forward_btn { + action-name: "win.forward"; + tooltip-text: _("Next Translation"); + icon-name: "go-next-symbolic"; + } + + /* Languages Selector */ + [langs-selector] + Box langs_button_box { + spacing: 6; + + $LangSelector src_lang_selector { + notify::selected => $_on_src_lang_changed(); + user-selection-changed => $_on_translation(); + tooltip-text: _("Change Source Language"); } - Button forward_btn { - action-name: "win.forward"; - tooltip-text: _("Next Translation"); - icon-name: "go-next-symbolic"; + Button switch_btn { + action-name: "win.switch"; + tooltip-text: _("Switch Languages"); + icon-name: "object-flip-horizontal-symbolic"; } - [end] - MenuButton menu_btn { - menu-model: app-menu; - tooltip-text: _("Main Menu"); - icon-name: "open-menu-symbolic"; - primary: true; + $LangSelector dest_lang_selector { + notify::selected => $_on_dest_lang_changed(); + user-selection-changed => $_on_translation(); + tooltip-text: _("Change Destination Language"); } } - Box { - orientation: vertical; + /* Translation Booxes */ + [translation] + Box translator_box { + vexpand: true; + spacing: 12; + homogeneous: true; - Box translator_box { - vexpand: true; - spacing: 12; - homogeneous: true; + styles [ + "translation-box", + ] + + Box { + orientation: vertical; + overflow: hidden; styles [ - "translation-box", + "card", + "translation-side-box", ] - Box { - orientation: vertical; - overflow: hidden; + ScrolledWindow { + vexpand: true; + + $TextView src_text { + left-margin: 9; + right-margin: 9; + top-margin: 9; + bottom-margin: 9; + + activate => $_on_src_activated(); + } styles [ - "card", - "translation-side-box", + "translation-scrolled" ] + } - ScrolledWindow { - vexpand: true; + Revealer src_pron_revealer { + transition-type: slide_down; + reveal-child: false; - $TextView src_text { - left-margin: 9; - right-margin: 9; - top-margin: 9; - bottom-margin: 9; + ScrolledWindow { + Label src_pron_label { + xalign: 0; + wrap: true; + wrap-mode: word_char; + selectable: true; + valign: end; - activate => $_on_src_activated(); + styles [ + "pronunciation", + "dim-label", + ] } styles [ - "translation-scrolled" + "translation-scrolled", + "top-undershoot" ] } + } - Revealer src_pron_revealer { - transition-type: slide_down; - reveal-child: false; + Revealer mistakes { + can-focus: false; + reveal-child: false; - ScrolledWindow { - Label src_pron_label { - xalign: 0; - wrap: true; - wrap-mode: word_char; - selectable: true; - valign: end; + Box { + can-focus: false; + orientation: horizontal; + spacing: 8; - styles [ - "pronunciation", - "dim-label", - ] - } + Image { + can-focus: false; + icon-name: "error-correct-symbolic"; + } - styles [ - "translation-scrolled", - "top-undershoot" - ] + Label mistakes_label { + can-focus: false; + wrap: true; + wrap-mode: word_char; + + activate-link => $_on_mistakes_clicked(); } + + styles [ + "card", + "mistakes" + ] } + } - Revealer mistakes { - can-focus: false; - reveal-child: false; + Box { + Button clear_btn { + action-name: "win.clear"; + tooltip-text: _("Clear"); + icon-name: "edit-clear-symbolic"; + } - Box { - can-focus: false; - orientation: horizontal; - spacing: 8; + Button paste_btn { + action-name: "win.paste"; + tooltip-text: _("Paste"); + icon-name: "edit-paste-symbolic"; + } - Image { - can-focus: false; - icon-name: "error-correct-symbolic"; - } + $SpeechButton src_speech_btn { + action-name: "win.listen-src"; + tooltip-text: _("Listen"); - Label mistakes_label { - can-focus: false; - wrap: true; - wrap-mode: word_char; + styles [ + "flat", + ] + } - activate-link => $_on_mistakes_clicked(); - } + Label char_counter { + margin-start: 4; + margin-end: 4; + hexpand: true; + halign: end; - styles [ - "card", - "mistakes" - ] - } + styles [ + "dim-label", + "caption-heading", + "numeric" + ] } - Box { - Button clear_btn { - action-name: "win.clear"; - tooltip-text: _("Clear"); - icon-name: "edit-clear-symbolic"; - } + Button translate_btn { + label: _("Translate"); + action-name: "win.translation"; - Button paste_btn { - action-name: "win.paste"; - tooltip-text: _("Paste"); - icon-name: "edit-paste-symbolic"; - } + styles [ + "suggested-action", + ] + } - $SpeechButton src_speech_btn { - action-name: "win.listen-src"; - tooltip-text: _("Listen"); + styles [ + "toolbar", + ] + } + } - styles [ - "flat", - ] - } + Box dest_box { + orientation: vertical; + overflow: hidden; + + styles [ + "card", + "translation-side-box", + ] - Label char_counter { - margin-start: 4; - margin-end: 4; - hexpand: true; - halign: end; + ScrolledWindow { + vexpand: true; - styles [ - "dim-label", - "caption-heading", - "numeric" - ] - } + $TextView dest_text { + editable: false; + left-margin: 9; + right-margin: 9; + top-margin: 9; + bottom-margin: 9; + } - Button translate_btn { - label: _("Translate"); - action-name: "win.translation"; + styles [ + "translation-scrolled" + ] + } + + Revealer dest_pron_revealer { + transition-type: slide_down; + reveal-child: false; + + ScrolledWindow { + Label dest_pron_label { + xalign: 0; + wrap: true; + wrap-mode: word_char; + selectable: true; + valign: end; styles [ - "suggested-action", + "pronunciation", + "dim-label", ] } styles [ - "toolbar", + "translation-scrolled", + "top-undershoot" ] } } - Box dest_box { - orientation: vertical; - overflow: hidden; + Stack dest_toolbar_stack { + transition-type: crossfade; - styles [ - "card", - "translation-side-box", - ] + StackPage { + name: "default"; + child: Box { + Adw.Spinner trans_spinner { + tooltip-text: _("Translating…"); + margin-start: 8; + } - ScrolledWindow { - vexpand: true; - - $TextView dest_text { - editable: false; - left-margin: 9; - right-margin: 9; - top-margin: 9; - bottom-margin: 9; - } + Image trans_warning { + tooltip-text: _("Could not Translate the Text"); + margin-start: 8; + icon-name: "dialog-warning-symbolic"; + } - styles [ - "translation-scrolled" - ] - } + Button copy_btn { + action-name: "win.copy"; + tooltip-text: _("Copy"); + icon-name: "edit-copy-symbolic"; + hexpand: true; + halign: end; + } - Revealer dest_pron_revealer { - transition-type: slide_down; - reveal-child: false; + Button edit_btn { + action-name: "win.suggest"; + tooltip-text: _("Suggest Translation"); + icon-name: "document-edit-symbolic"; + } - ScrolledWindow { - Label dest_pron_label { - xalign: 0; - wrap: true; - wrap-mode: word_char; - selectable: true; - valign: end; + $SpeechButton dest_speech_btn { + action-name: "win.listen-dest"; + tooltip-text: _("Listen"); styles [ - "pronunciation", - "dim-label", + "flat", ] } styles [ - "translation-scrolled", - "top-undershoot" + "toolbar", ] - } + }; } - Stack dest_toolbar_stack { - transition-type: crossfade; - - StackPage { - name: "default"; - child: Box { - Adw.Spinner trans_spinner { - tooltip-text: _("Translating…"); - margin-start: 8; - } - - Image trans_warning { - tooltip-text: _("Could not Translate the Text"); - margin-start: 8; - icon-name: "dialog-warning-symbolic"; - } - - Button copy_btn { - action-name: "win.copy"; - tooltip-text: _("Copy"); - icon-name: "edit-copy-symbolic"; - hexpand: true; - halign: end; - } - - Button edit_btn { - action-name: "win.suggest"; - tooltip-text: _("Suggest Translation"); - icon-name: "document-edit-symbolic"; - } - - $SpeechButton dest_speech_btn { - action-name: "win.listen-dest"; - tooltip-text: _("Listen"); - - styles [ - "flat", - ] - } - - styles [ - "toolbar", - ] - }; - } + StackPage { + name: "edit"; + child: Box { + Button cancel_btn { + action-name: "win.suggest-cancel"; + label: _("Cancel"); + hexpand: true; + halign: end; + } - StackPage { - name: "edit"; - child: Box { - Button cancel_btn { - action-name: "win.suggest-cancel"; - label: _("Cancel"); - hexpand: true; - halign: end; - } - - Button save_btn { - action-name: "win.suggest-ok"; - label: _("Save"); - - styles [ - "suggested-action", - ] - } + Button save_btn { + action-name: "win.suggest-ok"; + label: _("Save"); styles [ - "toolbar", + "suggested-action", ] - }; - } - } - } - } - - ActionBar actionbar { - sensitive: bind langs_button_box.sensitive no-sync-create; - revealed: false; - - styles [ - "flat", - ] - - $LangSelector src_lang_selector_m { - selected: bind src_lang_selector.selected bidirectional; - tooltip-text: _("Change Source Language"); - - user-selection-changed => $_on_translation(); - } - - [center] - Button switch_btn2 { - sensitive: bind switch_btn.sensitive no-sync-create; - tooltip-text: _("Switch Languages"); - action-name: "win.switch"; - icon-name: "object-flip-horizontal-symbolic"; - } - - [end] - $LangSelector dest_lang_selector_m { - selected: bind dest_lang_selector.selected bidirectional; - tooltip-text: _("Change Destination Language"); + } - user-selection-changed => $_on_translation(); + styles [ + "toolbar", + ] + }; + } } } } @@ -600,10 +560,7 @@ Gtk.SizeGroup { widgets [src_lang_selector, dest_lang_selector] } -Gtk.SizeGroup { - widgets [error_retry_btn, error_preferences_btn] -} - -Gtk.SizeGroup { - widgets [error_api_key_btn, error_preferences2_btn] +Gtk.SizeGroup mobile_buttons_size { + mode: none; + widgets [error_retry_btn, error_open_prefs_btn, rmv_key_btn, key_open_prefs_btn] } diff --git a/dialect/window.py b/dialect/window.py index 09b59120..90a14e4f 100644 --- a/dialect/window.py +++ b/dialect/window.py @@ -54,9 +54,7 @@ class DialectWindow(Adw.ApplicationWindow): translator_box: Gtk.Box = Gtk.Template.Child() # type: ignore key_page: Adw.StatusPage = Gtk.Template.Child() # type: ignore rmv_key_btn: Gtk.Button = Gtk.Template.Child() # type: ignore - error_api_key_btn: Gtk.Button = Gtk.Template.Child() # type: ignore - title_stack: Gtk.Stack = Gtk.Template.Child() # type: ignore langs_button_box: Gtk.Box = Gtk.Template.Child() # type: ignore switch_btn: Gtk.Button = Gtk.Template.Child() # type: ignore src_lang_selector: LangSelector = Gtk.Template.Child() # type: ignore @@ -87,10 +85,6 @@ class DialectWindow(Adw.ApplicationWindow): copy_btn: Gtk.Button = Gtk.Template.Child() # type: ignore dest_speech_btn: SpeechButton = Gtk.Template.Child() # type: ignore - actionbar: Gtk.ActionBar = Gtk.Template.Child() # type: ignore - src_lang_selector_m: LangSelector = Gtk.Template.Child() # type: ignore - dest_lang_selector_m: LangSelector = Gtk.Template.Child() # type: ignore - toast: Adw.Toast | None = None # for notification management toast_overlay: Adw.ToastOverlay = Gtk.Template.Child() # type: ignore @@ -261,13 +255,8 @@ def lang_names_func(code: str): # Src lang selector self.src_lang_selector.bind_models(self.src_lang_model, self.src_recent_lang_model) - self.src_lang_selector_m.bind_models(self.src_lang_model, self.src_recent_lang_model) - # Dest lang selector self.dest_lang_selector.bind_models(self.dest_lang_model, self.dest_recent_lang_model) - self.dest_lang_selector_m.bind_models(self.dest_lang_model, self.dest_recent_lang_model) - - self.langs_button_box.props.homogeneous = False def setup_translation(self): # Src buffer @@ -441,7 +430,6 @@ def show_translator_api_key_view(self, required=False): "Please set a valid API key or unset the API key in the preferences." ) self.rmv_key_btn.props.visible = True - self.error_api_key_btn.props.visible = True self.main_stack.props.visible_child_name = "api-key"