forked from reya/gpui-native-kit
feat: support app-defined icon sets
This commit is contained in:
+15
-10
@@ -6,7 +6,7 @@ use gpui::{
|
||||
IntoElement, ParentElement as _, RenderOnce, SharedString, StatefulInteractiveElement as _,
|
||||
Styled as _, Window, deferred, div, ease_out_quint, px,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::{Icon, IconRole, icon};
|
||||
use gpui_native_theme::{ActiveTheme as _, Size};
|
||||
|
||||
use crate::common::DismissHandler;
|
||||
@@ -33,7 +33,7 @@ pub struct Alert {
|
||||
title: Option<SharedString>,
|
||||
message: Option<SharedString>,
|
||||
severity: AlertSeverity,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
show_icon: bool,
|
||||
on_dismiss: Option<DismissHandler>,
|
||||
}
|
||||
@@ -78,9 +78,12 @@ impl Alert {
|
||||
self
|
||||
}
|
||||
|
||||
/// Overrides the severity-derived icon with this glyph.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
/// Overrides the severity-derived icon.
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -116,10 +119,12 @@ impl RenderOnce for Alert {
|
||||
AlertSeverity::Danger => theme.ramps.destructive,
|
||||
};
|
||||
|
||||
let icon_role = self.icon.unwrap_or(match self.severity {
|
||||
AlertSeverity::Info => IconRole::Info,
|
||||
AlertSeverity::Success => IconRole::Check,
|
||||
AlertSeverity::Warning | AlertSeverity::Danger => IconRole::Warning,
|
||||
let icon_glyph = self.icon.unwrap_or_else(|| {
|
||||
icon(match self.severity {
|
||||
AlertSeverity::Info => IconRole::Info,
|
||||
AlertSeverity::Success => IconRole::Check,
|
||||
AlertSeverity::Warning | AlertSeverity::Danger => IconRole::Warning,
|
||||
})
|
||||
});
|
||||
|
||||
let foreground = theme.colors.foreground;
|
||||
@@ -158,7 +163,7 @@ impl RenderOnce for Alert {
|
||||
.flex()
|
||||
.items_center()
|
||||
.h(window.line_height())
|
||||
.child(icon(icon_role).size(icon_size).color(ramp.base)),
|
||||
.child(icon_glyph.size(icon_size).color(ramp.base)),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
|
||||
@@ -6,7 +6,7 @@ use gpui::{
|
||||
RenderOnce, Role, SharedString, StatefulInteractiveElement as _, Styled as _, Window, canvas,
|
||||
relative, transparent_black,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::Icon;
|
||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
|
||||
use gpui_native_utils::{focus_ring, record_anchor_bounds};
|
||||
use std::rc::Rc;
|
||||
@@ -38,7 +38,7 @@ pub enum ButtonVariant {
|
||||
pub struct Button {
|
||||
id: ElementId,
|
||||
label: Option<SharedString>,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
variant: ButtonVariant,
|
||||
size: Size,
|
||||
disabled: bool,
|
||||
@@ -82,8 +82,11 @@ impl Button {
|
||||
}
|
||||
|
||||
/// Shows an icon beside the label.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -236,8 +239,8 @@ impl RenderOnce for Button {
|
||||
)
|
||||
})
|
||||
.when(!self.loading, |button| {
|
||||
button.when_some(self.icon, |button, role| {
|
||||
button.child(icon(role).size(metrics.icon_size).color(foreground))
|
||||
button.when_some(self.icon, |button, icon| {
|
||||
button.child(icon.size(metrics.icon_size).color(foreground))
|
||||
})
|
||||
})
|
||||
.when_some(self.label, |button, label| button.child(label))
|
||||
|
||||
@@ -9,7 +9,7 @@ use gpui::{
|
||||
StatefulInteractiveElement as _, Styled as _, Subscription, Window, actions, canvas, deferred,
|
||||
div, relative,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::Icon;
|
||||
use gpui_native_theme::{ActiveTheme as _, POPUP_MAX_HEIGHT, Size};
|
||||
|
||||
use crate::common::{h_flex, v_flex};
|
||||
@@ -49,7 +49,7 @@ pub struct Command {
|
||||
/// Gray text on the row's right edge, e.g. the keybinding.
|
||||
pub hint: Option<SharedString>,
|
||||
/// Leading glyph shown before the label.
|
||||
pub icon: Option<IconRole>,
|
||||
pub icon: Option<Icon>,
|
||||
}
|
||||
|
||||
impl Command {
|
||||
@@ -64,8 +64,11 @@ impl Command {
|
||||
}
|
||||
|
||||
/// Shows an icon before the label.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -361,8 +364,8 @@ impl Render for CommandPaletteState {
|
||||
state.update(cx, |state, cx| state.choose_row(row_ix, window, cx));
|
||||
}
|
||||
})
|
||||
.when_some(command.icon, |row, role| {
|
||||
row.child(icon(role).size(metrics.icon_size).color(colors.foreground))
|
||||
.when_some(command.icon.clone(), |row, icon| {
|
||||
row.child(icon.size(metrics.icon_size).color(colors.foreground))
|
||||
})
|
||||
.child(
|
||||
div()
|
||||
|
||||
@@ -9,7 +9,7 @@ use gpui::{
|
||||
MouseButton, MouseDownEvent, ParentElement as _, RenderOnce, Role, SharedString,
|
||||
StatefulInteractiveElement as _, Styled as _, Window, canvas, div, relative,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::{Icon, IconRole, icon};
|
||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
|
||||
use gpui_native_utils::{focus_handle, focus_ring};
|
||||
|
||||
@@ -27,7 +27,7 @@ type ItemsFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<MenuItem>>;
|
||||
pub struct DropdownMenu {
|
||||
id: ElementId,
|
||||
label: Option<SharedString>,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
variant: ButtonVariant,
|
||||
size: Size,
|
||||
disabled: bool,
|
||||
@@ -73,8 +73,11 @@ impl DropdownMenu {
|
||||
}
|
||||
|
||||
/// Shows an icon beside the label.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -267,8 +270,8 @@ impl RenderOnce for DropdownMenu {
|
||||
)
|
||||
})
|
||||
.when(!self.loading, |zone| {
|
||||
zone.when_some(self.icon, |zone, role| {
|
||||
zone.child(icon(role).size(metrics.icon_size).color(foreground))
|
||||
zone.when_some(self.icon, |zone, icon| {
|
||||
zone.child(icon.size(metrics.icon_size).color(foreground))
|
||||
})
|
||||
})
|
||||
.when_some(self.label, |zone, label| zone.child(label));
|
||||
|
||||
@@ -3,7 +3,7 @@ use gpui::{
|
||||
App, IntoElement, ParentElement as _, RenderOnce, SharedString, StyleRefinement, Styled as _,
|
||||
Window, div, relative,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::Icon;
|
||||
use gpui_native_theme::{ActiveTheme as _, Size};
|
||||
|
||||
use crate::common::{StyledExt, v_flex};
|
||||
@@ -11,7 +11,7 @@ use crate::common::{StyledExt, v_flex};
|
||||
/// The centered "nothing here yet" panel for lists, tables, and searches.
|
||||
#[derive(IntoElement)]
|
||||
pub struct EmptyState {
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
headline: SharedString,
|
||||
hint: Option<SharedString>,
|
||||
action: Option<gpui::AnyElement>,
|
||||
@@ -34,8 +34,11 @@ impl EmptyState {
|
||||
}
|
||||
|
||||
/// Sets a large muted icon above the headline.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -78,11 +81,11 @@ impl RenderOnce for EmptyState {
|
||||
.justify_center()
|
||||
.text_center()
|
||||
.p_4()
|
||||
.when_some(self.icon, |state, role| {
|
||||
.when_some(self.icon, |state, icon| {
|
||||
state.child(
|
||||
div()
|
||||
.mb_2()
|
||||
.child(icon(role).size(illustration).color(muted_foreground)),
|
||||
.child(icon.size(illustration).color(muted_foreground)),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
|
||||
+10
-7
@@ -9,7 +9,7 @@ use gpui::{
|
||||
StatefulInteractiveElement as _, Styled as _, Window, anchored, canvas, deferred, div,
|
||||
ease_out_quint, point, px, relative,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon as icon_glyph};
|
||||
use gpui_native_icons::{Icon, IconRole, icon as icon_glyph};
|
||||
use gpui_native_theme::{ActiveTheme as _, POPUP_MAX_HEIGHT};
|
||||
|
||||
use crate::common::v_flex;
|
||||
@@ -30,7 +30,7 @@ type ItemsFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<MenuItem>>;
|
||||
pub struct MenuItem {
|
||||
kind: MenuItemKind,
|
||||
label: SharedString,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
shortcut: Option<SharedString>,
|
||||
handler: Option<ItemHandler>,
|
||||
disabled: bool,
|
||||
@@ -86,8 +86,11 @@ impl MenuItem {
|
||||
}
|
||||
|
||||
/// Shows an icon before the label.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -526,7 +529,7 @@ fn render_item(
|
||||
let destructive = item.destructive;
|
||||
let checked = item.checked;
|
||||
let shortcut = item.shortcut.clone();
|
||||
let icon_role = item.icon;
|
||||
let icon = item.icon.clone();
|
||||
|
||||
div()
|
||||
.id(("menu-item", index))
|
||||
@@ -576,8 +579,8 @@ fn render_item(
|
||||
}
|
||||
}
|
||||
})
|
||||
.when_some(icon_role, |row, role| {
|
||||
row.child(icon_glyph(role).size(chrome.icon_size).color(label_color))
|
||||
.when_some(icon, |row, icon| {
|
||||
row.child(icon.size(chrome.icon_size).color(label_color))
|
||||
})
|
||||
.child(
|
||||
div()
|
||||
|
||||
@@ -7,7 +7,7 @@ use gpui::{
|
||||
SpringConfig, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Toggled, Window,
|
||||
canvas, div,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::Icon;
|
||||
use gpui_native_theme::{ActiveTheme as _, Size};
|
||||
use gpui_native_utils::{focus_handle, focus_ring};
|
||||
|
||||
@@ -21,7 +21,7 @@ type ChangeHandler<T> = Rc<dyn Fn(T, &mut Window, &mut App)>;
|
||||
pub struct SegmentedOption<T> {
|
||||
value: T,
|
||||
label: SharedString,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
}
|
||||
|
||||
impl<T> SegmentedOption<T> {
|
||||
@@ -38,8 +38,11 @@ impl<T> SegmentedOption<T> {
|
||||
}
|
||||
|
||||
/// Sets a leading icon for the segment.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -226,8 +229,8 @@ where
|
||||
.role(Role::RadioButton)
|
||||
.aria_label(segment.label.clone())
|
||||
.aria_toggled(Toggled::from(is_selected))
|
||||
.when_some(segment.icon, |segment, role| {
|
||||
segment.child(icon(role).size(icon_size).color(content_color))
|
||||
.when_some(segment.icon.clone(), |segment, icon| {
|
||||
segment.child(icon.size(icon_size).color(content_color))
|
||||
})
|
||||
.child(segment.label.clone())
|
||||
.child(
|
||||
|
||||
@@ -5,7 +5,7 @@ use gpui::{
|
||||
App, ElementId, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, Role,
|
||||
SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled as _, Window,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::{Icon, IconRole, icon};
|
||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size};
|
||||
use gpui_native_utils::focus_ring;
|
||||
|
||||
@@ -20,7 +20,7 @@ pub type SelectHandler<T> = Rc<dyn Fn(Option<T>, &mut Window, &mut App)>;
|
||||
pub struct SelectOption<T> {
|
||||
value: T,
|
||||
label: SharedString,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
}
|
||||
|
||||
impl<T> SelectOption<T> {
|
||||
@@ -37,8 +37,11 @@ impl<T> SelectOption<T> {
|
||||
}
|
||||
|
||||
/// Sets a leading icon for the option's row.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -272,8 +275,8 @@ where
|
||||
};
|
||||
|
||||
let mut row = MenuItem::new(option.label.clone());
|
||||
if let Some(role) = option.icon {
|
||||
row = row.icon(role);
|
||||
if let Some(icon) = option.icon.clone() {
|
||||
row = row.icon(icon);
|
||||
}
|
||||
row.checked(is_selected).handler(handler)
|
||||
})
|
||||
|
||||
@@ -9,7 +9,7 @@ use gpui::{
|
||||
IntoElement, ParentElement as _, RenderOnce, SharedString, StatefulInteractiveElement as _,
|
||||
Styled as _, Task, Window, deferred, div, ease_out_quint, px,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_icons::{Icon, IconRole, icon};
|
||||
use gpui_native_theme::ActiveTheme as _;
|
||||
|
||||
use crate::button::{Button, ButtonVariant};
|
||||
@@ -43,7 +43,7 @@ impl ToastAction {
|
||||
#[derive(Clone)]
|
||||
pub struct Toast {
|
||||
message: SharedString,
|
||||
icon: Option<IconRole>,
|
||||
icon: Option<Icon>,
|
||||
action: Option<ToastAction>,
|
||||
duration: Option<Duration>,
|
||||
on_dismiss: Option<DismissHandler>,
|
||||
@@ -68,8 +68,11 @@ impl Toast {
|
||||
}
|
||||
|
||||
/// Shows this glyph before the content.
|
||||
pub fn icon(mut self, role: IconRole) -> Self {
|
||||
self.icon = Some(role);
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
{
|
||||
self.icon = Some(icon.into());
|
||||
self
|
||||
}
|
||||
|
||||
@@ -285,14 +288,14 @@ fn pill(entry: &ToastEntry, toasts: &Entity<Toasts>, cx: &App) -> gpui::Stateful
|
||||
.border_color(border)
|
||||
.rounded(radius)
|
||||
.shadow(shadows.clone())
|
||||
.when_some(toast.icon, |pill, icon_role| {
|
||||
.when_some(toast.icon.clone(), |pill, icon| {
|
||||
pill.child(
|
||||
div()
|
||||
.flex_none()
|
||||
.mr_2()
|
||||
.flex()
|
||||
.items_center()
|
||||
.child(icon(icon_role).size(icon_size).color(foreground)),
|
||||
.child(icon.size(icon_size).color(foreground)),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
|
||||
Reference in New Issue
Block a user