Skip to content

Commit

Permalink
Explose a simple API
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 24, 2024
1 parent bbd9335 commit 70d528b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
24 changes: 14 additions & 10 deletions crates/egui/src/text_selection/label_text_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/text_selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 3 additions & 1 deletion crates/egui/src/widgets/hyperlink.rs
Original file line number Diff line number Diff line change
@@ -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`].
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion crates/egui/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 70d528b

Please sign in to comment.