Skip to content

Commit

Permalink
feat: consider viewports when expanding the dirty area
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Aug 23, 2024
1 parent 1e4b200 commit bdced12
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
40 changes: 34 additions & 6 deletions crates/core/src/elements/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ use freya_engine::prelude::{
use freya_native_core::{
prelude::NodeImmutable,
tags::TagName,
NodeId,
};
use freya_node_state::TransformState;
use torin::prelude::{
Area,
AreaModel,
CursorPoint,
LayoutNode,
use freya_node_state::{
TransformState,
ViewportState,
};
use torin::{
prelude::{
Area,
AreaModel,
CursorPoint,
LayoutNode,
},
torin::Torin,
};

use super::*;
Expand Down Expand Up @@ -60,6 +67,27 @@ pub trait ElementUtils {
layout_node.visible_area()
}

fn drawing_area_with_viewports(
&self,
layout_node: &LayoutNode,
node_ref: &DioxusNode,
layout: &Torin<NodeId>,
scale_factor: f32,
) -> Option<Area> {
let mut drawing_area = self.drawing_area(layout_node, node_ref, scale_factor);
let node_viewports = node_ref.get::<ViewportState>().unwrap();

for viewport_id in &node_viewports.viewports {
let viewport = layout.get(*viewport_id).unwrap().visible_area();
drawing_area.clip(&viewport);
if !viewport.intersects(&drawing_area) {
return None;
}
}

Some(drawing_area)
}

/// Measure the area for this element considering other
/// factors like shadows or borders, which are not part of the layout.
fn drawing_area(
Expand Down
11 changes: 9 additions & 2 deletions crates/core/src/render/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Compositor {
let node = rdom.get(node_id)?;
let utils = node.node_type().tag()?.utils()?;

Some(utils.drawing_area(layout_node, &node, scale_factor))
utils.drawing_area_with_viewports(layout_node, &node, layout, scale_factor)
}

#[inline]
Expand Down Expand Up @@ -182,7 +182,14 @@ impl Compositor {
let cached_area = cache.get(node_id);
let needs_cached_area = utils.needs_cached_area(&node_ref);

let area = utils.drawing_area(layout_node, &node_ref, scale_factor);
let Some(area) = utils.drawing_area_with_viewports(
layout_node,
&node_ref,
layout,
scale_factor,
) else {
return false;
};

let is_dirty = dirty_nodes.remove(node_id);
let cached_area_is_invalidated = cached_area
Expand Down
3 changes: 2 additions & 1 deletion crates/native-core/src/real_dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ impl<V: FromAnyValue + Send + Sync> RealDom<V> {
}

/// Borrow a component from the world without updating the dirty nodes.
#[inline(always)]
fn borrow_raw<'a, B: IntoBorrow>(&'a self) -> Result<B, GetStorage>
where
B::Borrow: shipyard::Borrow<'a, View = B>,
Expand Down Expand Up @@ -458,7 +459,7 @@ pub trait NodeImmutable<V: FromAnyValue + Send + Sync = ()>: Sized {
}

/// Get a component from the current node
#[inline]
#[inline(always)]
fn get<'a, T: Component + Sync + Send>(&'a self) -> Option<ViewEntry<'a, T>> {
// self.real_dom().tree.get(self.id())
let view: View<'a, T> = self.real_dom().borrow_raw().ok()?;
Expand Down
9 changes: 9 additions & 0 deletions crates/torin/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub trait AreaModel {
fn expand(&mut self, size: &Size2D);

fn max_area_when_rotated(&self, center: Point2D) -> Area;

fn clip(&mut self, other: &Self);
}

impl AreaModel for Area {
Expand Down Expand Up @@ -202,6 +204,13 @@ impl AreaModel for Area {
),
)
}

fn clip(&mut self, other: &Self) {
self.origin.x = self.origin.x.max(other.origin.x);
self.origin.y = self.origin.y.max(other.origin.y);
self.size.width = self.size.width.min(other.size.width);
self.size.height = self.size.height.min(other.size.height);
}
}

pub fn get_align_axis(
Expand Down
4 changes: 3 additions & 1 deletion examples/floating_editors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ fn app() -> Element {
rect {
key: "{id}",
direction: "horizontal",
width: "0",
height: "0",
rect {
offset_x: "{node.0}",
offset_y: "{node.1}",
Expand Down Expand Up @@ -336,7 +338,7 @@ fn Editor() -> Element {
}
paragraph {
height: "100%",
width: "100%",
width: "fill",
main_align: "center",
cursor_index: "{character_index}",
cursor_color: "white",
Expand Down

0 comments on commit bdced12

Please sign in to comment.