From d1a44db63f9050d01d94c6d99f73232a79da96bb Mon Sep 17 00:00:00 2001 From: Adoo Date: Thu, 19 Dec 2024 14:19:17 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=F0=9F=90=9B=20padding=20does=20no?= =?UTF-8?q?t=20work=20after=20being=20implemented=20as=20a=20WrapRender?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 +-- core/src/builtin_widgets/padding.rs | 44 ++++++++++-------- .../padding/tests/padding_draw.png | Bin 0 -> 924 bytes 3 files changed, 27 insertions(+), 23 deletions(-) create mode 100644 test_cases/ribir_core/builtin_widgets/padding/tests/padding_draw.png diff --git a/CHANGELOG.md b/CHANGELOG.md index 52056b0c5..48e656565 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he - **core**: Added `Measure` to enable support for percentage values for position, and `Anchor` now supports percentage values. (#672 @M-Adoo) - **core**: Add APIs `AppCtx::once_next_frame`, `Window::once_next_frame`, `Window::once_frame_finished` and `Window::once_before_layout`. (#672 @M-Adoo) -- **painter**: Typography now supports baselines (middle and alphabetic). (#pr @M-Adoo) +- **painter**: Typography now supports baselines (middle and alphabetic). (#674 @M-Adoo) ### Fixed @@ -37,8 +37,8 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he - **core**: Fix TextStyle cause providers mismatched (#671 @wjian23) - **core**: Running an animation that is already in progress does not trigger a smooth transition. (#672 @M-Adoo) - **core**: The framework incorrectly clamps the layout result of the render widget. (#672 @M-Adoo) -- **painter**: Fixed text line height does not work correctly. (#pr @M-Adoo) -- **painter**: Fixed issue with text not being drawn at the middle baseline by default. (#pr @M-Adoo) +- **painter**: Fixed text line height does not work correctly. (#674 @M-Adoo) +- **painter**: Fixed issue with text not being drawn at the middle baseline by default. (#674 @M-Adoo) ## [0.4.0-alpha.19] - 2024-12-18 diff --git a/core/src/builtin_widgets/padding.rs b/core/src/builtin_widgets/padding.rs index 2ca5150b2..21f710017 100644 --- a/core/src/builtin_widgets/padding.rs +++ b/core/src/builtin_widgets/padding.rs @@ -18,31 +18,20 @@ impl WrapRender for Padding { fn perform_layout(&self, clamp: BoxClamp, host: &dyn Render, ctx: &mut LayoutCtx) -> Size { let thickness = self.padding.thickness(); let zero = Size::zero(); - // Reset children position before layout - let (ctx, children) = ctx.split_children(); - for c in children { - ctx.update_position(c, Point::zero()); - } + // Shrink the clamp for child. let min = (clamp.min - thickness).max(zero); let max = (clamp.max - thickness).max(zero); - // Shrink the clamp of child. let child_clamp = BoxClamp { min, max }; - let mut size = host.perform_layout(child_clamp, ctx); + let size = host.perform_layout(child_clamp, ctx); - size = clamp.clamp(size + thickness); - - let (ctx, children) = ctx.split_children(); - // Update the children's positions to ensure they are correctly positioned after - // expansion with padding. - for c in children { - if let Some(pos) = ctx.widget_box_pos(c) { - let pos = pos + Vector::new(self.padding.left, self.padding.top); - ctx.update_position(c, pos); - } - } + clamp.clamp(size + thickness) + } - size + fn paint(&self, host: &dyn Render, ctx: &mut PaintingCtx) { + let EdgeInsets { left, top, .. } = self.padding; + ctx.painter().translate(left, top); + host.paint(ctx); } } @@ -56,7 +45,7 @@ mod tests { use ribir_dev_helper::*; use super::*; - use crate::test_helper::*; + use crate::{reset_test_env, test_helper::*}; widget_layout_test!( smoke, @@ -75,4 +64,19 @@ mod tests { .with_size(Size::new(100., 100.)) .with_x(1.) ); + + #[test] + fn fix_padding_draw() { + reset_test_env!(); + + assert_widget_eq_image!( + WidgetTester::new(text! { + padding: EdgeInsets::all(10.), + background: Color::GREEN, + text: "Hello, Ribir!" + }) + .with_wnd_size(Size::new(128., 48.)), + "padding_draw" + ); + } } diff --git a/test_cases/ribir_core/builtin_widgets/padding/tests/padding_draw.png b/test_cases/ribir_core/builtin_widgets/padding/tests/padding_draw.png new file mode 100644 index 0000000000000000000000000000000000000000..468ce57cbcfab25c4ce34f8da1b88af16f11e3e5 GIT binary patch literal 924 zcmeAS@N?(olHy`uVBq!ia0vp^O+akK!3HEJoMi4`U|>%6ba4!+V0?Rkd4Wpgv|}It zm%r;hab|guck;U2+uNWhvuEBkul4hcjFsN~EZd${wXEGb`|A2v#VPB4<$pe$|J8ZV z-NpC&{-=ek+r_marc^Xw=PRuRMOQ-|o~~Nma4IyMaq8-IOj_ZwtQZPDtpE7^z+9X9 zdVZ5QlkZISDf6C&^Vp~UbN*xd;roI6%;y=-6k9RXU9dSVFIjhIkHhqa_on)j_CK%J zZmRFyZ*V7V-unFpK7YRd=00o*gz^&mu75~>7CDcx=fH{XXYYBQxqcRZRs3M@R6aY&GVxY0GahhmxX=1euA=Z?YB}S3)p<|%vfNLOOWB{uutR#q0qYL&RZRIh zdb{I3upG{piVL@iug|H_|7dgMe^|)Bt#X`ux+>)ECj-NemjOr}-rFWFu7BkKcNs^{ zC;jR*4`d43AKqno)T(W>e0Ia`ii2`zK8yd!cYSJqm~8{+o`pa13vCN*KZHDB=XtO9 zFO=`c_R8{)`$Z1cw}p$|IlJHYJomPf`_DylOMQ@j7XMV~L)N4Jf=&NgL!Yo9&D-DSM^}xpZu0D zmPY??*YrQ!d*nZ>{hu)VBe!jTz5S8z_OyQSccJjV?Td&?q1ck}g)LHNL;v3oBqo_Z bkY_BBJ-OKTb+tDz*D-jy`njxgN@xNAtc}Vp literal 0 HcmV?d00001