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

refactor(core): 💡 skip paint for out of bounds widgets #677

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he
### Changed

- **core**: Optimize QueryId::is_same by not creating a String using format for every comparison (#678 @tashcan)
- **core**: Skip paint for out of bounds widgets (#677 @tashcan)

## [0.4.0-alpha.19] - 2024-12-18

Expand Down
35 changes: 28 additions & 7 deletions core/src/widget_tree/widget_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{convert::Infallible, rc::Rc};

use ahash::HashSetExt;
use indextree::{Node, NodeId};
use rxrust::ops::box_it::CloneableBoxOp;
use smallvec::smallvec;
Expand Down Expand Up @@ -289,21 +290,38 @@ impl WidgetId {

pub(crate) fn paint_subtree(self, ctx: &mut PaintingCtx) {
let mut w = Some(self);

let wnd = ctx.window();
let tree = wnd.tree();

let mut did_paint = HashSet::<WidgetId, ahash::RandomState>::new();
while let Some(id) = w {
ctx.id = id;
ctx.painter.save();
let wnd = ctx.window();
let tree = wnd.tree();

let mut need_paint = false;
if ctx.painter.alpha() != 0. {
if let Some(layout_box) = ctx.box_rect() {
let render = id.assert_get(tree);
let transform = *ctx.painter.transform();
ctx
.painter
.translate(layout_box.min_x(), layout_box.min_y());
render.paint(ctx);
need_paint = true;
if ctx
.painter()
.intersection_paint_bounds(&Rect::from_size(layout_box.size))
.is_some()
{
let render = id.assert_get(tree);
ctx.painter.set_transform(transform);
ctx.painter.save();
ctx
.painter
.translate(layout_box.min_x(), layout_box.min_y());
render.paint(ctx);
did_paint.insert(id);
need_paint = true;
} else {
ctx.painter.set_transform(transform);
}
}
}

Expand All @@ -314,7 +332,10 @@ impl WidgetId {
let mut node = w;
while let Some(p) = node {
// self node sub-tree paint finished, goto sibling
ctx.painter.restore();
if did_paint.contains(&p) {
did_paint.remove(&p);
ctx.painter.restore();
}
node = p.next_sibling(tree);
if node.is_some() {
break;
Expand Down
Loading