diff --git a/book/src/guides/style.md b/book/src/guides/style.md index c19c88d73..7adb44e92 100644 --- a/book/src/guides/style.md +++ b/book/src/guides/style.md @@ -7,6 +7,7 @@ Learn how the style attributes work. - [`corner radius & corner smoothing`](#corner_radius--corner_smoothing) - [`border`](#border) - [`overflow`](#overflow) +- [`opacity`](#opacity) - [`Color syntax`](#color-syntax) - [`Static colors`](#static-colors) - [`rgb() / hsl(`)](#rgb--hsl) @@ -121,6 +122,28 @@ fn app(cx: Scope) -> Element { Compatible elements: [`rect`](/guides/elements.html#rect) + +### opacity + +> Only available on the `main` branch. + +Specify the opacity of an element and all its desdendants. + +Example: + +```rust, no_run +fn app(cx: Scope) -> Element { + render!( + rect { + opacity: "0.5", // 50% visible + label { + "I am fading!" + } + } + ) +} +``` + ### Color syntax The attributes that have colors as values can use the following syntax: diff --git a/crates/elements/src/elements.rs b/crates/elements/src/elements.rs index 2ee069b22..3bf2ae741 100644 --- a/crates/elements/src/elements.rs +++ b/crates/elements/src/elements.rs @@ -196,6 +196,7 @@ builder_constructors! { position_right: String, position_bottom: String, position_left: String, + opacity: String, }; label { color: String, @@ -223,6 +224,7 @@ builder_constructors! { text_overflow: String, focusable: String, margin: String, + opacity: String, }; paragraph { layer: String, @@ -261,6 +263,7 @@ builder_constructors! { overflow: String, focusable: String, margin: String, + opacity: String, }; text { color: String, @@ -291,6 +294,7 @@ builder_constructors! { alt: String, name: String, focusable: String, + opacity: String, }; svg { margin: String, @@ -304,6 +308,7 @@ builder_constructors! { alt: String, name: String, focusable: String, + opacity: String, }; } diff --git a/crates/engine/src/mocked.rs b/crates/engine/src/mocked.rs index b8e3eca08..dcab75fe6 100644 --- a/crates/engine/src/mocked.rs +++ b/crates/engine/src/mocked.rs @@ -1003,6 +1003,10 @@ impl Canvas { pub fn draw_circle(&self, _center: impl Into, _radius: f32, _paint: &Paint) -> &Self { unimplemented!("This is mocked") } + + pub fn save_layer_alpha_f(&self, bounds: impl Into>, alpha: f32) -> usize { + unimplemented!("This is mocked") + } } #[repr(i32)] diff --git a/crates/renderer/src/renderer.rs b/crates/renderer/src/renderer.rs index b4b600199..6e6ebc131 100644 --- a/crates/renderer/src/renderer.rs +++ b/crates/renderer/src/renderer.rs @@ -5,7 +5,7 @@ use dioxus_native_core::NodeId; use freya_core::prelude::*; use freya_dom::prelude::DioxusNode; use freya_engine::prelude::*; -use freya_node_state::Transform; +use freya_node_state::{Style, Transform}; use torin::geometry::Area; use crate::elements::{render_image, render_label, render_paragraph, render_rect, render_svg}; @@ -33,13 +33,16 @@ pub fn render_skia( viewports: &Viewports, render_wireframe: bool, matrices: &mut Vec<(Matrix, Vec)>, + opacities: &mut Vec<(f32, Vec)>, ) { let node_type = &*dioxus_node.node_type(); if let NodeType::Element(ElementNode { tag, .. }) = node_type { canvas.save(); let node_transform = &*dioxus_node.get::().unwrap(); + let node_style = &*dioxus_node.get::