Skip to content

Commit 35227be

Browse files
leolost2605danirabbitzeebok
authored
Add BackButton (#692)
Co-authored-by: Danielle Foré <danielle@elementary.io> Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
1 parent 2125802 commit 35227be

File tree

5 files changed

+49
-46
lines changed

5 files changed

+49
-46
lines changed

demo/Views/ControlsView.vala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public class ControlsView : DemoPage {
2323
tooltip_text = "Gtk.ToggleButton.icon_name"
2424
};
2525

26-
var back_button = new Gtk.Button.with_label ("Granite.CssClass.BACK");
27-
back_button.add_css_class (Granite.CssClass.BACK);
26+
var back_button = new Granite.BackButton ("Granite.BackButton") {
27+
halign = START
28+
};
2829

2930
var destructive_button = new Gtk.Button.with_label ("Granite.CssClass.DESTRUCTIVE");
3031
destructive_button.add_css_class (Granite.CssClass.DESTRUCTIVE);

lib/Constants.vala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Granite {
1212
/**
1313
* Style class for shaping a {@link Gtk.Button}
1414
*/
15-
[Version (deprecated = true, deprecated_since = "7.7.0", replacement = "Granite.CssClass.BACK")]
15+
[Version (deprecated = true, deprecated_since = "7.7.0", replacement = "Granite.BackButton")]
1616
public const string STYLE_CLASS_BACK_BUTTON = "back-button";
1717
/**
1818
* Style class to match the window background
@@ -226,11 +226,6 @@ namespace Granite {
226226
*/
227227
public const string ACCENT = "accent";
228228

229-
/**
230-
* Style class for a {@link Gtk.Button} which is used to navigate backwards
231-
*/
232-
public const string BACK = "back-button";
233-
234229
/**
235230
* Style class for adding a small shadow to a container such as for image thumbnails
236231
*/

lib/Styles/Gtk/Button.scss

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -57,44 +57,6 @@ button {
5757
}
5858
}
5959

60-
// Stopgap since we can't do angled buttons in GtkCSS, and generating all
61-
// the necessary SVGs for light/dark and accent color combinations is
62-
// untenable. Ideally we'd deprecate this in favor of something like a
63-
// Granite.BackButton with custom drawing; until then, stick an icon in it.
64-
65-
&.back-button {
66-
background-repeat: no-repeat no-repeat;
67-
background-size: 16px, cover;
68-
69-
&:dir(ltr) {
70-
background-image:
71-
-gtk-icontheme('go-previous-symbolic'),
72-
linear-gradient(
73-
to bottom,
74-
#{'alpha(@highlight_color, 0.2)'},
75-
rgba(white, 0)
76-
);
77-
padding-left: calc(#{rem(9px)} + 16px);
78-
background-position:
79-
#{rem(6px)} 50%,
80-
center, center;
81-
}
82-
83-
&:dir(rtl) {
84-
background-image:
85-
-gtk-icontheme('go-next-symbolic'),
86-
linear-gradient(
87-
to bottom,
88-
#{'alpha(@highlight_color, 0.2)'},
89-
rgba(white, 0)
90-
);
91-
padding-right: calc(#{rem(9px)} + 16px);
92-
background-position:
93-
calc(100% - #{rem(6px)}) 50%,
94-
center, center;
95-
}
96-
}
97-
9860
&.osd {
9961
@include control;
10062
@include border-interactive-roundrect;
@@ -150,4 +112,9 @@ button {
150112
shadow(2);
151113
}
152114
}
115+
116+
// Almost certainly a button with text and image
117+
> box.horizontal {
118+
border-spacing: $button-spacing / 2;
119+
}
153120
}

lib/Widgets/BackButton.vala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025 elementary, Inc. (https://elementary.io)
3+
* SPDX-License-Identifier: GPL-2.0-or-later
4+
*/
5+
6+
/**
7+
* BackButton is meant to be used in headers to navigate in
8+
* {@link Adw.NavigationView}.
9+
*
10+
* By default `action_name` is set to `navigation.pop`
11+
*/
12+
[Version (since = "7.7.0")]
13+
public class Granite.BackButton : Gtk.Button {
14+
/**
15+
* Text of the label inside of #this
16+
*/
17+
public new string label { get; set; }
18+
19+
public BackButton (string label) {
20+
Object (label: label);
21+
}
22+
23+
construct {
24+
var image = new Gtk.Image.from_icon_name ("go-previous-symbolic");
25+
26+
var label_widget = new Gtk.Label ("");
27+
28+
var box = new Gtk.Box (HORIZONTAL, 0);
29+
box.append (image);
30+
box.append (label_widget);
31+
32+
action_name = "navigation.pop";
33+
child = box;
34+
tooltip_markup = Granite.markup_accel_tooltip ({"<alt>Left"});
35+
36+
add_css_class ("text-button");
37+
bind_property ("label", label_widget, "label");
38+
}
39+
}

lib/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ libgranite_sources = files(
1717
'Widgets/AbstractSettingsPage.vala',
1818
'Widgets/AbstractSimpleSettingsPage.vala',
1919
'Widgets/AccelLabel.vala',
20+
'Widgets/BackButton.vala',
2021
'Widgets/Bin.vala',
2122
'Widgets/Box.vala',
2223
'Widgets/DatePicker.vala',

0 commit comments

Comments
 (0)