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/demo/Views/CSSView.vala b/demo/Views/CSSView.vala index f5ed52ec5..6ea3cc586 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 7365e8c7f..21c99056e 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 new file mode 100644 index 000000000..d81399656 --- /dev/null +++ b/lib/Widgets/BackButton.vala @@ -0,0 +1,68 @@ +/* + * Copyright 2024 elementary, Inc. (https://elementary.io) + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/** + * 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. + */ +[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; } + + private Binding? title_binding = null; + + construct { + 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 () { + if (title_binding != null) { + title_binding.unbind (); + } + + var navigation_view = (Adw.NavigationView) get_ancestor (typeof (Adw.NavigationView)); + + if (navigation_view == null) { + return; + } + + var navigation_page = (Adw.NavigationPage) get_ancestor (typeof (Adw.NavigationPage)); + + if (navigation_view == null) { + return; + } + + var previous_page = navigation_view.get_previous_page (navigation_page); + + if (previous_page != null) { + title_binding = previous_page.bind_property ("title", this, "label", SYNC_CREATE); + } + } + + 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(