1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use std::{cell::RefCell, fmt::Debug, path::PathBuf};
use waybar_cffi::gtk::{
self as gtk, Border, CssProvider, IconLookupFlags, IconSize, IconTheme, ReliefStyle,
StateFlags,
gdk_pixbuf::Pixbuf,
prelude::{ButtonExt, CssProviderExt, GdkPixbufExt, IconThemeExt, StyleContextExt, WidgetExt},
};
use crate::state::State;
/// A taskbar button.
pub struct Button {
app_id: Option<String>,
button: gtk::Button,
state: State,
}
impl Debug for Button {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Button")
.field("app_id", &self.app_id)
.finish()
}
}
// These have to be declared as thread locals because Gtk objects are (generally) not Send.
// Practically, we're likely to be doing everything from the main thread anyway, but Glib can
// figure that out.
thread_local! {
static BUTTON_CSS_PROVIDER: CssProvider = {
let css = CssProvider::new();
if let Err(e) = css.load_from_data(include_bytes!("style.css")) {
tracing::error!(%e, "CSS parse error");
}
css
};
static ICON_THEME: IconTheme = {
IconTheme::default().unwrap_or_default()
}
}
impl Button {
/// Instantiates a new button, including creating a new Gtk button internally.
#[tracing::instrument(level = "TRACE", fields(app_id = &window.app_id))]
pub fn new(state: &State, window: &niri_ipc::Window) -> Self {
let state = state.clone();
// Set up the basic image button.
//
// Note that we don't actually set the image here: we need to know the size before doing so
// in order to load the most appropriate icon from the icon theme, and we won't know that
// until we get an actual size allocation.
let button = gtk::Button::new();
button.set_always_show_image(true);
button.set_relief(ReliefStyle::None);
// Provide the base CSS for each button that users can then extend.
BUTTON_CSS_PROVIDER.with(|provider| {
button
.style_context()
.add_provider(provider, gtk::STYLE_PROVIDER_PRIORITY_APPLICATION);
});
let app_id = window.app_id.clone();
let icon_path = app_id
.as_deref()
.and_then(|id| state.icon_cache().lookup(id));
let button = Self {
app_id,
button,
state,
};
// Set up our event handlers. It's easier to do this with self already available.
button.connect_click_handler(window.id);
button.connect_size_allocate(icon_path);
button
}
/// Sets whether the window represented by this button is currently focused.
#[tracing::instrument(level = "TRACE")]
pub fn set_focus(&self, focus: bool) {
let context = self.button.style_context();
if focus {
context.add_class("focused");
context.remove_class("urgent");
} else {
context.remove_class("focused");
}
}
/// Sets the window title.
#[tracing::instrument(level = "TRACE")]
pub fn set_title(&self, title: Option<&str>) {
self.button.set_tooltip_text(title);
// Apply any app styling rules.
if let Some(app_id) = &self.app_id {
if let Some(title) = title {
let config = self.state.config();
let context = self.button.style_context();
// First, remove all the possible classes for this app.
for class in config.app_classes(app_id) {
context.remove_class(class);
}
// Now add the classes that actually do match.
for class in config.app_matches(app_id, title) {
context.add_class(class);
}
}
}
}
/// Sets the window to urgent: that is, needing attention.
///
/// This state is automatically cleared the next time the window is focused.
#[tracing::instrument(level = "TRACE")]
pub fn set_urgent(&self) {
self.button.style_context().add_class("urgent");
}
/// Returns the actual [`gtk::Button`] widget.
pub fn widget(&self) -> >k::Button {
&self.button
}
fn connect_click_handler(&self, window_id: u64) {
let state = self.state.clone();
self.button.connect_clicked(move |_| {
if let Err(e) = state.niri().activate_window(window_id) {
tracing::warn!(%e, id = window_id, "error trying to activate window");
}
});
}
#[tracing::instrument(level = "TRACE")]
fn connect_size_allocate(&self, icon_path: Option<PathBuf>) {
let last_size = RefCell::new(None);
self.button
.connect_size_allocate(move |button, allocation| {
// Figure out if we actually need to redraw, since it's relatively expensive.
//
// The first condition is pretty easy: is there an image on the button? If not,
// then it's the first draw, and we have no choice but to draw.
let mut must_redraw = button.image().is_none();
// Otherwise, let's check if the size allocation has changed since the last time
// this was called.
if !must_redraw {
if let Some(last_size) = last_size.take() {
if &last_size != allocation {
must_redraw = true;
}
} else {
must_redraw = true;
}
last_size.replace(Some(*allocation));
}
if must_redraw {
// Calculate the actual image size we need.
//
// Gtk3 doesn't provide a useful way to get the actual inner size of the
// element after applying style rules, so we have to do that here, otherwise we
// may draw the image too big and cause the container to grow. (Which will then
// result in another size allocate signal, which will result in another
// recalculation, which then results in your taskbar taking up your entire
// display within a few seconds.)
//
// Blindly using StateFlags::NORMAL probably isn't actually the right
// behaviour, but it's the best we've got for now.
//
// Note that we have to do this _after_ we figure out if we need to redraw:
// calculating the style information is apparently expensive enough that Gtk
// essentially busy-waits, which (a) burns CPU, and (b) means that :hover
// styles don't get applied. What that means in practice is that, if waybar's
// dynamically reloading CSS feature is enabled, sizing changes won't be
// applied after the button is first rendered.
//
// That seems to be the price we have to pay, though, so here we are.
let context = button.style_context();
let border = context.border(StateFlags::NORMAL);
let margin = context.margin(StateFlags::NORMAL);
let padding = context.padding(StateFlags::NORMAL);
let size = allocation.height()
- border.vertical_size()
- margin.vertical_size()
- padding.vertical_size();
// Now we know the size, we can actually load the image.
let image =
Self::icon_image(icon_path.as_ref(), button, size).unwrap_or_else(|| {
// If we can't find an application icon, then we need to use a
// fallback.
static FALLBACK_ICON: &str = "application-x-executable";
// We'll try to look the icon up in the default icon theme, since then
// we can load up the actual image and control its scaling and display.
ICON_THEME
.with(|theme| {
theme.lookup_icon_for_scale(
FALLBACK_ICON,
size,
button.scale_factor(),
IconLookupFlags::empty(),
)
})
.and_then(|info| {
Self::icon_image(info.filename().as_ref(), button, size)
})
.unwrap_or_else(|| {
// But, if all else fails, we'll just use the default button
// size and YOLO it.
gtk::Image::from_icon_name(
Some(FALLBACK_ICON),
IconSize::Button,
)
})
});
// Finally, we can set the button image. Doing this from the callback doesn't
// seem to work reliably for reasons I don't understand at all, but doing it
// from the main loop as soon as possible does. :shrug:
let button = button.clone();
gtk::glib::source::idle_add_local_once(move || {
button.set_image(Some(&image));
});
}
});
}
fn icon_image(
icon_path: Option<&PathBuf>,
button: >k::Button,
size: i32,
) -> Option<gtk::Image> {
let size = size * button.scale_factor();
icon_path
.and_then(
|path| match Pixbuf::from_file_at_scale(path, size, size, true) {
Ok(pixbuf) => Some(pixbuf),
Err(e) => {
tracing::info!(%e, ?path, "cannot load icon");
None
}
},
)
.and_then(|pixbuf| pixbuf.create_surface(0, button.window().as_ref()))
.map(|surface| gtk::Image::from_surface(Some(&surface)))
}
}
trait BorderExt {
fn vertical_size(&self) -> i32;
}
impl BorderExt for Border {
fn vertical_size(&self) -> i32 {
(self.top + self.bottom).into()
}
}