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
//! The canvas handles drawing the selection frame
use iced::Event::{Keyboard, Mouse};
use iced::keyboard::Event::KeyPressed;
use iced::keyboard::Event::KeyReleased;
use iced::keyboard::Key::{Character, Named};
use iced::keyboard::Modifiers as Mods;
use iced::keyboard::key::Named::F11;
use iced::keyboard::key::Named::{Enter, Escape, Shift};
use iced::mouse::Button::{Left, Middle, Right};
use iced::mouse::Event::ButtonPressed;
use iced::mouse::Event::ButtonReleased;
use iced::mouse::Event::CursorMoved;
use iced::{
Rectangle, Renderer, Theme,
mouse::{self, Interaction},
widget::{self, Action, canvas},
};
/// Holds information about the mouse
#[derive(Default, Debug, Clone, Copy)]
pub struct MouseState {
/// Left mouse click is currently being held down
is_left_down: bool,
/// Left mouse click is currently being held down
is_right_down: bool,
/// Shift key is currently being held down
is_shift_down: bool,
}
use crate::selection::Speed;
use crate::{
App, CONFIG,
corners::SideOrCorner,
message::Message,
selection::{Selection, SelectionStatus, selection_lock::OptionalSelectionExt as _},
theme::THEME,
};
impl canvas::Program<Message> for App {
type State = MouseState;
fn draw(
&self,
_state: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<canvas::Geometry> {
let mut frame = canvas::Frame::new(renderer, bounds.size());
self.render_shade(&mut frame, bounds);
if let Some(selection) = self.selection.map(Selection::norm) {
selection.render_border(&mut frame, THEME.accent);
selection.corners().render_circles(&mut frame, THEME.accent);
}
vec![frame.into_geometry()]
}
fn mouse_interaction(
&self,
_state: &Self::State,
_bounds: Rectangle,
cursor: iced::advanced::mouse::Cursor,
) -> iced::advanced::mouse::Interaction {
self.selection
// mouse button for resizing the selection
.and_then(|sel| {
// if we are already resizing, then this cursor takes priority
// e.g. we are resizing horizontally but we are on the top left
// corner = we should have horizontal resize cursor.
(if let SelectionStatus::Resize { resize_side, .. } = sel.status {
Some(resize_side.mouse_icon())
} else if sel.status.is_move() {
Some(Interaction::Grabbing)
} else {
None
})
.or_else(|| {
// when we started dragging a side, even if we go outside of the bounds of that side (which
// happens often when we are dragging the mouse fast), we don't want the cursor to change
cursor.position().and_then(|cursor| {
sel.corners().side_at(cursor).map(SideOrCorner::mouse_icon)
})
})
})
.unwrap_or_else(|| {
if self.cursor_in_selection(cursor).is_some() {
Interaction::Grab
} else {
Interaction::Crosshair
}
})
}
fn update(
&self,
state: &mut Self::State,
event: &iced::Event,
_bounds: Rectangle,
cursor: iced::advanced::mouse::Cursor,
) -> Option<widget::Action<Message>> {
let message = match event {
Mouse(ButtonPressed(Left)) => {
state.is_left_down = true;
Message::LeftMouseDown(cursor)
}
Mouse(ButtonPressed(Right)) => {
state.is_right_down = true;
if let Some(cursor) = cursor.position() {
if let Some((selection, sel_is_some)) = self.selection.get() {
Message::ResizeToCursor {
cursor_pos: cursor,
selection: selection.norm(),
sel_is_some,
}
} else {
return None;
}
} else {
return None;
}
}
Mouse(ButtonReleased(Right)) => {
state.is_right_down = false;
Message::EnterIdle
}
Mouse(ButtonReleased(Left)) => {
state.is_left_down = false;
if CONFIG.instant && self.selections_created == 1 {
// we have created 1 selections in total, (the current one),
// in which case we want to copy it to the clipboard as the
// --instant flag was provided
Message::CopyToClipboard
} else {
Message::EnterIdle
}
}
Keyboard(KeyReleased { key, .. }) if *key == Named(Shift) => {
state.is_shift_down = false;
Message::NoOp
}
// Esc
Keyboard(KeyPressed { key, .. }) if *key == Named(Escape) => Message::Exit,
// Ctrl + C or Enter
Keyboard(KeyPressed { key, modifiers, .. })
if (*key == Named(Enter))
|| (*modifiers == Mods::CTRL && *key == Character("c".into())) =>
{
Message::CopyToClipboard
}
// Ctrl + S
Keyboard(KeyPressed { key, modifiers, .. })
if (*modifiers == Mods::CTRL && *key == Character("s".into())) =>
{
Message::SaveScreenshot
}
Keyboard(KeyPressed { key, .. }) if *key == Named(F11) => Message::SelectFullScreen,
Keyboard(KeyPressed { key, .. }) if *key == Named(Shift) => {
state.is_shift_down = true;
// If we are already resizing a side, and we press shift, we
// want to act as if we just started resizing from this point again
// so we do not get a surprising jump
if let Some((selection, sel_is_some)) = self.selection.get() {
cursor
.position()
.map_or(Message::NoOp, |current_cursor_pos| {
if let SelectionStatus::Resize { resize_side, .. } = selection.status {
Message::Resize {
resize_side,
// start resizing from this point on
current_cursor_pos,
initial_cursor_pos: current_cursor_pos,
// the current selection becomes the new starting point
initial_rect: selection.rect,
sel_is_some,
speed: Speed::Slow {
has_speed_changed: true,
},
}
} else if let SelectionStatus::Move { .. } = selection.status {
Message::MoveSelection {
current_cursor_pos,
initial_cursor_pos: current_cursor_pos,
current_selection: selection,
initial_rect_pos: selection.pos(),
speed: Speed::Slow {
has_speed_changed: true,
},
}
} else {
Message::NoOp
}
})
} else {
Message::NoOp
}
}
Mouse(CursorMoved { position }) if self.selection.is_some_and(Selection::is_resize) => {
// FIXME: this will not be necessary when we have `let_chains`
let (selection, sel_is_some) =
self.selection.get().expect("has `.is_some_and()` guard");
// FIXME: this will not be necessary when we have `let_chains`
let SelectionStatus::Resize {
resize_side,
initial_rect,
initial_cursor_pos,
} = selection.status
else {
unreachable!("has `.is_some_and(is_resized)` guard");
};
Message::Resize {
current_cursor_pos: *position,
resize_side,
initial_cursor_pos,
initial_rect,
sel_is_some,
speed: if state.is_shift_down {
Speed::Slow {
has_speed_changed: false,
}
} else {
Speed::Regular
},
}
}
Mouse(CursorMoved { position }) if self.selection.is_some_and(Selection::is_move) => {
// FIXME: this will not be necessary when we have `let_chains`
let current_selection = self.selection.expect("has `.is_some_and()` guard").norm();
// FIXME: this will not be necessary when we have `let_chains`
let SelectionStatus::Move {
initial_rect_pos,
initial_cursor_pos,
} = current_selection.status
else {
unreachable!();
};
Message::MoveSelection {
current_cursor_pos: *position,
initial_cursor_pos,
current_selection,
initial_rect_pos,
speed: if state.is_shift_down {
Speed::Slow {
has_speed_changed: false,
}
} else {
Speed::Regular
},
}
}
Mouse(CursorMoved { position }) if self.selection.is_some_and(Selection::is_create) => {
Message::ExtendNewSelection(*position)
}
Mouse(ButtonPressed(Middle)) => Message::SelectFullScreen,
_ => return None,
};
log::info!("Received message: {message:#?}");
Some(Action::publish(message))
}
}