From 56dbd683269b82da16d8eae3f98f352301750bf5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 28 Apr 2020 03:11:01 +0200 Subject: Move reusable `mouse` types to `iced_core` --- native/src/input.rs | 4 +-- native/src/input/button_state.rs | 9 ------ native/src/input/keyboard.rs | 2 +- native/src/input/mouse.rs | 5 +--- native/src/input/mouse/button.rs | 15 ---------- native/src/input/mouse/event.rs | 61 ---------------------------------------- 6 files changed, 3 insertions(+), 93 deletions(-) delete mode 100644 native/src/input/button_state.rs delete mode 100644 native/src/input/mouse/button.rs delete mode 100644 native/src/input/mouse/event.rs (limited to 'native') diff --git a/native/src/input.rs b/native/src/input.rs index 097fa730..7f5114c3 100644 --- a/native/src/input.rs +++ b/native/src/input.rs @@ -2,6 +2,4 @@ pub mod keyboard; pub mod mouse; -mod button_state; - -pub use button_state::ButtonState; +pub use iced_core::ButtonState; diff --git a/native/src/input/button_state.rs b/native/src/input/button_state.rs deleted file mode 100644 index 988043ba..00000000 --- a/native/src/input/button_state.rs +++ /dev/null @@ -1,9 +0,0 @@ -/// The state of a button. -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -pub enum ButtonState { - /// The button is pressed. - Pressed, - - /// The button is __not__ pressed. - Released, -} diff --git a/native/src/input/keyboard.rs b/native/src/input/keyboard.rs index 928bf492..220b7f17 100644 --- a/native/src/input/keyboard.rs +++ b/native/src/input/keyboard.rs @@ -2,4 +2,4 @@ mod event; pub use event::Event; -pub use iced_core::keyboard::{KeyCode, ModifiersState}; +pub use iced_core::keyboard::*; diff --git a/native/src/input/mouse.rs b/native/src/input/mouse.rs index 7198b233..ae3f1596 100644 --- a/native/src/input/mouse.rs +++ b/native/src/input/mouse.rs @@ -1,9 +1,6 @@ //! Build mouse events. -mod button; -mod event; pub mod click; -pub use button::Button; pub use click::Click; -pub use event::{Event, ScrollDelta}; +pub use iced_core::mouse::*; diff --git a/native/src/input/mouse/button.rs b/native/src/input/mouse/button.rs deleted file mode 100644 index aeb8a55d..00000000 --- a/native/src/input/mouse/button.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// The button of a mouse. -#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] -pub enum Button { - /// The left mouse button. - Left, - - /// The right mouse button. - Right, - - /// The middle (wheel) button. - Middle, - - /// Some other button. - Other(u8), -} diff --git a/native/src/input/mouse/event.rs b/native/src/input/mouse/event.rs deleted file mode 100644 index aafc4fe3..00000000 --- a/native/src/input/mouse/event.rs +++ /dev/null @@ -1,61 +0,0 @@ -use super::Button; -use crate::input::ButtonState; - -/// A mouse event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Event { - /// The mouse cursor entered the window. - CursorEntered, - - /// The mouse cursor left the window. - CursorLeft, - - /// The mouse cursor was moved - CursorMoved { - /// The X coordinate of the mouse position - x: f32, - - /// The Y coordinate of the mouse position - y: f32, - }, - - /// A mouse button was pressed or released. - Input { - /// The state of the button - state: ButtonState, - - /// The button identifier - button: Button, - }, - - /// The mouse wheel was scrolled. - WheelScrolled { - /// The scroll movement. - delta: ScrollDelta, - }, -} - -/// A scroll movement. -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum ScrollDelta { - /// A line-based scroll movement - Lines { - /// The number of horizontal lines scrolled - x: f32, - - /// The number of vertical lines scrolled - y: f32, - }, - /// A pixel-based scroll movement - Pixels { - /// The number of horizontal pixels scrolled - x: f32, - /// The number of vertical pixels scrolled - y: f32, - }, -} -- cgit From 59403b6ca80081fa419fbef76c92397db68f1ab1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:11:15 +0200 Subject: Remove `OutOfBounds` variant from `MouseCursor` --- native/src/mouse_cursor.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'native') diff --git a/native/src/mouse_cursor.rs b/native/src/mouse_cursor.rs index 0dad3edc..8bfa2b44 100644 --- a/native/src/mouse_cursor.rs +++ b/native/src/mouse_cursor.rs @@ -1,9 +1,6 @@ /// The state of the mouse cursor. #[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)] pub enum MouseCursor { - /// The cursor is out of the bounds of the user interface. - OutOfBounds, - /// The cursor is over a non-interactive widget. Idle, @@ -31,6 +28,6 @@ pub enum MouseCursor { impl Default for MouseCursor { fn default() -> MouseCursor { - MouseCursor::OutOfBounds + MouseCursor::Idle } } -- cgit From 0509710cc59797ce492f55943a6db60c47a2d599 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:14:59 +0200 Subject: Add `Crosshair` variant to `MouseCursor` --- native/src/mouse_cursor.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'native') diff --git a/native/src/mouse_cursor.rs b/native/src/mouse_cursor.rs index 8bfa2b44..78ddb0ae 100644 --- a/native/src/mouse_cursor.rs +++ b/native/src/mouse_cursor.rs @@ -13,12 +13,15 @@ pub enum MouseCursor { /// The cursor is over a grabbable widget. Grab, - /// The cursor is grabbing a widget. - Grabbing, - /// The cursor is over a text widget. Text, + /// The cursor is over a widget that requires precision. + Crosshair, + + /// The cursor is grabbing a widget. + Grabbing, + /// The cursor is resizing a widget horizontally. ResizingHorizontally, -- cgit From ec712c8032a25c5dc65152c3ab39bddaecbdce77 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 29 Apr 2020 03:21:46 +0200 Subject: Move `MouseCursor` to `iced_core` --- native/src/lib.rs | 6 ++---- native/src/mouse_cursor.rs | 36 ------------------------------------ 2 files changed, 2 insertions(+), 40 deletions(-) delete mode 100644 native/src/mouse_cursor.rs (limited to 'native') diff --git a/native/src/lib.rs b/native/src/lib.rs index 89612391..a3b581b3 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -50,13 +50,12 @@ mod clipboard; mod element; mod event; mod hasher; -mod mouse_cursor; mod runtime; mod user_interface; pub use iced_core::{ - Align, Background, Color, Font, HorizontalAlignment, Length, Point, - Rectangle, Size, Vector, VerticalAlignment, + Align, Background, Color, Font, HorizontalAlignment, Length, MouseCursor, + Point, Rectangle, Size, Vector, VerticalAlignment, }; pub use iced_futures::{executor, futures, Command}; @@ -68,7 +67,6 @@ pub use element::Element; pub use event::Event; pub use hasher::Hasher; pub use layout::Layout; -pub use mouse_cursor::MouseCursor; pub use renderer::Renderer; pub use runtime::Runtime; pub use subscription::Subscription; diff --git a/native/src/mouse_cursor.rs b/native/src/mouse_cursor.rs deleted file mode 100644 index 78ddb0ae..00000000 --- a/native/src/mouse_cursor.rs +++ /dev/null @@ -1,36 +0,0 @@ -/// The state of the mouse cursor. -#[derive(Debug, Eq, PartialEq, Clone, Copy, PartialOrd, Ord)] -pub enum MouseCursor { - /// The cursor is over a non-interactive widget. - Idle, - - /// The cursor is over a clickable widget. - Pointer, - - /// The cursor is over a busy widget. - Working, - - /// The cursor is over a grabbable widget. - Grab, - - /// The cursor is over a text widget. - Text, - - /// The cursor is over a widget that requires precision. - Crosshair, - - /// The cursor is grabbing a widget. - Grabbing, - - /// The cursor is resizing a widget horizontally. - ResizingHorizontally, - - /// The cursor is resizing a widget vertically. - ResizingVertically, -} - -impl Default for MouseCursor { - fn default() -> MouseCursor { - MouseCursor::Idle - } -} -- cgit From e139aae1439d362ada017a05c9554eaae0883888 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:34:29 +0200 Subject: Split `Input` keyboard event by `ButtonState` --- native/src/input/keyboard/event.rs | 15 ++++++++++----- native/src/widget/pane_grid.rs | 26 +++++++++++++------------- native/src/widget/text_input.rs | 9 +++------ 3 files changed, 26 insertions(+), 24 deletions(-) (limited to 'native') diff --git a/native/src/input/keyboard/event.rs b/native/src/input/keyboard/event.rs index 862f30c4..bc8437a8 100644 --- a/native/src/input/keyboard/event.rs +++ b/native/src/input/keyboard/event.rs @@ -1,5 +1,4 @@ use super::{KeyCode, ModifiersState}; -use crate::input::ButtonState; /// A keyboard event. /// @@ -9,11 +8,17 @@ use crate::input::ButtonState; /// [open an issue]: https://github.com/hecrj/iced/issues #[derive(Debug, Clone, Copy, PartialEq)] pub enum Event { - /// A keyboard key was pressed or released. - Input { - /// The state of the key - state: ButtonState, + /// A keyboard key was pressed. + KeyPressed { + /// The key identifier + key_code: KeyCode, + + /// The state of the modifier keys + modifiers: ModifiersState, + }, + /// A keyboard key was released. + KeyReleased { /// The key identifier key_code: KeyCode, diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index f84775ed..0b735ad3 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -532,24 +532,19 @@ where Event::Mouse(mouse::Event::CursorMoved { .. }) => { self.trigger_resize(layout, cursor_position, messages); } - Event::Keyboard(keyboard::Event::Input { + Event::Keyboard(keyboard::Event::KeyPressed { modifiers, key_code, - state, }) => { if let Some(on_key_press) = &self.on_key_press { // TODO: Discard when event is captured - if state == ButtonState::Pressed { - if let Some(_) = self.state.active_pane() { - if modifiers.matches(self.modifier_keys) { - if let Some(message) = - on_key_press(KeyPressEvent { - key_code, - modifiers, - }) - { - messages.push(message); - } + if let Some(_) = self.state.active_pane() { + if modifiers.matches(self.modifier_keys) { + if let Some(message) = on_key_press(KeyPressEvent { + key_code, + modifiers, + }) { + messages.push(message); } } } @@ -557,6 +552,11 @@ where *self.pressed_modifiers = modifiers; } + Event::Keyboard(keyboard::Event::KeyReleased { + modifiers, .. + }) => { + *self.pressed_modifiers = modifiers; + } _ => {} } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 7d1a7415..ea6921b5 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -327,9 +327,8 @@ where let message = (self.on_change)(editor.contents()); messages.push(message); } - Event::Keyboard(keyboard::Event::Input { + Event::Keyboard(keyboard::Event::KeyPressed { key_code, - state: ButtonState::Pressed, modifiers, }) if self.state.is_focused => match key_code { keyboard::KeyCode::Enter => { @@ -473,10 +472,8 @@ where } _ => {} }, - Event::Keyboard(keyboard::Event::Input { - key_code, - state: ButtonState::Released, - .. + Event::Keyboard(keyboard::Event::KeyReleased { + key_code, .. }) => match key_code { keyboard::KeyCode::V => { self.state.is_pasting = None; -- cgit From e55cd9652e7c7aea4dc2c6ccb83769246d1a808e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:53:15 +0200 Subject: Split `Input` mouse event by `ButtonState` --- native/src/widget/button.rs | 35 ++++---- native/src/widget/checkbox.rs | 8 +- native/src/widget/pane_grid.rs | 188 +++++++++++++++++++++------------------- native/src/widget/radio.rs | 8 +- native/src/widget/scrollable.rs | 20 ++--- native/src/widget/slider.rs | 25 +++--- native/src/widget/text_input.rs | 11 +-- 7 files changed, 139 insertions(+), 156 deletions(-) (limited to 'native') diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 3cf4f780..5d414023 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -5,9 +5,8 @@ //! [`Button`]: struct.Button.html //! [`State`]: struct.State.html use crate::{ - input::{mouse, ButtonState}, - layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, - Rectangle, Widget, + input::mouse, layout, Clipboard, Element, Event, Hasher, Layout, Length, + Point, Rectangle, Widget, }; use std::hash::Hash; @@ -185,28 +184,24 @@ where _clipboard: Option<&dyn Clipboard>, ) { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { + if self.on_press.is_some() { + let bounds = layout.bounds(); + + self.state.is_pressed = bounds.contains(cursor_position); + } + } + Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => { if let Some(on_press) = self.on_press.clone() { let bounds = layout.bounds(); - match state { - ButtonState::Pressed => { - self.state.is_pressed = - bounds.contains(cursor_position); - } - ButtonState::Released => { - let is_clicked = self.state.is_pressed - && bounds.contains(cursor_position); + let is_clicked = self.state.is_pressed + && bounds.contains(cursor_position); - self.state.is_pressed = false; + self.state.is_pressed = false; - if is_clicked { - messages.push(on_press); - } - } + if is_clicked { + messages.push(on_press); } } } diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 4d167df7..c49ac707 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -2,8 +2,7 @@ use std::hash::Hash; use crate::{ - input::{mouse, ButtonState}, - layout, row, text, Align, Clipboard, Element, Event, Hasher, + input::mouse, layout, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -152,10 +151,7 @@ where _clipboard: Option<&dyn Clipboard>, ) { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { let mouse_over = layout.bounds().contains(cursor_position); if mouse_over { diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 0b735ad3..fe2bbe07 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -22,7 +22,7 @@ pub use split::Split; pub use state::{Focus, State}; use crate::{ - input::{keyboard, mouse, ButtonState}, + input::{keyboard, mouse}, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Size, Widget, }; @@ -405,11 +405,8 @@ where clipboard: Option<&dyn Clipboard>, ) { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { + Event::Mouse(mouse_event) => match mouse_event { + mouse::Event::ButtonPressed(mouse::Button::Left) => { let mut clicked_region = self.elements.iter().zip(layout.children()).filter( |(_, layout)| { @@ -438,7 +435,7 @@ where self.state.unfocus(); } } - ButtonState::Released => { + mouse::Event::ButtonReleased(mouse::Button::Left) => { if let Some(pane) = self.state.picked_pane() { self.state.focus(&pane); @@ -465,97 +462,110 @@ where } } } - }, - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Right, - state: ButtonState::Pressed, - }) if self.on_resize.is_some() - && self.state.picked_pane().is_none() - && self.pressed_modifiers.matches(self.modifier_keys) => - { - let bounds = layout.bounds(); + mouse::Event::ButtonPressed(mouse::Button::Right) + if self.on_resize.is_some() + && self.state.picked_pane().is_none() + && self + .pressed_modifiers + .matches(self.modifier_keys) => + { + let bounds = layout.bounds(); + + if bounds.contains(cursor_position) { + let relative_cursor = Point::new( + cursor_position.x - bounds.x, + cursor_position.y - bounds.y, + ); - if bounds.contains(cursor_position) { - let relative_cursor = Point::new( - cursor_position.x - bounds.x, - cursor_position.y - bounds.y, - ); - - let splits = self.state.splits( - f32::from(self.spacing), - Size::new(bounds.width, bounds.height), - ); - - let mut sorted_splits: Vec<_> = splits - .iter() - .filter(|(_, (axis, rectangle, _))| match axis { - Axis::Horizontal => { - relative_cursor.x > rectangle.x - && relative_cursor.x - < rectangle.x + rectangle.width - } - Axis::Vertical => { - relative_cursor.y > rectangle.y - && relative_cursor.y - < rectangle.y + rectangle.height - } - }) - .collect(); - - sorted_splits.sort_by_key( - |(_, (axis, rectangle, ratio))| { - let distance = match axis { - Axis::Horizontal => (relative_cursor.y - - (rectangle.y + rectangle.height * ratio)) - .abs(), - Axis::Vertical => (relative_cursor.x - - (rectangle.x + rectangle.width * ratio)) - .abs(), - }; + let splits = self.state.splits( + f32::from(self.spacing), + Size::new(bounds.width, bounds.height), + ); - distance.round() as u32 - }, - ); + let mut sorted_splits: Vec<_> = splits + .iter() + .filter(|(_, (axis, rectangle, _))| match axis { + Axis::Horizontal => { + relative_cursor.x > rectangle.x + && relative_cursor.x + < rectangle.x + rectangle.width + } + Axis::Vertical => { + relative_cursor.y > rectangle.y + && relative_cursor.y + < rectangle.y + rectangle.height + } + }) + .collect(); + + sorted_splits.sort_by_key( + |(_, (axis, rectangle, ratio))| { + let distance = match axis { + Axis::Horizontal => (relative_cursor.y + - (rectangle.y + + rectangle.height * ratio)) + .abs(), + Axis::Vertical => (relative_cursor.x + - (rectangle.x + + rectangle.width * ratio)) + .abs(), + }; + + distance.round() as u32 + }, + ); - if let Some((split, (axis, _, _))) = sorted_splits.first() { - self.state.pick_split(split, *axis); - self.trigger_resize(layout, cursor_position, messages); + if let Some((split, (axis, _, _))) = + sorted_splits.first() + { + self.state.pick_split(split, *axis); + self.trigger_resize( + layout, + cursor_position, + messages, + ); + } } } - } - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Right, - state: ButtonState::Released, - }) if self.state.picked_split().is_some() => { - self.state.drop_split(); - } - Event::Mouse(mouse::Event::CursorMoved { .. }) => { - self.trigger_resize(layout, cursor_position, messages); - } - Event::Keyboard(keyboard::Event::KeyPressed { - modifiers, - key_code, - }) => { - if let Some(on_key_press) = &self.on_key_press { - // TODO: Discard when event is captured - if let Some(_) = self.state.active_pane() { - if modifiers.matches(self.modifier_keys) { - if let Some(message) = on_key_press(KeyPressEvent { - key_code, - modifiers, - }) { - messages.push(message); + mouse::Event::ButtonPressed(mouse::Button::Right) + if self.state.picked_split().is_some() => + { + self.state.drop_split(); + } + mouse::Event::CursorMoved { .. } => { + self.trigger_resize(layout, cursor_position, messages); + } + _ => {} + }, + Event::Keyboard(keyboard_event) => { + match keyboard_event { + keyboard::Event::KeyPressed { + modifiers, + key_code, + } => { + if let Some(on_key_press) = &self.on_key_press { + // TODO: Discard when event is captured + if let Some(_) = self.state.active_pane() { + if modifiers.matches(self.modifier_keys) { + if let Some(message) = + on_key_press(KeyPressEvent { + key_code, + modifiers, + }) + { + messages.push(message); + } + } } } + + *self.pressed_modifiers = modifiers; } + keyboard::Event::KeyReleased { modifiers, .. } => { + *self.pressed_modifiers = modifiers; + } + _ => {} } - - *self.pressed_modifiers = modifiers; - } - Event::Keyboard(keyboard::Event::KeyReleased { - modifiers, .. - }) => { - *self.pressed_modifiers = modifiers; } _ => {} } diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index a13d86a0..deea7034 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -1,7 +1,6 @@ //! Create choices using radio buttons. use crate::{ - input::{mouse, ButtonState}, - layout, row, text, Align, Clipboard, Element, Event, Hasher, + input::mouse, layout, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; @@ -123,10 +122,7 @@ where _clipboard: Option<&dyn Clipboard>, ) { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { if layout.bounds().contains(cursor_position) { messages.push(self.on_click.clone()); } diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 393095a4..da11c50c 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -1,9 +1,7 @@ //! Navigate an endless amount of content with a scrollbar. use crate::{ - column, - input::{mouse, ButtonState}, - layout, Align, Clipboard, Column, Element, Event, Hasher, Layout, Length, - Point, Rectangle, Size, Widget, + column, input::mouse, layout, Align, Clipboard, Column, Element, Event, + Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; use std::{f32, hash::Hash, u32}; @@ -188,10 +186,9 @@ where if self.state.is_scroller_grabbed() { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Released, - }) => { + Event::Mouse(mouse::Event::ButtonReleased( + mouse::Button::Left, + )) => { self.state.scroller_grabbed_at = None; } Event::Mouse(mouse::Event::CursorMoved { .. }) => { @@ -212,10 +209,9 @@ where } } else if is_mouse_over_scrollbar { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { + Event::Mouse(mouse::Event::ButtonPressed( + mouse::Button::Left, + )) => { if let Some(scrollbar) = scrollbar { if let Some(scroller_grabbed_at) = scrollbar.grab_scroller(cursor_position) diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 1feb7825..a00d7c8d 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -5,9 +5,8 @@ //! [`Slider`]: struct.Slider.html //! [`State`]: struct.State.html use crate::{ - input::{mouse, ButtonState}, - layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, - Rectangle, Size, Widget, + input::mouse, layout, Clipboard, Element, Event, Hasher, Layout, Length, + Point, Rectangle, Size, Widget, }; use std::{hash::Hash, ops::RangeInclusive}; @@ -164,25 +163,23 @@ where }; match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { + Event::Mouse(mouse_event) => match mouse_event { + mouse::Event::ButtonPressed(mouse::Button::Left) => { if layout.bounds().contains(cursor_position) { change(); self.state.is_dragging = true; } } - ButtonState::Released => { + mouse::Event::ButtonReleased(mouse::Button::Left) => { self.state.is_dragging = false; } - }, - Event::Mouse(mouse::Event::CursorMoved { .. }) => { - if self.state.is_dragging { - change(); + mouse::Event::CursorMoved { .. } => { + if self.state.is_dragging { + change(); + } } - } + _ => {} + }, _ => {} } } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index ea6921b5..b11269db 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -18,7 +18,6 @@ use crate::{ input::{ keyboard, mouse::{self, click}, - ButtonState, }, layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, Rectangle, Size, Widget, @@ -212,10 +211,7 @@ where clipboard: Option<&dyn Clipboard>, ) { match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { let is_clicked = layout.bounds().contains(cursor_position); if is_clicked { @@ -280,10 +276,7 @@ where self.state.is_dragging = is_clicked; self.state.is_focused = is_clicked; } - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Released, - }) => { + Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => { self.state.is_dragging = false; } Event::Mouse(mouse::Event::CursorMoved { x, .. }) => { -- cgit From d8b9e03481301228ffeda4c6e48a021cb66f896c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:54:49 +0200 Subject: Remove `ButtonState` --- native/src/input.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'native') diff --git a/native/src/input.rs b/native/src/input.rs index 7f5114c3..ad8ed252 100644 --- a/native/src/input.rs +++ b/native/src/input.rs @@ -1,5 +1,3 @@ //! Map your system events into input events that the runtime can understand. pub mod keyboard; pub mod mouse; - -pub use iced_core::ButtonState; -- cgit From 137664ca88a9bf2398380fd1c04b00c62c868383 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 04:59:07 +0200 Subject: Move `keyboard::Event` to `iced_core` --- native/src/input/keyboard.rs | 5 +---- native/src/input/keyboard/event.rs | 31 ------------------------------- native/src/input/mouse.rs | 2 +- 3 files changed, 2 insertions(+), 36 deletions(-) delete mode 100644 native/src/input/keyboard/event.rs (limited to 'native') diff --git a/native/src/input/keyboard.rs b/native/src/input/keyboard.rs index 220b7f17..012538e3 100644 --- a/native/src/input/keyboard.rs +++ b/native/src/input/keyboard.rs @@ -1,5 +1,2 @@ -//! Build keyboard events. -mod event; - -pub use event::Event; +//! Track keyboard events. pub use iced_core::keyboard::*; diff --git a/native/src/input/keyboard/event.rs b/native/src/input/keyboard/event.rs deleted file mode 100644 index bc8437a8..00000000 --- a/native/src/input/keyboard/event.rs +++ /dev/null @@ -1,31 +0,0 @@ -use super::{KeyCode, ModifiersState}; - -/// A keyboard event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Event { - /// A keyboard key was pressed. - KeyPressed { - /// The key identifier - key_code: KeyCode, - - /// The state of the modifier keys - modifiers: ModifiersState, - }, - - /// A keyboard key was released. - KeyReleased { - /// The key identifier - key_code: KeyCode, - - /// The state of the modifier keys - modifiers: ModifiersState, - }, - - /// A unicode character was received. - CharacterReceived(char), -} diff --git a/native/src/input/mouse.rs b/native/src/input/mouse.rs index ae3f1596..9ee406cf 100644 --- a/native/src/input/mouse.rs +++ b/native/src/input/mouse.rs @@ -1,4 +1,4 @@ -//! Build mouse events. +//! Track mouse events. pub mod click; -- cgit From bb9ccc4f62ceea08dc1ef0c6c4d3d219897e44a1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 05:04:45 +0200 Subject: Remove inconsistent `input` module in `iced_native` --- native/src/event.rs | 5 +-- native/src/input.rs | 3 -- native/src/input/keyboard.rs | 2 - native/src/input/mouse.rs | 6 --- native/src/input/mouse/click.rs | 76 ------------------------------------ native/src/keyboard.rs | 2 + native/src/lib.rs | 3 +- native/src/mouse.rs | 6 +++ native/src/mouse/click.rs | 76 ++++++++++++++++++++++++++++++++++++ native/src/user_interface.rs | 4 +- native/src/widget/button.rs | 4 +- native/src/widget/checkbox.rs | 2 +- native/src/widget/pane_grid.rs | 5 +-- native/src/widget/pane_grid/state.rs | 2 +- native/src/widget/radio.rs | 2 +- native/src/widget/scrollable.rs | 4 +- native/src/widget/slider.rs | 4 +- native/src/widget/text_input.rs | 12 +++--- 18 files changed, 104 insertions(+), 114 deletions(-) delete mode 100644 native/src/input.rs delete mode 100644 native/src/input/keyboard.rs delete mode 100644 native/src/input/mouse.rs delete mode 100644 native/src/input/mouse/click.rs create mode 100644 native/src/keyboard.rs create mode 100644 native/src/mouse.rs create mode 100644 native/src/mouse/click.rs (limited to 'native') diff --git a/native/src/event.rs b/native/src/event.rs index b2550ead..606a71d6 100644 --- a/native/src/event.rs +++ b/native/src/event.rs @@ -1,7 +1,4 @@ -use crate::{ - input::{keyboard, mouse}, - window, -}; +use crate::{keyboard, mouse, window}; /// A user interface event. /// diff --git a/native/src/input.rs b/native/src/input.rs deleted file mode 100644 index ad8ed252..00000000 --- a/native/src/input.rs +++ /dev/null @@ -1,3 +0,0 @@ -//! Map your system events into input events that the runtime can understand. -pub mod keyboard; -pub mod mouse; diff --git a/native/src/input/keyboard.rs b/native/src/input/keyboard.rs deleted file mode 100644 index 012538e3..00000000 --- a/native/src/input/keyboard.rs +++ /dev/null @@ -1,2 +0,0 @@ -//! Track keyboard events. -pub use iced_core::keyboard::*; diff --git a/native/src/input/mouse.rs b/native/src/input/mouse.rs deleted file mode 100644 index 9ee406cf..00000000 --- a/native/src/input/mouse.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Track mouse events. - -pub mod click; - -pub use click::Click; -pub use iced_core::mouse::*; diff --git a/native/src/input/mouse/click.rs b/native/src/input/mouse/click.rs deleted file mode 100644 index d27bc67e..00000000 --- a/native/src/input/mouse/click.rs +++ /dev/null @@ -1,76 +0,0 @@ -//! Track mouse clicks. -use crate::Point; -use std::time::Instant; - -/// A mouse click. -#[derive(Debug, Clone, Copy)] -pub struct Click { - kind: Kind, - position: Point, - time: Instant, -} - -/// The kind of mouse click. -#[derive(Debug, Clone, Copy)] -pub enum Kind { - /// A single click - Single, - - /// A double click - Double, - - /// A triple click - Triple, -} - -impl Kind { - fn next(&self) -> Kind { - match self { - Kind::Single => Kind::Double, - Kind::Double => Kind::Triple, - Kind::Triple => Kind::Double, - } - } -} - -impl Click { - /// Creates a new [`Click`] with the given position and previous last - /// [`Click`]. - /// - /// [`Click`]: struct.Click.html - pub fn new(position: Point, previous: Option) -> Click { - let time = Instant::now(); - - let kind = if let Some(previous) = previous { - if previous.is_consecutive(position, time) { - previous.kind.next() - } else { - Kind::Single - } - } else { - Kind::Single - }; - - Click { - kind, - position, - time, - } - } - - /// Returns the [`Kind`] of [`Click`]. - /// - /// [`Kind`]: enum.Kind.html - /// [`Click`]: struct.Click.html - pub fn kind(&self) -> Kind { - self.kind - } - - fn is_consecutive(&self, new_position: Point, time: Instant) -> bool { - self.position == new_position - && time - .checked_duration_since(self.time) - .map(|duration| duration.as_millis() <= 300) - .unwrap_or(false) - } -} diff --git a/native/src/keyboard.rs b/native/src/keyboard.rs new file mode 100644 index 00000000..012538e3 --- /dev/null +++ b/native/src/keyboard.rs @@ -0,0 +1,2 @@ +//! Track keyboard events. +pub use iced_core::keyboard::*; diff --git a/native/src/lib.rs b/native/src/lib.rs index a3b581b3..88bf4423 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -39,8 +39,9 @@ #![deny(unused_results)] #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] -pub mod input; +pub mod keyboard; pub mod layout; +pub mod mouse; pub mod renderer; pub mod subscription; pub mod widget; diff --git a/native/src/mouse.rs b/native/src/mouse.rs new file mode 100644 index 00000000..9ee406cf --- /dev/null +++ b/native/src/mouse.rs @@ -0,0 +1,6 @@ +//! Track mouse events. + +pub mod click; + +pub use click::Click; +pub use iced_core::mouse::*; diff --git a/native/src/mouse/click.rs b/native/src/mouse/click.rs new file mode 100644 index 00000000..d27bc67e --- /dev/null +++ b/native/src/mouse/click.rs @@ -0,0 +1,76 @@ +//! Track mouse clicks. +use crate::Point; +use std::time::Instant; + +/// A mouse click. +#[derive(Debug, Clone, Copy)] +pub struct Click { + kind: Kind, + position: Point, + time: Instant, +} + +/// The kind of mouse click. +#[derive(Debug, Clone, Copy)] +pub enum Kind { + /// A single click + Single, + + /// A double click + Double, + + /// A triple click + Triple, +} + +impl Kind { + fn next(&self) -> Kind { + match self { + Kind::Single => Kind::Double, + Kind::Double => Kind::Triple, + Kind::Triple => Kind::Double, + } + } +} + +impl Click { + /// Creates a new [`Click`] with the given position and previous last + /// [`Click`]. + /// + /// [`Click`]: struct.Click.html + pub fn new(position: Point, previous: Option) -> Click { + let time = Instant::now(); + + let kind = if let Some(previous) = previous { + if previous.is_consecutive(position, time) { + previous.kind.next() + } else { + Kind::Single + } + } else { + Kind::Single + }; + + Click { + kind, + position, + time, + } + } + + /// Returns the [`Kind`] of [`Click`]. + /// + /// [`Kind`]: enum.Kind.html + /// [`Click`]: struct.Click.html + pub fn kind(&self) -> Kind { + self.kind + } + + fn is_consecutive(&self, new_position: Point, time: Instant) -> bool { + self.position == new_position + && time + .checked_duration_since(self.time) + .map(|duration| duration.as_millis() <= 300) + .unwrap_or(false) + } +} diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 5d9221e9..48cd6111 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -1,6 +1,4 @@ -use crate::{ - input::mouse, layout, Clipboard, Element, Event, Layout, Point, Size, -}; +use crate::{layout, mouse, Clipboard, Element, Event, Layout, Point, Size}; use std::hash::Hasher; diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 5d414023..c932da2b 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -5,8 +5,8 @@ //! [`Button`]: struct.Button.html //! [`State`]: struct.State.html use crate::{ - input::mouse, layout, Clipboard, Element, Event, Hasher, Layout, Length, - Point, Rectangle, Widget, + layout, mouse, Clipboard, Element, Event, Hasher, Layout, Length, Point, + Rectangle, Widget, }; use std::hash::Hash; diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index c49ac707..5fb13290 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -2,7 +2,7 @@ use std::hash::Hash; use crate::{ - input::mouse, layout, row, text, Align, Clipboard, Element, Event, Hasher, + layout, mouse, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index fe2bbe07..c398a30b 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -22,9 +22,8 @@ pub use split::Split; pub use state::{Focus, State}; use crate::{ - input::{keyboard, mouse}, - layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Size, - Widget, + keyboard, layout, mouse, Clipboard, Element, Event, Hasher, Layout, Length, + Point, Size, Widget, }; /// A collection of panes distributed using either vertical or horizontal splits diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 0a8b8419..ed2813b8 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -1,5 +1,5 @@ use crate::{ - input::keyboard, + keyboard, pane_grid::{node::Node, Axis, Direction, Pane, Split}, Hasher, Point, Rectangle, Size, }; diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index deea7034..ab5bcf32 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -1,6 +1,6 @@ //! Create choices using radio buttons. use crate::{ - input::mouse, layout, row, text, Align, Clipboard, Element, Event, Hasher, + layout, mouse, row, text, Align, Clipboard, Element, Event, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, }; diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index da11c50c..3c8e5e5b 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -1,7 +1,7 @@ //! Navigate an endless amount of content with a scrollbar. use crate::{ - column, input::mouse, layout, Align, Clipboard, Column, Element, Event, - Hasher, Layout, Length, Point, Rectangle, Size, Widget, + column, layout, mouse, Align, Clipboard, Column, Element, Event, Hasher, + Layout, Length, Point, Rectangle, Size, Widget, }; use std::{f32, hash::Hash, u32}; diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index a00d7c8d..8cdfc3de 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -5,8 +5,8 @@ //! [`Slider`]: struct.Slider.html //! [`State`]: struct.State.html use crate::{ - input::mouse, layout, Clipboard, Element, Event, Hasher, Layout, Length, - Point, Rectangle, Size, Widget, + layout, mouse, Clipboard, Element, Event, Hasher, Layout, Length, Point, + Rectangle, Size, Widget, }; use std::{hash::Hash, ops::RangeInclusive}; diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index b11269db..1cdbe007 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -15,12 +15,10 @@ pub use value::Value; use editor::Editor; use crate::{ - input::{ - keyboard, - mouse::{self, click}, - }, - layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, - Rectangle, Size, Widget, + keyboard, layout, + mouse::{self, click}, + Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, Rectangle, + Size, Widget, }; use std::u32; @@ -739,7 +737,7 @@ fn find_cursor_position( } mod platform { - use crate::input::keyboard; + use crate::keyboard; pub fn is_jump_modifier_pressed( modifiers: keyboard::ModifiersState, -- cgit From 98bc8cf2a7c4944d762a0148ca9f615d6ccc0d6e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 30 Apr 2020 08:16:38 +0200 Subject: Rename `MouseCursor` to `mouse::Interaction` --- native/src/lib.rs | 4 ++-- native/src/window/backend.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'native') diff --git a/native/src/lib.rs b/native/src/lib.rs index 88bf4423..9882803f 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -55,8 +55,8 @@ mod runtime; mod user_interface; pub use iced_core::{ - Align, Background, Color, Font, HorizontalAlignment, Length, MouseCursor, - Point, Rectangle, Size, Vector, VerticalAlignment, + Align, Background, Color, Font, HorizontalAlignment, Length, Point, + Rectangle, Size, Vector, VerticalAlignment, }; pub use iced_futures::{executor, futures, Command}; diff --git a/native/src/window/backend.rs b/native/src/window/backend.rs index 3bc691cd..892d4bb9 100644 --- a/native/src/window/backend.rs +++ b/native/src/window/backend.rs @@ -1,4 +1,4 @@ -use crate::MouseCursor; +use crate::mouse; use raw_window_handle::HasRawWindowHandle; @@ -51,5 +51,5 @@ pub trait Backend: Sized { output: &::Output, scale_factor: f64, overlay: &[T], - ) -> MouseCursor; + ) -> mouse::Interaction; } -- cgit