Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

git_ui: Fix commit editor button hover colors (#65448)

Shuvro Roy93d745668805
diff --git a/crates/git_ui/src/git_panel.rs b/crates/git_ui/src/git_panel.rs
index 6f43798ee2a..28df7ea3650 100644
--- a/crates/git_ui/src/git_panel.rs
+++ b/crates/git_ui/src/git_panel.rs
@@ -6803,11 +6803,11 @@ impl GitPanel {
.h_full()
.gap_px()
.p_1p5()
- .opacity(0.6)
- .hover(|s| s.opacity(1.0))
.child(
IconButton::new("expand-commit-editor", IconName::MaximizeAlt)
.icon_size(IconSize::Small)
+ .icon_color(Color::Hidden)
+ .hover_color(Color::Muted)
.tooltip({
move |_window, cx| {
Tooltip::for_action_in(
@@ -6834,6 +6834,8 @@ impl GitPanel {
IconButton::new("fill-commit-editor", icon)
.icon_size(IconSize::Small)
+ .icon_color(Color::Hidden)
+ .hover_color(Color::Muted)
.tooltip({
move |_window, cx| {
Tooltip::for_action_in(
diff --git a/crates/ui/src/components/button/icon_button.rs b/crates/ui/src/components/button/icon_button.rs
index d14aa46a32d..330605f9472 100644
--- a/crates/ui/src/components/button/icon_button.rs
+++ b/crates/ui/src/components/button/icon_button.rs
@@ -21,6 +21,7 @@ pub struct IconButton {
icon: IconName,
icon_size: IconSize,
icon_color: Color,
+ hover_color: Option<Color>,
selected_icon: Option<IconName>,
selected_icon_color: Option<Color>,
selected_style: Option<ButtonStyle>,
@@ -37,6 +38,7 @@ impl IconButton {
icon,
icon_size: IconSize::default(),
icon_color: Color::Default,
+ hover_color: None,
selected_icon: None,
selected_icon_color: None,
selected_style: None,
@@ -105,6 +107,12 @@ impl IconButton {
self
}
+ /// Sets the icon color used while the button is hovered.
+ pub fn hover_color(mut self, color: Color) -> Self {
+ self.hover_color = Some(color);
+ self
+ }
+
pub fn selected_icon(mut self, icon: impl Into<Option<IconName>>) -> Self {
self.selected_icon = icon.into();
self
@@ -263,7 +271,15 @@ impl RenderOnce for IconButton {
Color::Custom(base_color.opacity(self.alpha.unwrap_or(1.0)))
};
- let icon_element = Icon::new(icon).size(self.icon_size).color(icon_color);
+ let hover_color = self
+ .hover_color
+ .filter(|_| !is_disabled && !is_selected)
+ .map(|color| Color::Custom(color.color(cx).opacity(self.alpha.unwrap_or(1.0))));
+
+ let icon_element = Icon::new(icon)
+ .size(self.icon_size)
+ .color(icon_color)
+ .group_hover_color(hover_color);
self.base
.map(|this| match self.shape {
@@ -453,6 +469,14 @@ impl Component for IconButton {
.layer(ElevationIndex::Background)
.into_any_element(),
),
+ single_example(
+ "With Hover Color",
+ IconButton::new("hover_color", IconName::Check)
+ .icon_color(Color::Muted)
+ .hover_color(Color::Default)
+ .layer(ElevationIndex::Background)
+ .into_any_element(),
+ ),
],
),
])
diff --git a/crates/ui/src/components/icon.rs b/crates/ui/src/components/icon.rs
index 8935e181afd..bc4c134b117 100644
--- a/crates/ui/src/components/icon.rs
+++ b/crates/ui/src/components/icon.rs
@@ -148,6 +148,7 @@ enum IconSource {
pub struct Icon {
source: IconSource,
color: Color,
+ group_hover_color: Option<Color>,
size: Rems,
transformation: Transformation,
}
@@ -157,6 +158,7 @@ impl Icon {
Self {
source: IconSource::Embedded(icon.path().into()),
color: Color::default(),
+ group_hover_color: None,
size: IconSize::default().rems(),
transformation: Transformation::default(),
}
@@ -175,6 +177,7 @@ impl Icon {
Self {
source,
color: Color::default(),
+ group_hover_color: None,
size: IconSize::default().rems(),
transformation: Transformation::default(),
}
@@ -184,6 +187,7 @@ impl Icon {
Self {
source: IconSource::ExternalSvg(svg),
color: Color::default(),
+ group_hover_color: None,
size: IconSize::default().rems(),
transformation: Transformation::default(),
}
@@ -205,6 +209,15 @@ impl Icon {
self
}
+ /// Sets the color used while the enclosing unnamed (`""`) group is hovered,
+ /// such as the one set up by [`crate::ButtonLike`].
+ ///
+ /// Not to be exposed outside of the `ui` crate.
+ pub(crate) fn group_hover_color(mut self, color: impl Into<Option<Color>>) -> Self {
+ self.group_hover_color = color.into();
+ self
+ }
+
/// Sets a custom size for the icon, in [`Rems`].
///
/// Not to be exposed outside of the `ui` crate.
@@ -223,27 +236,28 @@ impl Transformable for Icon {
impl RenderOnce for Icon {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
- match self.source {
- IconSource::Embedded(path) => svg()
- .with_transformation(self.transformation)
- .size(self.size)
- .flex_none()
- .path(path)
- .text_color(self.color.color(cx))
- .into_any_element(),
- IconSource::ExternalSvg(path) => svg()
- .external_path(path)
- .with_transformation(self.transformation)
- .size(self.size)
- .flex_none()
- .text_color(self.color.color(cx))
- .into_any_element(),
- IconSource::External(path) => img(path)
- .size(self.size)
- .flex_none()
- .text_color(self.color.color(cx))
- .into_any_element(),
- }
+ let color = self.color.color(cx);
+ let svg = match self.source {
+ IconSource::Embedded(path) => svg().path(path),
+ IconSource::ExternalSvg(path) => svg().external_path(path),
+ IconSource::External(path) => {
+ return img(path)
+ .size(self.size)
+ .flex_none()
+ .text_color(color)
+ .into_any_element();
+ }
+ };
+ let group_hover_color = self.group_hover_color.map(|color| color.color(cx));
+
+ svg.with_transformation(self.transformation)
+ .size(self.size)
+ .flex_none()
+ .text_color(color)
+ .when_some(group_hover_color, |this, hover_color| {
+ this.group_hover("", |style| style.text_color(hover_color))
+ })
+ .into_any_element()
}
}