yakui_widgets/
shorthand.rs

1//! Contains terse defaults for the most common widgets.
2//!
3//! Each function in this module is easy to read in order to enable extending a
4//! widget if its defaults don't work for you.
5
6use std::borrow::Cow;
7
8use yakui_core::geometry::{Color, Constraints, Dim2, Vec2};
9use yakui_core::widget::PaintContext;
10use yakui_core::{Alignment, ManagedTextureId, Pivot, Response, TextureId};
11
12use crate::widgets::{
13    Align, AlignResponse, Button, ButtonResponse, Canvas, CanvasResponse, Checkbox,
14    CheckboxResponse, Circle, CircleResponse, ColoredBox, ColoredBoxResponse, ConstrainedBox,
15    ConstrainedBoxResponse, CountGrid, Divider, DividerResponse, Draggable, DraggableResponse,
16    Flexible, FlexibleResponse, Image, ImageResponse, List, ListResponse, MaxWidth,
17    MaxWidthResponse, NineSlice, Offset, OffsetResponse, Opaque, OpaqueResponse, Pad, PadResponse,
18    Reflow, ReflowResponse, Scrollable, ScrollableResponse, Slider, SliderResponse, Spacer, Stack,
19    StackResponse, State, StateResponse, Text, TextBox, TextBoxResponse, TextResponse,
20};
21
22/// See [List].
23pub fn column<F: FnOnce()>(children: F) -> Response<ListResponse> {
24    List::column().show(children)
25}
26
27/// See [List].
28pub fn row<F: FnOnce()>(children: F) -> Response<ListResponse> {
29    List::row().show(children)
30}
31
32/// See [CountGrid].
33pub fn countgrid_column<F: FnOnce()>(n_columns: usize, children: F) -> Response<ListResponse> {
34    CountGrid::col(n_columns).show(children)
35}
36
37/// See [CountGrid].
38pub fn countgrid_row<F: FnOnce()>(n_rows: usize, children: F) -> Response<ListResponse> {
39    CountGrid::row(n_rows).show(children)
40}
41
42/// See [Align].
43pub fn center<F: FnOnce()>(children: F) -> Response<AlignResponse> {
44    Align::center().show(children)
45}
46
47/// See [Align].
48pub fn align<F: FnOnce()>(alignment: Alignment, children: F) -> Response<AlignResponse> {
49    Align::new(alignment).show(children)
50}
51
52/// See [Button].
53pub fn button<S: Into<Cow<'static, str>>>(text: S) -> Response<ButtonResponse> {
54    Button::styled(text.into()).show()
55}
56
57/// See [Circle].
58pub fn colored_circle<S: Into<f32>>(color: Color, size: S) -> Response<CircleResponse> {
59    let mut circle = Circle::new();
60    circle.min_radius = size.into();
61    circle.color = color;
62    circle.show()
63}
64
65/// See [ColoredBox].
66pub fn colored_box<S: Into<Vec2>>(color: Color, size: S) -> Response<ColoredBoxResponse> {
67    ColoredBox::sized(color, size.into()).show()
68}
69
70/// See [ColoredBox].
71pub fn colored_box_container<F: FnOnce()>(
72    color: Color,
73    children: F,
74) -> Response<ColoredBoxResponse> {
75    ColoredBox::container(color).show_children(children)
76}
77
78/// See [Image].
79pub fn image<I, S>(image: I, size: S) -> Response<ImageResponse>
80where
81    I: Into<TextureId>,
82    S: Into<Vec2>,
83{
84    Image::new(image.into(), size.into()).show()
85}
86
87/// See [Pad].
88pub fn pad<F: FnOnce()>(padding: Pad, children: F) -> Response<PadResponse> {
89    padding.show(children)
90}
91
92/// See [Text].
93pub fn text<S: Into<Cow<'static, str>>>(size: f32, text: S) -> Response<TextResponse> {
94    Text::new(size, text.into()).show()
95}
96
97/// See [Text].
98pub fn label<S: Into<Cow<'static, str>>>(text: S) -> Response<TextResponse> {
99    Text::label(text.into()).show()
100}
101
102/// See [TextBox].
103pub fn textbox<S: Into<String>>(text: S) -> Response<TextBoxResponse> {
104    TextBox::new(text.into()).show()
105}
106
107/// See [Flexible].
108pub fn flexible<F: FnOnce()>(flex: u32, children: F) -> Response<FlexibleResponse> {
109    Flexible::new(flex).show(children)
110}
111
112/// See [Flexible].
113pub fn expanded<F: FnOnce()>(children: F) -> Response<FlexibleResponse> {
114    Flexible::expanded().show(children)
115}
116
117/// See [ConstrainedBox].
118pub fn constrained<F: FnOnce()>(
119    constraints: Constraints,
120    children: F,
121) -> Response<ConstrainedBoxResponse> {
122    ConstrainedBox::new(constraints).show(children)
123}
124
125/// See [Checkbox].
126pub fn checkbox(checked: bool) -> Response<CheckboxResponse> {
127    Checkbox::new(checked).show()
128}
129
130/// See [Offset].
131pub fn offset<F: FnOnce()>(offset: Vec2, children: F) -> Response<OffsetResponse> {
132    Offset::new(offset).show(children)
133}
134
135/// See [Draggable].
136pub fn draggable<F: FnOnce()>(children: F) -> Response<DraggableResponse> {
137    Draggable::new().show(children)
138}
139
140/// See [NineSlice].
141pub fn nineslice(
142    texture: ManagedTextureId,
143    margins: Pad,
144    scale: f32,
145    children: impl FnOnce(),
146) -> Response<()> {
147    NineSlice::new(texture, margins, scale).show(children)
148}
149
150/// See [Divider].
151pub fn divider(color: Color, height: f32, thickness: f32) -> Response<DividerResponse> {
152    Divider::new(color, height, thickness).show()
153}
154
155/// See [Spacer].
156pub fn spacer(flex: u32) -> Response<FlexibleResponse> {
157    Spacer::new(flex).show()
158}
159
160/// See [Scrollable].
161pub fn scroll_vertical(children: impl FnOnce()) -> Response<ScrollableResponse> {
162    Scrollable::vertical().show(children)
163}
164
165/// See [Slider].
166pub fn slider(value: f64, min: f64, max: f64) -> Response<SliderResponse> {
167    Slider::new(value, min, max).show()
168}
169
170/// See [Reflow].
171pub fn reflow(
172    anchor: Alignment,
173    pivot: Pivot,
174    offset: Dim2,
175    children: impl FnOnce(),
176) -> Response<ReflowResponse> {
177    Reflow::new(anchor, pivot, offset).show(children)
178}
179
180/// See [Opaque].
181pub fn opaque(children: impl FnOnce()) -> Response<OpaqueResponse> {
182    Opaque::new().show(children)
183}
184
185/// See [Canvas].
186pub fn canvas(paint: impl Fn(&mut PaintContext<'_>) + 'static) -> Response<CanvasResponse> {
187    Canvas::new(paint).show()
188}
189
190/// See [MaxWidth].
191pub fn max_width(max_width: f32, children: impl FnOnce()) -> Response<MaxWidthResponse> {
192    MaxWidth::new(max_width).show(children)
193}
194
195/// See [Stack].
196pub fn stack(children: impl FnOnce()) -> Response<StackResponse> {
197    Stack::new().show(children)
198}
199
200pub fn use_state<F, T: 'static>(default: F) -> Response<StateResponse<T>>
201where
202    F: FnOnce() -> T + 'static,
203{
204    State::new(default).show()
205}