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

Add Board widget for absolute positioning #591

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
633 changes: 633 additions & 0 deletions masonry/src/widget/board.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions masonry/src/widget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod widget_state;
mod tests;

mod align;
mod board;
mod button;
mod checkbox;
mod flex;
Expand All @@ -34,6 +35,7 @@ mod widget_arena;

pub use self::image::Image;
pub use align::Align;
pub use board::{Board, BoardParams, KurboShape, Shape};
pub use button::Button;
pub use checkbox::Checkbox;
pub use flex::{Axis, CrossAxisAlignment, Flex, FlexParams, MainAxisAlignment};
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions xilem/examples/board.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

use masonry::{
widget::{CrossAxisAlignment, MainAxisAlignment},
Size,
};
use winit::error::EventLoopError;
use xilem::view::{
board, button, flex, label, Axis, BoardExt, BoardParams, FlexExt as _, FlexSpacer, ShapeExt,
};
use xilem::{Color, EventLoop, WidgetView, Xilem};

struct AppState {
buttons: Vec<bool>,
clicked: Option<usize>,
}

impl AppState {
fn view(&mut self) -> impl WidgetView<Self> {
flex((
FlexSpacer::Fixed(30.0),
flex((
button("B", |state: &mut AppState| state.buttons.push(true)),
button("C", |state: &mut AppState| state.buttons.push(false)),
button("-", |state: &mut AppState| {
state.buttons.pop();
state.clicked = None;
}),
label(self.clicked.map_or_else(
|| String::from("Nothing has been clicked."),
|i| format!("Button {i} has been clicked."),
)),
))
.direction(Axis::Horizontal),
FlexSpacer::Fixed(10.0),
board(
self.buttons
.iter()
.copied()
.enumerate()
.map(|(i, is_button)| {
let origin = i as f64 * 15. + 10.;
let size = Size::new(30., 30.);
if is_button {
button(i.to_string(), move |state: &mut AppState| {
state.clicked = Some(i);
})
.positioned(BoardParams::new((origin, origin), size))
.into_any_board()
} else {
vello::kurbo::Circle::new((origin + 15., origin + 15.), 15.)
.view()
.fill_brush(Color::NAVY)
.stroke_brush(Color::PAPAYA_WHIP)
.stroke_style(vello::kurbo::Stroke::new(2.))
.into_any_board()
}
})
.collect::<Vec<_>>(),
)
.flex(1.),
))
.direction(Axis::Vertical)
.cross_axis_alignment(CrossAxisAlignment::Center)
.main_axis_alignment(MainAxisAlignment::Center)
}
}

fn main() -> Result<(), EventLoopError> {
let app = Xilem::new(
AppState {
buttons: Vec::new(),
clicked: None,
},
AppState::view,
);
app.run_windowed(EventLoop::with_user_event(), "Board".into())?;
Ok(())
}
Loading