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

Replace BoxConstraints and update Layout function #701

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 9 additions & 4 deletions masonry/examples/calc_masonry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use accesskit::{DefaultActionVerb, NodeBuilder, Role};
use masonry::app_driver::{AppDriver, DriverCtx};
use masonry::dpi::LogicalSize;
use masonry::widget::{Align, CrossAxisAlignment, Flex, Label, RootWidget, SizedBox};
use masonry::widget::{Align, BiAxial, ContentFill, CrossAxisAlignment, Flex, Label, RootWidget, SizedBox};
use masonry::{
AccessCtx, AccessEvent, Action, BoxConstraints, Color, EventCtx, LayoutCtx, PaintCtx, Point,
AccessCtx, AccessEvent, Action, Color, EventCtx, LayoutCtx, PaintCtx, Point,
PointerEvent, RegisterCtx, Size, StatusChange, TextEvent, UpdateCtx, Widget, WidgetId,
WidgetPod,
};
Expand Down Expand Up @@ -213,8 +213,13 @@ impl Widget for CalcButton {
ctx.register_child(&mut self.inner);
}

fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
let size = ctx.run_layout(&mut self.inner, bc);
fn layout(
&mut self,
ctx: &mut LayoutCtx,
available_space: &BiAxial<f64>,
requested_fill: &BiAxial<ContentFill>,
) -> BiAxial<f64> {
let size = ctx.run_layout(&mut self.inner, available_space, requested_fill);
ctx.place_child(&mut self.inner, Point::ORIGIN);

size
Expand Down
32 changes: 18 additions & 14 deletions masonry/examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use accesskit::{NodeBuilder, Role};
use masonry::app_driver::{AppDriver, DriverCtx};
use masonry::kurbo::{BezPath, Stroke};
use masonry::widget::{ObjectFit, RootWidget};
use masonry::widget::{BiAxial, ContentFill, ObjectFit, RootWidget};
use masonry::{
AccessCtx, AccessEvent, Action, Affine, BoxConstraints, Color, EventCtx, LayoutCtx, PaintCtx,
AccessCtx, AccessEvent, Action, Affine, Color, EventCtx, LayoutCtx, PaintCtx,
Point, PointerEvent, Rect, RegisterCtx, Size, StatusChange, TextEvent, UpdateCtx, Widget,
WidgetId,
};
Expand Down Expand Up @@ -43,23 +43,27 @@ impl Widget for CustomWidget {

fn on_status_change(&mut self, _ctx: &mut UpdateCtx, _event: &StatusChange) {}

fn layout(&mut self, _layout_ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
// BoxConstraints are passed by the parent widget.
fn layout(
&mut self,
ctx: &mut LayoutCtx,
available_space: &BiAxial<f64>,
requested_fill: &BiAxial<ContentFill>,
) -> BiAxial<f64> {
// Available space is passed by the parent widget.
// This method can return any Size within those constraints:
// bc.constrain(my_size)
// available_space.constrain(my_size)
//
// To check if a dimension is infinite or not (e.g. scrolling):
// bc.is_width_bounded() / bc.is_height_bounded()
// available_space.is_width_bounded() / available_space.is_height_bounded()
//
// bx.max() returns the maximum size of the widget. Be careful
// using this, since always make sure the widget is bounded.
// If bx.max() is used in a scrolling widget things will probably
// not work correctly.
if bc.is_width_bounded() && bc.is_height_bounded() {
bc.max()
// Be careful using available_space. Always make sure the widget is bounded.
// If the full available space is used in a scrolling widget things will
// probably not work correctly.
if available_space.is_width_bounded() && available_space.is_height_bounded() {
available_space.clone()
} else {
let size = Size::new(100.0, 100.0);
bc.constrain(size)
let size = BiAxial::new(100.0, 100.0);
available_space.constrain(size)
}
}

Expand Down
1 change: 0 additions & 1 deletion masonry/examples/simple_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub fn main() {
let window_size = LogicalSize::new(650.0, 450.0);
let window_attributes = Window::default_attributes()
.with_title("Simple image example")
.with_min_inner_size(window_size)
.with_max_inner_size(window_size);

masonry::event_loop_runner::run(
Expand Down
Loading