1use 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
22pub fn column<F: FnOnce()>(children: F) -> Response<ListResponse> {
24 List::column().show(children)
25}
26
27pub fn row<F: FnOnce()>(children: F) -> Response<ListResponse> {
29 List::row().show(children)
30}
31
32pub fn countgrid_column<F: FnOnce()>(n_columns: usize, children: F) -> Response<ListResponse> {
34 CountGrid::col(n_columns).show(children)
35}
36
37pub fn countgrid_row<F: FnOnce()>(n_rows: usize, children: F) -> Response<ListResponse> {
39 CountGrid::row(n_rows).show(children)
40}
41
42pub fn center<F: FnOnce()>(children: F) -> Response<AlignResponse> {
44 Align::center().show(children)
45}
46
47pub fn align<F: FnOnce()>(alignment: Alignment, children: F) -> Response<AlignResponse> {
49 Align::new(alignment).show(children)
50}
51
52pub fn button<S: Into<Cow<'static, str>>>(text: S) -> Response<ButtonResponse> {
54 Button::styled(text.into()).show()
55}
56
57pub 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
65pub fn colored_box<S: Into<Vec2>>(color: Color, size: S) -> Response<ColoredBoxResponse> {
67 ColoredBox::sized(color, size.into()).show()
68}
69
70pub fn colored_box_container<F: FnOnce()>(
72 color: Color,
73 children: F,
74) -> Response<ColoredBoxResponse> {
75 ColoredBox::container(color).show_children(children)
76}
77
78pub 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
87pub fn pad<F: FnOnce()>(padding: Pad, children: F) -> Response<PadResponse> {
89 padding.show(children)
90}
91
92pub fn text<S: Into<Cow<'static, str>>>(size: f32, text: S) -> Response<TextResponse> {
94 Text::new(size, text.into()).show()
95}
96
97pub fn label<S: Into<Cow<'static, str>>>(text: S) -> Response<TextResponse> {
99 Text::label(text.into()).show()
100}
101
102pub fn textbox<S: Into<String>>(text: S) -> Response<TextBoxResponse> {
104 TextBox::new(text.into()).show()
105}
106
107pub fn flexible<F: FnOnce()>(flex: u32, children: F) -> Response<FlexibleResponse> {
109 Flexible::new(flex).show(children)
110}
111
112pub fn expanded<F: FnOnce()>(children: F) -> Response<FlexibleResponse> {
114 Flexible::expanded().show(children)
115}
116
117pub fn constrained<F: FnOnce()>(
119 constraints: Constraints,
120 children: F,
121) -> Response<ConstrainedBoxResponse> {
122 ConstrainedBox::new(constraints).show(children)
123}
124
125pub fn checkbox(checked: bool) -> Response<CheckboxResponse> {
127 Checkbox::new(checked).show()
128}
129
130pub fn offset<F: FnOnce()>(offset: Vec2, children: F) -> Response<OffsetResponse> {
132 Offset::new(offset).show(children)
133}
134
135pub fn draggable<F: FnOnce()>(children: F) -> Response<DraggableResponse> {
137 Draggable::new().show(children)
138}
139
140pub 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
150pub fn divider(color: Color, height: f32, thickness: f32) -> Response<DividerResponse> {
152 Divider::new(color, height, thickness).show()
153}
154
155pub fn spacer(flex: u32) -> Response<FlexibleResponse> {
157 Spacer::new(flex).show()
158}
159
160pub fn scroll_vertical(children: impl FnOnce()) -> Response<ScrollableResponse> {
162 Scrollable::vertical().show(children)
163}
164
165pub fn slider(value: f64, min: f64, max: f64) -> Response<SliderResponse> {
167 Slider::new(value, min, max).show()
168}
169
170pub 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
180pub fn opaque(children: impl FnOnce()) -> Response<OpaqueResponse> {
182 Opaque::new().show(children)
183}
184
185pub fn canvas(paint: impl Fn(&mut PaintContext<'_>) + 'static) -> Response<CanvasResponse> {
187 Canvas::new(paint).show()
188}
189
190pub fn max_width(max_width: f32, children: impl FnOnce()) -> Response<MaxWidthResponse> {
192 MaxWidth::new(max_width).show(children)
193}
194
195pub 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}