feat: port svg sizing to font icons

This commit is contained in:
2026-04-26 00:29:31 +01:00
parent 8b28f3d67f
commit dab18c40c5
5 changed files with 52 additions and 31 deletions

View File

@@ -9,8 +9,8 @@ use crate::{app, component::text::TextContent};
pub struct Button {
id: gpui::ElementId,
label: Option<gpui::AnyElement>,
leading: Option<gpui::Svg>,
trailing: Option<gpui::Svg>,
leading: Option<Box<dyn FnOnce(gpui::Rgba) -> gpui::AnyElement>>,
trailing: Option<Box<dyn FnOnce(gpui::Rgba) -> gpui::AnyElement>>,
on_click: Option<Box<dyn Fn(&gpui::ClickEvent, &mut gpui::Window, &mut gpui::App)>>,
enabled: bool,
}
@@ -32,13 +32,19 @@ impl Button {
self
}
pub fn leading(mut self, s: gpui::Svg) -> Self {
self.leading = Some(s.size_3());
pub fn leading(mut self, s: impl IntoElement + Styled + 'static) -> Self {
let s = s.size_3();
self.leading = Some(Box::new(move |color| {
s.text_color(color).into_any_element()
}));
self
}
pub fn trailing(mut self, s: gpui::Svg) -> Self {
self.trailing = Some(s.size_3());
pub fn trailing(mut self, s: impl IntoElement + Styled + 'static) -> Self {
let s = s.size_3();
self.trailing = Some(Box::new(move |color| {
s.text_color(color).into_any_element()
}));
self
}
@@ -62,21 +68,13 @@ impl gpui::RenderOnce for Button {
let mut children: Vec<AnyElement> = Vec::with_capacity(3);
if let Some(leading) = self.leading {
children.push(
leading
.text_color(theme.colors.accent_text)
.into_any_element(),
);
children.push(leading(theme.colors.accent_text));
}
if let Some(label) = self.label {
children.push(label);
}
if let Some(trailing) = self.trailing {
children.push(
trailing
.text_color(theme.colors.accent_text)
.into_any_element(),
);
children.push(trailing(theme.colors.accent_text));
}
div()

View File

@@ -1,4 +1,4 @@
use gpui::{Styled, svg};
use gpui::{Refineable as _, RenderOnce, Styled, svg};
use paste::paste;
use crate::app;
@@ -28,7 +28,33 @@ macro_rules! define_font_icons {
define_font_icons!(Check, ChevronDown, FolderGit, Github, ArrowRight);
pub fn font_icon<T>(icon: FontIcon, cx: &gpui::Context<T>) -> gpui::Svg {
let theme = cx.global::<app::Global>().current_theme;
svg().path(icon_path(icon)).text_color(theme.colors.text)
#[derive(gpui::IntoElement)]
pub struct FontIconSvg {
icon: FontIcon,
style: gpui::StyleRefinement,
}
pub fn font_icon(icon: FontIcon) -> FontIconSvg {
FontIconSvg {
icon,
style: gpui::StyleRefinement::default(),
}
}
impl Styled for FontIconSvg {
fn style(&mut self) -> &mut gpui::StyleRefinement {
&mut self.style
}
}
impl RenderOnce for FontIconSvg {
fn render(self, _window: &mut gpui::Window, cx: &mut gpui::App) -> impl gpui::IntoElement {
let theme = app::current_theme(cx);
let mut svg = svg()
.path(icon_path(self.icon))
.text_color(theme.colors.text);
svg.style().refine(&self.style);
svg
}
}

View File

@@ -443,7 +443,7 @@ fn connected_body(user: &api::user::User, cx: &gpui::Context<GithubStepView>) ->
.rounded_full()
.bg(theme.colors.accent)
.p_1()
.child(font_icon(FontIcon::Check, cx).size_4()),
.child(font_icon(FontIcon::Check).size_4()),
),
)
}

View File

@@ -88,11 +88,11 @@ impl Screen {
}
}
fn step_list(&self, cx: &gpui::Context<Self>) -> impl gpui::IntoElement {
fn step_list(&self, _cx: &gpui::Context<Self>) -> impl gpui::IntoElement {
let children: Vec<gpui::Div> = ALL_SETUP_STEPS
.iter()
.enumerate()
.map(|(i, step)| {
.map(|(i, step)| {
let label = match step {
Step::Welcome => "Welcome!",
Step::ConnectToGithub => "Connect to GitHub",
@@ -106,10 +106,7 @@ impl Screen {
.items_center()
.gap_2p5()
.child(if is_completed {
font_icon(FontIcon::Check, cx)
.size_4()
.into_any_element()
.into_any_element()
font_icon(FontIcon::Check).size_4().into_any_element()
} else {
div().size_4().into_any_element()
})

View File

@@ -36,7 +36,7 @@ impl gpui::Render for TitleBar {
let user_avatar = match user {
QueryStatus::Err(api::Error::Unauthenticated) => div().absolute().right_2p5().child(
button("login-btn")
.leading(font_icon(FontIcon::Github, cx))
.leading(font_icon(FontIcon::Github))
.label("Login"),
),
@@ -69,14 +69,14 @@ impl RepoSelector {
}
}
fn repo_selector<T: 'static>(cx: &gpui::Context<T>) -> gpui::Div {
fn repo_selector<T: 'static>(_cx: &gpui::Context<T>) -> gpui::Div {
div()
.flex()
.flex_row()
.items_center()
.gap_1()
.text_xs()
.child(font_icon(FontIcon::FolderGit, cx).size_3())
.child(font_icon(FontIcon::FolderGit).size_3())
.child(text("test/repo"))
.child(font_icon(FontIcon::ChevronDown, cx).size_3())
.child(font_icon(FontIcon::ChevronDown).size_3())
}