Skip to content

Commit

Permalink
Added initial touch events to support iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay committed Feb 24, 2020
1 parent d6c2b11 commit 13ff494
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 49 deletions.
5 changes: 4 additions & 1 deletion native/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
input::{keyboard, mouse},
input::{keyboard, mouse, touch},
window,
};

Expand All @@ -19,4 +19,7 @@ pub enum Event {

/// A window event
Window(window::Event),

/// A touch event
Touch(touch::Touch),
}
1 change: 1 addition & 0 deletions native/src/input.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
37 changes: 37 additions & 0 deletions native/src/input/touch.rs
Original file line number Diff line number Diff line change
@@ -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,
},
}
14 changes: 11 additions & 3 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(
Expand Down
35 changes: 18 additions & 17 deletions native/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions native/src/widget/checkbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions native/src/widget/radio.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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());
}
Expand Down
44 changes: 40 additions & 4 deletions native/src/widget/scrollable.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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;
}
_ => {}
}
}
Expand All @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -326,6 +356,7 @@ where
#[derive(Debug, Clone, Copy, Default)]
pub struct State {
scroller_grabbed_at: Option<f32>,
scroll_box_touched_at: Option<Point>,
offset: f32,
}

Expand Down Expand Up @@ -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`].
Expand Down
30 changes: 17 additions & 13 deletions native/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions native/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 0 additions & 1 deletion winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ pub trait Application: Sized {
physical_size.width,
physical_size.height,
);

resized = false;
}

Expand Down
Loading

0 comments on commit 13ff494

Please sign in to comment.