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: Improve and optimize elements clipping #353

Merged
merged 31 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c9115fa
feat: WIP Enhanced alignments
marc2332 Oct 14, 2023
5d67298
improvements
marc2332 Oct 14, 2023
c926baa
tweak
marc2332 Oct 14, 2023
74544bf
remove display attribute, remove 'both' direction, fix tests, fix doc…
marc2332 Oct 14, 2023
2cfc46a
feat: Updated examples
marc2332 Oct 15, 2023
736dda1
tests
marc2332 Oct 15, 2023
da4a1a2
fixed tests
marc2332 Oct 15, 2023
1bc2a53
Merge branch 'main' into feat/enhanced-alignments
marc2332 Oct 16, 2023
3d49e1d
update table component and example
marc2332 Oct 16, 2023
bb1de28
clean up
marc2332 Oct 17, 2023
981fe9a
clean up
marc2332 Oct 17, 2023
dcc3c57
improvements
marc2332 Oct 18, 2023
60e0ebf
improvements
marc2332 Oct 19, 2023
2e8056a
fixes
marc2332 Oct 19, 2023
f9e43b0
fix tests
marc2332 Oct 19, 2023
e9acaa3
tweak
marc2332 Oct 19, 2023
6503c07
tweaks
marc2332 Oct 20, 2023
2419b51
unsized alignment test
marc2332 Oct 21, 2023
9645714
document code
marc2332 Oct 21, 2023
bbf6c20
tweak
marc2332 Oct 21, 2023
00455d0
rename attributes
marc2332 Oct 22, 2023
d6e548f
fixes and cleanup
marc2332 Oct 22, 2023
24b22ce
Merge branch 'main' into feat/enhanced-alignments
marc2332 Oct 22, 2023
b0194ef
fixes and improvements
marc2332 Oct 22, 2023
3e8c68a
Merge branch 'feat/enhanced-alignments' of https://github.com/marc233…
marc2332 Oct 22, 2023
e7fb53f
Merge branch 'main' into feat/enhanced-alignments
marc2332 Oct 22, 2023
19b61aa
typo
marc2332 Oct 22, 2023
04bba37
feat: Improve and optimize elements clipping
marc2332 Oct 22, 2023
b2ae311
improvements
marc2332 Oct 24, 2023
a3b3753
Merge branch 'main' into feat/improve-and-optimize-elements-clipping
marc2332 Oct 26, 2023
fe81183
fix
marc2332 Oct 26, 2023
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
24 changes: 16 additions & 8 deletions crates/core/src/viewports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,30 @@ pub fn calculate_viewports(

// Clip any overflow from it's children
if style.overflow == OverflowMode::Clip {
viewports_collection
let viewport = viewports_collection
.entry(*node_id)
.or_insert_with(|| (None, Vec::new()))
.0 = Some(node_areas.area);
.or_insert_with(|| (None, Vec::new()));
viewport.0 = Some(node_areas.visible_area());
}

// Pass viewports to the children
if let Some((_, mut inherited_viewports)) =
viewports_collection.get(node_id).cloned()
{
// Add the itself
inherited_viewports.push(*node_id);
// Only pass the inherited viewports if they are not empty
// or this same element has a clipped overflow
if !inherited_viewports.is_empty() || style.overflow == OverflowMode::Clip {
// Add itself
inherited_viewports.push(*node_id);

for child in node.children() {
viewports_collection
.insert(child.id(), (None, inherited_viewports.clone()));
for child in node.children() {
if let NodeType::Element(..) = *child.node_type() {
viewports_collection
.entry(child.id())
.or_insert_with(|| (None, Vec::new()))
.1 = inherited_viewports.clone();
}
}
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions crates/renderer/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ use torin::geometry::Area;

use crate::elements::{render_image, render_label, render_paragraph, render_rect, render_svg};

fn clip_viewport(canvas: &Canvas, viewport: &Area) {
canvas.clip_rect(
Rect::new(
viewport.min_x(),
viewport.min_y(),
viewport.max_x(),
viewport.max_y(),
),
ClipOp::Intersect,
true,
);
}

/// Render a node into the Skia canvas
#[allow(clippy::too_many_arguments)]
pub fn render_skia(
Expand Down Expand Up @@ -53,20 +66,19 @@ pub fn render_skia(
let viewports = viewports_collection.get(&dioxus_node.id());

// Clip all elements with their corresponding viewports
if let Some((_, viewports)) = viewports {
if let Some((element_viewport, viewports)) = viewports {
// Only clip the element iself when it's paragraph because
// it will render the inner text spans on it's own, so if these spans overflow the paragraph,
// It is the paragraph job to make sure they are clipped
if tag.as_str() == "paragraph" {
if let Some(element_viewport) = element_viewport {
clip_viewport(canvas, element_viewport);
}
}
for viewport_id in viewports {
let viewport = viewports_collection.get(viewport_id).unwrap().0;
if let Some(viewport) = viewport {
canvas.clip_rect(
Rect::new(
viewport.min_x(),
viewport.min_y(),
viewport.max_x(),
viewport.max_y(),
),
ClipOp::Intersect,
true,
);
clip_viewport(canvas, &viewport);
}
}
}
Expand Down