Primitives · Icon::from_path draws an application's SVG with the theme's size and color; a path the asset source lacks fails at render

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Amit VermaandClaude Opus 5.5 authored and 雷电芽衣 committed 2026-10-04 17:24:39 -04:00
1 parent 2f02c82c6a
commit 2fb505ee98
9 files changed
+145 -17

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 2 22 12 12 22 2 12Z"/>
<path d="M8 12h8M12 8v8"/>
</svg>

After

Width:  |  Height:  |  Size: 256 B

+31 -5
View File
@@ -1,4 +1,4 @@
use std::f32::consts::FRAC_PI_4;
use std::{borrow::Cow, f32::consts::FRAC_PI_4};
use ely_gpui_component::{
Assets,
@@ -7,10 +7,31 @@ use ely_gpui_component::{
theme::{ActiveTheme, IconSize},
};
use gpui::{
App, AppContext, Context, FocusHandle, IntoElement, ParentElement, Radians, Render, Styled,
Window, WindowOptions, div,
App, AppContext, AssetSource, Context, FocusHandle, IntoElement, ParentElement, Radians,
Render, SharedString, Styled, Window, WindowOptions, div,
};
const CUSTOM_ICON: &str = "app-icons/mark.svg";
struct AppAssets;
impl AssetSource for AppAssets {
fn load(&self, path: &str) -> gpui::Result<Option<Cow<'static, [u8]>>> {
if path == CUSTOM_ICON {
return Ok(Some(Cow::Borrowed(include_bytes!("../assets/mark.svg"))));
}
Assets.load(path)
}
fn list(&self, path: &str) -> gpui::Result<Vec<SharedString>> {
let mut assets = Assets.list(path)?;
if CUSTOM_ICON.starts_with(path) {
assets.push(CUSTOM_ICON.into());
}
Ok(assets)
}
}
struct Icons {
focus: FocusHandle,
}
@@ -43,7 +64,12 @@ impl Render for Icons {
.child(Icon::new(IconName::Check))
.child(Icon::new(IconName::Rocket).size(IconSize::Xl))
.child(Icon::new(IconName::CircleCheck).color(theme.colors.success))
.child(Icon::new(IconName::Package).rotate(Radians(FRAC_PI_4))),
.child(Icon::new(IconName::Package).rotate(Radians(FRAC_PI_4)))
.child(
Icon::from_path(CUSTOM_ICON)
.size(IconSize::Xl)
.color(theme.colors.accent),
),
)
.child(
div()
@@ -59,7 +85,7 @@ impl Render for Icons {
fn main() {
gpui_platform::application()
.with_assets(Assets)
.with_assets(AppAssets)
.run(|cx: &mut App| {
ely_gpui_component::init(cx);
IconTheme::default()
+25
View File
@@ -0,0 +1,25 @@
use std::borrow::Cow;
use ely_gpui_component::Assets;
use gpui::{AssetSource, SharedString};
pub const CUSTOM_ICON: &str = "app-icons/mark.svg";
pub struct GalleryAssets;
impl AssetSource for GalleryAssets {
fn load(&self, path: &str) -> gpui::Result<Option<Cow<'static, [u8]>>> {
if path == CUSTOM_ICON {
return Ok(Some(Cow::Borrowed(include_bytes!("../assets/mark.svg"))));
}
Assets.load(path)
}
fn list(&self, path: &str) -> gpui::Result<Vec<SharedString>> {
let mut assets = Assets.list(path)?;
if CUSTOM_ICON.starts_with(path) {
assets.push(CUSTOM_ICON.into());
}
Ok(assets)
}
}
+3 -2
View File
@@ -20,6 +20,7 @@ macro_rules! asset {
};
}
mod assets;
#[cfg(not(target_family = "wasm"))]
mod capture;
mod pages;
@@ -42,7 +43,7 @@ use std::path::PathBuf;
#[cfg(not(target_family = "wasm"))]
use anyhow::{Context as _, Result, bail};
#[cfg(not(target_family = "wasm"))]
use ely_gpui_component::Assets;
use assets::GalleryAssets;
use ely_gpui_component::theme::{Mode, Theme};
use gpui::{
App, AppContext, Bounds, KeyBinding, Menu, MenuItem, SharedString, TitlebarOptions,
@@ -201,7 +202,7 @@ fn main() -> Result<()> {
// A script aims across its page; stories hold part.
let scripted = start.narrow.is_none() && start.story.is_none();
gpui_platform::application()
.with_assets(Assets)
.with_assets(GalleryAssets)
.run(move |cx: &mut App| {
let window = launch(start, cx);
if let Some(dir) = capture {
+11 -1
View File
@@ -14,6 +14,7 @@ use gpui::{
};
use crate::{
assets::CUSTOM_ICON,
probe::probe,
ui::{UNREAD, blocked, code, picture, row, section, specimen, specimens},
};
@@ -44,7 +45,7 @@ pub fn icon(cx: &App) -> impl IntoElement + use<> {
];
section(
"Icon",
"Lucide, drawn as a mask. Color comes from the theme.",
"Bundled Lucide or an application's SVG asset, drawn as a themed mask.",
cx,
)
.child(specimens().children(
@@ -55,6 +56,15 @@ pub fn icon(cx: &App) -> impl IntoElement + use<> {
specimen(name, Icon::new(IconName::CircleCheck).color(color), cx)
})),
)
.child(
specimens().child(specimen(
"custom SVG",
Icon::from_path(CUSTOM_ICON)
.size(IconSize::Xl)
.color(colors.accent),
cx,
)),
)
}
pub fn icon_set(cx: &App) -> impl IntoElement + use<> {
+4 -3
View File
@@ -6,14 +6,15 @@ use std::{
};
use anyhow::{Context as _, Result, anyhow, bail};
use ely_gpui_component::Assets;
use gpui::{Application, AsyncApp, SharedString, WindowHandle};
use gpui_web::{CanvasFontFallback, WebBackendPreference, WebPlatform};
use wasm_bindgen::prelude::*;
use web_sys::UrlSearchParams;
use crate::{
Start, choose, launch, pages,
Start,
assets::GalleryAssets,
choose, launch, pages,
shell::{Choice, Gallery},
};
@@ -97,7 +98,7 @@ pub fn start() {
let http = Arc::new(platform.fetch_http_client());
Application::with_platform(platform)
.with_http_client(http)
.with_assets(Assets)
.with_assets(GalleryAssets)
.run(move |cx| {
cx.text_system()
.add_fonts(vec![Cow::Borrowed(HEBREW)])