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

added stack widget #187

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.0, "hello");
yakui::text(12.0, " 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
}
}
Loading