Take Rectangle by value in Cursor API

This commit is contained in:
Héctor Ramón Jiménez 2023-06-08 20:16:46 +02:00
parent 34451bff18
commit 5c8cfb411e
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
21 changed files with 57 additions and 62 deletions

View file

@ -24,7 +24,7 @@ impl Cursor {
/// ///
/// If the [`Cursor`] is not over the provided bounds, this method will /// If the [`Cursor`] is not over the provided bounds, this method will
/// return `None`. /// return `None`.
pub fn position_over(self, bounds: &Rectangle) -> Option<Point> { pub fn position_over(self, bounds: Rectangle) -> Option<Point> {
self.position().filter(|p| bounds.contains(*p)) self.position().filter(|p| bounds.contains(*p))
} }
@ -33,7 +33,7 @@ impl Cursor {
/// ///
/// If the [`Cursor`] is not over the provided bounds, this method will /// If the [`Cursor`] is not over the provided bounds, this method will
/// return `None`. /// return `None`.
pub fn position_in(self, bounds: &Rectangle) -> Option<Point> { pub fn position_in(self, bounds: Rectangle) -> Option<Point> {
self.position_over(bounds) self.position_over(bounds)
.map(|p| p - Vector::new(bounds.x, bounds.y)) .map(|p| p - Vector::new(bounds.x, bounds.y))
} }
@ -45,7 +45,7 @@ impl Cursor {
} }
/// Returns true if the [`Cursor`] is over the given `bounds`. /// Returns true if the [`Cursor`] is over the given `bounds`.
pub fn is_over(self, bounds: &Rectangle) -> bool { pub fn is_over(self, bounds: Rectangle) -> bool {
self.position_over(bounds).is_some() self.position_over(bounds).is_some()
} }
} }

View file

@ -101,7 +101,7 @@ mod bezier {
cursor: mouse::Cursor, cursor: mouse::Cursor,
) -> (event::Status, Option<Curve>) { ) -> (event::Status, Option<Curve>) {
let cursor_position = let cursor_position =
if let Some(position) = cursor.position_in(&bounds) { if let Some(position) = cursor.position_in(bounds) {
position position
} else { } else {
return (event::Status::Ignored, None); return (event::Status::Ignored, None);
@ -183,7 +183,7 @@ mod bezier {
bounds: Rectangle, bounds: Rectangle,
cursor: mouse::Cursor, cursor: mouse::Cursor,
) -> mouse::Interaction { ) -> mouse::Interaction {
if cursor.is_over(&bounds) { if cursor.is_over(bounds) {
mouse::Interaction::Crosshair mouse::Interaction::Crosshair
} else { } else {
mouse::Interaction::default() mouse::Interaction::default()
@ -226,7 +226,7 @@ mod bezier {
) -> Geometry { ) -> Geometry {
let mut frame = Frame::new(renderer, bounds.size()); let mut frame = Frame::new(renderer, bounds.size());
if let Some(cursor_position) = cursor.position_in(&bounds) { if let Some(cursor_position) = cursor.position_in(bounds) {
match *self { match *self {
Pending::One { from } => { Pending::One { from } => {
let line = Path::line(from, cursor_position); let line = Path::line(from, cursor_position);

View file

@ -407,7 +407,7 @@ mod grid {
} }
let cursor_position = let cursor_position =
if let Some(position) = cursor.position_in(&bounds) { if let Some(position) = cursor.position_in(bounds) {
position position
} else { } else {
return (event::Status::Ignored, None); return (event::Status::Ignored, None);
@ -567,10 +567,9 @@ mod grid {
let overlay = { let overlay = {
let mut frame = Frame::new(renderer, bounds.size()); let mut frame = Frame::new(renderer, bounds.size());
let hovered_cell = let hovered_cell = cursor.position_in(bounds).map(|position| {
cursor.position_in(&bounds).map(|position| { Cell::at(self.project(position, frame.size()))
Cell::at(self.project(position, frame.size())) });
});
if let Some(cell) = hovered_cell { if let Some(cell) = hovered_cell {
frame.with_save(|frame| { frame.with_save(|frame| {
@ -675,7 +674,7 @@ mod grid {
Interaction::Drawing => mouse::Interaction::Crosshair, Interaction::Drawing => mouse::Interaction::Crosshair,
Interaction::Erasing => mouse::Interaction::Crosshair, Interaction::Erasing => mouse::Interaction::Crosshair,
Interaction::Panning { .. } => mouse::Interaction::Grabbing, Interaction::Panning { .. } => mouse::Interaction::Grabbing,
Interaction::None if cursor.is_over(&bounds) => { Interaction::None if cursor.is_over(bounds) => {
mouse::Interaction::Crosshair mouse::Interaction::Crosshair
} }
_ => mouse::Interaction::default(), _ => mouse::Interaction::default(),

View file

@ -62,7 +62,7 @@ mod rainbow {
let color_v = [0.75, 0.0, 0.5, 1.0]; let color_v = [0.75, 0.0, 0.5, 1.0];
let posn_center = { let posn_center = {
if let Some(cursor_position) = cursor.position_in(&bounds) { if let Some(cursor_position) = cursor.position_in(bounds) {
[cursor_position.x, cursor_position.y] [cursor_position.x, cursor_position.y]
} else { } else {
[bounds.width / 2.0, bounds.height / 2.0] [bounds.width / 2.0, bounds.height / 2.0]

View file

@ -390,7 +390,7 @@ mod modal {
mouse::Button::Left, mouse::Button::Left,
)) = &event )) = &event
{ {
if !cursor.is_over(&content_bounds) { if !cursor.is_over(content_bounds) {
shell.publish(message.clone()); shell.publish(message.clone());
return event::Status::Captured; return event::Status::Captured;
} }

View file

@ -108,12 +108,12 @@ impl canvas::Program<Message> for SierpinskiGraph {
bounds: Rectangle, bounds: Rectangle,
cursor: mouse::Cursor, cursor: mouse::Cursor,
) -> (event::Status, Option<Message>) { ) -> (event::Status, Option<Message>) {
let cursor_position = let cursor_position = if let Some(position) = cursor.position_in(bounds)
if let Some(position) = cursor.position_in(&bounds) { {
position position
} else { } else {
return (event::Status::Ignored, None); return (event::Status::Ignored, None);
}; };
match event { match event {
Event::Mouse(mouse_event) => { Event::Mouse(mouse_event) => {

View file

@ -311,7 +311,7 @@ pub fn update<'a, Message: Clone>(
if on_press.is_some() { if on_press.is_some() {
let bounds = layout.bounds(); let bounds = layout.bounds();
if cursor.is_over(&bounds) { if cursor.is_over(bounds) {
let state = state(); let state = state();
state.is_pressed = true; state.is_pressed = true;
@ -330,7 +330,7 @@ pub fn update<'a, Message: Clone>(
let bounds = layout.bounds(); let bounds = layout.bounds();
if cursor.is_over(&bounds) { if cursor.is_over(bounds) {
shell.publish(on_press); shell.publish(on_press);
} }
@ -364,7 +364,7 @@ pub fn draw<'a, Renderer: crate::core::Renderer>(
where where
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
{ {
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
let styling = if !is_enabled { let styling = if !is_enabled {
style_sheet.disabled(style) style_sheet.disabled(style)
@ -440,7 +440,7 @@ pub fn mouse_interaction(
cursor: mouse::Cursor, cursor: mouse::Cursor,
is_enabled: bool, is_enabled: bool,
) -> mouse::Interaction { ) -> mouse::Interaction {
let is_mouse_over = cursor.is_over(&layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
if is_mouse_over && is_enabled { if is_mouse_over && is_enabled {
mouse::Interaction::Pointer mouse::Interaction::Pointer

View file

@ -212,7 +212,7 @@ where
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
let mouse_over = cursor.is_over(&layout.bounds()); let mouse_over = cursor.is_over(layout.bounds());
if mouse_over { if mouse_over {
shell.publish((self.on_toggle)(!self.is_checked)); shell.publish((self.on_toggle)(!self.is_checked));
@ -234,7 +234,7 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
if cursor.is_over(&layout.bounds()) { if cursor.is_over(layout.bounds()) {
mouse::Interaction::Pointer mouse::Interaction::Pointer
} else { } else {
mouse::Interaction::default() mouse::Interaction::default()
@ -251,7 +251,7 @@ where
cursor: mouse::Cursor, cursor: mouse::Cursor,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
let is_mouse_over = cursor.is_over(&layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
let mut children = layout.children(); let mut children = layout.children();

View file

@ -286,7 +286,7 @@ where
) -> mouse::Interaction { ) -> mouse::Interaction {
let state = tree.state.downcast_ref::<State>(); let state = tree.state.downcast_ref::<State>();
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
if state.is_cursor_grabbed() { if state.is_cursor_grabbed() {
mouse::Interaction::Grabbing mouse::Interaction::Grabbing

View file

@ -240,7 +240,7 @@ fn update<Message: Clone, Renderer>(
cursor: mouse::Cursor, cursor: mouse::Cursor,
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
) -> event::Status { ) -> event::Status {
if !cursor.is_over(&layout.bounds()) { if !cursor.is_over(layout.bounds()) {
return event::Status::Ignored; return event::Status::Ignored;
} }

View file

@ -376,9 +376,7 @@ where
) -> event::Status { ) -> event::Status {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
let bounds = layout.bounds(); if cursor.is_over(layout.bounds()) {
if cursor.is_over(&bounds) {
if let Some(index) = *self.hovered_option { if let Some(index) = *self.hovered_option {
if let Some(option) = self.options.get(index) { if let Some(option) = self.options.get(index) {
*self.last_selection = Some(option.clone()); *self.last_selection = Some(option.clone());
@ -388,7 +386,7 @@ where
} }
Event::Mouse(mouse::Event::CursorMoved { .. }) => { Event::Mouse(mouse::Event::CursorMoved { .. }) => {
if let Some(cursor_position) = if let Some(cursor_position) =
cursor.position_in(&layout.bounds()) cursor.position_in(layout.bounds())
{ {
let text_size = self let text_size = self
.text_size .text_size
@ -404,7 +402,7 @@ where
} }
Event::Touch(touch::Event::FingerPressed { .. }) => { Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = if let Some(cursor_position) =
cursor.position_in(&layout.bounds()) cursor.position_in(layout.bounds())
{ {
let text_size = self let text_size = self
.text_size .text_size
@ -438,7 +436,7 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
let is_mouse_over = cursor.is_over(&layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
if is_mouse_over { if is_mouse_over {
mouse::Interaction::Pointer mouse::Interaction::Pointer

View file

@ -524,7 +524,7 @@ pub fn update<'a, Message, T: Draggable>(
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
let bounds = layout.bounds(); let bounds = layout.bounds();
if let Some(cursor_position) = cursor.position_over(&bounds) { if let Some(cursor_position) = cursor.position_over(bounds) {
event_status = event::Status::Captured; event_status = event::Status::Captured;
match on_resize { match on_resize {

View file

@ -113,7 +113,7 @@ where
let title_bar_layout = children.next().unwrap(); let title_bar_layout = children.next().unwrap();
let body_layout = children.next().unwrap(); let body_layout = children.next().unwrap();
let show_controls = cursor.is_over(&bounds); let show_controls = cursor.is_over(bounds);
self.body.as_widget().draw( self.body.as_widget().draw(
&tree.children[0], &tree.children[0],

View file

@ -452,7 +452,7 @@ where
state.is_open = false; state.is_open = false;
event::Status::Captured event::Status::Captured
} else if cursor.is_over(&layout.bounds()) { } else if cursor.is_over(layout.bounds()) {
state.is_open = true; state.is_open = true;
state.hovered_option = state.hovered_option =
options.iter().position(|option| Some(option) == selected); options.iter().position(|option| Some(option) == selected);
@ -478,7 +478,7 @@ where
let state = state(); let state = state();
if state.keyboard_modifiers.command() if state.keyboard_modifiers.command()
&& cursor.is_over(&layout.bounds()) && cursor.is_over(layout.bounds())
&& !state.is_open && !state.is_open
{ {
fn find_next<'a, T: PartialEq>( fn find_next<'a, T: PartialEq>(
@ -532,7 +532,7 @@ pub fn mouse_interaction(
cursor: mouse::Cursor, cursor: mouse::Cursor,
) -> mouse::Interaction { ) -> mouse::Interaction {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
if is_mouse_over { if is_mouse_over {
mouse::Interaction::Pointer mouse::Interaction::Pointer
@ -610,7 +610,7 @@ pub fn draw<'a, T, Renderer>(
T: ToString + 'a, T: ToString + 'a,
{ {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
let is_selected = selected.is_some(); let is_selected = selected.is_some();
let style = if is_mouse_over { let style = if is_mouse_over {

View file

@ -237,7 +237,7 @@ where
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
if cursor.is_over(&layout.bounds()) { if cursor.is_over(layout.bounds()) {
shell.publish(self.on_click.clone()); shell.publish(self.on_click.clone());
return event::Status::Captured; return event::Status::Captured;
@ -257,7 +257,7 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
if cursor.is_over(&layout.bounds()) { if cursor.is_over(layout.bounds()) {
mouse::Interaction::Pointer mouse::Interaction::Pointer
} else { } else {
mouse::Interaction::default() mouse::Interaction::default()
@ -274,7 +274,7 @@ where
cursor: mouse::Cursor, cursor: mouse::Cursor,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
let is_mouse_over = cursor.is_over(&layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
let mut children = layout.children(); let mut children = layout.children();

View file

@ -443,7 +443,7 @@ pub fn update<Message>(
) -> event::Status, ) -> event::Status,
) -> event::Status { ) -> event::Status {
let bounds = layout.bounds(); let bounds = layout.bounds();
let cursor_over_scrollable = cursor.position_over(&bounds); let cursor_over_scrollable = cursor.position_over(bounds);
let content = layout.children().next().unwrap(); let content = layout.children().next().unwrap();
let content_bounds = content.bounds(); let content_bounds = content.bounds();
@ -698,7 +698,7 @@ pub fn mouse_interaction(
) -> mouse::Interaction, ) -> mouse::Interaction,
) -> mouse::Interaction { ) -> mouse::Interaction {
let bounds = layout.bounds(); let bounds = layout.bounds();
let cursor_over_scrollable = cursor.position_over(&bounds); let cursor_over_scrollable = cursor.position_over(bounds);
let content_layout = layout.children().next().unwrap(); let content_layout = layout.children().next().unwrap();
let content_bounds = content_layout.bounds(); let content_bounds = content_layout.bounds();
@ -759,7 +759,7 @@ pub fn draw<Renderer>(
let scrollbars = let scrollbars =
Scrollbars::new(state, vertical, horizontal, bounds, content_bounds); Scrollbars::new(state, vertical, horizontal, bounds, content_bounds);
let cursor_over_scrollable = cursor.position_over(&bounds); let cursor_over_scrollable = cursor.position_over(bounds);
let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) = let (mouse_over_y_scrollbar, mouse_over_x_scrollbar) =
scrollbars.is_mouse_over(cursor); scrollbars.is_mouse_over(cursor);

View file

@ -305,8 +305,7 @@ where
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = if let Some(cursor_position) = cursor.position_over(layout.bounds())
cursor.position_over(&layout.bounds())
{ {
change(cursor_position); change(cursor_position);
state.is_dragging = true; state.is_dragging = true;
@ -356,7 +355,7 @@ pub fn draw<T, R>(
R::Theme: StyleSheet, R::Theme: StyleSheet,
{ {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
let style = if state.is_dragging { let style = if state.is_dragging {
style_sheet.dragging(style) style_sheet.dragging(style)
@ -446,7 +445,7 @@ pub fn mouse_interaction(
state: &State, state: &State,
) -> mouse::Interaction { ) -> mouse::Interaction {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
if state.is_dragging { if state.is_dragging {
mouse::Interaction::Grabbing mouse::Interaction::Grabbing

View file

@ -552,7 +552,7 @@ where
let state = state(); let state = state();
let click_position = if on_input.is_some() { let click_position = if on_input.is_some() {
cursor.position_over(&layout.bounds()) cursor.position_over(layout.bounds())
} else { } else {
None None
}; };
@ -971,7 +971,7 @@ pub fn draw<Renderer>(
let mut children_layout = layout.children(); let mut children_layout = layout.children();
let text_bounds = children_layout.next().unwrap().bounds(); let text_bounds = children_layout.next().unwrap().bounds();
let is_mouse_over = cursor.is_over(&bounds); let is_mouse_over = cursor.is_over(bounds);
let appearance = if is_disabled { let appearance = if is_disabled {
theme.disabled(style) theme.disabled(style)
@ -1161,7 +1161,7 @@ pub fn mouse_interaction(
cursor: mouse::Cursor, cursor: mouse::Cursor,
is_disabled: bool, is_disabled: bool,
) -> mouse::Interaction { ) -> mouse::Interaction {
if cursor.is_over(&layout.bounds()) { if cursor.is_over(layout.bounds()) {
if is_disabled { if is_disabled {
mouse::Interaction::NotAllowed mouse::Interaction::NotAllowed
} else { } else {

View file

@ -209,7 +209,7 @@ where
) -> event::Status { ) -> event::Status {
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
let mouse_over = cursor.is_over(&layout.bounds()); let mouse_over = cursor.is_over(layout.bounds());
if mouse_over { if mouse_over {
shell.publish((self.on_toggle)(!self.is_toggled)); shell.publish((self.on_toggle)(!self.is_toggled));
@ -231,7 +231,7 @@ where
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
if cursor.is_over(&layout.bounds()) { if cursor.is_over(layout.bounds()) {
mouse::Interaction::Pointer mouse::Interaction::Pointer
} else { } else {
mouse::Interaction::default() mouse::Interaction::default()
@ -278,7 +278,7 @@ where
let toggler_layout = children.next().unwrap(); let toggler_layout = children.next().unwrap();
let bounds = toggler_layout.bounds(); let bounds = toggler_layout.bounds();
let is_mouse_over = cursor.is_over(&layout.bounds()); let is_mouse_over = cursor.is_over(layout.bounds());
let style = if is_mouse_over { let style = if is_mouse_over {
theme.hovered(&self.style, self.is_toggled) theme.hovered(&self.style, self.is_toggled)

View file

@ -287,7 +287,7 @@ pub fn draw<Renderer>(
let bounds = layout.bounds(); let bounds = layout.bounds();
if let Some(cursor_position) = cursor.position_over(&bounds) { if let Some(cursor_position) = cursor.position_over(bounds) {
let style = theme.appearance(style); let style = theme.appearance(style);
let defaults = renderer::Style { let defaults = renderer::Style {

View file

@ -304,8 +304,7 @@ where
match event { match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => { | Event::Touch(touch::Event::FingerPressed { .. }) => {
if let Some(cursor_position) = if let Some(cursor_position) = cursor.position_over(layout.bounds())
cursor.position_over(&layout.bounds())
{ {
change(cursor_position); change(cursor_position);
state.is_dragging = true; state.is_dragging = true;
@ -355,7 +354,7 @@ pub fn draw<T, R>(
R::Theme: StyleSheet, R::Theme: StyleSheet,
{ {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.position_over(&bounds).is_some(); let is_mouse_over = cursor.is_over(bounds);
let style = if state.is_dragging { let style = if state.is_dragging {
style_sheet.dragging(style) style_sheet.dragging(style)
@ -445,7 +444,7 @@ pub fn mouse_interaction(
state: &State, state: &State,
) -> mouse::Interaction { ) -> mouse::Interaction {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = cursor.position_over(&bounds).is_some(); let is_mouse_over = cursor.is_over(bounds);
if state.is_dragging { if state.is_dragging {
mouse::Interaction::Grabbing mouse::Interaction::Grabbing