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

Grid layout #570

Merged
merged 25 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a8d2cc5
Initial grid layout
jaredoconnell Aug 20, 2024
0425d56
Completed minimum viable grid
jaredoconnell Aug 21, 2024
ada6816
Optimize layout calls
jaredoconnell Aug 22, 2024
583b24c
Fix error due to layout run needed
jaredoconnell Aug 22, 2024
dc604f9
Update inputs to grid layout functions
jaredoconnell Sep 2, 2024
9b9ecb5
Added grid to Xilem, and made calc example use grid
jaredoconnell Sep 3, 2024
b99b8ef
Remove completed TODO
jaredoconnell Sep 3, 2024
7bdab6b
Update to address changes to main
jaredoconnell Sep 3, 2024
bff8b76
Apply suggestions from code review
jaredoconnell Sep 3, 2024
b4e40b3
Address review comments
jaredoconnell Sep 3, 2024
e437cb3
Address linter errors
jaredoconnell Sep 3, 2024
556a145
Address example linter errors
jaredoconnell Sep 3, 2024
e541c64
Commit change that wasn't added
jaredoconnell Sep 3, 2024
19a0761
Improve documentation for Grid
jaredoconnell Sep 5, 2024
6430d8b
Fixed clippy linter error
jaredoconnell Sep 5, 2024
edeb58a
Added tests to grid widget
jaredoconnell Sep 8, 2024
11c1fef
Fix linter errors
jaredoconnell Sep 8, 2024
cd44ea5
Apply suggestions from code review
jaredoconnell Sep 11, 2024
aa107f0
Remove commented out layout caching
jaredoconnell Sep 11, 2024
9912f05
Reorder code and fix formatting
jaredoconnell Sep 11, 2024
9bba00c
Merge branch 'main' into grid-layout
jaredoconnell Sep 11, 2024
dec9ff7
Update to layout pass spec
jaredoconnell Sep 11, 2024
cb8e984
Update grid example based on feedback
jaredoconnell Sep 11, 2024
5bab9c7
Address review comments
jaredoconnell Sep 11, 2024
b5858a1
Fix formatting
jaredoconnell Sep 11, 2024
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
121 changes: 121 additions & 0 deletions masonry/examples/grid_masonry.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example works well. The UX pattern shown is a bit weird, but it achieves the aims.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. The way I did it in the example isn't intended for typical use. Probably just spreadsheets and things like calc, where I had the top flex span all four cells.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

//! Shows how to use a grid layout in Masonry.

// On Windows platform, don't show a console when opening the app.
#![windows_subsystem = "windows"]

use masonry::app_driver::{AppDriver, DriverCtx};
use masonry::dpi::LogicalSize;
use masonry::widget::{Button, Grid, GridParams, Prose, RootWidget, SizedBox};
use masonry::{Action, Color, PointerButton, WidgetId};
use parley::layout::Alignment;
use winit::window::Window;

struct Driver {
grid_spacing: f64,
}

impl AppDriver for Driver {
fn on_action(&mut self, ctx: &mut DriverCtx<'_>, _widget_id: WidgetId, action: Action) {
if let Action::ButtonPressed(button) = action {
if button == PointerButton::Primary {
self.grid_spacing += 1.0;
} else if button == PointerButton::Secondary {
self.grid_spacing -= 1.0;
} else {
self.grid_spacing += 0.5;
}

ctx.get_root::<RootWidget<Grid>>()
.get_element()
.set_spacing(self.grid_spacing);
}
}
}

fn grid_button(params: GridParams) -> Button {
Button::new(format!(
"X: {}, Y: {}, W: {}, H: {}",
params.x, params.y, params.width, params.height
))
}

pub fn main() {
let label = SizedBox::new(
Prose::new("Change spacing by right and left clicking on the buttons")
.with_text_size(14.0)
.with_text_alignment(Alignment::Middle),
)
.border(Color::rgb8(40, 40, 80), 1.0);
let button_inputs = vec![
GridParams {
x: 0,
y: 0,
width: 1,
height: 1,
},
GridParams {
x: 2,
y: 0,
width: 2,
height: 1,
},
GridParams {
x: 0,
y: 1,
width: 1,
height: 2,
},
GridParams {
x: 1,
y: 1,
width: 2,
height: 2,
},
GridParams {
x: 3,
y: 1,
width: 1,
height: 1,
},
GridParams {
x: 3,
y: 2,
width: 1,
height: 1,
},
GridParams {
x: 0,
y: 3,
width: 4,
height: 1,
},
];

let driver = Driver { grid_spacing: 1.0 };

// Arrange widgets in a 4 by 4 grid.
let mut main_widget = Grid::with_dimensions(4, 4)
.with_spacing(driver.grid_spacing)
.with_child(label, GridParams::new(1, 0, 1, 1));
for button_input in button_inputs {
let button = grid_button(button_input);
main_widget = main_widget.with_child(button, button_input);
}

let window_size = LogicalSize::new(800.0, 500.0);
let window_attributes = Window::default_attributes()
.with_title("Grid Layout")
.with_resizable(true)
.with_min_inner_size(window_size);

masonry::event_loop_runner::run(
masonry::event_loop_runner::EventLoop::with_user_event(),
window_attributes,
RootWidget::new(main_widget),
driver,
)
.unwrap();
}
2 changes: 1 addition & 1 deletion masonry/src/widget/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ impl<'a> WidgetMut<'a, Flex> {
///
/// # Panics
///
/// Panics if the the element at `idx` is not a widget.
/// Panics if the element at `idx` is not a widget.
pub fn update_child_flex_params(&mut self, idx: usize, params: impl Into<FlexParams>) {
let child = &mut self.widget.children[idx];
let child_val = std::mem::replace(child, Child::FixedSpacer(0.0, 0.0));
Expand Down
Loading