diff options
| author | Polesznyák Márk <contact@pml68.dev> | 2026-02-05 14:20:35 +0100 |
|---|---|---|
| committer | Polesznyák Márk <contact@pml68.dev> | 2026-02-05 14:21:32 +0100 |
| commit | 5eddb62f3cae4740680eaa81d448c3eeda88068a (patch) | |
| tree | f45e6b7afaa8ed3a2a5dea5e65685f83017c0eb7 /src/text | |
| parent | chore: bump MSRV to match iced's (diff) | |
| download | iced_selection-5eddb62f3cae4740680eaa81d448c3eeda88068a.tar.gz | |
feat: make click interval for double & triple clicks customizable
Diffstat (limited to 'src/text')
| -rw-r--r-- | src/text/rich.rs | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/src/text/rich.rs b/src/text/rich.rs index 865f64a..c148566 100644 --- a/src/text/rich.rs +++ b/src/text/rich.rs @@ -1,6 +1,7 @@ use iced_widget::graphics::text::Paragraph; use iced_widget::graphics::text::cosmic_text; +use crate::click; use crate::core::alignment; use crate::core::clipboard; use crate::core::keyboard; @@ -9,6 +10,7 @@ use crate::core::layout; use crate::core::mouse; use crate::core::renderer; use crate::core::text::{Paragraph as _, Span}; +use crate::core::time::Duration; use crate::core::touch; use crate::core::widget::text::{Alignment, LineHeight, Shaping, Wrapping}; use crate::core::widget::tree::{self, Tree}; @@ -40,6 +42,7 @@ pub struct Rich< align_x: Alignment, align_y: alignment::Vertical, wrapping: Wrapping, + click_interval: Option<Duration>, class: Theme::Class<'a>, on_link_click: Option<Box<dyn Fn(Link) -> Message + 'a>>, on_link_hover: Option<Box<dyn Fn(Link) -> Message + 'a>>, @@ -66,6 +69,7 @@ where align_x: Alignment::Default, align_y: alignment::Vertical::Top, wrapping: Wrapping::default(), + click_interval: None, class: Theme::default(), on_link_click: None, on_link_hover: None, @@ -140,6 +144,15 @@ where self } + /// The maximum delay required for two consecutive clicks to be interpreted as a double click + /// (also applies to triple clicks). + /// + /// Defaults to 300ms. + pub fn click_interval(mut self, click_interval: Duration) -> Self { + self.click_interval = Some(click_interval); + self + } + /// Sets the message that will be produced when a link of the [`Rich`] text /// is clicked. /// @@ -223,7 +236,7 @@ struct State<Link> { is_hovered: bool, selection: Selection, dragging: Option<Dragging>, - last_click: Option<mouse::Click>, + last_click: Option<click::Click>, keyboard_modifiers: keyboard::Modifiers, visual_lines_bounds: Vec<core::Rectangle>, } @@ -588,10 +601,11 @@ where } if let Some(position) = cursor.position_over(bounds) { - let click = mouse::Click::new( + let click = click::Click::new( position, mouse::Button::Left, state.last_click, + self.click_interval, ); let (line, index) = state @@ -599,7 +613,7 @@ where .unwrap_or((0, 0)); match click.kind() { - mouse::click::Kind::Single => { + click::Kind::Single => { let new_end = SelectionEnd { line, index }; if state.keyboard_modifiers.shift() { @@ -610,7 +624,7 @@ where state.dragging = Some(Dragging::Grapheme); } - mouse::click::Kind::Double => { + click::Kind::Double => { state.selection.select_word( line, index, @@ -618,7 +632,7 @@ where ); state.dragging = Some(Dragging::Word); } - mouse::click::Kind::Triple => { + click::Kind::Triple => { state.selection.select_line(line, &state.paragraph); state.dragging = Some(Dragging::Line); } |
