linebender / druid

A data-first Rust-native UI design toolkit.
https://linebender.org/druid/
Apache License 2.0
9.57k stars 566 forks source link

Druid ignoring custom painter #2405

Closed barnabasd closed 3 months ago

barnabasd commented 3 months ago

Hello. I'm trying to style a button using a simple painter but whatever i change the button's colors stay the same. Here is my code:

fn create_button(data: (String, String, Arc<[u8]>)) -> impl Widget<u32> {
    let painter = Painter::new(move |ctx, _, _env| {
        let bounds = ctx.size().to_rounded_rect(RoundedRectRadii::from_single_radius(BUTTON_BORDER_RADIUS));
        ctx.fill(bounds, &Color::rgb8(28, 28, 28));
        if ctx.is_hot() { ctx.fill(bounds, &Color::rgb8(255, 0, 0)); }
        if ctx.is_active() { ctx.fill(bounds, &Color::rgb8(0, 255, 0)); }
    });

    Button::new(data.0).fix_height(ICON_DIMENSIONS).fix_width(ICON_DIMENSIONS).background(painter).on_click(|_event, _data, _env| {})
}

fn ui_builder(icons: Vec<(String, String, Arc<[u8]>)>) -> impl Widget<u32> {
    let mut row = Flex::row();

    row.add_spacer(WINDOW_PADDING);
    for (index, icon) in icons.iter().enumerate() {
        row = row.with_child(create_button(icon.clone()));
        if index != icons.len() - 1 { row.add_spacer(MID_ICON_SPACING); }
    }

    Container::new(row)
        .background(BackgroundBrush::Color(Color::from_hex_str("#1c1c1c").unwrap()))
        .rounded(RoundedRectRadii::from_single_radius(5.))
        .fix_height(46.).height(46.)
}

The background is always a gradient from black to some sort of gray. I want to change that. Thank you.

barnabasd commented 3 months ago

I have resolved the problem by using a label instead of a button. But why can't I style a button this way?