multi-click support#24023
Conversation
Added SelectWordAtPoint TextEdit. Added on_pointer_click observer to text_input. On double and triple click events queue appropriate TextEdits
| /// Duration between the pointer pressed and lifted for this click | ||
| pub duration: Duration, | ||
| /// Number of consecutive clicks, starting at `1`. | ||
| pub count: u8, |
There was a problem hiding this comment.
New minigame idea just dropped!
alice-i-cecile
left a comment
There was a problem hiding this comment.
Oh lovely. That's very simple!
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
| // TODO: add optional feature-flagged support for fetching this from the OS preferences | ||
| pub const MULTI_CLICK_DURATION: Duration = Duration::from_millis(500); |
There was a problem hiding this comment.
This should be a resource instead so it can be overwritten.
(I wish that there already was a crate to fetch this setting, but I haven't found one)
There was a problem hiding this comment.
There's no crate that does this AFAIK. Instead, we'll eventually need to add OS-specific code in bevy_platform. For example, the windows version looks like this:
#[cfg(target_os = "windows")]
mod platform {
use super::*;
pub fn get() -> Option<Duration> {
// GetDoubleClickTime returns the interval in milliseconds.
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdoubleclicktime
extern "system" {
fn GetDoubleClickTime() -> u32;
}
let ms = unsafe { GetDoubleClickTime() };
if ms == 0 {
None
} else {
Some(Duration::from_millis(ms as u64))
}
}
}Also, on Linux there's no single setting, you have to parse the Gnome or KDE settings files, this will take some time at startup.
There was a problem hiding this comment.
Which is why a separate crate would be nice – Bevy isn't the only library having this issue.
There was a problem hiding this comment.
Should this be a resource instead? (I wish that there already was a crate to fetch this setting, but I haven't found one)
I'm just using a constant here because I wanted to keep this a narrow PR focused on the multi-click implementation. It should definitely be user configurable, or fetched from the OS if possible, though.
There was a problem hiding this comment.
There's spatial tolerance as well, which we'd also want to get from the OS. Left it out from this PR because it has to account for scale factor (which is annoying).
Co-authored-by: Talin <viridia@gmail.com>
Objective
Implement minimal multi-click support.
Add double click to select word and triple click to select all support to text input widgets.
Fixes #23874
Solution
New
MULTI_CLICK_DURATIONconstant that sets the max time between clicks for them to count as consecutive.Added a field
counttoClick. 1 is a single click, 2 is a double click, 3 is a triple click and so on. It's au8and saturates at 255 if you click that many times.Current multi-click state is stored in a field
clickingonPointerButtonState.The
clickingstate is cleared afterMULTI_CLICK_DURATIONwithout another click.New
SelectWordAtPointTextEdit.New observer system
on_pointer_clickinbevy_ui_widgets::text_input. This queuesSelectWordAtPointfor 2 clicks andSelectAllfor 3 or more.Clickevents are dispatched on release, so they happen afterPress. ThePressfor a double click might move the cursor but that doesn't affect theSelectWordAtPointedit that is dispatched.The implementation is deliberately minimal, I just added here what was needed for the text input. Left for followups:
Click.We could add a separate
MultiClickevent instead. It seems to me things would be more complicated with bothClickandMultiClickobserver systems though.Testing
Text inputs now support double click to select a word and triple click to select all: