Skip to content

Commit

Permalink
added stack widget
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbox-irl committed Sep 1, 2024
1 parent 114d0bc commit 2bb91bc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
9 changes: 7 additions & 2 deletions crates/yakui-widgets/src/shorthand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::widgets::{
ConstrainedBoxResponse, CountGrid, Divider, DividerResponse, Draggable, DraggableResponse,
Flexible, FlexibleResponse, Image, ImageResponse, List, ListResponse, MaxWidth,
MaxWidthResponse, NineSlice, Offset, OffsetResponse, Opaque, OpaqueResponse, Pad, PadResponse,
Reflow, ReflowResponse, Scrollable, ScrollableResponse, Slider, SliderResponse, Spacer, State,
StateResponse, Text, TextBox, TextBoxResponse, TextResponse,
Reflow, ReflowResponse, Scrollable, ScrollableResponse, Slider, SliderResponse, Spacer, Stack,
StackResponse, State, StateResponse, Text, TextBox, TextBoxResponse, TextResponse,
};

/// See [List].
Expand Down Expand Up @@ -192,6 +192,11 @@ pub fn max_width(max_width: f32, children: impl FnOnce()) -> Response<MaxWidthRe
MaxWidth::new(max_width).show(children)
}

/// See [Stack].
pub fn stack(children: impl FnOnce()) -> Response<StackResponse> {
Stack::new().show(children)
}

pub fn use_state<F, T: 'static>(default: F) -> Response<StateResponse<T>>
where
F: FnOnce() -> T + 'static,
Expand Down
2 changes: 2 additions & 0 deletions crates/yakui-widgets/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mod round_rect;
mod scrollable;
mod slider;
mod spacer;
mod stack;
mod state;
mod text;
mod textbox;
Expand Down Expand Up @@ -60,6 +61,7 @@ pub use self::round_rect::*;
pub use self::scrollable::*;
pub use self::slider::*;
pub use self::spacer::*;
pub use self::stack::*;
pub use self::state::*;
pub use self::text::*;
pub use self::textbox::*;
Expand Down
62 changes: 62 additions & 0 deletions crates/yakui-widgets/src/widgets/stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use yakui_core::{widget::Widget, Response};

use crate::util::widget_children;

/**
A [Stack] widget. This widget does nothing interesting on its own, but
when used "inside" other layouts, such as [List](crate::widgets::List),
it will stacks its own children, rather than following the layout of its own parent.
This internal layouting is just using yakui's default layout algorithm.
Responds with [StackResponse].
Shorthand:
```rust
# let _handle = yakui_widgets::DocTest::start();
yakui::column(|| {
yakui::label("on top");
yakui::stack(|| {
// this would compose, by being stacked,
// to appear like "hello world", barring issues from spacing,
// as opposed to continuing the columnar layout setup above.
yakui::text(12, "hello");
yakui::text(12, " world");
});
yakui::label("on bottom");
});
```
*/
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct Stack {}

impl Stack {
/// Creates a new [Stack].
pub fn new() -> Self {
Self {}
}

/// Shows the [Stack] along with its children.
pub fn show<F: FnOnce()>(self, children: F) -> Response<StackResponse> {
widget_children::<StackWidget, F>(children, self)
}
}

#[derive(Debug)]
pub struct StackWidget;

pub type StackResponse = ();

impl Widget for StackWidget {
type Props<'a> = Stack;

type Response = StackResponse;

fn new() -> Self {
Self
}

fn update(&mut self, _props: Self::Props<'_>) -> Self::Response {
// nothing here
}
}

0 comments on commit 2bb91bc

Please sign in to comment.