-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathnotification_block.rs
More file actions
145 lines (125 loc) · 4.2 KB
/
notification_block.rs
File metadata and controls
145 lines (125 loc) · 4.2 KB
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
use std::time::Duration;
use serde::Deserialize;
use crate::bus::dbus::Urgency;
use crate::config::Color;
use crate::maths_utility::{self, Rect, Vec2};
use crate::rendering::layout::{DrawableLayoutElement, Hook};
use crate::rendering::window::{NotifyWindow, UpdateModes};
#[derive(Debug, Deserialize, Clone)]
pub struct NotificationBlockParameters {
pub monitor: i8,
pub border_width: f64,
pub border_rounding: f64,
pub rounded_border_corners: bool,
pub background_color: Color,
pub border_color: Color,
pub gap: Vec2,
pub notification_hook: Hook,
pub border_color_low: Option<Color>,
pub border_color_critical: Option<Color>,
pub border_color_paused: Option<Color>,
#[serde(skip)]
current_update_mode: UpdateModes,
}
impl DrawableLayoutElement for NotificationBlockParameters {
fn draw(
&self,
_hook: &Hook,
_offset: &Vec2,
parent_rect: &Rect,
window: &NotifyWindow,
) -> Result<Rect, cairo::Error> {
// Clear
window.context.set_operator(cairo::Operator::Clear);
window.context.paint()?;
window.context.set_operator(cairo::Operator::Source);
// Draw border + background.
// If anything isn't updating, we count it as paused, which overrides urgency.
// Otherwise, we evaluate urgency.
let bd_color = {
if window.update_mode != UpdateModes::all() {
self.border_color_paused.as_ref().unwrap_or(&self.border_color)
} else {
match window.notification.urgency {
Urgency::Low => self.border_color_low.as_ref().unwrap_or(&self.border_color),
Urgency::Normal => &self.border_color,
Urgency::Critical => self.border_color_critical.as_ref().unwrap_or(&self.border_color),
}
}
};
//let bd_color = &self.border_color;
let bg_color = &self.background_color;
let bw = &self.border_width;
let radius = &self.border_rounding;
let w = parent_rect.width();
let h = parent_rect.height();
let outer_radius = if self.rounded_border_corners {
radius.min(w.min(h) / 2.0 )
} else {
0.0
};
let inner_radius = (radius - bw).max(0.0).min((w - bw * 2.0).min(h - bw * 2.0) / 2.0);
// Draw border
window.context.new_path();
maths_utility::cairo_path_rounded_rectangle(
&window.context,
0.0,
0.0,
w,
h,
outer_radius,
)?;
window.context.new_sub_path();
maths_utility::cairo_path_rounded_rectangle(
&window.context,
*bw,
*bw, // x, y
w - bw * 2.0,
h - bw * 2.0,
inner_radius,
)?;
window.context.set_source_rgba(bd_color.r, bd_color.g, bd_color.b, bd_color.a);
window.context.set_fill_rule(cairo::FillRule::EvenOdd);
window.context.fill()?;
// Draw background
maths_utility::cairo_path_rounded_rectangle(
&window.context,
*bw,
*bw, // x, y
w - bw * 2.0,
h - bw * 2.0,
inner_radius,
)?;
window.context.set_source_rgba(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
window.context.set_fill_rule(cairo::FillRule::Winding);
window.context.fill()?;
Ok(Rect::new(
parent_rect.x(),
parent_rect.y(),
parent_rect.width(),
parent_rect.height(),
))
}
fn predict_rect_and_init(
&mut self,
_hook: &Hook,
_offset: &Vec2,
parent_rect: &Rect,
window: &NotifyWindow,
) -> Rect {
self.current_update_mode = window.update_mode;
Rect::new(
parent_rect.x(),
parent_rect.y(),
parent_rect.width(),
parent_rect.height(),
)
}
fn update(&mut self, _delta_time: Duration, window: &NotifyWindow) -> bool {
if window.update_mode != self.current_update_mode {
self.current_update_mode = window.update_mode;
return true;
}
false
}
}