Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Colored Sliders and Radio Button #3528

Open
AmesingFlank opened this issue Nov 4, 2023 · 1 comment
Open

Feature request: Colored Sliders and Radio Button #3528

AmesingFlank opened this issue Nov 4, 2023 · 1 comment

Comments

@AmesingFlank
Copy link
Contributor

I'm trying to create colored sliders and radio buttons, similar to these ones:

Screenshot 2023-11-04 at 11 39 38

Since egui doesn't offer an API for this right now, I had to copy the egui::RadioButton types into my project, and extending its functionality. But I'm hesitant to do the same for egui::Slider, as there is a big of chunk of code there. It would be amazing if coloring these widgets is supported by egui natively.

I'm happy to take a jab at adding these to egui, but I wanted to get some agreements on the API first. For RadioButtons, I propose that we can add arguments base_color: Color32, checked_color: Color32 to RadioButton::new(). For sliders, I'd like for the API to support transitions, and its not obvious to me how the API should look like.

I'd appreciate feedback on

  1. Should egui have an API for coloring sliders?
  2. If yes, how can I help add one?
  3. If no, what would be the recommended approach to implement this in my own project? Is there a better way other than copying egui::Slider and extending it?
@YgorSouza
Copy link
Contributor

I think it is possible to do what you want without copying the entire widget code. It's just somewhat cumbersome. You can change the color of some parts of the widget, so you can make parts of it transparent and paint other things behind or in front of it. For example:

ui.horizontal(|ui| {
    ui.style_mut().visuals.widgets.inactive.bg_fill = Color32::RED;
    ui.radio(false, "");
    ui.style_mut().visuals.widgets.inactive.bg_fill = Color32::GREEN;
    ui.radio(true, "");
    ui.style_mut().visuals.widgets.inactive.bg_fill = Color32::BLUE;
    ui.radio(false, "");
});
let rect = ui.label("Color gradient here").rect;
ui.allocate_ui_at_rect(rect, |ui| {
    let previous_fill = ui.style().visuals.widgets.inactive.bg_fill;
    ui.style_mut().visuals.widgets.inactive.bg_fill = Color32::TRANSPARENT;
    ui.add(Slider::new(&mut self.age, 1..=100).show_value(false));
    ui.style_mut().visuals.widgets.inactive.bg_fill = previous_fill;
});

image

I didn't include the code to make the actual gradient behind the slider because it is kind of complicated, but you can find it in the demo app.

There are some ideas to make this more ergonomic being discussed in #3284.

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

No branches or pull requests

2 participants