From cbd7dd0f61c8f5593b676cbf0096640e6997a4d1 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Fri, 1 Mar 2024 23:35:11 +0100 Subject: [PATCH 1/7] Add BackButton --- .github/workflows/main.yml | 2 +- README.md | 1 + lib/Widgets/BackButton.vala | 48 +++++++++++++++++++++++++++++++++++++ lib/meson.build | 1 + meson.build | 1 + 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 lib/Widgets/BackButton.vala diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ab94f0a9e..b79cfc2d9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,7 +23,7 @@ jobs: - name: Install Dependencies run: | apt update - apt install -y meson gobject-introspection libgee-0.8-dev libgirepository1.0-dev libgtk-4-dev sassc valac + apt install -y meson gobject-introspection libgee-0.8-dev libgirepository1.0-dev libgtk-4-dev libadwaita-1-dev sassc valac - name: Build env: DESTDIR: out diff --git a/README.md b/README.md index 912f33406..815ccb5a4 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ You'll need the following dependencies: * libgee-0.8-dev * libgirepository1.0-dev * libgtk-4-dev >= 4.4.0 +* libadwaita-1-dev >= 1.4 * sassc * valac diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala new file mode 100644 index 000000000..07997ec9a --- /dev/null +++ b/lib/Widgets/BackButton.vala @@ -0,0 +1,48 @@ +/* + * Copyright 2024 elementary, Inc. (https://elementary.io) + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/** + * BackButton is meant to be used in hearders to navigate in + * {@link Adw.NavigationView}s. It will automatically detect when under a + * {@link Adw.NavigationView} and label itself with the title of the preceding page. + */ + public class Granite.BackButton : Gtk.Button { + construct { + add_css_class (Granite.STYLE_CLASS_BACK_BUTTON); + + map.connect (on_map); + clicked.connect (on_click); + } + + private void on_map () { + var navigation_view = (Adw.NavigationView) get_ancestor (typeof (Adw.NavigationView)); + + if (navigation_view == null) { + warning ("Granite.BackButton used outside of Adw.NavigationView"); + return; + } + + var navigation_page = (Adw.NavigationPage) get_ancestor (typeof (Adw.NavigationPage)); + + if (navigation_view == null) { + warning ("Granite.BackButton used outside of Adw.NavigationPage"); + return; + } + + var previous_page = navigation_view.get_previous_page (navigation_page); + + if (previous_page != null) { + label = previous_page.title; + } + } + + private void on_click () { + var navigation_view = (Adw.NavigationView) get_ancestor (typeof (Adw.NavigationView)); + + if (navigation_view != null) { + navigation_view.pop (); + } + } +} diff --git a/lib/meson.build b/lib/meson.build index 25d4a1337..d4cac12b0 100644 --- a/lib/meson.build +++ b/lib/meson.build @@ -16,6 +16,7 @@ libgranite_sources = files( 'Widgets/AbstractSettingsPage.vala', 'Widgets/AbstractSimpleSettingsPage.vala', 'Widgets/AccelLabel.vala', + 'Widgets/BackButton.vala', 'Widgets/DatePicker.vala', 'Widgets/Dialog.vala', 'Widgets/HeaderLabel.vala', diff --git a/meson.build b/meson.build index e5e5fd425..987d08796 100644 --- a/meson.build +++ b/meson.build @@ -50,6 +50,7 @@ libgranite_deps = [ dependency('glib-2.0', version: '>=' + glib_min_version), dependency('gobject-2.0', version: '>=' + glib_min_version), dependency('gtk4', version: '>=4.4'), + dependency('libadwaita-1', version: '>=1.4'), ] icons_dir = join_paths( From bc9594dd1d9796ce28b021dd74ab4335e13223fc Mon Sep 17 00:00:00 2001 From: Leonhard Date: Fri, 1 Mar 2024 23:43:15 +0100 Subject: [PATCH 2/7] Hide when last page --- lib/Widgets/BackButton.vala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala index 07997ec9a..0d70dee1e 100644 --- a/lib/Widgets/BackButton.vala +++ b/lib/Widgets/BackButton.vala @@ -4,7 +4,7 @@ */ /** - * BackButton is meant to be used in hearders to navigate in + * BackButton is meant to be used in headers to navigate in * {@link Adw.NavigationView}s. It will automatically detect when under a * {@link Adw.NavigationView} and label itself with the title of the preceding page. */ @@ -34,7 +34,10 @@ var previous_page = navigation_view.get_previous_page (navigation_page); if (previous_page != null) { + visible = true; label = previous_page.title; + } else { + visible = false; } } From 2d92c89bb1e6c1b3cb0801d2b692df049d8636a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Fri, 1 Mar 2024 16:10:47 -0800 Subject: [PATCH 3/7] Use Gtk.Image, deprecate style class, demo (#694) * Use Gtk.Image, deprecate style class, demo * Update BackButton.vala --- demo/Views/CSSView.vala | 8 ++++---- lib/Constants.vala | 1 + lib/Widgets/BackButton.vala | 20 ++++++++++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/demo/Views/CSSView.vala b/demo/Views/CSSView.vala index c75830bc9..11292a45c 100644 --- a/demo/Views/CSSView.vala +++ b/demo/Views/CSSView.vala @@ -70,12 +70,12 @@ public class CSSView : Gtk.Box { }; terminal_scroll.add_css_class (Granite.STYLE_CLASS_TERMINAL); - var back_button_label = new Granite.HeaderLabel ("\"back-button\" style class") ; + var back_button_label = new Granite.HeaderLabel ("BackButton") ; - var back_button = new Gtk.Button.with_label ("Back Button") { - halign = Gtk.Align.START + var back_button = new Granite.BackButton () { + halign = START, + label = "Back" }; - back_button.add_css_class (Granite.STYLE_CLASS_BACK_BUTTON); var scales_header = new Granite.HeaderLabel ("Scales") { secondary_text = "\"warmth\" and \"temperature\" style classes" diff --git a/lib/Constants.vala b/lib/Constants.vala index e59ea6990..05995d706 100644 --- a/lib/Constants.vala +++ b/lib/Constants.vala @@ -11,6 +11,7 @@ namespace Granite { /** * Style class for shaping a {@link Gtk.Button} */ + [Version (deprecated = true, deprecated_since = "7.5.0", replacement = "Granite.BackButton")] public const string STYLE_CLASS_BACK_BUTTON = "back-button"; /** * Style class to match the window background diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala index 0d70dee1e..04a8dc4c4 100644 --- a/lib/Widgets/BackButton.vala +++ b/lib/Widgets/BackButton.vala @@ -8,12 +8,28 @@ * {@link Adw.NavigationView}s. It will automatically detect when under a * {@link Adw.NavigationView} and label itself with the title of the preceding page. */ - public class Granite.BackButton : Gtk.Button { +[Version (since = "7.5.0")] +public class Granite.BackButton : Gtk.Button { + /** + * A manually set label when used outside of {@link Adw.NavigationView} + */ + public new string label { get; set; } + construct { - add_css_class (Granite.STYLE_CLASS_BACK_BUTTON); + var image = new Gtk.Image.from_icon_name ("go-previous-symbolic"); + + var label_widget = new Gtk.Label (""); + + var box = new Gtk.Box (HORIZONTAL, 0); + box.append (image); + box.append (label_widget); + + child = box; map.connect (on_map); clicked.connect (on_click); + + bind_property ("label", label_widget, "label"); } private void on_map () { From 476bde989cbcfddb30f1a0c6679b64804276df83 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Sat, 2 Mar 2024 01:12:36 +0100 Subject: [PATCH 4/7] Don't warn when used outside navigation view --- lib/Widgets/BackButton.vala | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala index 04a8dc4c4..b9eb05acb 100644 --- a/lib/Widgets/BackButton.vala +++ b/lib/Widgets/BackButton.vala @@ -36,14 +36,12 @@ public class Granite.BackButton : Gtk.Button { var navigation_view = (Adw.NavigationView) get_ancestor (typeof (Adw.NavigationView)); if (navigation_view == null) { - warning ("Granite.BackButton used outside of Adw.NavigationView"); return; } var navigation_page = (Adw.NavigationPage) get_ancestor (typeof (Adw.NavigationPage)); if (navigation_view == null) { - warning ("Granite.BackButton used outside of Adw.NavigationPage"); return; } From 79aaada2a992f37b06a447fba7c0094e035db3a2 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Sat, 2 Mar 2024 01:33:35 +0100 Subject: [PATCH 5/7] Don't hide --- lib/Widgets/BackButton.vala | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala index b9eb05acb..314e6c674 100644 --- a/lib/Widgets/BackButton.vala +++ b/lib/Widgets/BackButton.vala @@ -48,10 +48,7 @@ public class Granite.BackButton : Gtk.Button { var previous_page = navigation_view.get_previous_page (navigation_page); if (previous_page != null) { - visible = true; label = previous_page.title; - } else { - visible = false; } } From cb566a7474d9ee69394dba49419c2fc233065615 Mon Sep 17 00:00:00 2001 From: Leonhard Date: Sat, 2 Mar 2024 14:28:13 +0100 Subject: [PATCH 6/7] Bind title --- lib/Widgets/BackButton.vala | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala index 314e6c674..ead60c65f 100644 --- a/lib/Widgets/BackButton.vala +++ b/lib/Widgets/BackButton.vala @@ -15,6 +15,8 @@ public class Granite.BackButton : Gtk.Button { */ public new string label { get; set; } + private Binding? title_binding = null; + construct { var image = new Gtk.Image.from_icon_name ("go-previous-symbolic"); @@ -33,6 +35,10 @@ public class Granite.BackButton : Gtk.Button { } private void on_map () { + if (title_binding != null) { + title_binding.unbind (); + } + var navigation_view = (Adw.NavigationView) get_ancestor (typeof (Adw.NavigationView)); if (navigation_view == null) { @@ -48,7 +54,7 @@ public class Granite.BackButton : Gtk.Button { var previous_page = navigation_view.get_previous_page (navigation_page); if (previous_page != null) { - label = previous_page.title; + title_binding = bind_property (previous_page, "title", this, "label", SYNC_CREATE); } } From c63f8bbbcb16042fad2d79d0b9e63e183be52f3f Mon Sep 17 00:00:00 2001 From: Leonhard Date: Sat, 2 Mar 2024 14:31:59 +0100 Subject: [PATCH 7/7] Fix build --- lib/Widgets/BackButton.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Widgets/BackButton.vala b/lib/Widgets/BackButton.vala index ead60c65f..d81399656 100644 --- a/lib/Widgets/BackButton.vala +++ b/lib/Widgets/BackButton.vala @@ -54,7 +54,7 @@ public class Granite.BackButton : Gtk.Button { var previous_page = navigation_view.get_previous_page (navigation_page); if (previous_page != null) { - title_binding = bind_property (previous_page, "title", this, "label", SYNC_CREATE); + title_binding = previous_page.bind_property ("title", this, "label", SYNC_CREATE); } }