Take Rectangle by value in Cursor API
This commit is contained in:
parent
34451bff18
commit
5c8cfb411e
21 changed files with 57 additions and 62 deletions
|
|
@ -24,7 +24,7 @@ impl Cursor {
|
|||
///
|
||||
/// If the [`Cursor`] is not over the provided bounds, this method will
|
||||
/// 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))
|
||||
}
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ impl Cursor {
|
|||
///
|
||||
/// If the [`Cursor`] is not over the provided bounds, this method will
|
||||
/// return `None`.
|
||||
pub fn position_in(self, bounds: &Rectangle) -> Option<Point> {
|
||||
pub fn position_in(self, bounds: Rectangle) -> Option<Point> {
|
||||
self.position_over(bounds)
|
||||
.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`.
|
||||
pub fn is_over(self, bounds: &Rectangle) -> bool {
|
||||
pub fn is_over(self, bounds: Rectangle) -> bool {
|
||||
self.position_over(bounds).is_some()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -101,7 +101,7 @@ mod bezier {
|
|||
cursor: mouse::Cursor,
|
||||
) -> (event::Status, Option<Curve>) {
|
||||
let cursor_position =
|
||||
if let Some(position) = cursor.position_in(&bounds) {
|
||||
if let Some(position) = cursor.position_in(bounds) {
|
||||
position
|
||||
} else {
|
||||
return (event::Status::Ignored, None);
|
||||
|
|
@ -183,7 +183,7 @@ mod bezier {
|
|||
bounds: Rectangle,
|
||||
cursor: mouse::Cursor,
|
||||
) -> mouse::Interaction {
|
||||
if cursor.is_over(&bounds) {
|
||||
if cursor.is_over(bounds) {
|
||||
mouse::Interaction::Crosshair
|
||||
} else {
|
||||
mouse::Interaction::default()
|
||||
|
|
@ -226,7 +226,7 @@ mod bezier {
|
|||
) -> Geometry {
|
||||
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 {
|
||||
Pending::One { from } => {
|
||||
let line = Path::line(from, cursor_position);
|
||||
|
|
|
|||
|
|
@ -407,7 +407,7 @@ mod grid {
|
|||
}
|
||||
|
||||
let cursor_position =
|
||||
if let Some(position) = cursor.position_in(&bounds) {
|
||||
if let Some(position) = cursor.position_in(bounds) {
|
||||
position
|
||||
} else {
|
||||
return (event::Status::Ignored, None);
|
||||
|
|
@ -567,8 +567,7 @@ mod grid {
|
|||
let overlay = {
|
||||
let mut frame = Frame::new(renderer, bounds.size());
|
||||
|
||||
let hovered_cell =
|
||||
cursor.position_in(&bounds).map(|position| {
|
||||
let hovered_cell = cursor.position_in(bounds).map(|position| {
|
||||
Cell::at(self.project(position, frame.size()))
|
||||
});
|
||||
|
||||
|
|
@ -675,7 +674,7 @@ mod grid {
|
|||
Interaction::Drawing => mouse::Interaction::Crosshair,
|
||||
Interaction::Erasing => mouse::Interaction::Crosshair,
|
||||
Interaction::Panning { .. } => mouse::Interaction::Grabbing,
|
||||
Interaction::None if cursor.is_over(&bounds) => {
|
||||
Interaction::None if cursor.is_over(bounds) => {
|
||||
mouse::Interaction::Crosshair
|
||||
}
|
||||
_ => mouse::Interaction::default(),
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ mod rainbow {
|
|||
let color_v = [0.75, 0.0, 0.5, 1.0];
|
||||
|
||||
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]
|
||||
} else {
|
||||
[bounds.width / 2.0, bounds.height / 2.0]
|
||||
|
|
|
|||
|
|
@ -390,7 +390,7 @@ mod modal {
|
|||
mouse::Button::Left,
|
||||
)) = &event
|
||||
{
|
||||
if !cursor.is_over(&content_bounds) {
|
||||
if !cursor.is_over(content_bounds) {
|
||||
shell.publish(message.clone());
|
||||
return event::Status::Captured;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,8 +108,8 @@ impl canvas::Program<Message> for SierpinskiGraph {
|
|||
bounds: Rectangle,
|
||||
cursor: mouse::Cursor,
|
||||
) -> (event::Status, Option<Message>) {
|
||||
let cursor_position =
|
||||
if let Some(position) = cursor.position_in(&bounds) {
|
||||
let cursor_position = if let Some(position) = cursor.position_in(bounds)
|
||||
{
|
||||
position
|
||||
} else {
|
||||
return (event::Status::Ignored, None);
|
||||
|
|
|
|||
|
|
@ -311,7 +311,7 @@ pub fn update<'a, Message: Clone>(
|
|||
if on_press.is_some() {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
if cursor.is_over(&bounds) {
|
||||
if cursor.is_over(bounds) {
|
||||
let state = state();
|
||||
|
||||
state.is_pressed = true;
|
||||
|
|
@ -330,7 +330,7 @@ pub fn update<'a, Message: Clone>(
|
|||
|
||||
let bounds = layout.bounds();
|
||||
|
||||
if cursor.is_over(&bounds) {
|
||||
if cursor.is_over(bounds) {
|
||||
shell.publish(on_press);
|
||||
}
|
||||
|
||||
|
|
@ -364,7 +364,7 @@ pub fn draw<'a, Renderer: crate::core::Renderer>(
|
|||
where
|
||||
Renderer::Theme: StyleSheet,
|
||||
{
|
||||
let is_mouse_over = cursor.is_over(&bounds);
|
||||
let is_mouse_over = cursor.is_over(bounds);
|
||||
|
||||
let styling = if !is_enabled {
|
||||
style_sheet.disabled(style)
|
||||
|
|
@ -440,7 +440,7 @@ pub fn mouse_interaction(
|
|||
cursor: mouse::Cursor,
|
||||
is_enabled: bool,
|
||||
) -> 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 {
|
||||
mouse::Interaction::Pointer
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ where
|
|||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
let mouse_over = cursor.is_over(&layout.bounds());
|
||||
let mouse_over = cursor.is_over(layout.bounds());
|
||||
|
||||
if mouse_over {
|
||||
shell.publish((self.on_toggle)(!self.is_checked));
|
||||
|
|
@ -234,7 +234,7 @@ where
|
|||
_viewport: &Rectangle,
|
||||
_renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
if cursor.is_over(&layout.bounds()) {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
mouse::Interaction::Pointer
|
||||
} else {
|
||||
mouse::Interaction::default()
|
||||
|
|
@ -251,7 +251,7 @@ where
|
|||
cursor: mouse::Cursor,
|
||||
_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();
|
||||
|
||||
|
|
|
|||
|
|
@ -286,7 +286,7 @@ where
|
|||
) -> mouse::Interaction {
|
||||
let state = tree.state.downcast_ref::<State>();
|
||||
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() {
|
||||
mouse::Interaction::Grabbing
|
||||
|
|
|
|||
|
|
@ -240,7 +240,7 @@ fn update<Message: Clone, Renderer>(
|
|||
cursor: mouse::Cursor,
|
||||
shell: &mut Shell<'_, Message>,
|
||||
) -> event::Status {
|
||||
if !cursor.is_over(&layout.bounds()) {
|
||||
if !cursor.is_over(layout.bounds()) {
|
||||
return event::Status::Ignored;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -376,9 +376,7 @@ where
|
|||
) -> event::Status {
|
||||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
||||
let bounds = layout.bounds();
|
||||
|
||||
if cursor.is_over(&bounds) {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
if let Some(index) = *self.hovered_option {
|
||||
if let Some(option) = self.options.get(index) {
|
||||
*self.last_selection = Some(option.clone());
|
||||
|
|
@ -388,7 +386,7 @@ where
|
|||
}
|
||||
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
||||
if let Some(cursor_position) =
|
||||
cursor.position_in(&layout.bounds())
|
||||
cursor.position_in(layout.bounds())
|
||||
{
|
||||
let text_size = self
|
||||
.text_size
|
||||
|
|
@ -404,7 +402,7 @@ where
|
|||
}
|
||||
Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
if let Some(cursor_position) =
|
||||
cursor.position_in(&layout.bounds())
|
||||
cursor.position_in(layout.bounds())
|
||||
{
|
||||
let text_size = self
|
||||
.text_size
|
||||
|
|
@ -438,7 +436,7 @@ where
|
|||
_viewport: &Rectangle,
|
||||
_renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
let is_mouse_over = cursor.is_over(&layout.bounds());
|
||||
let is_mouse_over = cursor.is_over(layout.bounds());
|
||||
|
||||
if is_mouse_over {
|
||||
mouse::Interaction::Pointer
|
||||
|
|
|
|||
|
|
@ -524,7 +524,7 @@ pub fn update<'a, Message, T: Draggable>(
|
|||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
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;
|
||||
|
||||
match on_resize {
|
||||
|
|
|
|||
|
|
@ -113,7 +113,7 @@ where
|
|||
let title_bar_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(
|
||||
&tree.children[0],
|
||||
|
|
|
|||
|
|
@ -452,7 +452,7 @@ where
|
|||
state.is_open = false;
|
||||
|
||||
event::Status::Captured
|
||||
} else if cursor.is_over(&layout.bounds()) {
|
||||
} else if cursor.is_over(layout.bounds()) {
|
||||
state.is_open = true;
|
||||
state.hovered_option =
|
||||
options.iter().position(|option| Some(option) == selected);
|
||||
|
|
@ -478,7 +478,7 @@ where
|
|||
let state = state();
|
||||
|
||||
if state.keyboard_modifiers.command()
|
||||
&& cursor.is_over(&layout.bounds())
|
||||
&& cursor.is_over(layout.bounds())
|
||||
&& !state.is_open
|
||||
{
|
||||
fn find_next<'a, T: PartialEq>(
|
||||
|
|
@ -532,7 +532,7 @@ pub fn mouse_interaction(
|
|||
cursor: mouse::Cursor,
|
||||
) -> mouse::Interaction {
|
||||
let bounds = layout.bounds();
|
||||
let is_mouse_over = cursor.is_over(&bounds);
|
||||
let is_mouse_over = cursor.is_over(bounds);
|
||||
|
||||
if is_mouse_over {
|
||||
mouse::Interaction::Pointer
|
||||
|
|
@ -610,7 +610,7 @@ pub fn draw<'a, T, Renderer>(
|
|||
T: ToString + 'a,
|
||||
{
|
||||
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 style = if is_mouse_over {
|
||||
|
|
|
|||
|
|
@ -237,7 +237,7 @@ where
|
|||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
if cursor.is_over(&layout.bounds()) {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
shell.publish(self.on_click.clone());
|
||||
|
||||
return event::Status::Captured;
|
||||
|
|
@ -257,7 +257,7 @@ where
|
|||
_viewport: &Rectangle,
|
||||
_renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
if cursor.is_over(&layout.bounds()) {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
mouse::Interaction::Pointer
|
||||
} else {
|
||||
mouse::Interaction::default()
|
||||
|
|
@ -274,7 +274,7 @@ where
|
|||
cursor: mouse::Cursor,
|
||||
_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();
|
||||
|
||||
|
|
|
|||
|
|
@ -443,7 +443,7 @@ pub fn update<Message>(
|
|||
) -> event::Status,
|
||||
) -> event::Status {
|
||||
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_bounds = content.bounds();
|
||||
|
|
@ -698,7 +698,7 @@ pub fn mouse_interaction(
|
|||
) -> mouse::Interaction,
|
||||
) -> mouse::Interaction {
|
||||
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_bounds = content_layout.bounds();
|
||||
|
|
@ -759,7 +759,7 @@ pub fn draw<Renderer>(
|
|||
let scrollbars =
|
||||
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) =
|
||||
scrollbars.is_mouse_over(cursor);
|
||||
|
||||
|
|
|
|||
|
|
@ -305,8 +305,7 @@ where
|
|||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
if let Some(cursor_position) =
|
||||
cursor.position_over(&layout.bounds())
|
||||
if let Some(cursor_position) = cursor.position_over(layout.bounds())
|
||||
{
|
||||
change(cursor_position);
|
||||
state.is_dragging = true;
|
||||
|
|
@ -356,7 +355,7 @@ pub fn draw<T, R>(
|
|||
R::Theme: StyleSheet,
|
||||
{
|
||||
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 {
|
||||
style_sheet.dragging(style)
|
||||
|
|
@ -446,7 +445,7 @@ pub fn mouse_interaction(
|
|||
state: &State,
|
||||
) -> mouse::Interaction {
|
||||
let bounds = layout.bounds();
|
||||
let is_mouse_over = cursor.is_over(&bounds);
|
||||
let is_mouse_over = cursor.is_over(bounds);
|
||||
|
||||
if state.is_dragging {
|
||||
mouse::Interaction::Grabbing
|
||||
|
|
|
|||
|
|
@ -552,7 +552,7 @@ where
|
|||
let state = state();
|
||||
|
||||
let click_position = if on_input.is_some() {
|
||||
cursor.position_over(&layout.bounds())
|
||||
cursor.position_over(layout.bounds())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
|
@ -971,7 +971,7 @@ pub fn draw<Renderer>(
|
|||
let mut children_layout = layout.children();
|
||||
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 {
|
||||
theme.disabled(style)
|
||||
|
|
@ -1161,7 +1161,7 @@ pub fn mouse_interaction(
|
|||
cursor: mouse::Cursor,
|
||||
is_disabled: bool,
|
||||
) -> mouse::Interaction {
|
||||
if cursor.is_over(&layout.bounds()) {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
if is_disabled {
|
||||
mouse::Interaction::NotAllowed
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ where
|
|||
) -> event::Status {
|
||||
match event {
|
||||
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 {
|
||||
shell.publish((self.on_toggle)(!self.is_toggled));
|
||||
|
|
@ -231,7 +231,7 @@ where
|
|||
_viewport: &Rectangle,
|
||||
_renderer: &Renderer,
|
||||
) -> mouse::Interaction {
|
||||
if cursor.is_over(&layout.bounds()) {
|
||||
if cursor.is_over(layout.bounds()) {
|
||||
mouse::Interaction::Pointer
|
||||
} else {
|
||||
mouse::Interaction::default()
|
||||
|
|
@ -278,7 +278,7 @@ where
|
|||
let toggler_layout = children.next().unwrap();
|
||||
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 {
|
||||
theme.hovered(&self.style, self.is_toggled)
|
||||
|
|
|
|||
|
|
@ -287,7 +287,7 @@ pub fn draw<Renderer>(
|
|||
|
||||
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 defaults = renderer::Style {
|
||||
|
|
|
|||
|
|
@ -304,8 +304,7 @@ where
|
|||
match event {
|
||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||
| Event::Touch(touch::Event::FingerPressed { .. }) => {
|
||||
if let Some(cursor_position) =
|
||||
cursor.position_over(&layout.bounds())
|
||||
if let Some(cursor_position) = cursor.position_over(layout.bounds())
|
||||
{
|
||||
change(cursor_position);
|
||||
state.is_dragging = true;
|
||||
|
|
@ -355,7 +354,7 @@ pub fn draw<T, R>(
|
|||
R::Theme: StyleSheet,
|
||||
{
|
||||
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 {
|
||||
style_sheet.dragging(style)
|
||||
|
|
@ -445,7 +444,7 @@ pub fn mouse_interaction(
|
|||
state: &State,
|
||||
) -> mouse::Interaction {
|
||||
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 {
|
||||
mouse::Interaction::Grabbing
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue