From 70d528ba33788267afc6491c05034cb829990b5d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 24 Jan 2024 14:14:10 +0100 Subject: [PATCH] Explose a simple API --- .../text_selection/label_text_selection.rs | 24 +++++++++++-------- crates/egui/src/text_selection/mod.rs | 2 +- crates/egui/src/widgets/hyperlink.rs | 4 +++- crates/egui/src/widgets/label.rs | 4 +++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/crates/egui/src/text_selection/label_text_selection.rs b/crates/egui/src/text_selection/label_text_selection.rs index d35cb859af0..975b64eaf63 100644 --- a/crates/egui/src/text_selection/label_text_selection.rs +++ b/crates/egui/src/text_selection/label_text_selection.rs @@ -10,16 +10,6 @@ use super::{ /// Turn on to help debug this const DEBUG: bool = false; // TODO: don't merge this while `true` -/// Handle text selection state for a label or similar widget. -/// -/// Make sure the widget senses clicks and drags. -/// -/// This should be called after painting the text, because this will also -/// paint the text cursor/selection on top. -pub fn label_text_selection(ui: &Ui, response: &Response, galley_pos: Pos2, galley: &Galley) { - LabelSelectionState::label_text_selection(ui, response, galley_pos, galley); -} - fn paint_selection( ui: &Ui, _response: &Response, @@ -238,6 +228,14 @@ impl LabelSelectionState { state.store(ctx); } + pub fn has_selection(&self) -> bool { + self.selection.is_some() + } + + pub fn clear_selection(&mut self) { + self.selection = None; + } + fn copy_text(&mut self, galley_pos: Pos2, galley: &Galley, cursor_range: &CursorRange) { let new_galley_rect = Rect::from_min_size(galley_pos, galley.size()); let new_text = selected_text(galley, cursor_range); @@ -285,6 +283,12 @@ impl LabelSelectionState { self.last_copied_galley_rect = Some(new_galley_rect); } + /// Handle text selection state for a label or similar widget. + /// + /// Make sure the widget senses clicks and drags. + /// + /// This should be called after painting the text, because this will also + /// paint the text cursor/selection on top. pub fn label_text_selection(ui: &Ui, response: &Response, galley_pos: Pos2, galley: &Galley) { let mut state = Self::load(ui.ctx()); state.on_label(ui, response, galley_pos, galley); diff --git a/crates/egui/src/text_selection/mod.rs b/crates/egui/src/text_selection/mod.rs index bf193fbcbef..5be95eb53bf 100644 --- a/crates/egui/src/text_selection/mod.rs +++ b/crates/egui/src/text_selection/mod.rs @@ -9,5 +9,5 @@ pub mod text_cursor_state; pub mod visuals; pub use cursor_range::{CCursorRange, CursorRange, PCursorRange}; -pub use label_text_selection::{label_text_selection, LabelSelectionState}; +pub use label_text_selection::LabelSelectionState; pub use text_cursor_state::TextCursorState; diff --git a/crates/egui/src/widgets/hyperlink.rs b/crates/egui/src/widgets/hyperlink.rs index ce8089ba60c..f4a508e64f7 100644 --- a/crates/egui/src/widgets/hyperlink.rs +++ b/crates/egui/src/widgets/hyperlink.rs @@ -1,5 +1,7 @@ use crate::*; +use self::text_selection::LabelSelectionState; + /// Clickable text, that looks like a hyperlink. /// /// To link to a web page, use [`Hyperlink`], [`Ui::hyperlink`] or [`Ui::hyperlink_to`]. @@ -53,7 +55,7 @@ impl Widget for Link { let selectable = ui.style().interaction.selectable_labels; if selectable { - crate::text_selection::label_text_selection(ui, &response, galley_pos, &galley); + LabelSelectionState::label_text_selection(ui, &response, galley_pos, &galley); } if response.hovered() { diff --git a/crates/egui/src/widgets/label.rs b/crates/egui/src/widgets/label.rs index b780c40b999..b68fb4337e1 100644 --- a/crates/egui/src/widgets/label.rs +++ b/crates/egui/src/widgets/label.rs @@ -2,6 +2,8 @@ use std::sync::Arc; use crate::*; +use self::text_selection::LabelSelectionState; + /// Static text. /// /// Usually it is more convenient to use [`Ui::label`]. @@ -257,7 +259,7 @@ impl Widget for Label { let selectable = selectable.unwrap_or_else(|| ui.style().interaction.selectable_labels); if selectable { - crate::text_selection::label_text_selection(ui, &response, galley_pos, &galley); + LabelSelectionState::label_text_selection(ui, &response, galley_pos, &galley); } }