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

fix(button): image button rendering fixes #201

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/theme/style/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ pub fn appearance(
}

Button::Image => {
appearance.background = Some(Background::Color(cosmic.bg_color().into()));
appearance.background = None;
appearance.text_color = Some(cosmic.accent.base.into());
appearance.icon_color = Some(cosmic.accent.base.into());

corner_radii = &cosmic.corner_radii.radius_s;
appearance.border_radius = (*corner_radii).into();

if focused {
appearance.border_width = 3.0;
appearance.border_width = 2.0;
appearance.border_color = cosmic.accent.base.into();
}

Expand Down Expand Up @@ -178,13 +178,18 @@ impl StyleSheet for crate::Theme {
return hovered(focused, self);
}

appearance(self, focused, style, |component| {
(
component.hover.into(),
Some(component.on.into()),
Some(component.on.into()),
)
})
appearance(
self,
focused || matches!(style, Button::Image),
style,
|component| {
(
component.hover.into(),
Some(component.on.into()),
Some(component.on.into()),
)
},
)
}

fn pressed(&self, focused: bool, style: &Self::Style) -> Appearance {
Expand All @@ -200,4 +205,8 @@ impl StyleSheet for crate::Theme {
)
})
}

fn selection_background(&self) -> Background {
Background::Color(self.cosmic().bg_color().into())
}
}
2 changes: 1 addition & 1 deletion src/widget/button/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ where
.width(builder.width)
.height(builder.height)
.apply(widget::button)
.padding(0)
.selected(builder.variant.selected)
.id(builder.id)
.padding(0)
.on_press_maybe(builder.on_press)
.style(builder.style)
.into()
Expand Down
3 changes: 3 additions & 0 deletions src/widget/button/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ pub trait StyleSheet {

/// Produces the pressed [`Appearance`] of a button.
fn pressed(&self, focused: bool, style: &Self::Style) -> Appearance;

/// Background color of the selection indicator
fn selection_background(&self) -> Background;
}
72 changes: 42 additions & 30 deletions src/widget/button/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ where
/// Sets the widget to a selected state.
///
/// Displays a selection indicator on image buttons.
pub(super) fn selected(mut self, selected: bool) -> Self {
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected.then(|| Selected {
icon: crate::widget::icon::from_name("object-select-symbolic")
.size(16)
Expand Down Expand Up @@ -323,20 +323,21 @@ where
theme,
&self.style,
|| tree.state.downcast_ref::<State>(),
);

self.content.as_widget().draw(
&tree.children[0],
renderer,
theme,
&renderer::Style {
icon_color: styling.icon_color.unwrap_or(renderer_style.icon_color),
text_color: styling.text_color.unwrap_or(renderer_style.icon_color),
scale_factor: renderer_style.scale_factor,
|renderer, styling| {
self.content.as_widget().draw(
&tree.children[0],
renderer,
theme,
&renderer::Style {
icon_color: styling.icon_color.unwrap_or(renderer_style.icon_color),
text_color: styling.text_color.unwrap_or(renderer_style.icon_color),
scale_factor: renderer_style.scale_factor,
},
content_layout,
cursor,
&bounds,
);
},
content_layout,
cursor,
&bounds,
);

if let Some(ref selected) = self.selected {
Expand All @@ -345,16 +346,14 @@ where
bounds: Rectangle {
width: 24.0,
height: 20.0,
x: bounds.x,
y: bounds.y + (bounds.height - 20.0),
x: bounds.x + styling.border_width,
y: bounds.y + (bounds.height - 20.0 - styling.border_width),
},
border_radius: [0.0, 8.0, 0.0, 8.0].into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
styling
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
theme.selection_background(),
);

iced_core::svg::Renderer::draw(
Expand All @@ -364,8 +363,8 @@ where
Rectangle {
width: 16.0,
height: 16.0,
x: bounds.x + 4.0,
y: bounds.y + (bounds.height - 16.0),
x: bounds.x + 5.0 + styling.border_width,
y: bounds.y + (bounds.height - 18.0 - styling.border_width),
},
);
}
Expand Down Expand Up @@ -604,6 +603,7 @@ pub fn draw<'a, Renderer: iced_core::Renderer>(
style_sheet: &dyn StyleSheet<Style = <Renderer::Theme as StyleSheet>::Style>,
style: &<Renderer::Theme as StyleSheet>::Style,
state: impl FnOnce() -> &'a State,
draw_contents: impl FnOnce(&mut Renderer, Appearance),
) -> Appearance
where
Renderer::Theme: StyleSheet,
Expand Down Expand Up @@ -663,22 +663,34 @@ where
);
}

// Draw the button background first.
if let Some(background) = styling.background {
renderer.fill_quad(
renderer::Quad {
bounds,
border_radius: styling.border_radius,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
background,
);
}

// Then draw the button contents onto the background.
draw_contents(renderer, styling);

// Finish by drawing the border above the contents.
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x + if is_selected { -1.0 } else { 0.0 },
y: bounds.y + if is_selected { -1.0 } else { 0.0 },
width: bounds.width + if is_selected { 2.0 } else { 0.0 },
height: bounds.height + if is_selected { 2.0 } else { 0.0 },
},
bounds,
border_radius: styling.border_radius,
border_width: styling.border_width,
border_color: styling.border_color,
},
styling
.background
.unwrap_or(Background::Color(Color::TRANSPARENT)),
Color::TRANSPARENT,
);
} else {
draw_contents(renderer, styling);
}

styling
Expand Down
Loading