Merge branch 'ios-support-wip' into feature/touch-support
This commit is contained in:
commit
09110a93b0
@ -1,3 +1,5 @@
|
|||||||
|
use crate::Point;
|
||||||
|
|
||||||
use super::Button;
|
use super::Button;
|
||||||
|
|
||||||
/// A mouse event.
|
/// A mouse event.
|
||||||
@ -16,11 +18,8 @@ pub enum Event {
|
|||||||
|
|
||||||
/// The mouse cursor was moved
|
/// The mouse cursor was moved
|
||||||
CursorMoved {
|
CursorMoved {
|
||||||
/// The X coordinate of the mouse position
|
/// The new position of the mouse cursor
|
||||||
x: f32,
|
position: Point,
|
||||||
|
|
||||||
/// The Y coordinate of the mouse position
|
|
||||||
y: f32,
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/// A mouse button was pressed.
|
/// A mouse button was pressed.
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
//! Handle events of a user interface.
|
//! Handle events of a user interface.
|
||||||
use crate::{keyboard, mouse, window};
|
use crate::keyboard;
|
||||||
|
use crate::mouse;
|
||||||
|
use crate::touch;
|
||||||
|
use crate::window;
|
||||||
|
|
||||||
/// A user interface event.
|
/// A user interface event.
|
||||||
///
|
///
|
||||||
@ -17,6 +20,9 @@ pub enum Event {
|
|||||||
|
|
||||||
/// A window event
|
/// A window event
|
||||||
Window(window::Event),
|
Window(window::Event),
|
||||||
|
|
||||||
|
/// A touch event
|
||||||
|
Touch(touch::Touch),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The status of an [`Event`] after being processed.
|
/// The status of an [`Event`] after being processed.
|
||||||
|
@ -41,6 +41,7 @@ pub mod overlay;
|
|||||||
pub mod program;
|
pub mod program;
|
||||||
pub mod renderer;
|
pub mod renderer;
|
||||||
pub mod subscription;
|
pub mod subscription;
|
||||||
|
pub mod touch;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
pub mod window;
|
pub mod window;
|
||||||
|
|
||||||
|
35
native/src/touch.rs
Normal file
35
native/src/touch.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
//! Build touch events.
|
||||||
|
use crate::Point;
|
||||||
|
|
||||||
|
/// A touch interaction.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub struct Touch {
|
||||||
|
/// The finger of the touch.
|
||||||
|
pub finger: Finger,
|
||||||
|
|
||||||
|
/// The position of the touch.
|
||||||
|
pub position: Point,
|
||||||
|
|
||||||
|
/// The state of the touch.
|
||||||
|
pub phase: Phase,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A unique identifier representing a finger on a touch interaction.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct Finger(pub u64);
|
||||||
|
|
||||||
|
/// The state of a touch interaction.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum Phase {
|
||||||
|
/// A touch interaction was started.
|
||||||
|
Started,
|
||||||
|
|
||||||
|
/// An on-going touch interaction was moved.
|
||||||
|
Moved,
|
||||||
|
|
||||||
|
/// A touch interaction was ended.
|
||||||
|
Ended,
|
||||||
|
|
||||||
|
/// A touch interaction was canceled.
|
||||||
|
Canceled,
|
||||||
|
}
|
@ -4,6 +4,7 @@
|
|||||||
use crate::event::{self, Event};
|
use crate::event::{self, Event};
|
||||||
use crate::layout;
|
use crate::layout;
|
||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
|
use crate::touch::{self, Touch};
|
||||||
use crate::{
|
use crate::{
|
||||||
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Widget,
|
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Widget,
|
||||||
};
|
};
|
||||||
@ -164,7 +165,11 @@ where
|
|||||||
_clipboard: Option<&dyn Clipboard>,
|
_clipboard: Option<&dyn Clipboard>,
|
||||||
) -> event::Status {
|
) -> event::Status {
|
||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Started,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
if self.on_press.is_some() {
|
if self.on_press.is_some() {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
@ -175,7 +180,11 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Ended,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
if let Some(on_press) = self.on_press.clone() {
|
if let Some(on_press) = self.on_press.clone() {
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
|
|
||||||
@ -190,6 +199,12 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Canceled,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
self.state.is_pressed = false;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ use crate::layout;
|
|||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
use crate::row;
|
use crate::row;
|
||||||
use crate::text;
|
use crate::text;
|
||||||
|
use crate::touch::{self, Touch};
|
||||||
use crate::{
|
use crate::{
|
||||||
Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length,
|
Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length,
|
||||||
Point, Rectangle, Row, Text, VerticalAlignment, Widget,
|
Point, Rectangle, Row, Text, VerticalAlignment, Widget,
|
||||||
@ -154,7 +155,11 @@ where
|
|||||||
_clipboard: Option<&dyn Clipboard>,
|
_clipboard: Option<&dyn Clipboard>,
|
||||||
) -> event::Status {
|
) -> event::Status {
|
||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Started,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
let mouse_over = layout.bounds().contains(cursor_position);
|
let mouse_over = layout.bounds().contains(cursor_position);
|
||||||
|
|
||||||
if mouse_over {
|
if mouse_over {
|
||||||
|
@ -4,6 +4,7 @@ use crate::layout;
|
|||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
use crate::row;
|
use crate::row;
|
||||||
use crate::text;
|
use crate::text;
|
||||||
|
use crate::touch::{self, Touch};
|
||||||
use crate::{
|
use crate::{
|
||||||
Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length,
|
Align, Clipboard, Element, Hasher, HorizontalAlignment, Layout, Length,
|
||||||
Point, Rectangle, Row, Text, VerticalAlignment, Widget,
|
Point, Rectangle, Row, Text, VerticalAlignment, Widget,
|
||||||
@ -160,7 +161,11 @@ where
|
|||||||
_clipboard: Option<&dyn Clipboard>,
|
_clipboard: Option<&dyn Clipboard>,
|
||||||
) -> event::Status {
|
) -> event::Status {
|
||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Started,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
if layout.bounds().contains(cursor_position) {
|
if layout.bounds().contains(cursor_position) {
|
||||||
messages.push(self.on_click.clone());
|
messages.push(self.on_click.clone());
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ use crate::event::{self, Event};
|
|||||||
use crate::layout;
|
use crate::layout;
|
||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
use crate::overlay;
|
use crate::overlay;
|
||||||
|
use crate::touch::{self, Touch};
|
||||||
use crate::{
|
use crate::{
|
||||||
Align, Clipboard, Column, Element, Hasher, Layout, Length, Point,
|
Align, Clipboard, Column, Element, Hasher, Layout, Length, Point,
|
||||||
Rectangle, Size, Vector, Widget,
|
Rectangle, Size, Vector, Widget,
|
||||||
@ -229,6 +230,26 @@ where
|
|||||||
|
|
||||||
return event::Status::Captured;
|
return event::Status::Captured;
|
||||||
}
|
}
|
||||||
|
Event::Touch(Touch { phase, .. }) => match phase {
|
||||||
|
touch::Phase::Started => {
|
||||||
|
self.state.scroll_box_touched_at =
|
||||||
|
Some(cursor_position);
|
||||||
|
}
|
||||||
|
touch::Phase::Moved => {
|
||||||
|
if let Some(scroll_box_touched_at) =
|
||||||
|
self.state.scroll_box_touched_at
|
||||||
|
{
|
||||||
|
let delta =
|
||||||
|
cursor_position.y - scroll_box_touched_at.y;
|
||||||
|
self.state.scroll(delta, bounds, content_bounds);
|
||||||
|
self.state.scroll_box_touched_at =
|
||||||
|
Some(cursor_position);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
touch::Phase::Ended | touch::Phase::Canceled => {
|
||||||
|
self.state.scroll_box_touched_at = None;
|
||||||
|
}
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -237,12 +258,24 @@ where
|
|||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonReleased(
|
Event::Mouse(mouse::Event::ButtonReleased(
|
||||||
mouse::Button::Left,
|
mouse::Button::Left,
|
||||||
)) => {
|
))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Ended,
|
||||||
|
..
|
||||||
|
})
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Canceled,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
self.state.scroller_grabbed_at = None;
|
self.state.scroller_grabbed_at = None;
|
||||||
|
|
||||||
return event::Status::Captured;
|
return event::Status::Captured;
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
|
Event::Mouse(mouse::Event::CursorMoved { .. })
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Moved,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
if let (Some(scrollbar), Some(scroller_grabbed_at)) =
|
if let (Some(scrollbar), Some(scroller_grabbed_at)) =
|
||||||
(scrollbar, self.state.scroller_grabbed_at)
|
(scrollbar, self.state.scroller_grabbed_at)
|
||||||
{
|
{
|
||||||
@ -264,7 +297,11 @@ where
|
|||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(
|
Event::Mouse(mouse::Event::ButtonPressed(
|
||||||
mouse::Button::Left,
|
mouse::Button::Left,
|
||||||
)) => {
|
))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Started,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
if let Some(scrollbar) = scrollbar {
|
if let Some(scrollbar) = scrollbar {
|
||||||
if let Some(scroller_grabbed_at) =
|
if let Some(scroller_grabbed_at) =
|
||||||
scrollbar.grab_scroller(cursor_position)
|
scrollbar.grab_scroller(cursor_position)
|
||||||
@ -385,6 +422,7 @@ where
|
|||||||
#[derive(Debug, Clone, Copy, Default)]
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
scroller_grabbed_at: Option<f32>,
|
scroller_grabbed_at: Option<f32>,
|
||||||
|
scroll_box_touched_at: Option<Point>,
|
||||||
offset: f32,
|
offset: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -439,6 +477,11 @@ impl State {
|
|||||||
pub fn is_scroller_grabbed(&self) -> bool {
|
pub fn is_scroller_grabbed(&self) -> bool {
|
||||||
self.scroller_grabbed_at.is_some()
|
self.scroller_grabbed_at.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns whether the scroll box is currently touched or not.
|
||||||
|
pub fn is_scroll_box_touched(&self) -> bool {
|
||||||
|
self.scroll_box_touched_at.is_some()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The scrollbar of a [`Scrollable`].
|
/// The scrollbar of a [`Scrollable`].
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
use crate::event::{self, Event};
|
use crate::event::{self, Event};
|
||||||
use crate::layout;
|
use crate::layout;
|
||||||
use crate::mouse;
|
use crate::mouse;
|
||||||
|
use crate::touch::{self, Touch};
|
||||||
use crate::{
|
use crate::{
|
||||||
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
|
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
|
||||||
};
|
};
|
||||||
@ -207,34 +208,43 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse_event) => match mouse_event {
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
mouse::Event::ButtonPressed(mouse::Button::Left) => {
|
| Event::Touch(Touch {
|
||||||
if layout.bounds().contains(cursor_position) {
|
phase: touch::Phase::Started,
|
||||||
change();
|
..
|
||||||
self.state.is_dragging = true;
|
}) => {
|
||||||
|
if layout.bounds().contains(cursor_position) {
|
||||||
|
change();
|
||||||
|
self.state.is_dragging = true;
|
||||||
|
|
||||||
return event::Status::Captured;
|
return event::Status::Captured;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mouse::Event::ButtonReleased(mouse::Button::Left) => {
|
}
|
||||||
if self.state.is_dragging {
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
|
||||||
if let Some(on_release) = self.on_release.clone() {
|
| Event::Touch(Touch {
|
||||||
messages.push(on_release);
|
phase: touch::Phase::Ended,
|
||||||
}
|
..
|
||||||
self.state.is_dragging = false;
|
}) => {
|
||||||
|
if self.state.is_dragging {
|
||||||
|
if let Some(on_release) = self.on_release.clone() {
|
||||||
|
messages.push(on_release);
|
||||||
|
}
|
||||||
|
self.state.is_dragging = false;
|
||||||
|
|
||||||
return event::Status::Captured;
|
return event::Status::Captured;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mouse::Event::CursorMoved { .. } => {
|
}
|
||||||
if self.state.is_dragging {
|
Event::Mouse(mouse::Event::CursorMoved { .. })
|
||||||
change();
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Moved,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
|
if self.state.is_dragging {
|
||||||
|
change();
|
||||||
|
|
||||||
return event::Status::Captured;
|
return event::Status::Captured;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => {}
|
}
|
||||||
},
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ use crate::keyboard;
|
|||||||
use crate::layout;
|
use crate::layout;
|
||||||
use crate::mouse::{self, click};
|
use crate::mouse::{self, click};
|
||||||
use crate::text;
|
use crate::text;
|
||||||
|
use crate::touch::{self, Touch};
|
||||||
use crate::{
|
use crate::{
|
||||||
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
|
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
|
||||||
};
|
};
|
||||||
@ -247,7 +248,11 @@ where
|
|||||||
clipboard: Option<&dyn Clipboard>,
|
clipboard: Option<&dyn Clipboard>,
|
||||||
) -> event::Status {
|
) -> event::Status {
|
||||||
match event {
|
match event {
|
||||||
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
|
||||||
|
| Event::Touch(Touch {
|
||||||
|
phase: touch::Phase::Started,
|
||||||
|
..
|
||||||
|
}) => {
|
||||||
let is_clicked = layout.bounds().contains(cursor_position);
|
let is_clicked = layout.bounds().contains(cursor_position);
|
||||||
|
|
||||||
self.state.is_focused = is_clicked;
|
self.state.is_focused = is_clicked;
|
||||||
@ -321,10 +326,10 @@ where
|
|||||||
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
|
||||||
self.state.is_dragging = false;
|
self.state.is_dragging = false;
|
||||||
}
|
}
|
||||||
Event::Mouse(mouse::Event::CursorMoved { x, .. }) => {
|
Event::Mouse(mouse::Event::CursorMoved { position }) => {
|
||||||
if self.state.is_dragging {
|
if self.state.is_dragging {
|
||||||
let text_layout = layout.children().next().unwrap();
|
let text_layout = layout.children().next().unwrap();
|
||||||
let target = x - text_layout.bounds().x;
|
let target = position.x - text_layout.bounds().x;
|
||||||
|
|
||||||
if target > 0.0 {
|
if target > 0.0 {
|
||||||
let value = if self.is_secure {
|
let value = if self.is_secure {
|
||||||
|
@ -4,7 +4,9 @@
|
|||||||
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
|
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
|
||||||
use crate::{
|
use crate::{
|
||||||
keyboard::{self, KeyCode, Modifiers},
|
keyboard::{self, KeyCode, Modifiers},
|
||||||
mouse, window, Event, Mode, Point,
|
mouse,
|
||||||
|
touch::{self, Touch},
|
||||||
|
window, Event, Mode, Point,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Converts a winit window event into an iced event.
|
/// Converts a winit window event into an iced event.
|
||||||
@ -36,8 +38,7 @@ pub fn window_event(
|
|||||||
let position = position.to_logical::<f64>(scale_factor);
|
let position = position.to_logical::<f64>(scale_factor);
|
||||||
|
|
||||||
Some(Event::Mouse(mouse::Event::CursorMoved {
|
Some(Event::Mouse(mouse::Event::CursorMoved {
|
||||||
x: position.x as f32,
|
position: Point::new(position.x as f32, position.y as f32),
|
||||||
y: position.y as f32,
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
WindowEvent::CursorEntered { .. } => {
|
WindowEvent::CursorEntered { .. } => {
|
||||||
@ -118,6 +119,7 @@ pub fn window_event(
|
|||||||
WindowEvent::HoveredFileCancelled => {
|
WindowEvent::HoveredFileCancelled => {
|
||||||
Some(Event::Window(window::Event::FilesHoveredLeft))
|
Some(Event::Window(window::Event::FilesHoveredLeft))
|
||||||
}
|
}
|
||||||
|
WindowEvent::Touch(touch) => Some(Event::Touch(touch_event(*touch))),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,6 +202,25 @@ pub fn cursor_position(
|
|||||||
Point::new(logical_position.x, logical_position.y)
|
Point::new(logical_position.x, logical_position.y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event.
|
||||||
|
///
|
||||||
|
/// [`winit`]: https://github.com/rust-windowing/winit
|
||||||
|
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
|
||||||
|
pub fn touch_event(touch: winit::event::Touch) -> Touch {
|
||||||
|
let phase = match touch.phase {
|
||||||
|
winit::event::TouchPhase::Started => touch::Phase::Started,
|
||||||
|
winit::event::TouchPhase::Moved => touch::Phase::Moved,
|
||||||
|
winit::event::TouchPhase::Ended => touch::Phase::Ended,
|
||||||
|
winit::event::TouchPhase::Cancelled => touch::Phase::Canceled,
|
||||||
|
};
|
||||||
|
|
||||||
|
Touch {
|
||||||
|
finger: touch::Finger(touch.id),
|
||||||
|
position: Point::new(touch.location.x as f32, touch.location.y as f32),
|
||||||
|
phase,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
|
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
|
||||||
///
|
///
|
||||||
/// [`winit`]: https://github.com/rust-windowing/winit
|
/// [`winit`]: https://github.com/rust-windowing/winit
|
||||||
|
Loading…
Reference in New Issue
Block a user