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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use std::ops::{Add, Sub};
use anyhow::Result;
use femtovg::{Paint, Path};
use relm4::{
gtk::gdk::{Key, ModifierType},
Sender,
};
use serde_derive::Deserialize;
use crate::{
configuration::APP_CONFIG,
math::{self, Vec2D},
sketch_board::{MouseButton, MouseEventMsg, MouseEventType, SketchBoardInput},
style::Style,
tools::DrawableClone,
};
use satty_cli::command_line;
use super::{Drawable, Tool, ToolUpdateResult, Tools};
const HIGHLIGHT_OPACITY: f64 = 0.4;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Highlighters {
Block = 0,
Freehand = 1,
}
impl From<command_line::Highlighters> for Highlighters {
fn from(tool: command_line::Highlighters) -> Self {
match tool {
command_line::Highlighters::Block => Self::Block,
command_line::Highlighters::Freehand => Self::Freehand,
}
}
}
#[derive(Clone, Debug)]
struct BlockHighlight {
top_left: Vec2D,
size: Option<Vec2D>,
}
#[derive(Clone, Debug)]
struct FreehandHighlight {
points: Vec<Vec2D>,
shift_pressed: bool,
}
#[derive(Clone, Debug)]
struct Highlighter<T> {
data: T,
style: Style,
}
trait Highlight {
fn highlight(&self, canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>) -> Result<()>;
}
impl Highlight for Highlighter<FreehandHighlight> {
fn highlight(&self, canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>) -> Result<()> {
canvas.save();
let mut path = Path::new();
let first = self
.data
.points
.first()
.expect("should exist at least one point in highlight instance.");
path.move_to(first.x, first.y);
for p in self.data.points.iter().skip(1) {
path.line_to(first.x + p.x, first.y + p.y);
}
let mut paint = Paint::color(femtovg::Color::rgba(
self.style.color.r,
self.style.color.g,
self.style.color.b,
(255.0 * HIGHLIGHT_OPACITY) as u8,
));
paint.set_line_width(
self.style
.size
.to_highlight_width(self.style.annotation_size_factor),
);
paint.set_line_join(femtovg::LineJoin::Round);
paint.set_line_cap(femtovg::LineCap::Square);
canvas.stroke_path(&path, &paint);
canvas.restore();
Ok(())
}
}
impl Highlight for Highlighter<BlockHighlight> {
fn highlight(&self, canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>) -> Result<()> {
let size = match self.data.size {
Some(s) => s,
None => return Ok(()), // early exit if size is none
};
let (pos, size) = math::rect_ensure_positive_size(self.data.top_left, size);
let mut shadow_path = Path::new();
shadow_path.rounded_rect(
pos.x,
pos.y,
size.x,
size.y,
APP_CONFIG.read().corner_roundness(),
);
let shadow_paint = Paint::color(femtovg::Color::rgba(
self.style.color.r,
self.style.color.g,
self.style.color.b,
(255.0 * HIGHLIGHT_OPACITY) as u8,
));
canvas.fill_path(&shadow_path, &shadow_paint);
Ok(())
}
}
#[derive(Clone, Debug)]
enum HighlightKind {
Block(Highlighter<BlockHighlight>),
Freehand(Highlighter<FreehandHighlight>),
}
#[derive(Default, Clone, Debug)]
pub struct HighlightTool {
highlighter: Option<HighlightKind>,
style: Style,
input_enabled: bool,
sender: Option<Sender<SketchBoardInput>>,
}
impl Drawable for HighlightKind {
fn draw(
&self,
canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>,
_font: femtovg::FontId,
_bounds: (Vec2D, Vec2D),
) -> Result<()> {
match self {
HighlightKind::Block(highlighter) => highlighter.highlight(canvas),
HighlightKind::Freehand(highlighter) => highlighter.highlight(canvas),
}
}
}
impl Tool for HighlightTool {
fn input_enabled(&self) -> bool {
self.input_enabled
}
fn set_input_enabled(&mut self, value: bool) {
self.input_enabled = value;
}
fn get_tool_type(&self) -> super::Tools {
Tools::Highlight
}
fn handle_mouse_event(&mut self, event: MouseEventMsg) -> ToolUpdateResult {
let shift_pressed = event.modifier.intersects(ModifierType::SHIFT_MASK);
let ctrl_pressed = event.modifier.intersects(ModifierType::CONTROL_MASK);
let primary_highlighter = APP_CONFIG.read().primary_highlighter();
match event.type_ {
MouseEventType::BeginDrag => {
if event.button == MouseButton::Middle {
return ToolUpdateResult::Unmodified;
}
// There exists two types of highlighting modes currently: freehand, block
// A user may set a primary highlighter mode, with the other being accessible
// by clicking CTRL when starting a highlight (doesn't need to be held).
match (primary_highlighter, ctrl_pressed) {
// This matches when CTRL is not pressed and the primary highlighting mode
// is block, along with its inverse, CTRL pressed with the freehand mode
// being their primary highlighting mode.
(Highlighters::Block, false) | (Highlighters::Freehand, true) => {
self.highlighter =
Some(HighlightKind::Block(Highlighter::<BlockHighlight> {
data: BlockHighlight {
top_left: event.pos,
size: None,
},
style: self.style,
}))
}
// This matches the remaining two cases, which is when the user has the
// freehand mode as the primary mode and CTRL is not pressed, and conversely,
// when CTRL is pressed and the users primary mode is block.
(Highlighters::Freehand, false) | (Highlighters::Block, true) => {
self.highlighter =
Some(HighlightKind::Freehand(Highlighter::<FreehandHighlight> {
data: FreehandHighlight {
points: vec![event.pos],
shift_pressed,
},
style: self.style,
}))
}
}
ToolUpdateResult::Redraw
}
MouseEventType::UpdateDrag | MouseEventType::EndDrag => {
if event.button == MouseButton::Middle {
return ToolUpdateResult::Unmodified;
}
if self.highlighter.is_none() {
return ToolUpdateResult::Unmodified;
}
let mut highlighter_kind = self.highlighter.as_mut().unwrap();
let update: ToolUpdateResult = match &mut highlighter_kind {
HighlightKind::Block(highlighter) => {
// When shift is pressed when using the block highlighter, it transforms
// the area into a perfect square (in the direction they intended).
if shift_pressed {
let max_size = event.pos.x.abs().max(event.pos.y.abs());
highlighter.data.size = Some(Vec2D {
x: max_size * event.pos.x.signum(),
y: max_size * event.pos.y.signum(),
});
} else {
highlighter.data.size = Some(event.pos);
};
ToolUpdateResult::Redraw
}
HighlightKind::Freehand(highlighter) => {
if event.pos == Vec2D::zero() {
return ToolUpdateResult::Unmodified;
};
// The freehand highlighter has a more complex shift model:
// when pressing shift it begins a straight line, which is aligned
// from the point after shift was pressed, to any 15*n degree rotation.
//
// After releasing shift, it creates an extra point, this is useful since
// it means that users do not need to move their mouse to achieve perfectly
// aligned turns, since they can release, then hold shift again to continue
// another aligned line.
// This extra point can be removed by releasing shift again (if the cursor
// hasn't moved)
if shift_pressed {
// if shift was pressed before we remove an extra point which would
// have been the previous aligned point. However ignore if there is
// only one point which means the highlight has just started.
if highlighter.data.shift_pressed && highlighter.data.points.len() >= 2
{
highlighter
.data
.points
.pop()
.expect("at least 2 points in highlight path.");
};
// use the last point to position the snapping guide, or 0 if the point
// is the first one.
let last = if highlighter.data.points.len() == 1 {
Vec2D::zero()
} else {
*highlighter
.data
.points
.last_mut()
.expect("at least one point")
};
let snapped_pos = event.pos.sub(last).snapped_vector_15deg().add(last);
highlighter.data.points.push(snapped_pos);
} else {
highlighter.data.points.push(event.pos);
}
highlighter.data.shift_pressed = shift_pressed;
ToolUpdateResult::Redraw
}
};
if event.type_ == MouseEventType::UpdateDrag {
return update;
};
let result = highlighter_kind.clone_box();
self.highlighter = None;
ToolUpdateResult::Commit(result)
}
_ => ToolUpdateResult::Unmodified,
}
}
fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.highlighter.is_some() {
self.highlighter = None;
return ToolUpdateResult::Redraw;
}
ToolUpdateResult::Unmodified
}
fn handle_key_release_event(
&mut self,
event: crate::sketch_board::KeyEventMsg,
) -> ToolUpdateResult {
// Adds an extra point when shift is released in the freehand mode, this
// allows for users to make sharper turns. Release shift a second time
// to remove the added point (only if the cursor has not moved).
if event.key == Key::Shift_L || event.key == Key::Shift_R {
if let Some(HighlightKind::Freehand(highlighter)) = &mut self.highlighter {
let points = &mut highlighter.data.points;
let last = points
.last()
.expect("line highlight must have at least one point");
if points.len() >= 2 {
if *last == points[points.len() - 2] {
points.pop();
} else {
points.push(*last);
}
return ToolUpdateResult::Redraw;
};
};
}
ToolUpdateResult::Unmodified
}
fn handle_style_event(&mut self, style: Style) -> ToolUpdateResult {
self.style = style;
ToolUpdateResult::Unmodified
}
fn get_drawable(&self) -> Option<&dyn Drawable> {
match &self.highlighter {
Some(d) => Some(d),
None => None,
}
}
fn set_sender(&mut self, sender: Sender<SketchBoardInput>) {
self.sender = Some(sender);
}
}