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/color icon #661

Merged
merged 3 commits into from
Nov 20, 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 @@ -32,6 +32,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he
- **core**: Added `QueryId` as a replacement for `TypeId` to facilitate querying types by Provider across different binaries. (#656 @M-Adoo)
- **core**: Added `OverrideClass` to override a single class within a subtree. (#657 @M-Adoo)
- **widgets**: Added `LinearProgress` and `SpinnerProgress` widgets along with their respective material themes. (#630 @wjian23 @M-Adoo)
- **painter**: SVG now supports switching the default color, allowing for icon color changes. (#661 @M-Adoo)

### Changed

Expand Down
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ colored = "2.1.0"
derive_more = "1.0.0"
dssim-core="3.2.9"
env_logger = "0.7.1"
euclid = "0.22.6"
euclid = "0.22.11"
fontdb = "0.23.0"
futures = "0.3.26"
guillotiere = "0.6.0"
image = { version = "0.24.5" }
indextree = "4.7.3"
log = "0.4.14"
lyon_algorithms = "1.0.1"
lyon_geom = "1.0.1"
lyon_path = "1.0.1"
lyon_tessellation = "1.0.1"
lyon_algorithms = "1.0.4"
lyon_geom = "1.0.6"
lyon_path = "1.0.6"
lyon_tessellation = "1.0.15"
material-color-utilities-rs = "0.2.1"
icrate = "0.0.4"
paste = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion core/src/builtin_widgets/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Render for Clip {
let child_size = ctx.assert_perform_single_child_layout(clamp);
match self.clip {
ClipType::Auto => child_size,
ClipType::Path(ref path) => path.bounds().max().to_tuple().into(),
ClipType::Path(ref path) => path.bounds(None).max().to_tuple().into(),
}
}

Expand Down
34 changes: 21 additions & 13 deletions core/src/builtin_widgets/painting_style.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
use crate::{prelude::*, wrap_render::*};

/// Explain the method for rendering shapes and paths, including filling or
/// stroking them.
#[derive(Clone, Debug, Default)]
pub enum PaintingStyle {
/// Fill the path.
#[default]
Fill,
/// Stroke path with line width.
Stroke(StrokeOptions),
}

/// A widget that sets the strategies for painting shapes and paths . It's can
/// be inherited by its descendants.
#[derive(Default)]
Expand All @@ -24,11 +13,30 @@ impl Declare for PaintingStyleWidget {
fn declarer() -> Self::Builder { FatObj::new(()) }
}

impl_compose_child_for_wrap_render!(PaintingStyleWidget);
impl<'c> ComposeChild<'c> for PaintingStyleWidget {
type Child = Widget<'c>;

fn compose_child(this: impl StateWriter<Value = Self>, child: Self::Child) -> Widget<'c> {
// We need to provide the text style for the children to access.
match this.try_into_value() {
Ok(this) => {
let style = this.painting_style.clone();
WrapRender::combine_child(State::value(this), child).attach_data(Box::new(Queryable(style)))
}
Err(this) => {
let style = this.map_reader(|w| PartData::from_ref(&w.painting_style));
WrapRender::combine_child(this, child).attach_data(Box::new(style))
}
}
}
}

impl WrapRender for PaintingStyleWidget {
fn perform_layout(&self, clamp: BoxClamp, host: &dyn Render, ctx: &mut LayoutCtx) -> Size {
host.perform_layout(clamp, ctx)
let old = ctx.set_painting_style(self.painting_style.clone());
let size = host.perform_layout(clamp, ctx);
ctx.set_painting_style(old);
size
}

fn paint(&self, host: &dyn Render, ctx: &mut PaintingCtx) {
Expand Down
8 changes: 4 additions & 4 deletions core/src/builtin_widgets/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::prelude::*;

impl Render for Svg {
#[inline]
fn perform_layout(&self, clamp: BoxClamp, _: &mut LayoutCtx) -> Size { clamp.clamp(self.size) }
fn perform_layout(&self, clamp: BoxClamp, _: &mut LayoutCtx) -> Size { clamp.clamp(self.size()) }

fn paint(&self, ctx: &mut PaintingCtx) {
let painter = ctx.painter();
Expand All @@ -17,7 +17,7 @@ pub mod named_svgs {

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 svg = include_crate_svg!("src/builtin_widgets/default_named.svg", true, false);
let mut set = ahash::AHashMap::new();
set.insert(DEFAULT_SVG_KEY, svg);
Mutex::new(set)
Expand Down Expand Up @@ -51,8 +51,8 @@ mod tests {
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(),
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(),
true, false
).unwrap(),
);
let mut painter = Painter::new(Rect::from_size(Size::new(128., 64.)));
Expand Down
5 changes: 4 additions & 1 deletion core/src/builtin_widgets/text_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ impl<'c> ComposeChild<'c> for TextStyleWidget {

impl WrapRender for TextStyleWidget {
fn perform_layout(&self, clamp: BoxClamp, host: &dyn Render, ctx: &mut LayoutCtx) -> Size {
host.perform_layout(clamp, ctx)
let old = ctx.set_text_style(self.text_style.clone());
let size = host.perform_layout(clamp, ctx);
ctx.set_text_style(old);
size
}

fn paint(&self, host: &dyn Render, ctx: &mut PaintingCtx) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/builtin_widgets/theme/icon_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! define_named_svg {
macro_rules! fill_svgs {
($theme: expr, $($name: path: $path: literal),+) => {
$(
let icon = Resource::new(include_crate_svg!($path));
let icon = Resource::new(include_crate_svg!($path, true, false));
$theme.set_svg($name, icon);
)+
};
Expand All @@ -70,7 +70,7 @@ impl Compose for NamedSvg {

impl IconTheme {
pub fn new(icon_size: IconSize) -> Self {
let svg = include_crate_svg!("src/builtin_widgets/default_named.svg");
let svg = include_crate_svg!("src/builtin_widgets/default_named.svg", true, false);
let miss_icon = Resource::new(svg);
let mut icons = HashMap::<_, _, ahash::RandomState>::default();
icons.insert(MISS_ICON, miss_icon);
Expand Down
36 changes: 35 additions & 1 deletion core/src/context/layout_ctx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ribir_geom::{Point, Size};
use ribir_painter::{PaintingStyle, TextStyle};

use super::{WidgetCtx, WidgetCtxImpl};
use crate::{
Expand All @@ -17,6 +18,8 @@ pub struct LayoutCtx<'a> {
/// The widget tree of the window, not borrow it from `wnd` is because a
/// `LayoutCtx` always in a mutable borrow.
pub(crate) tree: &'a mut WidgetTree,
painting_style: PaintingStyle,
text_style: TextStyle,
}

impl<'a> WidgetCtxImpl for LayoutCtx<'a> {
Expand All @@ -28,6 +31,20 @@ impl<'a> WidgetCtxImpl for LayoutCtx<'a> {
}

impl<'a> LayoutCtx<'a> {
pub(crate) fn new(id: WidgetId, tree: &'a mut WidgetTree) -> Self {
let painting_style = if let Some(style) = id.query_ancestors_ref::<PaintingStyle>(tree) {
style.clone()
} else {
PaintingStyle::Fill
};
let text_style = if let Some(style) = id.query_ancestors_ref::<TextStyle>(tree) {
style.clone()
} else {
TextStyle::default()
};

Self { id, tree, painting_style, text_style }
}
/// Perform layout of the child widget referenced, resetting the widget
/// position to (0, 0) relative to the parent if its position is not set by
/// its layout logic, and return the resulting size after layout.
Expand All @@ -40,7 +57,12 @@ impl<'a> LayoutCtx<'a> {
// Safety: the `tree` just use to get the widget of `id`, and `tree2` not drop
// or modify it during perform layout.
let tree2 = unsafe { &mut *(self.tree as *mut WidgetTree) };
let mut ctx = LayoutCtx { id: child, tree: tree2 };
let mut ctx = LayoutCtx {
id: child,
tree: tree2,
painting_style: self.painting_style.clone(),
text_style: self.text_style.clone(),
};
let size = child
.assert_get(self.tree)
.perform_layout(clamp, &mut ctx);
Expand Down Expand Up @@ -129,4 +151,16 @@ impl<'a> LayoutCtx<'a> {
assert_eq!(child.parent(self.tree), Some(self.id));
self.tree.store.force_layout(child).is_some()
}

pub fn set_painting_style(&mut self, style: PaintingStyle) -> PaintingStyle {
std::mem::replace(&mut self.painting_style, style)
}

pub fn painting_style(&self) -> &PaintingStyle { &self.painting_style }

pub fn set_text_style(&mut self, style: TextStyle) -> TextStyle {
std::mem::replace(&mut self.text_style, style)
}

pub fn text_style(&self) -> &TextStyle { &self.text_style }
}
9 changes: 7 additions & 2 deletions core/src/render_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ impl<R: Render> RenderProxy for Sc<R> {
}

impl Render for Resource<Path> {
fn perform_layout(&self, clamp: BoxClamp, _: &mut LayoutCtx) -> Size {
let size = self.bounds().max().to_vector().to_size();
fn perform_layout(&self, clamp: BoxClamp, ctx: &mut LayoutCtx) -> Size {
let line_width = ctx.painting_style().line_width();
let size = self
.bounds(line_width)
.max()
.to_vector()
.to_size();
clamp.clamp(size)
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/widget_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl WidgetTree {
.map(|info| info.clamp)
.unwrap_or_else(|| BoxClamp { min: Size::zero(), max: win_size });

let mut ctx = LayoutCtx { id: wid, tree: self };
let mut ctx = LayoutCtx::new(wid, self);
ctx.perform_child_layout(wid, clamp);
}
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/widget_tree/widget_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ impl WidgetId {
.and_then(QueryHandle::into_ref)
}

pub(crate) fn query_ancestors_ref<T: Any>(self, tree: &WidgetTree) -> Option<QueryRef<T>> {
self
.ancestors(tree)
.find_map(|id| id.query_ref::<T>(tree))
}

/// return if this object contain type `T`
pub(crate) fn contain_type<T: Any>(self, tree: &WidgetTree) -> bool {
self
Expand Down
Loading
Loading