mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Selectable text in Labels (#3814)
* Closes https://github.com/emilk/egui/issues/3804 Add ability to select the text in labels with mouse-drag, double-click, and keyboard (once clicked). Hit Cmd+C to copy the text. If everything of a label with elided text is selected, the copy command will copy the full non-elided text. IME and accesskit _should_ work, but is untested. You can control wether or not text in labels is selected globally in `style.interaction.selectable_labels` or on a per-label basis in `Label::selectable`. The default is ON. This also cleans up the `TextEdit` code somewhat, fixing a couple smaller bugs along the way. This does _not_ implement selecting text across multiple widgets. Text selection is only supported within a single `Label`, `TextEdit`, `Link` or `Hyperlink`.  ## TODO * [x] Test
This commit is contained in:
@@ -53,9 +53,7 @@ impl super::View for TextEditDemo {
|
||||
ui.spacing_mut().item_spacing.x = 0.0;
|
||||
ui.label("Selected text: ");
|
||||
if let Some(text_cursor_range) = output.cursor_range {
|
||||
use egui::TextBuffer as _;
|
||||
let selected_chars = text_cursor_range.as_sorted_char_range();
|
||||
let selected_text = text.char_range(selected_chars);
|
||||
let selected_text = text_cursor_range.slice_str(text);
|
||||
ui.code(selected_text);
|
||||
}
|
||||
});
|
||||
@@ -92,7 +90,9 @@ impl super::View for TextEditDemo {
|
||||
let text_edit_id = output.response.id;
|
||||
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), text_edit_id) {
|
||||
let ccursor = egui::text::CCursor::new(0);
|
||||
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
|
||||
state
|
||||
.cursor
|
||||
.set_char_range(Some(egui::text::CCursorRange::one(ccursor)));
|
||||
state.store(ui.ctx(), text_edit_id);
|
||||
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id)); // give focus back to the [`TextEdit`].
|
||||
}
|
||||
@@ -102,7 +102,9 @@ impl super::View for TextEditDemo {
|
||||
let text_edit_id = output.response.id;
|
||||
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), text_edit_id) {
|
||||
let ccursor = egui::text::CCursor::new(text.chars().count());
|
||||
state.set_ccursor_range(Some(egui::text::CCursorRange::one(ccursor)));
|
||||
state
|
||||
.cursor
|
||||
.set_char_range(Some(egui::text::CCursorRange::one(ccursor)));
|
||||
state.store(ui.ctx(), text_edit_id);
|
||||
ui.ctx().memory_mut(|mem| mem.request_focus(text_edit_id)); // give focus back to the [`TextEdit`].
|
||||
}
|
||||
|
||||
@@ -95,10 +95,10 @@ impl EasyMarkEditor {
|
||||
};
|
||||
|
||||
if let Some(mut state) = TextEdit::load_state(ui.ctx(), response.id) {
|
||||
if let Some(mut ccursor_range) = state.ccursor_range() {
|
||||
if let Some(mut ccursor_range) = state.cursor.char_range() {
|
||||
let any_change = shortcuts(ui, code, &mut ccursor_range);
|
||||
if any_change {
|
||||
state.set_ccursor_range(Some(ccursor_range));
|
||||
state.cursor.set_char_range(Some(ccursor_range));
|
||||
state.store(ui.ctx(), response.id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user