-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathgrid_masonry.rs
147 lines (130 loc) · 3.92 KB
/
grid_masonry.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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::dpi::LogicalSize;
use masonry::text::StyleProperty;
use masonry::widget::{Button, Grid, GridParams, Prose, RootWidget, SizedBox, TextArea};
use masonry::{Action, AppDriver, Color, DriverCtx, 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.render_root().edit_root_widget(|mut root| {
let mut root = root.downcast::<RootWidget<Grid>>();
let mut grid = RootWidget::child_mut(&mut root);
Grid::set_spacing(&mut grid, self.grid_spacing);
});
}
}
}
fn grid_button(params: GridParams) -> Button {
Button::new(format!(
"X: {}, Y: {}, W: {}, H: {}",
params.x, params.y, params.width, params.height
))
}
fn make_grid(grid_spacing: f64) -> Grid {
let label = SizedBox::new(Prose::from_text_area(
TextArea::new_immutable("Change spacing by right and left clicking on the buttons")
.with_style(StyleProperty::FontSize(14.0))
.with_alignment(Alignment::Middle),
))
.border(Color::from_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,
},
];
// Arrange widgets in a 4 by 4 grid.
let mut main_widget = Grid::with_dimensions(4, 4)
.with_spacing(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);
}
main_widget
}
fn main() {
let driver = Driver { grid_spacing: 1.0 };
let main_widget = make_grid(driver.grid_spacing);
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();
}
// --- MARK: TESTS ---
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;
use masonry::assert_render_snapshot;
use masonry::testing::TestHarness;
use super::*;
#[test]
fn screenshot_test() {
let mut harness = TestHarness::create(make_grid(1.0));
assert_debug_snapshot!(harness.root_widget());
assert_render_snapshot!(harness, "initial_screenshot");
// TODO - Test clicking buttons
}
}