diff --git a/Cargo.lock b/Cargo.lock index 1ea090e2..a49af3f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5533,6 +5533,7 @@ dependencies = [ "paste", "regex", "resvg", + "rust-embed", "rust-i18n", "serde", "serde_json", diff --git a/crates/story/src/fixtures/pie.json b/crates/story/src/fixtures/pie.json index 63c1d7d0..28f87412 100644 --- a/crates/story/src/fixtures/pie.json +++ b/crates/story/src/fixtures/pie.json @@ -1,6 +1,6 @@ { "border_radius": 8, - "font_family": "sans-serif", + "font_family": "Noto Sans", "height": 400, "inner_radius": 30, "legend_margin": { diff --git a/crates/ui/Cargo.toml b/crates/ui/Cargo.toml index d028cbcf..5f86d9c7 100644 --- a/crates/ui/Cargo.toml +++ b/crates/ui/Cargo.toml @@ -9,13 +9,11 @@ version = "0.1.0" doctest = false [dependencies] -anyhow = "1" gpui.workspace = true -itertools = "0.13.0" -serde = "1.0.203" -serde_json = "1" +anyhow = "1" image = "0.25.1" +itertools = "0.13.0" once_cell = "1.19.0" paste = "1" regex = "1" @@ -23,7 +21,10 @@ resvg = { version = "0.44.0", default-features = false, features = [ "system-fonts", "text", ] } +rust-embed = "8.5.0" rust-i18n = "3" +serde = "1.0.203" +serde_json = "1" smallvec = "1.13.2" smol = "1" unicode-segmentation = "1.11.0" diff --git a/crates/ui/assets/fonts/NotoSans-Regular.ttf b/crates/ui/assets/fonts/NotoSans-Regular.ttf new file mode 100644 index 00000000..9b4f9dbb Binary files /dev/null and b/crates/ui/assets/fonts/NotoSans-Regular.ttf differ diff --git a/crates/ui/src/context_menu.rs b/crates/ui/src/context_menu.rs index 65c12f07..83633d56 100644 --- a/crates/ui/src/context_menu.rs +++ b/crates/ui/src/context_menu.rs @@ -2,15 +2,13 @@ use std::{cell::RefCell, rc::Rc}; use gpui::{ anchored, deferred, div, prelude::FluentBuilder, px, relative, AnchorCorner, AnyElement, - AppContext, DismissEvent, DispatchPhase, Element, ElementId, Focusable, GlobalElementId, + DismissEvent, DispatchPhase, Element, ElementId, Focusable, GlobalElementId, InteractiveElement, IntoElement, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Position, Stateful, Style, View, ViewContext, WindowContext, }; use crate::popup_menu::PopupMenu; -pub fn init(_cx: &mut AppContext) {} - pub trait ContextMenuExt: ParentElement + Sized { fn context_menu( self, diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index 761f3166..3944e1ef 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -61,15 +61,22 @@ pub use svg_img::*; use std::ops::Deref; +use rust_embed::RustEmbed; + rust_i18n::i18n!("locales", fallback = "en"); +#[derive(RustEmbed)] +#[folder = "./assets"] +#[include = "fonts/**/*"] +#[exclude = "*.DS_Store"] +pub struct Assets; + /// Initialize the UI module. /// /// This must be called before using any of the UI components. /// You can initialize the UI module at your application's entry point. pub fn init(cx: &mut gpui::AppContext) { theme::init(cx); - context_menu::init(cx); date_picker::init(cx); dock::init(cx); drawer::init(cx); @@ -80,7 +87,6 @@ pub fn init(cx: &mut gpui::AppContext) { popover::init(cx); popup_menu::init(cx); table::init(cx); - webview::init(cx); } pub fn locale() -> impl Deref { diff --git a/crates/ui/src/svg_img.rs b/crates/ui/src/svg_img.rs index b084b52e..bf820ff0 100644 --- a/crates/ui/src/svg_img.rs +++ b/crates/ui/src/svg_img.rs @@ -1,4 +1,8 @@ -use std::{hash::Hash, ops::Deref, sync::Arc}; +use std::{ + hash::Hash, + ops::Deref, + sync::{Arc, LazyLock}, +}; use gpui::{ px, size, AppContext, Asset, Bounds, Element, Hitbox, ImageCacheError, InteractiveElement, @@ -10,6 +14,17 @@ use smallvec::SmallVec; use image::ImageBuffer; +use crate::Assets; + +const FONT_PATH: &str = "fonts/NotoSans-Regular.ttf"; +static OPTIONS: LazyLock = LazyLock::new(|| { + let mut options = usvg::Options::default(); + if let Some(font_data) = Assets::get(FONT_PATH).map(|f| f.data) { + options.fontdb_mut().load_font_data(font_data.into()); + } + options +}); + #[derive(Debug, Clone, Hash)] pub enum SvgSource { /// A svg bytes @@ -103,10 +118,7 @@ impl Asset for Image { } }; - let mut options = usvg::Options::default(); - options.fontdb_mut().load_system_fonts(); - - let tree = usvg::Tree::from_data(&bytes, &options)?; + let tree = usvg::Tree::from_data(&bytes, &OPTIONS)?; let mut pixmap = resvg::tiny_skia::Pixmap::new(size.width.0 as u32, size.height.0 as u32) diff --git a/crates/ui/src/webview.rs b/crates/ui/src/webview.rs index 52a4817e..7b76c336 100644 --- a/crates/ui/src/webview.rs +++ b/crates/ui/src/webview.rs @@ -6,14 +6,12 @@ use wry::{ }; use gpui::{ - div, AppContext, Bounds, ContentMask, DismissEvent, Element, ElementId, EventEmitter, - FocusHandle, FocusableView, GlobalElementId, Hitbox, InteractiveElement, IntoElement, LayoutId, + div, Bounds, ContentMask, DismissEvent, Element, ElementId, EventEmitter, FocusHandle, + FocusableView, GlobalElementId, Hitbox, InteractiveElement, IntoElement, LayoutId, MouseDownEvent, ParentElement as _, Pixels, Render, Size, Style, Styled as _, View, WindowContext, }; -pub fn init(_cx: &AppContext) {} - pub struct WebView { focus_handle: FocusHandle, webview: Rc,