Skip to content

Commit

Permalink
Allowing rendering 0-size layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Nov 22, 2024
1 parent 5201e8b commit 57f0c06
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
overflowing math.
- Fonts already loaded into a `FontCollection` when the window is first opened
are now properly loaded.
- Widgets of 0-size are now still rendered, even though all the drawing
operations will be clipped away. This allows the `ComponentProbe` to still
actively probe when placed in a location and given no space.

### Added

Expand Down
17 changes: 17 additions & 0 deletions examples/component-probe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cushy::styles::components::PrimaryColor;
use cushy::widget::MakeWidget;
use cushy::widgets::{ComponentProbe, Space};
use cushy::Run;
use kludgine::Color;

fn main() -> cushy::Result {
let probe = ComponentProbe::new(PrimaryColor, Color::CLEAR_WHITE);

Space::colored(probe.value().clone())
.expand()
.and(probe)
.into_layers()
.contain()
.pad()
.run()
}
5 changes: 1 addition & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,10 +689,7 @@ impl<'context, 'clip, 'gfx, 'pass> GraphicsContext<'context, 'clip, 'gfx, 'pass>
/// Invokes [`Widget::redraw()`](crate::widget::Widget::redraw) on this
/// context's widget.
pub fn redraw(&mut self) {
let Some(layout) = self.last_layout() else {
return;
};
if layout.size.width <= 0 || layout.size.height <= 0 {
if self.last_layout().is_none() {
return;
}

Expand Down
9 changes: 9 additions & 0 deletions src/widgets/component_probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ where
&mut self.child
}

fn adjust_child_constraints(
&mut self,
available_space: figures::Size<crate::ConstraintLimit>,
context: &mut crate::context::LayoutContext<'_, '_, '_, '_>,
) -> figures::Size<crate::ConstraintLimit> {
self.probed.set(context.get(&self.component));
available_space
}

fn redraw_foreground(&mut self, context: &mut crate::context::GraphicsContext<'_, '_, '_, '_>) {
self.probed.set(context.get(&self.component));
}
Expand Down
34 changes: 15 additions & 19 deletions src/widgets/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ impl Stack {

impl Widget for Stack {
fn redraw(&mut self, context: &mut GraphicsContext<'_, '_, '_, '_>) {
for (layout, child) in self.layout.iter().zip(&self.synced_children) {
if layout.size > 0 {
context.for_other(child).redraw();
}
for child in &self.synced_children {
context.for_other(child).redraw();
}
}

Expand Down Expand Up @@ -166,21 +164,19 @@ impl Widget for Stack {
);

for (layout, child) in self.layout.iter().zip(&self.synced_children) {
if layout.size > 0 {
context.set_child_layout(
child,
Rect::new(
self.layout
.orientation
.make_point(layout.offset, UPx::ZERO)
.into_signed(),
self.layout
.orientation
.make_size(layout.size, self.layout.others[0])
.into_signed(),
),
);
}
context.set_child_layout(
child,
Rect::new(
self.layout
.orientation
.make_point(layout.offset, UPx::ZERO)
.into_signed(),
self.layout
.orientation
.make_size(layout.size, self.layout.others[0])
.into_signed(),
),
);
}

content_size
Expand Down

0 comments on commit 57f0c06

Please sign in to comment.