emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
20.61k stars 1.49k forks source link

eink text selection and SelectableLabel #4663

Open sbechet opened 2 weeks ago

sbechet commented 2 weeks ago

This is not really a bug nor really a feature. I'm not sure how to solve this question simply but it would be nice to find a solution.

The method of changing color in the context of selections or SelectableLabel does not seem generic.

Example in src/text_selection/visuals.rs

let color = visuals.selection.bg_fill.linear_multiply(0.5);

This doesn't work well if the screen supports a limited number and colors like e-ink screens.

This is an example where I try to create a basic style sheet. This screen supports 8 colors including obviously white and black.

Colors are:

eink-example

As you can see from SelectableLabel, the selected one is unreadable. Same in case of text selection. Maybe there are other places?

YgorSouza commented 1 week ago

You mean this?

https://github.com/emilk/egui/blob/814ad0783cf8b826a258e29ed4c50ae6daa2e890/crates/egui/src/text_selection/visuals.rs#L19-L20

I think this affects the text selection, but not the SelectableLabel. What color did you use for the background and stroke? Maybe the default colors are a little too close to each other and end up the same after rounding to 1 bit.

image

And for the text selection, I guess it would be a matter of making the text even darker and the selection lighter to begin with? So they don't end up the same color after blending and rounding.

sbechet commented 1 week ago

What you need to understand with these screens is that there are really only 8 colors and that if you are outside of them, the code in the screen will "try" (by doing a sort of average with the colors of the surrounding pixels?) to give it a close value but it is not reliable. It is therefore absolutely necessary to be precise and choose only from these 8 colors.

I was planning to submit an eink pull request, but in the meantime, here is my test:

fn selection_eink() -> Selection {
    Selection {
        bg_fill: Color32::from_rgb(128, 255, 255),
        stroke: Stroke::new(1.0, Color32::BLUE),
}
}

pub fn widgets_eink() -> Widgets {
    Widgets {
        noninteractive: WidgetVisuals {
            weak_bg_fill: Color32::WHITE,
            bg_fill: Color32::WHITE,
            bg_stroke: Stroke::new(1.0, Color32::BLACK),
            fg_stroke: Stroke::new(1.0, Color32::BLACK),
            rounding: Rounding::same(2.0),
            expansion: 0.0,
        },
        inactive: WidgetVisuals {
            weak_bg_fill: Color32::WHITE,
            bg_fill: Color32::from_rgb(0, 255, 255),
            bg_stroke: Stroke::new(1.0, Color32::BLACK),
            fg_stroke: Stroke::new(2.0, Color32::BLACK),
            rounding: Rounding::same(2.0),
            expansion: 1.0,
        },
        hovered: WidgetVisuals {
            weak_bg_fill: Color32::WHITE,
            bg_fill: Color32::WHITE,
            bg_stroke: Stroke::new(1.0, Color32::from_rgb(0, 255, 255)),
            fg_stroke: Stroke::new(1.5, Color32::BLUE),
            rounding: Rounding::same(3.0),
            expansion: 1.0,
        },
        active: WidgetVisuals {
            weak_bg_fill: Color32::WHITE,
            bg_fill: Color32::WHITE,
            bg_stroke: Stroke::new(1.0, Color32::from_rgb(0, 255, 255)),
            fg_stroke: Stroke::new(1.0, Color32::BLUE),
            rounding: Rounding::same(2.0),
            expansion: 0.0,
        },
        open: WidgetVisuals {
            weak_bg_fill: Color32::WHITE,
            bg_fill: Color32::WHITE,
            bg_stroke: Stroke::new(1.0, Color32::from_rgb(0, 255, 255)),
            fg_stroke: Stroke::new(1.0, Color32::BLUE),
            rounding: Rounding::same(2.0),
            expansion: 0.0,
        },
    }
}

pub fn visuals_eink() -> Visuals {
    Visuals {
        dark_mode: false,
        override_text_color: None,
        widgets: widgets_eink(),
        selection: selection_eink(),
        hyperlink_color: Color32::from_rgb(0, 255, 255), // cyan
        faint_bg_color: Color32::WHITE,
        extreme_bg_color: Color32::WHITE,
        code_bg_color: Color32::WHITE,
        warn_fg_color: Color32::from_rgb(255, 0, 255),
        error_fg_color: Color32::RED,

        window_rounding: Rounding::same(6.0),
        window_shadow: Shadow {
            offset: vec2(0.0, 0.0),
            blur: 0.0,
            spread: 0.0,
            color: Color32::BLACK,
        },
        window_fill: Color32::WHITE,
        window_stroke: Stroke::new(1.0, Color32::BLACK),

        window_highlight_topmost: true,

        menu_rounding: Rounding::same(6.0),

        panel_fill: Color32::WHITE,

        popup_shadow: Shadow {
            offset: vec2(0.0, 0.0),
            blur: 0.0,
            spread: 0.0,
            color: Color32::BLACK,
        },

        resize_corner_size: 12.0,

        text_cursor: Default::default(),

        text_cursor_preview: true,

        clip_rect_margin: 3.0,

        button_frame: false,

        collapsing_header_frame: false,

        indent_has_left_vline: true,

        striped: false,

        slider_trailing_fill: false,

        handle_shape: HandleShape::Circle,

        interact_cursor: None,

        image_loading_spinners: false,

        numeric_color_space: NumericColorSpace::GammaByte,

    }
}
sbechet commented 1 week ago

Colors are:

Color32 hex
Color32::BLACK #000000
Color32::BLUE #0000FF
Color32::GREEN #00FF00
"Color32::CYAN" #00FFFF
Color32::RED #FF0000
"Color32::MAGENTA" #FF00FF
Color32::YELLOW #FFFF00
Color32::WHITE #FFFFFF

As you can see binary a simple binary increment.

YgorSouza commented 1 week ago

bg_fill: Color32::from_rgb(128, 255, 255),

That's not one of the allowed colors. Are you sure it doesn't work for the SelectableLabel? It seems to respect the colors you set.

image

As for the text selection, the problem is that egui paints the selection after the text as mentioned in the code comment above, so the colors blend. The logic would have to be changed so the selection is painted first without changing the color of the text. There are other UIs that work like this, such as this GitHub page (and web pages in general), so maybe there is an argument to be made that egui should work like that as well, if it is possible.

sbechet commented 1 week ago

bg_fill: Color32::from_rgb(128, 255, 255),

Yes, I have done further testing, this color "seems" good for my own testing but is not "safe" as a long term solution as it is a color generated by the firmware of the screen.

the problem is that egui paints the selection after the text

I think the problem is here: we can't choose the reverse color.

YgorSouza commented 1 week ago

There could be an option to override the text color when it is highlighted. Here on GitHub we have both possibilities:

image

But before that can solve your problem, egui would have to paint the selection behind the text, instead of a transparent overlay on top of it, so the colors don't blend. Technically you could do that yourself, by making the default selection bg invisible and painting it manually using a LayoutJob to add a background to the part of the text that is selected. Maybe custom Label and TextEdit widgets that work like that. But it sounds inconvenient, and I don't know if egui even provides all the necessary public API to do that. Then again, if this can be done, it could be added as a option to egui itself, like a highlight_behind_text in the Visuals struct.

YgorSouza commented 4 hours ago

So now there are plans to make the egui highlight work more like a web page highlight #4727. That should solve this e-paper problem as well.