Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): 🎸 added named_svgs module to share SVG #658

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

### Features

- **core**: Added the `named_svgs` module to enable sharing SVGs using string keys, replacing the need for `IconTheme`. (#658 @M-Adoo)
- **core**: The `keyframes!` macro has been introduced to manage the intermediate steps of animation states. (#653 @M-Adoo)
- **core**: Added `QueryId` as a replacement for `TypeId` to facilitate querying types by Provider across different binaries. (#656 @M-Adoo)
- **widgets**: Added `LinearProgress` and `SpinnerProgress` widgets along with their respective material themes. (#630 @wjian23 @M-Adoo)
Expand Down
60 changes: 60 additions & 0 deletions core/src/builtin_widgets/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,63 @@ impl Render for Svg {
painter.draw_svg(self);
}
}

pub mod named_svgs {
use std::sync::{LazyLock, Mutex};

pub use super::*;

const DEFAULT_SVG_KEY: &str = "__RIRBIR_DEFAULT_SVG__";
static SVGS: LazyLock<Mutex<ahash::AHashMap<&'static str, Svg>>> = LazyLock::new(|| {
let svg = include_crate_svg!("src/builtin_widgets/default_named.svg");
let mut set = ahash::AHashMap::new();
set.insert(DEFAULT_SVG_KEY, svg);
Mutex::new(set)
});

/// Register an SVG with a specific name. You can then use the same `name`
/// parameter with [`named_svgs::get`](get) to retrieve it.
///
/// To prevent conflicts, it is recommended to add a namespace prefix from
/// your library or application to the name, such as `ribir::add`.
pub fn register(name: &'static str, svg: Svg) { SVGS.lock().unwrap().insert(name, svg); }

/// Retrieve a named SVG that was registered using
/// [`named_svgs::register`](register).
pub fn get(name: &str) -> Option<Svg> { SVGS.lock().unwrap().get(name).cloned() }

/// Functions similarly to [`named_svgs::get`](get), but returns the
/// default SVG if not found.
pub fn get_or_default(name: &str) -> Svg {
get(name).unwrap_or_else(|| get(DEFAULT_SVG_KEY).unwrap())
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use ribir_dev_helper::*;

use super::*;

fn svgs_smoke() -> Painter {
named_svgs::register(
"test::add",
Svg::parse_from_bytes(
r#"<svg xmlns="http://www.w3.org/2000/svg" height="48" width="48"><path d="M22.5 38V25.5H10v-3h12.5V10h3v12.5H38v3H25.5V38Z"/></svg>"#.as_bytes(),
).unwrap(),
);
let mut painter = Painter::new(Rect::from_size(Size::new(128., 64.)));
let add = named_svgs::get("test::add").unwrap();
let x = named_svgs::get_or_default("x");

painter
.draw_svg(&add)
.translate(64., 0.)
.draw_svg(&x);

painter
}

painter_backend_eq_image_test!(svgs_smoke, comparison = 0.001);
}
4 changes: 3 additions & 1 deletion core/src/builtin_widgets/theme/icon_theme.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// todo: replace the icon theme with named_svgs and icon fonts for improved
// functionality.
use std::collections::HashMap;

use crate::{prelude::*, render_helper::RenderProxy};
Expand Down Expand Up @@ -68,7 +70,7 @@ impl Compose for NamedSvg {

impl IconTheme {
pub fn new(icon_size: IconSize) -> Self {
let svg = include_crate_svg!("src/builtin_widgets/theme/miss_icon.svg");
let svg = include_crate_svg!("src/builtin_widgets/default_named.svg");
let miss_icon = Resource::new(svg);
let mut icons = HashMap::<_, _, ahash::RandomState>::default();
icons.insert(MISS_ICON, miss_icon);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading