- Add `mix` method to mix two color. - Export `ui::theme::*` to crate root, before `use ui:theme::ActiveTheme` to `use ui::ActiveTheme`. ## Break Changes - Moved `ui::theme::Colorize` to `ui::Colorize`. - Merged `ColorExt` to `Colorize`. - Renamed `to_hex_string` to `to_hex`, `parse_hex_string` to `parse_hex`.
321 lines
9.2 KiB
Rust
321 lines
9.2 KiB
Rust
use crate::{ActiveTheme, Sizable, Size};
|
|
use gpui::{
|
|
prelude::FluentBuilder as _, svg, AnyElement, Hsla, IntoElement, Radians, Render, RenderOnce,
|
|
SharedString, StyleRefinement, Styled, Svg, Transformation, View, VisualContext, WindowContext,
|
|
};
|
|
|
|
#[derive(IntoElement, Clone)]
|
|
pub enum IconName {
|
|
ALargeSmall,
|
|
ArrowDown,
|
|
ArrowLeft,
|
|
ArrowRight,
|
|
ArrowUp,
|
|
Asterisk,
|
|
Bell,
|
|
BookOpen,
|
|
Bot,
|
|
Calendar,
|
|
ChartPie,
|
|
Check,
|
|
ChevronDown,
|
|
ChevronLeft,
|
|
ChevronRight,
|
|
ChevronUp,
|
|
ChevronsUpDown,
|
|
CircleCheck,
|
|
CircleUser,
|
|
CircleX,
|
|
Close,
|
|
Copy,
|
|
Dash,
|
|
Delete,
|
|
Ellipsis,
|
|
EllipsisVertical,
|
|
Eye,
|
|
EyeOff,
|
|
Frame,
|
|
GalleryVerticalEnd,
|
|
GitHub,
|
|
Globe,
|
|
Heart,
|
|
HeartOff,
|
|
Inbox,
|
|
Info,
|
|
LayoutDashboard,
|
|
Loader,
|
|
LoaderCircle,
|
|
Map,
|
|
Maximize,
|
|
Menu,
|
|
Minimize,
|
|
Minus,
|
|
Moon,
|
|
Palette,
|
|
PanelBottom,
|
|
PanelBottomOpen,
|
|
PanelLeft,
|
|
PanelLeftClose,
|
|
PanelLeftOpen,
|
|
PanelRight,
|
|
PanelRightClose,
|
|
PanelRightOpen,
|
|
Plus,
|
|
ResizeCorner,
|
|
Search,
|
|
Settings,
|
|
Settings2,
|
|
SortAscending,
|
|
SortDescending,
|
|
SquareTerminal,
|
|
Star,
|
|
StarOff,
|
|
Sun,
|
|
ThumbsDown,
|
|
ThumbsUp,
|
|
TriangleAlert,
|
|
WindowClose,
|
|
WindowMaximize,
|
|
WindowMinimize,
|
|
WindowRestore,
|
|
}
|
|
|
|
impl IconName {
|
|
pub fn path(self) -> SharedString {
|
|
match self {
|
|
Self::ALargeSmall => "icons/a-large-small.svg",
|
|
Self::ArrowDown => "icons/arrow-down.svg",
|
|
Self::ArrowLeft => "icons/arrow-left.svg",
|
|
Self::ArrowRight => "icons/arrow-right.svg",
|
|
Self::ArrowUp => "icons/arrow-up.svg",
|
|
Self::Asterisk => "icons/asterisk.svg",
|
|
Self::Bell => "icons/bell.svg",
|
|
Self::BookOpen => "icons/book-open.svg",
|
|
Self::Bot => "icons/bot.svg",
|
|
Self::Calendar => "icons/calendar.svg",
|
|
Self::ChartPie => "icons/chart-pie.svg",
|
|
Self::Check => "icons/check.svg",
|
|
Self::ChevronDown => "icons/chevron-down.svg",
|
|
Self::ChevronLeft => "icons/chevron-left.svg",
|
|
Self::ChevronRight => "icons/chevron-right.svg",
|
|
Self::ChevronUp => "icons/chevron-up.svg",
|
|
Self::ChevronsUpDown => "icons/chevrons-up-down.svg",
|
|
Self::CircleCheck => "icons/circle-check.svg",
|
|
Self::CircleUser => "icons/circle-user.svg",
|
|
Self::CircleX => "icons/circle-x.svg",
|
|
Self::Close => "icons/close.svg",
|
|
Self::Copy => "icons/copy.svg",
|
|
Self::Dash => "icons/dash.svg",
|
|
Self::Delete => "icons/delete.svg",
|
|
Self::Ellipsis => "icons/ellipsis.svg",
|
|
Self::EllipsisVertical => "icons/ellipsis-vertical.svg",
|
|
Self::Eye => "icons/eye.svg",
|
|
Self::EyeOff => "icons/eye-off.svg",
|
|
Self::Frame => "icons/frame.svg",
|
|
Self::GalleryVerticalEnd => "icons/gallery-vertical-end.svg",
|
|
Self::GitHub => "icons/github.svg",
|
|
Self::Globe => "icons/globe.svg",
|
|
Self::Heart => "icons/heart.svg",
|
|
Self::HeartOff => "icons/heart-off.svg",
|
|
Self::Inbox => "icons/inbox.svg",
|
|
Self::Info => "icons/info.svg",
|
|
Self::LayoutDashboard => "icons/layout-dashboard.svg",
|
|
Self::Loader => "icons/loader.svg",
|
|
Self::LoaderCircle => "icons/loader-circle.svg",
|
|
Self::Map => "icons/map.svg",
|
|
Self::Maximize => "icons/maximize.svg",
|
|
Self::Menu => "icons/menu.svg",
|
|
Self::Minimize => "icons/minimize.svg",
|
|
Self::Minus => "icons/minus.svg",
|
|
Self::Moon => "icons/moon.svg",
|
|
Self::Palette => "icons/palette.svg",
|
|
Self::PanelBottom => "icons/panel-bottom.svg",
|
|
Self::PanelBottomOpen => "icons/panel-bottom-open.svg",
|
|
Self::PanelLeft => "icons/panel-left.svg",
|
|
Self::PanelLeftClose => "icons/panel-left-close.svg",
|
|
Self::PanelLeftOpen => "icons/panel-left-open.svg",
|
|
Self::PanelRight => "icons/panel-right.svg",
|
|
Self::PanelRightClose => "icons/panel-right-close.svg",
|
|
Self::PanelRightOpen => "icons/panel-right-open.svg",
|
|
Self::Plus => "icons/plus.svg",
|
|
Self::ResizeCorner => "icons/resize-corner.svg",
|
|
Self::Search => "icons/search.svg",
|
|
Self::Settings => "icons/settings.svg",
|
|
Self::Settings2 => "icons/settings-2.svg",
|
|
Self::SortAscending => "icons/sort-ascending.svg",
|
|
Self::SortDescending => "icons/sort-descending.svg",
|
|
Self::SquareTerminal => "icons/square-terminal.svg",
|
|
Self::Star => "icons/star.svg",
|
|
Self::StarOff => "icons/star-off.svg",
|
|
Self::Sun => "icons/sun.svg",
|
|
Self::ThumbsDown => "icons/thumbs-down.svg",
|
|
Self::ThumbsUp => "icons/thumbs-up.svg",
|
|
Self::TriangleAlert => "icons/triangle-alert.svg",
|
|
Self::WindowClose => "icons/window-close.svg",
|
|
Self::WindowMaximize => "icons/window-maximize.svg",
|
|
Self::WindowMinimize => "icons/window-minimize.svg",
|
|
Self::WindowRestore => "icons/window-restore.svg",
|
|
}
|
|
.into()
|
|
}
|
|
|
|
/// Return the icon as a View<Icon>
|
|
pub fn view(self, cx: &mut WindowContext) -> View<Icon> {
|
|
Icon::build(self).view(cx)
|
|
}
|
|
}
|
|
|
|
impl From<IconName> for Icon {
|
|
fn from(val: IconName) -> Self {
|
|
Icon::build(val)
|
|
}
|
|
}
|
|
|
|
impl From<IconName> for AnyElement {
|
|
fn from(val: IconName) -> Self {
|
|
Icon::build(val).into_any_element()
|
|
}
|
|
}
|
|
|
|
impl RenderOnce for IconName {
|
|
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
|
|
Icon::build(self)
|
|
}
|
|
}
|
|
|
|
#[derive(IntoElement)]
|
|
pub struct Icon {
|
|
base: Svg,
|
|
path: SharedString,
|
|
text_color: Option<Hsla>,
|
|
size: Option<Size>,
|
|
rotation: Option<Radians>,
|
|
}
|
|
|
|
impl Default for Icon {
|
|
fn default() -> Self {
|
|
Self {
|
|
base: svg().flex_none().size_4(),
|
|
path: "".into(),
|
|
text_color: None,
|
|
size: None,
|
|
rotation: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Clone for Icon {
|
|
fn clone(&self) -> Self {
|
|
let mut this = Self::default().path(self.path.clone());
|
|
if let Some(size) = self.size {
|
|
this = this.with_size(size);
|
|
}
|
|
this
|
|
}
|
|
}
|
|
|
|
pub trait IconNamed {
|
|
fn path(&self) -> SharedString;
|
|
}
|
|
|
|
impl Icon {
|
|
pub fn new(icon: impl Into<Icon>) -> Self {
|
|
icon.into()
|
|
}
|
|
|
|
fn build(name: IconName) -> Self {
|
|
Self::default().path(name.path())
|
|
}
|
|
|
|
/// Set the icon path of the Assets bundle
|
|
///
|
|
/// For example: `icons/foo.svg`
|
|
pub fn path(mut self, path: impl Into<SharedString>) -> Self {
|
|
self.path = path.into();
|
|
self
|
|
}
|
|
|
|
/// Create a new view for the icon
|
|
pub fn view(self, cx: &mut WindowContext) -> View<Icon> {
|
|
cx.new_view(|_| self)
|
|
}
|
|
|
|
pub fn transform(mut self, transformation: gpui::Transformation) -> Self {
|
|
self.base = self.base.with_transformation(transformation);
|
|
self
|
|
}
|
|
|
|
pub fn empty() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Rotate the icon by the given angle
|
|
pub fn rotate(mut self, radians: impl Into<Radians>) -> Self {
|
|
self.base = self
|
|
.base
|
|
.with_transformation(Transformation::rotate(radians));
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Styled for Icon {
|
|
fn style(&mut self) -> &mut StyleRefinement {
|
|
self.base.style()
|
|
}
|
|
|
|
fn text_color(mut self, color: impl Into<Hsla>) -> Self {
|
|
self.text_color = Some(color.into());
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Sizable for Icon {
|
|
fn with_size(mut self, size: impl Into<Size>) -> Self {
|
|
self.size = Some(size.into());
|
|
self
|
|
}
|
|
}
|
|
|
|
impl RenderOnce for Icon {
|
|
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
|
let text_color = self.text_color.unwrap_or_else(|| cx.text_style().color);
|
|
|
|
self.base
|
|
.text_color(text_color)
|
|
.when_some(self.size, |this, size| match size {
|
|
Size::Size(px) => this.size(px),
|
|
Size::XSmall => this.size_3(),
|
|
Size::Small => this.size_3p5(),
|
|
Size::Medium => this.size_4(),
|
|
Size::Large => this.size_6(),
|
|
})
|
|
.path(self.path)
|
|
}
|
|
}
|
|
|
|
impl From<Icon> for AnyElement {
|
|
fn from(val: Icon) -> Self {
|
|
val.into_any_element()
|
|
}
|
|
}
|
|
|
|
impl Render for Icon {
|
|
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
|
let text_color = self.text_color.unwrap_or_else(|| cx.theme().foreground);
|
|
|
|
svg()
|
|
.flex_none()
|
|
.text_color(text_color)
|
|
.when_some(self.size, |this, size| match size {
|
|
Size::Size(px) => this.size(px),
|
|
Size::XSmall => this.size_3(),
|
|
Size::Small => this.size_3p5(),
|
|
Size::Medium => this.size_4(),
|
|
Size::Large => this.size_6(),
|
|
})
|
|
.path(self.path.clone())
|
|
.when_some(self.rotation, |this, rotation| {
|
|
this.with_transformation(Transformation::rotate(rotation))
|
|
})
|
|
}
|
|
}
|