From 13ff494e9d5f073b4cacd48fff13f31d6672a9fa Mon Sep 17 00:00:00 2001 From: Sebastian Imlay Date: Mon, 11 Nov 2019 20:29:58 -0800 Subject: [PATCH] Added initial touch events to support iOS --- native/src/event.rs | 5 +++- native/src/input.rs | 1 + native/src/input/touch.rs | 37 +++++++++++++++++++++++++++ native/src/user_interface.rs | 14 ++++++++--- native/src/widget/button.rs | 35 +++++++++++++------------- native/src/widget/checkbox.rs | 5 ++-- native/src/widget/radio.rs | 5 ++-- native/src/widget/scrollable.rs | 44 ++++++++++++++++++++++++++++++--- native/src/widget/slider.rs | 30 ++++++++++++---------- native/src/widget/text_input.rs | 7 ++++-- wgpu/Cargo.toml | 2 +- winit/Cargo.toml | 5 ++-- winit/src/application.rs | 1 - winit/src/conversion.rs | 26 ++++++++++++++++++- 14 files changed, 168 insertions(+), 49 deletions(-) create mode 100644 native/src/input/touch.rs diff --git a/native/src/event.rs b/native/src/event.rs index b2550eadf8..fb5b9977b0 100644 --- a/native/src/event.rs +++ b/native/src/event.rs @@ -1,5 +1,5 @@ use crate::{ - input::{keyboard, mouse}, + input::{keyboard, mouse, touch}, window, }; @@ -19,4 +19,7 @@ pub enum Event { /// A window event Window(window::Event), + + /// A touch event + Touch(touch::Touch), } diff --git a/native/src/input.rs b/native/src/input.rs index 097fa73071..c08beaf974 100644 --- a/native/src/input.rs +++ b/native/src/input.rs @@ -1,6 +1,7 @@ //! Map your system events into input events that the runtime can understand. pub mod keyboard; pub mod mouse; +pub mod touch; mod button_state; diff --git a/native/src/input/touch.rs b/native/src/input/touch.rs new file mode 100644 index 0000000000..c8b62e14d3 --- /dev/null +++ b/native/src/input/touch.rs @@ -0,0 +1,37 @@ +/// The touch of a mobile device. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Touch { + Started { + /// The X coordinate of the touch position + x: f32, + + /// The Y coordinate of the touch position + y: f32, + }, + /// The touch cursor was ended + Ended { + /// The X coordinate of the touch position + x: f32, + + /// The Y coordinate of the touch position + y: f32, + }, + + /// The touch was moved. + Moved { + /// The X coordinate of the touch position + x: f32, + + /// The Y coordinate of the touch position + y: f32, + }, + + /// Some canceled button. + Cancelled { + /// The X coordinate of the touch position + x: f32, + + /// The Y coordinate of the touch position + y: f32, + }, +} diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 08914bed37..751b26525e 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -1,5 +1,6 @@ use crate::{ - input::mouse, layout, Clipboard, Element, Event, Layout, Point, Size, + input::{mouse, touch}, + layout, Clipboard, Element, Event, Layout, Point, Size, }; use std::hash::Hasher; @@ -181,8 +182,15 @@ where let mut messages = Vec::new(); for event in events { - if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { - self.cursor_position = Point::new(x, y); + match event { + Event::Mouse(mouse::Event::CursorMoved { x, y }) + | Event::Touch(touch::Touch::Started { x, y }) + | Event::Touch(touch::Touch::Ended { x, y }) + | Event::Touch(touch::Touch::Moved { x, y }) + | Event::Touch(touch::Touch::Cancelled { x, y }) => { + self.cursor_position = Point::new(x, y); + } + _ => {} } self.root.widget.on_event( diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index f1d4693682..8c397bc179 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -5,7 +5,7 @@ //! [`Button`]: struct.Button.html //! [`State`]: struct.State.html use crate::{ - input::{mouse, ButtonState}, + input::{mouse, touch::Touch, ButtonState}, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle, Widget, }; @@ -187,26 +187,27 @@ where match event { Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, - state, - }) => { + state: ButtonState::Pressed, + }) + | Event::Touch(Touch::Started { .. }) => { + let bounds = layout.bounds(); + + self.state.is_pressed = bounds.contains(cursor_position); + } + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Released, + }) + | Event::Touch(Touch::Ended { .. }) => { if let Some(on_press) = self.on_press.clone() { let bounds = layout.bounds(); + let is_clicked = self.state.is_pressed + && bounds.contains(cursor_position); - 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); - - 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 b36d10a491..26665d8b89 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, ButtonState}, + input::{mouse, touch::Touch, ButtonState}, layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, @@ -155,7 +155,8 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - }) => { + }) + | Event::Touch(Touch::Started { .. }) => { let mouse_over = layout.bounds().contains(cursor_position); if mouse_over { diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index cdc4862cd1..8a9c02ce22 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, ButtonState}, + input::{mouse, touch, ButtonState}, layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, VerticalAlignment, Widget, @@ -121,7 +121,8 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - }) => { + }) + | Event::Touch(touch::Touch::Started { .. }) => { 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 e83f25af9b..10a3f0f752 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, ButtonState}, + input::{mouse, touch, ButtonState}, layout, Align, Clipboard, Column, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -175,6 +175,22 @@ where } } } + Event::Touch(touch::Touch::Started { .. }) => { + self.state.scroll_box_touched_at = Some(cursor_position); + } + Event::Touch(touch::Touch::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); + } + } + Event::Touch(touch::Touch::Ended { .. }) => { + self.state.scroll_box_touched_at = None; + } _ => {} } } @@ -191,10 +207,23 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Released, - }) => { + }) + | Event::Touch(touch::Touch::Ended { .. }) => { self.state.scroller_grabbed_at = None; } - Event::Mouse(mouse::Event::CursorMoved { .. }) => { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) + | Event::Touch(touch::Touch::Started { .. }) => { + self.state.scroll_to( + cursor_position.y / (bounds.y + bounds.height), + bounds, + content_bounds, + ); + } + Event::Mouse(mouse::Event::CursorMoved { .. }) + | Event::Touch(touch::Touch::Moved { .. }) => { if let (Some(scrollbar), Some(scroller_grabbed_at)) = (scrollbar, self.state.scroller_grabbed_at) { @@ -215,7 +244,8 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - }) => { + }) + | Event::Touch(touch::Touch::Started { .. }) => { if let Some(scrollbar) = scrollbar { if let Some(scroller_grabbed_at) = scrollbar.grab_scroller(cursor_position) @@ -326,6 +356,7 @@ where #[derive(Debug, Clone, Copy, Default)] pub struct State { scroller_grabbed_at: Option, + scroll_box_touched_at: Option, offset: f32, } @@ -391,6 +422,11 @@ impl State { pub fn is_scroller_grabbed(&self) -> bool { 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`]. diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 008203fe32..95f63921b5 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -5,7 +5,7 @@ //! [`Slider`]: struct.Slider.html //! [`State`]: struct.State.html use crate::{ - input::{mouse, ButtonState}, + input::{mouse, touch::Touch, ButtonState}, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -166,19 +166,23 @@ where match event { Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { - if layout.bounds().contains(cursor_position) { - change(); - self.state.is_dragging = true; - } - } - ButtonState::Released => { - self.state.is_dragging = false; + state: ButtonState::Pressed, + }) + | Event::Touch(Touch::Started { .. }) => { + if layout.bounds().contains(cursor_position) { + change(); + self.state.is_dragging = true; } - }, - Event::Mouse(mouse::Event::CursorMoved { .. }) => { + } + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Released, + }) + | Event::Touch(Touch::Ended { .. }) => { + self.state.is_dragging = false; + } + Event::Mouse(mouse::Event::CursorMoved { .. }) + | Event::Touch(Touch::Moved { .. }) => { if self.state.is_dragging { change(); } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index c068b895d6..10ca5ffe9a 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -5,7 +5,7 @@ //! [`TextInput`]: struct.TextInput.html //! [`State`]: struct.State.html use crate::{ - input::{keyboard, mouse, ButtonState}, + input::{keyboard, mouse, touch, ButtonState}, layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; @@ -202,7 +202,10 @@ where Event::Mouse(mouse::Event::Input { button: mouse::Button::Left, state: ButtonState::Pressed, - }) => { + }) + | Event::Touch(touch::Touch::Started { .. }) => { + self.state.is_focused = + layout.bounds().contains(cursor_position); let is_clicked = layout.bounds().contains(cursor_position); if is_clicked { diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 887c2d2167..f2e8cfe55d 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -19,7 +19,7 @@ glyph_brush = "0.6" wgpu_glyph = { version = "0.7", git = "https://github.com/hecrj/wgpu_glyph", branch = "fix/font-load-panic" } raw-window-handle = "0.3" glam = "0.8" -font-kit = "0.4" +font-kit = { git = "https://github.com/servo/font-kit" } log = "0.4" [dependencies.image] diff --git a/winit/Cargo.toml b/winit/Cargo.toml index 63df1d6338..8d5f8a14a9 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -22,8 +22,9 @@ version = "0.1.0-alpha" path = "../native" [dependencies.window_clipboard] -git = "https://github.com/hecrj/window_clipboard" -rev = "22c6dd6c04cd05d528029b50a30c56417cd4bebf" +git = "https://github.com/simlay/window_clipboard" +branch = "ios-unimplemented-stub" +#rev = "22c6dd6c04cd05d528029b50a30c56417cd4bebf" [target.'cfg(target_os = "windows")'.dependencies.winapi] version = "0.3.6" diff --git a/winit/src/application.rs b/winit/src/application.rs index f5aa799c64..60284f626c 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -310,7 +310,6 @@ pub trait Application: Sized { physical_size.width, physical_size.height, ); - resized = false; } diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index b6a0b64b4d..db5ea9c657 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -5,7 +5,7 @@ use crate::{ input::{ keyboard::{self, KeyCode, ModifiersState}, - mouse, ButtonState, + mouse, touch, ButtonState, }, window, Event, Mode, MouseCursor, }; @@ -84,6 +84,9 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } + WindowEvent::Touch(touch) => { + Some(Event::Touch(conversion::touch_event(touch))) + } _ => None, } } @@ -342,3 +345,24 @@ pub(crate) fn is_private_use_character(c: char) -> bool { _ => false, } } +pub fn touch_event(touch: winit::event::Touch) -> touch::Touch { + let location = touch.location; + match touch.phase { + winit::event::TouchPhase::Started => touch::Touch::Started { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Ended => touch::Touch::Ended { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Moved => touch::Touch::Moved { + x: location.x as f32, + y: location.y as f32, + }, + winit::event::TouchPhase::Cancelled => touch::Touch::Cancelled { + x: location.x as f32, + y: location.y as f32, + }, + } +}