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

no way to open context menus on touchscreens #3444

Closed
amomentunfolding opened this issue Oct 7, 2023 · 2 comments · Fixed by #4195
Closed

no way to open context menus on touchscreens #3444

amomentunfolding opened this issue Oct 7, 2023 · 2 comments · Fixed by #4195
Labels
bug Something is broken

Comments

@amomentunfolding
Copy link

Describe the bug

When using context_menu it seems to be only accessible by secondary click and can therefore not be opened on a touch screen device.

To Reproduce
Steps to reproduce the behavior:

  1. Try to open a context menu on a touch screen
  2. Realize there is no way

Expected behavior

A reasonable default I feel would be to have it open on a long press.

I reckon it would be somewhat straightforard to implement with egui::PointerState::press_start_time - at least that's what I will do for a workaround in my project, but perhaps it should be part of the API as well.

Smartphone (please complete the following information):

  • Device: Xiaomi Redmi Pad
  • OS: Android 13
  • Browser: Chrome Beta / also tried Firefox.
  • Version: Latest
@amomentunfolding amomentunfolding added the bug Something is broken label Oct 7, 2023
@amomentunfolding
Copy link
Author

for anyone dealing with the same issue, i wrote this workaround:

use egui::{
    Ui,
    Response
};
// cannot be longer than .7~ seconds because after that Response.hovered() returns false
// probably it could be longer by using pointer_pos check instead of hovered() but .4s is a good speed so w/e
const LONG_PRESS_TIME: f64 = 0.4;

pub trait TouchEnabledContextMenu {
    fn context_menu_touch_enabled(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) -> Self;
}

impl TouchEnabledContextMenu for Response {
    fn context_menu_touch_enabled(self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui)) -> Self {
        let popup_id = ui.make_persistent_id(self.id.with("context_touch_menu"));
        let is_open = ui.memory(|mem| mem.is_popup_open(popup_id));
        let mut should_open = false;

        if self.secondary_clicked() {
            should_open = true
        } else if self.hovered() && !is_open {
            ui.input(|i| {
                if let Some(pst) = i.pointer.press_start_time() {
                    if (i.time - pst) > LONG_PRESS_TIME {
                        should_open = true
                    }
                }
            });
        };

        if should_open {
            ui.memory_mut(|mem| mem.toggle_popup(popup_id));
        }

        egui::popup::popup_above_or_below_widget(
            ui,
            popup_id,
            &self,
            egui::AboveOrBelow::Above,
            |ui| {
                ui.set_min_width(120.0);  // popup is extremely thin without this, regardless of contents
                add_contents(ui);
            }
        );

        self
    }
}

@abey79
Copy link
Collaborator

abey79 commented Feb 16, 2024

This might be a duplicate of #865, but best kept open for the useful workaround.

emilk added a commit that referenced this issue Mar 20, 2024
* Closes #3444
* Closes #865

On a touch screen, if you press down on a widget and hold for 0.6
seconds (`MAX_CLICK_DURATION`), it will now trigger a secondary click,
i.e. `Response::secondary_clicked` will be `true`. This means you can
now open context menus on touch screens.
hacknus pushed a commit to hacknus/egui that referenced this issue Oct 30, 2024
* Closes emilk#3444
* Closes emilk#865

On a touch screen, if you press down on a widget and hold for 0.6
seconds (`MAX_CLICK_DURATION`), it will now trigger a secondary click,
i.e. `Response::secondary_clicked` will be `true`. This means you can
now open context menus on touch screens.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants