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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Mutex;
use arrayvec::ArrayVec;
use niri_config::Config;
use ordered_float::NotNan;
use pangocairo::cairo::{self, ImageSurface};
use pangocairo::pango::{Alignment, FontDescription};
use smithay::backend::renderer::element::utils::RescaleRenderElement;
use smithay::backend::renderer::element::Kind;
use smithay::output::Output;
use smithay::reexports::gbm::Format as Fourcc;
use smithay::utils::{Point, Transform};
use crate::animation::{Animation, Clock};
use crate::niri_render_elements;
use crate::render_helpers::memory::MemoryBuffer;
use crate::render_helpers::primary_gpu_texture::PrimaryGpuTextureRenderElement;
use crate::render_helpers::renderer::NiriRenderer;
use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
use crate::render_helpers::texture::{TextureBuffer, TextureRenderElement};
use crate::utils::{output_size, to_physical_precise_round};
const KEY_NAME: &str = "Enter";
const PADDING: i32 = 16;
const FONT: &str = "sans 14px";
const BORDER: i32 = 8;
const BACKDROP_COLOR: [f32; 4] = [0., 0., 0., 0.4];
pub struct ExitConfirmDialog {
state: State,
buffers: RefCell<HashMap<NotNan<f64>, Option<MemoryBuffer>>>,
clock: Clock,
config: Rc<RefCell<Config>>,
}
niri_render_elements! {
ExitConfirmDialogRenderElement => {
Texture = RescaleRenderElement<PrimaryGpuTextureRenderElement>,
SolidColor = SolidColorRenderElement,
}
}
struct OutputData {
backdrop: SolidColorBuffer,
}
enum State {
Hidden,
Showing(Animation),
Visible,
Hiding(Animation),
}
impl ExitConfirmDialog {
pub fn new(clock: Clock, config: Rc<RefCell<Config>>) -> Self {
let buffer = match render(1.) {
Ok(x) => Some(x),
Err(err) => {
warn!("error creating the exit confirm dialog: {err:?}");
None
}
};
Self {
state: State::Hidden,
buffers: RefCell::new(HashMap::from([(NotNan::new(1.).unwrap(), buffer)])),
clock,
config,
}
}
pub fn can_show(&self) -> bool {
let buffers = self.buffers.borrow();
let fallback = &buffers[&NotNan::new(1.).unwrap()];
fallback.is_some()
}
fn animation(&self, from: f64, to: f64) -> Animation {
let c = self.config.borrow();
Animation::new(
self.clock.clone(),
from,
to,
0.,
c.animations.exit_confirmation_open_close.0,
)
}
fn value(&self) -> f64 {
match &self.state {
State::Hidden => 0.,
State::Showing(anim) | State::Hiding(anim) => anim.value(),
State::Visible => 1.,
}
}
/// Returns true if the dialog will be shown (even if it is already shown).
pub fn show(&mut self) -> bool {
if !self.can_show() {
return false;
}
if self.is_open() {
return true;
}
self.state = State::Showing(self.animation(self.value(), 1.));
true
}
/// Returns true if started the hide animation.
pub fn hide(&mut self) -> bool {
if !self.is_open() {
return false;
}
self.state = State::Hiding(self.animation(self.value(), 0.));
true
}
pub fn is_open(&self) -> bool {
matches!(self.state, State::Showing(_) | State::Visible)
}
pub fn advance_animations(&mut self) {
match &mut self.state {
State::Hidden => (),
State::Showing(anim) => {
if anim.is_done() {
self.state = State::Visible;
}
}
State::Visible => (),
State::Hiding(anim) => {
if anim.is_clamped_done() {
self.state = State::Hidden;
}
}
}
}
pub fn are_animations_ongoing(&self) -> bool {
matches!(self.state, State::Showing(_) | State::Hiding(_))
}
pub fn render<R: NiriRenderer>(
&self,
renderer: &mut R,
output: &Output,
) -> ArrayVec<ExitConfirmDialogRenderElement, 2> {
let mut rv = ArrayVec::new();
let (value, clamped_value) = match &self.state {
State::Hidden => return rv,
State::Showing(anim) | State::Hiding(anim) => (anim.value(), anim.clamped_value()),
State::Visible => (1., 1.),
};
// Can be out of range when starting from past 0. or 1. from a spring bounce.
let clamped_value = clamped_value.clamp(0., 1.);
let scale = output.current_scale().fractional_scale();
let output_size = output_size(output);
let mut buffers = self.buffers.borrow_mut();
let Some(fallback) = buffers[&NotNan::new(1.).unwrap()].clone() else {
error!("exit confirm dialog opened without fallback buffer");
return rv;
};
let buffer = buffers
.entry(NotNan::new(scale).unwrap())
.or_insert_with(|| render(scale).ok());
let buffer = buffer.as_ref().unwrap_or(&fallback);
let size = buffer.logical_size();
let Ok(buffer) = TextureBuffer::from_memory_buffer(renderer.as_gles_renderer(), buffer)
else {
return rv;
};
let location = (output_size.to_point() - size.to_point()).downscale(2.);
let mut location = location.to_physical_precise_round(scale).to_logical(scale);
location.x = f64::max(0., location.x);
location.y = f64::max(0., location.y);
let elem = TextureRenderElement::from_texture_buffer(
buffer,
location,
clamped_value as f32,
None,
None,
Kind::Unspecified,
);
let elem = PrimaryGpuTextureRenderElement(elem);
let elem = RescaleRenderElement::from_element(
elem,
(location + size.downscale(2.)).to_physical_precise_round(scale),
value.max(0.) * 0.2 + 0.8,
);
rv.push(ExitConfirmDialogRenderElement::Texture(elem));
// Backdrop.
let data = output.user_data().get_or_insert(|| {
Mutex::new(OutputData {
backdrop: SolidColorBuffer::new(output_size, BACKDROP_COLOR),
})
});
let mut data = data.lock().unwrap();
data.backdrop.resize(output_size);
let elem = SolidColorRenderElement::from_buffer(
&data.backdrop,
Point::new(0., 0.),
clamped_value as f32,
Kind::Unspecified,
);
rv.push(ExitConfirmDialogRenderElement::SolidColor(elem));
rv
}
}
fn render(scale: f64) -> anyhow::Result<MemoryBuffer> {
let _span = tracy_client::span!("exit_confirm_dialog::render");
let markup = text(true);
let
|