Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch support for PaneGrid and PickList #650

Merged
merged 14 commits into from
Feb 12, 2021
Merged
26 changes: 24 additions & 2 deletions native/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::mouse;
use crate::overlay;
use crate::scrollable;
use crate::text;
use crate::touch;
use crate::{
Clipboard, Container, Element, Hasher, Layout, Length, Point, Rectangle,
Scrollable, Size, Vector, Widget,
Expand Down Expand Up @@ -337,15 +338,36 @@ where
}
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
let bounds = layout.bounds();
let text_size =
self.text_size.unwrap_or(renderer.default_size());

if bounds.contains(cursor_position) {
let text_size =
self.text_size.unwrap_or(renderer.default_size());

*self.hovered_option = Some(
((cursor_position.y - bounds.y)
/ f32::from(text_size + self.padding * 2))
as usize,
);
}
}
Event::Touch(touch::Event::FingerPressed { .. }) => {
let bounds = layout.bounds();

if bounds.contains(cursor_position) {
let text_size =
self.text_size.unwrap_or(renderer.default_size());

*self.hovered_option = Some(
((cursor_position.y - bounds.y)
/ f32::from(text_size + self.padding * 2))
as usize,
);

if let Some(index) = *self.hovered_option {
if let Some(option) = self.options.get(index) {
*self.last_selection = Some(option.clone());
}
}
}
}
_ => {}
Expand Down
131 changes: 64 additions & 67 deletions native/src/widget/pane_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::row;
use crate::touch;
use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector,
Widget,
Expand Down Expand Up @@ -368,90 +369,86 @@ where
let mut event_status = event::Status::Ignored;

match event {
Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::ButtonPressed(mouse::Button::Left) => {
let bounds = layout.bounds();

if bounds.contains(cursor_position) {
event_status = event::Status::Captured;

match self.on_resize {
Some((leeway, _)) => {
let relative_cursor = Point::new(
cursor_position.x - bounds.x,
cursor_position.y - bounds.y,
);

let splits = self.state.split_regions(
f32::from(self.spacing),
Size::new(bounds.width, bounds.height),
);

let clicked_split = hovered_split(
splits.iter(),
f32::from(self.spacing + leeway),
relative_cursor,
);
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let bounds = layout.bounds();

if let Some((split, axis, _)) = clicked_split {
self.state.pick_split(&split, axis);
} else {
self.click_pane(
layout,
cursor_position,
messages,
);
}
}
None => {
if bounds.contains(cursor_position) {
event_status = event::Status::Captured;

match self.on_resize {
Some((leeway, _)) => {
let relative_cursor = Point::new(
cursor_position.x - bounds.x,
cursor_position.y - bounds.y,
);

let splits = self.state.split_regions(
f32::from(self.spacing),
Size::new(bounds.width, bounds.height),
);

let clicked_split = hovered_split(
splits.iter(),
f32::from(self.spacing + leeway),
relative_cursor,
);

if let Some((split, axis, _)) = clicked_split {
self.state.pick_split(&split, axis);
} else {
self.click_pane(
layout,
cursor_position,
messages,
);
}
}
None => {
self.click_pane(layout, cursor_position, messages);
}
}
}
mouse::Event::ButtonReleased(mouse::Button::Left) => {
if let Some((pane, _)) = self.state.picked_pane() {
if let Some(on_drag) = &self.on_drag {
let mut dropped_region = self
.elements
.iter()
.zip(layout.children())
.filter(|(_, layout)| {
}
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left))
| Event::Touch(touch::Event::FingerLifted { .. })
| Event::Touch(touch::Event::FingerLost { .. }) => {
if let Some((pane, _)) = self.state.picked_pane() {
if let Some(on_drag) = &self.on_drag {
let mut dropped_region =
self.elements.iter().zip(layout.children()).filter(
|(_, layout)| {
layout.bounds().contains(cursor_position)
});

let event = match dropped_region.next() {
Some(((target, _), _)) if pane != *target => {
DragEvent::Dropped {
pane,
target: *target,
}
},
);

let event = match dropped_region.next() {
Some(((target, _), _)) if pane != *target => {
DragEvent::Dropped {
pane,
target: *target,
}
_ => DragEvent::Canceled { pane },
};
}
_ => DragEvent::Canceled { pane },
};

messages.push(on_drag(event));
}
messages.push(on_drag(event));
}

self.state.idle();
self.state.idle();

event_status = event::Status::Captured;
} else if self.state.picked_split().is_some() {
self.state.idle();
event_status = event::Status::Captured;
} else if self.state.picked_split().is_some() {
self.state.idle();

event_status = event::Status::Captured;
}
}
mouse::Event::CursorMoved { .. } => {
event_status =
self.trigger_resize(layout, cursor_position, messages);
event_status = event::Status::Captured;
}
_ => {}
},
}
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Event::FingerMoved { .. }) => {
event_status =
self.trigger_resize(layout, cursor_position, messages);
}
_ => {}
}

Expand Down
4 changes: 3 additions & 1 deletion native/src/widget/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::overlay;
use crate::overlay::menu::{self, Menu};
use crate::scrollable;
use crate::text;
use crate::touch;
use crate::{
Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
};
Expand Down Expand Up @@ -214,7 +215,8 @@ where
_clipboard: Option<&dyn Clipboard>,
) -> event::Status {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
| Event::Touch(touch::Event::FingerPressed { .. }) => {
let event_status = if *self.is_open {
// TODO: Encode cursor availability in the type system
*self.is_open =
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Pipeline {
let default_font = default_font.map(|slice| slice.to_vec());

// TODO: Font customization
#[cfg(not(target_os = "ios"))]
#[cfg(feature = "default_system_font")]
let default_font = {
default_font.or_else(|| {
Expand Down