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

Fix hover_pos on touch screens #180

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/widgets/dock_area/show/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {

self.tab_bar_scroll(
ui,
state,
(surface_index, node_index),
actual_width,
available_width,
Expand Down Expand Up @@ -360,7 +361,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
state.drag_start = response.hover_pos();
}

if let Some(pos) = tabs_ui.input(|i| i.pointer.hover_pos()) {
if let Some(pos) = state.last_hover_pos {
// Use response.rect.contains instead of
// response.hovered as the dragged tab covers
// the underlying tab
Expand Down Expand Up @@ -620,9 +621,11 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
(response, close_response)
}

#[allow(clippy::too_many_arguments)]
fn tab_bar_scroll(
&mut self,
ui: &mut Ui,
state: &mut State,
(surface_index, node_index): (SurfaceIndex, NodeIndex),
actual_width: f32,
available_width: f32,
Expand Down Expand Up @@ -672,7 +675,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {

*scroll -= scroll_bar_handle_response.drag_delta().x * points_to_scroll_coefficient;

if let Some(pos) = ui.input(|i| i.pointer.hover_pos()) {
if let Some(pos) = state.last_hover_pos {
if scroll_bar_rect.contains(pos) {
*scroll += ui.input(|i| i.scroll_delta.y + i.scroll_delta.x)
* points_to_scroll_coefficient;
Expand Down Expand Up @@ -731,7 +734,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
*viewport = body_rect;

if ui.input(|i| i.pointer.any_click()) {
if let Some(pos) = ui.input(|i| i.pointer.hover_pos()) {
if let Some(pos) = state.last_hover_pos {
if body_rect.contains(pos) && Some(ui.layer_id()) == ui.ctx().layer_id_at(pos) {
self.new_focused = Some((surface_index, node_index));
}
Expand Down Expand Up @@ -797,7 +800,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
}

//change hover destination
if let Some(pointer) = ui.input(|i| i.pointer.hover_pos()) {
if let Some(pointer) = state.last_hover_pos {
// Prevent borrow checker issues.
let rect = rect.to_owned();

Expand Down
9 changes: 7 additions & 2 deletions src/widgets/dock_area/show/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
.get_or_insert(Style::from_egui(ui.style().as_ref()));
self.window_bounds.get_or_insert(ui.ctx().screen_rect());
let mut state = State::load(ui.ctx(), self.id);
// Delay hover position one frame. On touch screens hover_pos() is None when any_released()
if !ui.input(|i| i.pointer.any_released()) {
state.last_hover_pos = ui.input(|i| i.pointer.hover_pos());
}

let style = self.style.as_ref().unwrap();
let fade_surface =
self.hovered_window_surface(&mut state, style.overlay.feel.fade_hold_time, ui.ctx());
Expand Down Expand Up @@ -105,7 +110,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
}

for (surface_index, node_index, tab_index) in self.to_detach.drain(..).rev() {
let mouse_pos = ui.input(|input| input.pointer.hover_pos());
let mouse_pos = state.last_hover_pos;
self.dock_state.detach_tab(
(surface_index, node_index, tab_index),
Rect::from_min_size(
Expand Down Expand Up @@ -214,7 +219,7 @@ impl<'tree, Tab> DockArea<'tree, Tab> {
_ => todo!("collections of tabs, like nodes or surfaces, can't be dragged! (yet)"),
};

if let Some(pointer) = ui.input(|i| i.pointer.hover_pos()) {
if let Some(pointer) = state.last_hover_pos {
drag_state.pointer = pointer;
}

Expand Down
2 changes: 2 additions & 0 deletions src/widgets/dock_area/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::drag_and_drop::{DragData, DragDropState, HoverData};
#[derive(Clone, Debug, Default)]
pub(super) struct State {
pub drag_start: Option<Pos2>,
pub last_hover_pos: Option<Pos2>,
pub dnd: Option<DragDropState>,
pub window_fade: Option<(f64, SurfaceIndex)>,
}
Expand All @@ -16,6 +17,7 @@ impl State {
pub(super) fn load(ctx: &Context, id: Id) -> Self {
ctx.data_mut(|d| d.get_temp(id)).unwrap_or(Self {
drag_start: None,
last_hover_pos: None,
dnd: None,
window_fade: None,
})
Expand Down
Loading