Skip to content

Commit

Permalink
chore: Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Aug 12, 2024
1 parent 72b8dbd commit c3fff31
Show file tree
Hide file tree
Showing 5 changed files with 559 additions and 48 deletions.
36 changes: 17 additions & 19 deletions crates/common/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ use freya_engine::prelude::{
Surface,
};
use freya_native_core::NodeId;
use itertools::{
sorted,
Itertools,
use itertools::sorted;
use rustc_hash::{
FxHashMap,
FxHashSet,
};
use rustc_hash::FxHashMap;

use crate::Layers;

Expand Down Expand Up @@ -71,27 +71,25 @@ impl Compositor {
layers: &Layers,
) {
let mut dirty_nodes = dirty_nodes.get();
let dirty_nodes = dirty_nodes.drain().collect::<Vec<(NodeId, DirtyTarget)>>();
let mut invalidated_nodes = Vec::new();

let mut invalidated_nodes_buffer = dirty_nodes.clone();
let (mut invalidated_nodes, mut dirty_nodes) = {
(
FxHashSet::from_iter(dirty_nodes.keys().copied()),
dirty_nodes.drain().collect::<Vec<(NodeId, DirtyTarget)>>(),
)
};

// Mark children
while let Some((node_id, target)) = invalidated_nodes_buffer.pop() {
while let Some((node_id, target)) = dirty_nodes.pop() {
// Mark this node as invalidated
invalidated_nodes.insert(node_id);

let traverse_children = target == DirtyTarget::ItselfAndNested;
let affected = get_affected(node_id, traverse_children)
.into_iter()
.filter(|id| !invalidated_nodes.contains(id))
.collect_vec();

// Save this node
invalidated_nodes.push(node_id);

// Save the affected nodes
invalidated_nodes.extend(affected.clone());
.filter(|id| !invalidated_nodes.contains(id));

// Continue searching in the affected nodes
invalidated_nodes_buffer.extend(
dirty_nodes.extend(
affected
.into_iter()
.map(|id| (id, DirtyTarget::ItselfAndNested)),
Expand Down Expand Up @@ -126,7 +124,7 @@ impl Compositor {
}
}

self.full_render.store(false, Ordering::Relaxed)
self.full_render.store(false, Ordering::Relaxed);
}

pub fn reset_invalidated_layers(&self) {
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ pub fn process_render(
layer_canvas.clear(Color::TRANSPARENT);

let nodes = layers.get(&layer).unwrap();

'elements: for node_id in nodes {
let node = rdom.get(*node_id).unwrap();
let node_viewports = node.get::<ViewportState>().unwrap();
Expand All @@ -93,13 +92,11 @@ pub fn process_render(
continue 'elements;
}
}

// Render the element
render_fn(fdom, node_id, layout_node, &layout, layer_canvas);
}
}
}

layer_surface.draw(canvas, IPoint::new(0, 0), SamplingOptions::default(), None);
}

Expand Down
7 changes: 6 additions & 1 deletion crates/state/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use freya_common::ParagraphElements;
use freya_common::{
CompositorDirtyNodes,
ParagraphElements,
};
use freya_engine::prelude::*;
use freya_native_core::{
attributes::AttributeName,
Expand Down Expand Up @@ -144,6 +147,7 @@ impl State<CustomAttributeValues> for CursorState {
context: &SendAnyMap,
) -> bool {
let paragraphs = context.get::<ParagraphElements>().unwrap();
let compositor_dirty_nodes = context.get::<CompositorDirtyNodes>().unwrap();
let mut cursor = parent.map(|(p,)| p.clone()).unwrap_or_default();

if let Some(attributes) = node_view.attributes() {
Expand All @@ -159,6 +163,7 @@ impl State<CustomAttributeValues> for CursorState {
paragraphs.insert_paragraph(node_view.node_id(), cursor_ref.text_id)
}
}
compositor_dirty_nodes.invalidate(node_view.node_id());
}

*self = cursor;
Expand Down
50 changes: 29 additions & 21 deletions examples/corner_radius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,41 @@
use freya::prelude::*;

fn main() {
launch(app);
launch_cfg(
app,
LaunchConfig::<()>::new()
.with_title("Performance Overlay Plugin")
.with_size(700., 500.)
.with_plugin(PerformanceOverlayPlugin::default()),
)
}

fn app() -> Element {
let mut radius = use_signal(|| 30f32);

let onwheel = move |e: WheelEvent| {
let y = e.get_delta_y() as f32;
radius.with_mut(|radius| *radius = (*radius + y).clamp(0.0, 300.0));
};
let mut count = use_signal(|| 0);

rsx!(
rect {
overflow: "clip",
height: "100%",
height: "50%",
width: "100%",
main_align: "center",
cross_align: "center",
background: "rgb({count}, 119, 182)",
color: "white",
shadow: "0 4 20 5 rgb(0, 0, 0, 80)",
label {
font_size: "75",
font_weight: "bold",
"{count}"
}
}
rect {
height: "50%",
width: "100%",
padding: "60",
onwheel: onwheel,
rect {
shadow: "0 0 25 0 rgb(0, 0, 0, 170)",
corner_radius: "{radius} {radius * 0.7} {radius * 0.4} {radius * 0.2}",
corner_smoothing: "100%",
height: "100%",
width: "100%",
background: "red",
border: "7 solid white",
border_align: "outer"
main_align: "center",
cross_align: "center",
direction: "horizontal",
Button {
onclick: move |_| count += 1,
label { "Increase" }
}
}
)
Expand Down
Loading

0 comments on commit c3fff31

Please sign in to comment.