Skip to content

Commit

Permalink
feat: static_bytes_to_data (#505)
Browse files Browse the repository at this point in the history
* feat: static_bytes_to_data

* fmt
  • Loading branch information
marc2332 authored Feb 13, 2024
1 parent c7ce6bc commit d0ea546
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/elements/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ builder_constructors! {
/// static RUST_LOGO: &[u8] = include_bytes!("./rust_logo.png");
///
/// fn app() -> Element {
/// let image_data = bytes_to_data(RUST_LOGO);
/// let image_data = static_bytes_to_data(RUST_LOGO);
/// rsx!(
/// image {
/// image_data: image_data,
Expand Down
2 changes: 1 addition & 1 deletion crates/freya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub mod prelude {
pub use freya_elements::elements as dioxus_elements;
pub use freya_elements::events::*;
pub use freya_hooks::*;
pub use freya_node_state::{bytes_to_data, CustomAttributeValues};
pub use freya_node_state::{bytes_to_data, static_bytes_to_data, CustomAttributeValues};
pub use freya_renderer::*;
pub use torin::prelude::*;
}
2 changes: 1 addition & 1 deletion crates/renderer/src/elements/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ pub fn render_image(area: &Area, node_ref: &DioxusNode, canvas: &Canvas) {
draw_img(image_data)
}
} else if let Some(image_data) = &node_style.image_data {
draw_img(image_data)
draw_img(image_data.as_slice())
}
}
2 changes: 1 addition & 1 deletion crates/renderer/src/elements/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn render_svg(area: &Area, node_ref: &DioxusNode, canvas: &Canvas) {
let x = area.min_x();
let y = area.min_y();
if let Some(svg_data) = &node_style.svg_data {
let svg_dom = svg::Dom::from_bytes(svg_data);
let svg_dom = svg::Dom::from_bytes(svg_data.as_slice());
if let Ok(mut svg_dom) = svg_dom {
canvas.save();
canvas.translate((x, y));
Expand Down
26 changes: 24 additions & 2 deletions crates/state/src/custom_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,27 @@ impl Display for CursorReference {
}
}

#[derive(Debug, Clone, PartialEq)]
pub enum AttributesBytes {
Dynamic(Arc<Vec<u8>>),
Static(&'static [u8]),
}

impl AttributesBytes {
pub fn as_slice(&self) -> &[u8] {
match self {
Self::Dynamic(bytes) => bytes.as_slice(),
Self::Static(bytes) => bytes,
}
}
}

/// Group all the custom attribute types
#[derive(Clone, PartialEq)]
pub enum CustomAttributeValues {
Reference(NodeReference),
CursorReference(CursorReference),
Bytes(Vec<u8>),
Bytes(AttributesBytes),
ImageReference(ImageReference),
AccessibilityId(AccessibilityId),
TextHighlights(Vec<(usize, usize)>),
Expand Down Expand Up @@ -138,5 +153,12 @@ impl FromAnyValue for CustomAttributeValues {

/// Transform some bytes (e.g: raw image, raw svg) into attribute data
pub fn bytes_to_data(bytes: &[u8]) -> AttributeValue {
AttributeValue::any_value(CustomAttributeValues::Bytes(bytes.to_vec()))
AttributeValue::any_value(CustomAttributeValues::Bytes(AttributesBytes::Dynamic(
Arc::new(bytes.to_vec()),
)))
}

/// Transform some static bytes (e.g: raw image, raw svg) into attribute data
pub fn static_bytes_to_data(bytes: &'static [u8]) -> AttributeValue {
AttributeValue::any_value(CustomAttributeValues::Bytes(AttributesBytes::Static(bytes)))
}
13 changes: 8 additions & 5 deletions crates/state/src/style.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use dioxus_native_core::{
exports::shipyard::Component,
node::OwnedAttributeValue,
Expand All @@ -9,8 +11,8 @@ use dioxus_native_core_macro::partial_derive_state;
use torin::scaled::Scaled;

use crate::{
parsing::ExtSplit, Border, BorderAlignment, CornerRadius, CustomAttributeValues, Fill,
OverflowMode, Parse, Shadow,
parsing::ExtSplit, AttributesBytes, Border, BorderAlignment, CornerRadius,
CustomAttributeValues, Fill, OverflowMode, Parse, Shadow,
};

#[derive(Default, Debug, Clone, PartialEq, Component)]
Expand All @@ -20,8 +22,8 @@ pub struct Style {
pub border: Border,
pub shadows: Vec<Shadow>,
pub corner_radius: CornerRadius,
pub image_data: Option<Vec<u8>>,
pub svg_data: Option<Vec<u8>>,
pub image_data: Option<AttributesBytes>,
pub svg_data: Option<AttributesBytes>,
pub overflow: OverflowMode,
pub opacity: Option<f32>,
}
Expand Down Expand Up @@ -142,7 +144,8 @@ impl State<CustomAttributeValues> for Style {
}
"svg_content" => {
let text = attr.value.as_text();
style.svg_data = text.map(|v| v.as_bytes().to_owned());
style.svg_data = text
.map(|v| AttributesBytes::Dynamic(Arc::new(v.as_bytes().to_owned())));
}
"overflow" => {
if let Some(value) = attr.value.as_text() {
Expand Down
2 changes: 1 addition & 1 deletion examples/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
static RUST_LOGO: &[u8] = include_bytes!("./rust_logo.png");

fn app() -> Element {
let image_data = bytes_to_data(RUST_LOGO);
let image_data = static_bytes_to_data(RUST_LOGO);
let mut size = use_signal(|| 250);

let onwheel = move |e: WheelEvent| {
Expand Down
2 changes: 1 addition & 1 deletion examples/opacity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
}

fn app() -> Element {
let ferris = bytes_to_data(FERRIS);
let ferris = static_bytes_to_data(FERRIS);
let mut opacity = use_signal(|| 70.0);

rsx!(
Expand Down
2 changes: 1 addition & 1 deletion examples/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() {
static FERRIS: &[u8] = include_bytes!("./ferris.svg");

fn app() -> Element {
let ferris_a = bytes_to_data(FERRIS);
let ferris_a = static_bytes_to_data(FERRIS);
let ferris_b = bytes_to_data(FERRIS);
rsx!(
svg {
Expand Down

0 comments on commit d0ea546

Please sign in to comment.