-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwindow.rs
333 lines (279 loc) · 12 KB
/
window.rs
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::time::Duration;
use winit::{
dpi::{PhysicalPosition, PhysicalSize},
event_loop::EventLoopWindowTarget,
platform::unix::{WindowBuilderExtUnix, WindowExtUnix, XWindowType},
window::{Window, WindowBuilder},
};
use chrono::{DateTime, Local};
use cairo::{Context, Surface};
use cairo_sys;
use crate::{
bus::dbus::Notification,
config::Config,
manager::NotifyWindowManager,
maths_utility::{Rect, Vec2},
rendering::layout::LayoutBlock,
rendering::text::TextRenderer,
};
// FuseOnly probably won't be used, but it's here for completion's sake.
bitflags! {
#[derive(Default)]
pub struct UpdateModes: u8 {
const DRAW = 0b00000001;
const FUSE = 0b00000010;
}
}
#[derive(Debug)]
pub struct NotifyWindow {
// Context/Surface are placed at the top (in order) so that they are dropped first when a
// window is dropped.
pub context: Context,
pub surface: Surface,
// Each window has a text renderer to handle all text rendering for that window.
pub text: TextRenderer,
pub winit: Window,
pub notification: Notification,
// Layout is cloned from config so each notification can have its own mutable copy.
// This is pretty much just so we can change some params on LayoutBlocks, which is a bit
// wasteful, but easy.
pub layout: Option<LayoutBlock>,
pub marked_for_destroy: bool,
// Master offset is used to offset all *elements* when drawing.
// It is useful when the notification expands in either left or top direction.
pub master_offset: Vec2,
pub fuse: i32,
// `update_enabled` is primarily used for pause functionality right now.
//pub update_enabled: bool,
pub update_mode: UpdateModes,
// Dirty state -- will be redrawn if this is true.
pub dirty: bool,
pub creation_timestamp: DateTime<Local>,
// Last mouse pos, relative to top left of window.
last_mouse_pos: Vec2,
}
impl NotifyWindow {
pub fn new(
el: &EventLoopWindowTarget<()>,
notification: Notification,
manager: &NotifyWindowManager,
) -> Self {
let cfg = Config::get();
// The minimum window width and height is 1.0. We need this size to generate an initial window.
let (width, height) = (
(cfg.min_window_width as f64).max(1.0),
(cfg.min_window_height as f64).max(1.0),
);
// @NOTE: this is pretty messed up... It's annoying that winit only exposes a handle to the
// xlib display through an existing window, which means we have to use a dummy (hidden)
// window to grab it.
// We need the display to do `XMatchVisualInfo`, which we can't set after we've created the
// window.
// We might consider moving away from winit and just using xlib directly. The only part
// we're really using at the moment is the event loop.
let xlib_display = manager
.base_window
.xlib_display()
.expect("Couldn't get xlib_display.");
let visual_info = unsafe {
let mut vinfo = std::mem::MaybeUninit::<x11::xlib::XVisualInfo>::uninit();
let status = (x11::xlib::XMatchVisualInfo)(
xlib_display as _,
x11::xlib::XDefaultScreen(xlib_display as _) as i32,
32,
x11::xlib::TrueColor,
vinfo.as_mut_ptr(),
);
if status == 0 {
panic!("Couldn't get valid XVisualInfo.");
}
vinfo.assume_init()
};
let winit = WindowBuilder::new()
.with_inner_size(PhysicalSize { width, height })
.with_x11_window_type(vec![XWindowType::Notification, XWindowType::Utility])
.with_title("wired")
.with_x11_visual(&visual_info)
.with_transparent(true)
// This was originally here for the below reason, but it causes issues and I haven't
// been able to observe any actual issue, so we leave it out.
//.with_visible(false) // We don't draw/position stuff until later, so best not to show the
// window for now.
// NOTE: you (apparently) can't draw to a window that is not
// visible! So we need to make sure we set this to true before drawing.
.with_override_redirect(true)
.build(el)
.expect("Couldn't build winit window.");
// If these fail, it probably means we aren't on linux.
// In that case, we should fail before now however (`.with_x11_window_type()`).
//let xlib_display = winit.xlib_display().expect("Couldn't get xlib display.");
let xlib_window = winit
.xlib_window()
.expect("Couldn't get xlib window, make sure you're running X11.");
let surface = unsafe {
/*
let visual = x11::xlib::XDefaultVisual(
xlib_display as _,
0,
);
*/
let sfc_raw = cairo_sys::cairo_xlib_surface_create(
xlib_display as _,
xlib_window,
visual_info.visual,
width as _,
height as _,
);
Surface::from_raw_full(sfc_raw)
}.expect("Failed to create cairo surface.");
let context = cairo::Context::new(&surface).expect("Failed to create cairo context.");
let text = TextRenderer::new(&context);
let fuse = notification.timeout;
let mut window = Self {
context,
surface,
text,
winit,
notification,
layout: None,
marked_for_destroy: false,
master_offset: Vec2::default(),
fuse,
update_mode: UpdateModes::all(),
dirty: true, // New windows are dirty -- no drawing has happened yet.
creation_timestamp: Local::now(),
last_mouse_pos: Vec2::new(0.0, 0.0),
};
// When we spawn a window, we get a `RedrawRequested` event which we draw from, so we don't
// manually draw here.
// `Rect::new(0.0, 0.0, width, height) is basically the same as `window.get_inner_rect()`,
// but we don't trust it to be initialized yet.
let mut layout = cfg.layout.as_ref().unwrap().clone();
let rect = layout.predict_rect_tree_and_init(
&window,
//&window.get_inner_rect(),
&Rect::new(0.0, 0.0, width, height), // This parameter is only used for positioning
Rect::new(0.0, 0.0, width, height), // .. so we should also pass the min_size rect here
// to ensure we don't get 0.0 width / 0.0 height.
);
let delta = Vec2::new(-rect.x(), -rect.y());
window.layout = Some(layout);
window.set_size(rect.width(), rect.height());
window.master_offset = delta;
window
}
pub fn replace_notification(&mut self, new_notification: Notification) {
let cfg = Config::get();
self.notification = new_notification;
// Refresh timeout if configured
if cfg.replacing_resets_timeout {
self.fuse = self.notification.timeout;
}
// The minimum window width and height is 1.0. We need this size to generate an initial window.
// TODO: merge the above function with this so we don't get regressions from not doing
// things the same way.
let (width, height) = (
(cfg.min_window_width as f64).max(1.0),
(cfg.min_window_height as f64).max(1.0),
);
// As above. May be valuable to put this into a function like `prepare_notification` or
// something if we keep changing stuff.
let mut layout = Config::get().layout.as_ref().unwrap().clone();
let rect = layout.predict_rect_tree_and_init(self, &Rect::new(0.0, 0.0, width, height), Rect::new(0.0, 0.0, width, height));
let delta = Vec2::new(-rect.x(), -rect.y());
self.layout = Some(layout);
self.set_size(rect.width(), rect.height());
self.master_offset = delta;
self.dirty = true;
}
pub fn _layout(&self) -> &LayoutBlock {
self.layout.as_ref().unwrap()
}
pub fn layout_take(&mut self) -> LayoutBlock {
self.layout.take().unwrap()
}
pub fn set_position(&self, x: f64, y: f64) {
self.winit.set_outer_position(PhysicalPosition { x, y });
}
pub fn _set_visible(&self, visible: bool) {
self.winit.set_visible(visible);
}
pub fn set_size(&self, width: f64, height: f64) {
self.winit.set_inner_size(PhysicalSize { width, height });
unsafe {
cairo_sys::cairo_xlib_surface_set_size(self.surface.to_raw_none(), width as i32, height as i32);
}
}
// Positioned rect on the desktop.
pub fn _get_rect(&self) -> Rect {
let size = self.winit.inner_size();
let pos = self.winit.outer_position().expect("Window no longer exists.");
Rect::new(pos.x.into(), pos.y.into(), size.width.into(), size.height.into())
}
// Pure rectangle, ignoring the window's position.
pub fn get_inner_rect(&self) -> Rect {
let size = self.winit.inner_size();
Rect::new(0.0, 0.0, size.width.into(), size.height.into())
}
/*
pub fn predict_size(&self) -> (Rect, Vec2) {
let layout = self.layout();
let rect = layout.predict_rect_tree(&self, &self.get_inner_rect(), &Rect::EMPTY);
// If x or y are not 0, then we have to offset our drawing by that amount.
let delta = Vec2::new(-rect.x(), -rect.y());
(rect, delta)
}
*/
// This should only ever be called by the windows own `update()`.
// To trigger a redraw, `window.dirty` should be set to `true`.
fn draw(&mut self) {
if !self.dirty {
eprintln!("A draw was triggered for a window that wasn't dirty!");
}
let mut inner_rect = self.get_inner_rect();
// If the master offset is anything other than `(0.0, 0.0)` it means that one of the
// blocks is going to expand the big rectangle leftwards and/or upwards, which would
// cause blocks to be drawn off canvas.
// To fix this, we offset the initial drawing rect to make sure everything fits in the
// canvas.
inner_rect.set_xy(self.master_offset.x, self.master_offset.y);
let mut layout = self.layout_take();
layout.draw_tree(self, &inner_rect, Rect::empty());
self.layout = Some(layout);
}
pub fn update(&mut self, delta_time: Duration) -> bool {
if self.update_mode.contains(UpdateModes::FUSE) {
self.fuse -= delta_time.as_millis() as i32;
if self.fuse <= 0 {
// Window will be destroyed after others have been repositioned to replace it.
// We can return early because drawing will be discarded anyway.
self.marked_for_destroy = true;
return true;
}
}
if self.update_mode.contains(UpdateModes::DRAW) {
let mut layout = self.layout_take();
self.dirty |= layout.update_tree(delta_time, self);
self.layout = Some(layout);
}
if self.dirty {
self.draw();
}
// Clean now, updated everything, but still need to inform manager that we may have changed.
let dirty = self.dirty;
self.dirty = false;
dirty
}
pub fn process_mouse_click(&mut self) {
let mut layout = self.layout_take();
self.dirty |= layout.check_and_send_click(&self.last_mouse_pos, self);
self.layout = Some(layout);
}
pub fn process_mouse_move(&mut self, position: PhysicalPosition<f64>) {
self.last_mouse_pos.x = position.x;
self.last_mouse_pos.y = position.y;
let mut layout = self.layout_take();
self.dirty |= layout.check_and_send_hover(&self.last_mouse_pos, self);
self.layout = Some(layout);
}
}