Skip to main content

uzor_framework/widgets/
atomics.rs

1//! The atomic half of the `lm` set: text, checkbox, toggle, separator,
2//! chevron, tooltip. Each call registers with the hook's coordinator and
3//! draws with the library function.
4
5use uzor::render::RenderContext;
6use uzor::ui::widgets::atomic::checkbox::{
7    draw_checkbox, register_input_coordinator_checkbox, CheckboxRenderKind, CheckboxSettings,
8    CheckboxState, CheckboxView,
9};
10use uzor::ui::widgets::atomic::chevron::{
11    draw_chevron, register_input_coordinator_chevron, ChevronSettings, ChevronView,
12};
13use uzor::ui::widgets::atomic::separator::{
14    draw_separator, register_input_coordinator_separator, SeparatorDragState, SeparatorKind,
15    SeparatorSettings, SeparatorView,
16};
17use uzor::ui::widgets::atomic::text::{
18    draw_text, register_input_coordinator_text, TextSettings, TextView,
19};
20use uzor::ui::widgets::atomic::toggle::{
21    draw_toggle, register_input_coordinator_toggle, ToggleRenderKind, ToggleSettings, ToggleState,
22    ToggleView,
23};
24use uzor::ui::widgets::atomic::tooltip::{
25    draw_tooltip, register_input_coordinator_tooltip, TooltipConfig, TooltipSettings, TooltipState,
26};
27use uzor::Rect;
28use uzor::WidgetId;
29
30use crate::handle::{VisualView, Widgets};
31use crate::types::spec::Spec;
32
33use super::host::ContentCx;
34use super::paint::{interaction_state, parts};
35
36/// Register and draw a text label. Hover colour is `view.hovered` (the
37/// library field), not a separate widget state.
38pub fn text<'a, C: ContentCx<'a>>(
39    cx: &mut C,
40    id: impl Into<WidgetId>,
41    rect: Rect,
42    view: &TextView<'_>,
43    settings: &TextSettings,
44) {
45    let id = id.into();
46    cx.with_paint(|render, widgets, _visual, _tokens| {
47        paint_text(render, widgets, id, rect, view, settings);
48    });
49}
50
51pub(crate) fn paint_text(
52    render: &mut dyn RenderContext,
53    widgets: &mut Widgets<'_>,
54    id: WidgetId,
55    rect: Rect,
56    view: &TextView<'_>,
57    settings: &TextSettings,
58) {
59    let (coord, _states, layer) = parts(widgets);
60    register_input_coordinator_text(coord, id, rect, layer);
61    draw_text(render, rect, view, settings);
62}
63
64/// Register and draw a checkbox. `font` is the label font shorthand.
65pub fn checkbox<'a, C: ContentCx<'a>>(
66    cx: &mut C,
67    id: impl Into<WidgetId>,
68    rect: Rect,
69    view: &CheckboxView<'_>,
70    settings: &CheckboxSettings,
71    kind: &CheckboxRenderKind<'_>,
72    font: &str,
73) {
74    let id = id.into();
75    cx.with_paint(|render, widgets, visual, _tokens| {
76        paint_checkbox(
77            render, widgets, visual, id, rect, view, settings, kind, font,
78        );
79    });
80}
81
82pub(crate) fn paint_checkbox<S: Spec>(
83    render: &mut dyn RenderContext,
84    widgets: &mut Widgets<'_>,
85    visual: &VisualView<S>,
86    id: WidgetId,
87    rect: Rect,
88    view: &CheckboxView<'_>,
89    settings: &CheckboxSettings,
90    kind: &CheckboxRenderKind<'_>,
91    font: &str,
92) {
93    let ws = interaction_state(visual, &id, false);
94    let (coord, states, layer) = parts(widgets);
95    let state = states.get_or_insert_with(id.clone(), CheckboxState::default);
96    register_input_coordinator_checkbox(coord, id, rect, layer, state);
97    draw_checkbox(render, rect, ws, view, settings, kind, font);
98}
99
100/// Register and draw a toggle. Disabled comes from `view.disabled`.
101pub fn toggle<'a, C: ContentCx<'a>>(
102    cx: &mut C,
103    id: impl Into<WidgetId>,
104    rect: Rect,
105    view: &ToggleView<'_>,
106    settings: &ToggleSettings,
107    kind: &ToggleRenderKind<'_>,
108) {
109    let id = id.into();
110    cx.with_paint(|render, widgets, visual, _tokens| {
111        paint_toggle(render, widgets, visual, id, rect, view, settings, kind);
112    });
113}
114
115pub(crate) fn paint_toggle<S: Spec>(
116    render: &mut dyn RenderContext,
117    widgets: &mut Widgets<'_>,
118    visual: &VisualView<S>,
119    id: WidgetId,
120    rect: Rect,
121    view: &ToggleView<'_>,
122    settings: &ToggleSettings,
123    kind: &ToggleRenderKind<'_>,
124) {
125    let ws = interaction_state(visual, &id, view.disabled);
126    let (coord, states, layer) = parts(widgets);
127    let state = states.get_or_insert_with(id.clone(), ToggleState::default);
128    register_input_coordinator_toggle(coord, id, rect, layer, state);
129    draw_toggle(render, rect, ws, view, settings, kind, |_, _, _, _| {});
130}
131
132/// Register and draw a separator. `kind` picks the hit sense (divider or
133/// resize handle); `view` picks the paint.
134pub fn separator<'a, C: ContentCx<'a>>(
135    cx: &mut C,
136    id: impl Into<WidgetId>,
137    rect: Rect,
138    kind: SeparatorKind,
139    view: &SeparatorView,
140    settings: &SeparatorSettings,
141) {
142    let id = id.into();
143    cx.with_paint(|render, widgets, _visual, _tokens| {
144        paint_separator(render, widgets, id, rect, kind, view, settings);
145    });
146}
147
148pub(crate) fn paint_separator(
149    render: &mut dyn RenderContext,
150    widgets: &mut Widgets<'_>,
151    id: WidgetId,
152    rect: Rect,
153    kind: SeparatorKind,
154    view: &SeparatorView,
155    settings: &SeparatorSettings,
156) {
157    let (coord, states, layer) = parts(widgets);
158    let state = states.get_or_insert_with(id.clone(), SeparatorDragState::default);
159    register_input_coordinator_separator(coord, id, rect, kind, layer, state);
160    draw_separator(render, rect, view, settings);
161}
162
163/// Register and draw a chevron. The hit rect follows `view.hit_area`.
164pub fn chevron<'a, C: ContentCx<'a>>(
165    cx: &mut C,
166    id: impl Into<WidgetId>,
167    rect: Rect,
168    view: &ChevronView,
169    settings: &ChevronSettings,
170) {
171    let id = id.into();
172    cx.with_paint(|render, widgets, _visual, _tokens| {
173        paint_chevron(render, widgets, id, rect, view, settings);
174    });
175}
176
177pub(crate) fn paint_chevron(
178    render: &mut dyn RenderContext,
179    widgets: &mut Widgets<'_>,
180    id: WidgetId,
181    rect: Rect,
182    view: &ChevronView,
183    settings: &ChevronSettings,
184) {
185    let (coord, _states, layer) = parts(widgets);
186    register_input_coordinator_chevron(coord, id, rect, view, layer);
187    draw_chevron(render, rect, view, settings);
188}
189
190/// Register and draw a tooltip at `alpha` (0.0..=1.0, caller-animated).
191pub fn tooltip<'a, C: ContentCx<'a>>(
192    cx: &mut C,
193    id: impl Into<WidgetId>,
194    rect: Rect,
195    config: &TooltipConfig,
196    alpha: f64,
197    settings: &TooltipSettings,
198) {
199    let id = id.into();
200    cx.with_paint(|render, widgets, _visual, _tokens| {
201        paint_tooltip(render, widgets, id, rect, config, alpha, settings);
202    });
203}
204
205pub(crate) fn paint_tooltip(
206    render: &mut dyn RenderContext,
207    widgets: &mut Widgets<'_>,
208    id: WidgetId,
209    rect: Rect,
210    config: &TooltipConfig,
211    alpha: f64,
212    settings: &TooltipSettings,
213) {
214    let (coord, states, layer) = parts(widgets);
215    let state = states.get_or_insert_with(id.clone(), TooltipState::default);
216    register_input_coordinator_tooltip(coord, id, rect, layer, state);
217    draw_tooltip(render, rect, config, alpha, settings);
218}