Get selected text from Label #4913
-
This seems simple, but I can't find any information on this: suppose I have a ui.add(egui::Label::new("here is some text")
.selectable(true)); How do I get the text that has been selected from this label? The returned |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
If you do Since this is a new feature, it seems that there will be additional features in the future. |
Beta Was this translation helpful? Give feedback.
-
As rustbasic said, the selected text is already copied to the clipboard when a Copy event is received. I don't think it is possible to copy or access the selected text under different circumstances. The text to be copied is stored in this private field: egui/crates/egui/src/text_selection/label_text_selection.rs Lines 122 to 123 in d856f7b This field is normally empty, as it is not needed on every frame. When a copy event arrives, every label appends its selected text to the accumulated selection as it is painted: egui/crates/egui/src/text_selection/label_text_selection.rs Lines 510 to 512 in d856f7b Then, at the end of the frame, the accumulated text is moved to the clipboard (using a public method that you can also call from your code): egui/crates/egui/src/text_selection/label_text_selection.rs Lines 228 to 231 in d856f7b So you can put arbitrary strings in the clipboard using copy_text, and egui will put the selected text in the clipboard automatically when the user presses Ctrl+C (or the Copy shortcut in their system), but you can't retrieve the selected text to put it in the clipboard yourself. On the other hand, you can use a #[derive(Default)]
struct MyApp {}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
use egui::TextBuffer as _;
let text = "The quick brown fox jumps over the lazy dog";
let response = TextEdit::multiline(&mut text.as_str()).show(ui);
let selection = response
.cursor_range
.as_ref()
.map(|c| text.char_range(c.as_sorted_char_range()));
if let Some(selection) = selection {
ui.label(selection);
}
});
}
} But if you plan on copying it to the clipboard when the user clicks a button on something similar, be aware that clicking the button will clear the selection before you have a chance to copy it to the clipboard, so you have to store it and get the selection from a previous frame when the button is clicked. |
Beta Was this translation helpful? Give feedback.
As rustbasic said, the selected text is already copied to the clipboard when a Copy event is received. I don't think it is possible to copy or access the selected text under different circumstances. The text to be copied is stored in this private field:
egui/crates/egui/src/text_selection/label_text_selection.rs
Lines 122 to 123 in d856f7b
This field is normally empty, as it is not needed on every frame. When a copy event arrives, every label appends its selected text to the accumulated selection as it is painted:
egui/crates/egui/src/text_selection/label_text_selection.rs
Lines 510 to 512 in d856f7b