-
Notifications
You must be signed in to change notification settings - Fork 117
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
Grid layout #570
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
a8d2cc5
Initial grid layout
jaredoconnell 0425d56
Completed minimum viable grid
jaredoconnell ada6816
Optimize layout calls
jaredoconnell 583b24c
Fix error due to layout run needed
jaredoconnell dc604f9
Update inputs to grid layout functions
jaredoconnell 9b9ecb5
Added grid to Xilem, and made calc example use grid
jaredoconnell b99b8ef
Remove completed TODO
jaredoconnell 7bdab6b
Update to address changes to main
jaredoconnell bff8b76
Apply suggestions from code review
jaredoconnell b4e40b3
Address review comments
jaredoconnell e437cb3
Address linter errors
jaredoconnell 556a145
Address example linter errors
jaredoconnell e541c64
Commit change that wasn't added
jaredoconnell 19a0761
Improve documentation for Grid
jaredoconnell 6430d8b
Fixed clippy linter error
jaredoconnell edeb58a
Added tests to grid widget
jaredoconnell 11c1fef
Fix linter errors
jaredoconnell cd44ea5
Apply suggestions from code review
jaredoconnell aa107f0
Remove commented out layout caching
jaredoconnell 9912f05
Reorder code and fix formatting
jaredoconnell 9bba00c
Merge branch 'main' into grid-layout
jaredoconnell dec9ff7
Update to layout pass spec
jaredoconnell cb8e984
Update grid example based on feedback
jaredoconnell 5bab9c7
Address review comments
jaredoconnell b5858a1
Fix formatting
jaredoconnell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.