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
Show file tree
Hide file tree
Changes from 4 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
632 changes: 632 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, ConcreteShape, KurboShape};
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.
27 changes: 27 additions & 0 deletions masonry/src/widget/widget_pod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ impl<W: Widget> WidgetPod<W> {
pub fn id(&self) -> WidgetId {
self.id
}

/// Take the inner widget, if it has not been inserted yet.
pub fn inner(self) -> Option<W> {
if let WidgetPodInner::Created(w) = self.inner {
Some(w)
} else {
None
}
}

/// Get access to the inner widget, if it has not been inserted yet.
pub fn as_ref(&self) -> Option<&W> {
if let WidgetPodInner::Created(w) = &self.inner {
Some(w)
} else {
None
}
}

/// Get access to the inner widget, if it has not been inserted yet.
pub fn as_mut(&mut self) -> Option<&mut W> {
if let WidgetPodInner::Created(w) = &mut self.inner {
Some(w)
} else {
None
}
}
}

impl<W: Widget + 'static> WidgetPod<W> {
Expand Down
2 changes: 1 addition & 1 deletion xilem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ crate-type = ["cdylib"]
workspace = true

[dependencies]
xilem_core.workspace = true
xilem_core = { workspace = true, features = ["kurbo"] }
masonry.workspace = true
winit.workspace = true
tracing.workspace = true
Expand Down
78 changes: 78 additions & 0 deletions xilem/examples/board.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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, GraphicsExt,
};
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.)
.fill(Color::NAVY)
.stroke(Color::PAPAYA_WHIP, 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
Loading