Skip to content

Conversation

mankinskin
Copy link
Contributor

@mankinskin mankinskin commented Sep 14, 2025

image
let label = ui.add(Label::new(&text).selectable(true));
if let Some(range) = label.selected_char_range() {
    println!("Selected char range: {:?}", range);
}
label

These changes were generated by Claude AI.

Copy link

Preview available at https://egui-pr-preview.github.io/pr/7541-labelselectedcharrange
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@lucasmerlin
Copy link
Collaborator

Sorry this is very conflicted now that I've merged the new plugin trait. You'll have to update it to fetch the label selection state via the plugin helpers

@emilk
Copy link
Owner

emilk commented Sep 16, 2025

Cool!

It would be nice if the egui_demo_lib used this new functionality.

Also: please add a kittest test of it 🙏

@mankinskin
Copy link
Contributor Author

mankinskin commented Sep 16, 2025

I still need to find a way to get selections across multiple widgets..

So far I was only able to get the latest selection in the current Context per Viewport. For my app I need to be able to fetch all current selections, across any widgets in the current context and viewport.

I will look at the new plugin api and go from there.

@mankinskin mankinskin force-pushed the label_selected_char_range branch from 3dd884f to 2cf4a21 Compare September 16, 2025 19:52
@mankinskin
Copy link
Contributor Author

mankinskin commented Sep 19, 2025

I reworked the api a bit and added an example project as a demo.

I have added a map in LabelSelectionState from widget_id: Id to Arc<Galley> to be able to calculate the selected char range on Responses without providing the galley.

I am thinking about storing an entire WidgetSelectionState in this map, to store secondary_reached: bool and primary_reached: bool at render time for each widget.

Otherwise the range calculation will be wrong if the calls to widget_cursor_range on a Response come after another widget has been rendered (because they all share the LabelSelectionState and update primary_reached, ...).

The example shows how to do it correctly at the moment, by extracting the cursor_range in the same iteration the Label is rendered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add access to selected char range on selectable labels

5 participants