Skip to content

Commit d436c59

Browse files
committed
Rename mouse::Cursor to Mouse
1 parent 363b0e9 commit d436c59

88 files changed

Lines changed: 1000 additions & 1353 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

benches/wgpu.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
33

44
use iced::alignment;
5-
use iced::mouse;
65
use iced::widget::{canvas, scrollable, stack, text};
76
use iced::{
8-
Color, Element, Font, Length, Pixels, Point, Rectangle, Size, Theme,
7+
Color, Element, Font, Length, Mouse, Pixels, Point, Rectangle, Size, Theme,
98
};
109
use iced_wgpu::Renderer;
1110
use iced_wgpu::wgpu;
@@ -129,7 +128,7 @@ fn benchmark<'a>(
129128
&core::renderer::Style {
130129
text_color: Color::WHITE,
131130
},
132-
mouse::Cursor::Unavailable,
131+
Mouse::Unavailable,
133132
);
134133

135134
cache = Some(user_interface.into_cache());
@@ -172,7 +171,7 @@ fn scene<'a, Message: 'a>(n: usize) -> Element<'a, Message, Theme, Renderer> {
172171
renderer: &Renderer,
173172
_theme: &Theme,
174173
bounds: Rectangle,
175-
_cursor: mouse::Cursor,
174+
_mouse: Mouse,
176175
) -> Vec<canvas::Geometry<Renderer>> {
177176
vec![cache.draw(renderer, bounds.size(), |frame| {
178177
for i in 0..self.n {

core/src/element.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::renderer;
55
use crate::widget;
66
use crate::widget::tree::{self, Tree};
77
use crate::{
8-
Border, Clipboard, Color, Event, Layout, Length, Rectangle, Shell, Size,
9-
Vector, Widget,
8+
Border, Clipboard, Color, Event, Layout, Length, Mouse, Rectangle, Shell,
9+
Size, Vector, Widget,
1010
};
1111

1212
use std::borrow::Borrow;
@@ -314,7 +314,7 @@ where
314314
tree: &mut Tree,
315315
event: &Event,
316316
layout: Layout<'_>,
317-
cursor: mouse::Cursor,
317+
mouse: Mouse,
318318
renderer: &Renderer,
319319
clipboard: &mut dyn Clipboard,
320320
shell: &mut Shell<'_, B>,
@@ -327,7 +327,7 @@ where
327327
tree,
328328
event,
329329
layout,
330-
cursor,
330+
mouse,
331331
renderer,
332332
clipboard,
333333
&mut local_shell,
@@ -344,23 +344,23 @@ where
344344
theme: &Theme,
345345
style: &renderer::Style,
346346
layout: Layout<'_>,
347-
cursor: mouse::Cursor,
347+
mouse: Mouse,
348348
viewport: &Rectangle,
349349
) {
350350
self.widget
351-
.draw(tree, renderer, theme, style, layout, cursor, viewport);
351+
.draw(tree, renderer, theme, style, layout, mouse, viewport);
352352
}
353353

354354
fn mouse_interaction(
355355
&self,
356356
tree: &Tree,
357357
layout: Layout<'_>,
358-
cursor: mouse::Cursor,
358+
mouse: Mouse,
359359
viewport: &Rectangle,
360360
renderer: &Renderer,
361361
) -> mouse::Interaction {
362362
self.widget
363-
.mouse_interaction(tree, layout, cursor, viewport, renderer)
363+
.mouse_interaction(tree, layout, mouse, viewport, renderer)
364364
}
365365

366366
fn overlay<'b>(
@@ -450,14 +450,14 @@ where
450450
state: &mut Tree,
451451
event: &Event,
452452
layout: Layout<'_>,
453-
cursor: mouse::Cursor,
453+
mouse: Mouse,
454454
renderer: &Renderer,
455455
clipboard: &mut dyn Clipboard,
456456
shell: &mut Shell<'_, Message>,
457457
viewport: &Rectangle,
458458
) {
459459
self.element.widget.update(
460-
state, event, layout, cursor, renderer, clipboard, shell, viewport,
460+
state, event, layout, mouse, renderer, clipboard, shell, viewport,
461461
);
462462
}
463463

@@ -468,7 +468,7 @@ where
468468
theme: &Theme,
469469
style: &renderer::Style,
470470
layout: Layout<'_>,
471-
cursor: mouse::Cursor,
471+
mouse: Mouse,
472472
viewport: &Rectangle,
473473
) {
474474
fn explain_layout<Renderer: crate::Renderer>(
@@ -496,7 +496,7 @@ where
496496

497497
self.element
498498
.widget
499-
.draw(state, renderer, theme, style, layout, cursor, viewport);
499+
.draw(state, renderer, theme, style, layout, mouse, viewport);
500500

501501
explain_layout(renderer, self.color, layout);
502502
}
@@ -505,13 +505,13 @@ where
505505
&self,
506506
state: &Tree,
507507
layout: Layout<'_>,
508-
cursor: mouse::Cursor,
508+
mouse: Mouse,
509509
viewport: &Rectangle,
510510
renderer: &Renderer,
511511
) -> mouse::Interaction {
512512
self.element
513513
.widget
514-
.mouse_interaction(state, layout, cursor, viewport, renderer)
514+
.mouse_interaction(state, layout, mouse, viewport, renderer)
515515
}
516516

517517
fn overlay<'b>(

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub use image::Image;
6565
pub use input_method::InputMethod;
6666
pub use layout::Layout;
6767
pub use length::Length;
68+
pub use mouse::Mouse;
6869
pub use overlay::Overlay;
6970
pub use padding::Padding;
7071
pub use pixels::Pixels;

core/src/mouse.rs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,103 @@
22
pub mod click;
33

44
mod button;
5-
mod cursor;
65
mod event;
76
mod interaction;
87

98
pub use button::Button;
109
pub use click::Click;
11-
pub use cursor::Cursor;
1210
pub use event::{Event, ScrollDelta};
1311
pub use interaction::Interaction;
12+
13+
use crate::{Point, Rectangle, Transformation, Vector};
14+
15+
/// The mouse state.
16+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
17+
pub enum Mouse {
18+
/// The mouse has a defined position.
19+
Available(Point),
20+
21+
/// The mouse has a defined position, but it's levitating over a layer above.
22+
Levitating(Point),
23+
24+
/// The mouse is currently unavailable (i.e. out of bounds or busy).
25+
#[default]
26+
Unavailable,
27+
}
28+
29+
impl Mouse {
30+
/// Returns the absolute position of the [`Mouse`], if available.
31+
pub fn position(self) -> Option<Point> {
32+
match self {
33+
Mouse::Available(position) => Some(position),
34+
Mouse::Levitating(_) | Mouse::Unavailable => None,
35+
}
36+
}
37+
38+
/// Returns the absolute position of the [`Mouse`], if available and inside
39+
/// the given bounds.
40+
///
41+
/// If the [`Mouse`] is not over the provided bounds, this method will
42+
/// return `None`.
43+
pub fn position_over(self, bounds: Rectangle) -> Option<Point> {
44+
self.position().filter(|p| bounds.contains(*p))
45+
}
46+
47+
/// Returns the relative position of the [`Mouse`] inside the given bounds,
48+
/// if available.
49+
///
50+
/// If the [`Mouse`] is not over the provided bounds, this method will
51+
/// return `None`.
52+
pub fn position_in(self, bounds: Rectangle) -> Option<Point> {
53+
self.position_over(bounds)
54+
.map(|p| p - Vector::new(bounds.x, bounds.y))
55+
}
56+
57+
/// Returns the relative position of the [`Mouse`] from the given origin,
58+
/// if available.
59+
pub fn position_from(self, origin: Point) -> Option<Point> {
60+
self.position().map(|p| p - Vector::new(origin.x, origin.y))
61+
}
62+
63+
/// Returns true if the [`Mouse`] is over the given `bounds`.
64+
pub fn is_over(self, bounds: Rectangle) -> bool {
65+
self.position_over(bounds).is_some()
66+
}
67+
68+
/// Returns true if the [`Mouse`] is levitating over a layer above.
69+
pub fn is_levitating(self) -> bool {
70+
matches!(self, Self::Levitating(_))
71+
}
72+
73+
/// Makes the [`Mouse`] levitate over a layer above.
74+
pub fn levitate(self) -> Self {
75+
match self {
76+
Self::Available(position) => Self::Levitating(position),
77+
_ => self,
78+
}
79+
}
80+
81+
/// Brings the [`Mouse`] back to the current layer.
82+
pub fn land(self) -> Self {
83+
match self {
84+
Mouse::Levitating(position) => Mouse::Available(position),
85+
_ => self,
86+
}
87+
}
88+
}
89+
90+
impl std::ops::Mul<Transformation> for Mouse {
91+
type Output = Self;
92+
93+
fn mul(self, transformation: Transformation) -> Self {
94+
match self {
95+
Self::Available(position) => {
96+
Self::Available(position * transformation)
97+
}
98+
Self::Levitating(position) => {
99+
Self::Levitating(position * transformation)
100+
}
101+
Self::Unavailable => Self::Unavailable,
102+
}
103+
}
104+
}

core/src/mouse/cursor.rs

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)