1use uzor::render::{TextAlign, TextBaseline};
11use uzor::ui::widgets::atomic::button::render::HoverChevronSpec;
12use uzor::ui::widgets::atomic::button::{ButtonSettings, ButtonView};
13use uzor::ui::widgets::atomic::checkbox::{CheckboxRenderKind, CheckboxSettings, CheckboxView};
14use uzor::ui::widgets::atomic::chevron::{
15 ChevronDirection, ChevronSettings, ChevronUseCase, ChevronView, ChevronVisualKind,
16 DefaultChevronStyle, HitAreaPolicy, PlacementPolicy, VisibilityPolicy,
17};
18use uzor::ui::widgets::atomic::separator::{
19 DefaultSeparatorStyle, SeparatorKind, SeparatorOrientation, SeparatorSettings, SeparatorType,
20 SeparatorView,
21};
22use uzor::ui::widgets::atomic::text::{TextOverflow, TextSettings, TextView};
23use uzor::ui::widgets::atomic::toggle::{ToggleRenderKind, ToggleSettings, ToggleView};
24use uzor::ui::widgets::atomic::tooltip::{
25 DefaultTooltipStyle, TooltipConfig, TooltipPosition, TooltipSettings,
26};
27use uzor::ui::widgets::composite::chrome::{
28 measure as measure_chrome, ChromeRenderKind, ChromeSettings, ChromeState, ChromeTabConfig,
29 ChromeView, DefaultChromeStyle,
30};
31use uzor::Rect;
32use uzor::WidgetId;
33
34use super::atomics::{
35 paint_checkbox, paint_chevron, paint_separator, paint_text, paint_toggle, paint_tooltip,
36};
37use super::button::{button_settings, paint_button};
38use super::host::ContentCx;
39use super::paint::theme_of;
40
41pub struct ButtonBuilder<'a> {
45 id: WidgetId,
46 rect: Rect,
47 text: Option<&'a str>,
48 icon: Option<&'a uzor::IconId>,
49 active: bool,
50 disabled: bool,
51 active_border: Option<bool>,
52 hover_chevron: Option<HoverChevronSpec>,
53 settings: Option<ButtonSettings>,
54 on_click: Option<Box<dyn FnOnce() + 'a>>,
55 bind_count: Option<&'a mut u32>,
56}
57
58pub fn button<'a>(id: impl Into<WidgetId>, rect: Rect) -> ButtonBuilder<'a> {
60 ButtonBuilder {
61 id: id.into(),
62 rect,
63 text: None,
64 icon: None,
65 active: false,
66 disabled: false,
67 active_border: None,
68 hover_chevron: None,
69 settings: None,
70 on_click: None,
71 bind_count: None,
72 }
73}
74
75impl<'a> ButtonBuilder<'a> {
76 pub fn text(mut self, t: &'a str) -> Self {
78 self.text = Some(t);
79 self
80 }
81 pub fn icon(mut self, i: &'a uzor::IconId) -> Self {
83 self.icon = Some(i);
84 self
85 }
86 pub fn active(mut self, on: bool) -> Self {
88 self.active = on;
89 self
90 }
91 pub fn disabled(mut self, on: bool) -> Self {
93 self.disabled = on;
94 self
95 }
96 pub fn active_border(mut self, b: Option<bool>) -> Self {
98 self.active_border = b;
99 self
100 }
101 pub fn hover_chevron(mut self, c: HoverChevronSpec) -> Self {
103 self.hover_chevron = Some(c);
104 self
105 }
106 pub fn settings(mut self, s: ButtonSettings) -> Self {
108 self.settings = Some(s);
109 self
110 }
111 pub fn on_click(mut self, f: impl FnOnce() + 'a) -> Self {
113 self.on_click = Some(Box::new(f));
114 self
115 }
116 pub fn bind_count(mut self, n: &'a mut u32) -> Self {
118 self.bind_count = Some(n);
119 self
120 }
121
122 pub(crate) fn id_ref(&self) -> &WidgetId {
124 &self.id
125 }
126
127 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
129 let theme = theme_of(cx);
130 cx.with_paint(|render, widgets, visual, tokens| {
131 if !self.disabled && visual.clicked(&self.id) {
132 if let Some(cb) = self.on_click {
133 cb();
134 }
135 if let Some(n) = self.bind_count {
136 *n = n.wrapping_add(1);
137 }
138 }
139 let view = ButtonView {
140 icon: self.icon,
141 text: self.text,
142 active: self.active,
143 disabled: self.disabled,
144 active_border: self.active_border,
145 hover_chevron: self.hover_chevron,
146 };
147 let settings = self.settings.unwrap_or_else(|| {
148 let mut s = button_settings(tokens);
149 s.theme = Box::new(theme);
150 s
151 });
152 paint_button(
153 render, widgets, visual, self.id, self.rect, &view, &settings,
154 );
155 });
156 }
157}
158
159pub struct TextBuilder<'a> {
163 id: WidgetId,
164 rect: Rect,
165 text: &'a str,
166 color: Option<&'a str>,
167 align: TextAlign,
168 baseline: TextBaseline,
169 font: Option<&'a str>,
170 overflow: TextOverflow,
171 hovered: bool,
172 settings: Option<TextSettings>,
173}
174
175pub fn text<'a>(id: impl Into<WidgetId>, rect: Rect, text: &'a str) -> TextBuilder<'a> {
177 TextBuilder {
178 id: id.into(),
179 rect,
180 text,
181 color: None,
182 align: TextAlign::Left,
183 baseline: TextBaseline::Middle,
184 font: None,
185 overflow: TextOverflow::Clip,
186 hovered: false,
187 settings: None,
188 }
189}
190
191impl<'a> TextBuilder<'a> {
192 pub fn color(mut self, c: &'a str) -> Self {
194 self.color = Some(c);
195 self
196 }
197 pub fn align(mut self, a: TextAlign) -> Self {
199 self.align = a;
200 self
201 }
202 pub fn baseline(mut self, b: TextBaseline) -> Self {
204 self.baseline = b;
205 self
206 }
207 pub fn font(mut self, f: &'a str) -> Self {
209 self.font = Some(f);
210 self
211 }
212 pub fn overflow(mut self, o: TextOverflow) -> Self {
214 self.overflow = o;
215 self
216 }
217 pub fn hovered(mut self, on: bool) -> Self {
219 self.hovered = on;
220 self
221 }
222 pub fn settings(mut self, s: TextSettings) -> Self {
224 self.settings = Some(s);
225 self
226 }
227
228 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
230 let explicit = self.settings.is_some();
231 let theme = theme_of(cx);
232 cx.with_paint(|render, widgets, _visual, _tokens| {
233 let view = TextView {
234 text: self.text,
235 color: self.color,
236 align: self.align,
237 baseline: self.baseline,
238 font: self.font,
239 overflow: self.overflow,
240 hovered: self.hovered,
241 };
242 let mut settings = self.settings.unwrap_or_default();
243 if !explicit {
244 settings.theme = Box::new(theme);
245 }
246 paint_text(render, widgets, self.id, self.rect, &view, &settings);
247 });
248 }
249}
250
251pub struct CheckboxBuilder<'a> {
255 id: WidgetId,
256 rect: Rect,
257 checked: bool,
258 bind: Option<&'a mut bool>,
259 label: Option<&'a str>,
260 kind: CheckboxRenderKind<'a>,
261 font: &'a str,
262 settings: Option<CheckboxSettings>,
263}
264
265pub fn checkbox<'a>(id: impl Into<WidgetId>, rect: Rect) -> CheckboxBuilder<'a> {
267 CheckboxBuilder {
268 id: id.into(),
269 rect,
270 checked: false,
271 bind: None,
272 label: None,
273 kind: CheckboxRenderKind::Standard,
274 font: "13px sans-serif",
275 settings: None,
276 }
277}
278
279impl<'a> CheckboxBuilder<'a> {
280 pub fn checked(mut self, on: bool) -> Self {
282 self.checked = on;
283 self
284 }
285 pub fn bind(mut self, flag: &'a mut bool) -> Self {
287 self.bind = Some(flag);
288 self
289 }
290 pub fn label(mut self, l: &'a str) -> Self {
292 self.label = Some(l);
293 self
294 }
295 pub fn kind(mut self, k: CheckboxRenderKind<'a>) -> Self {
297 self.kind = k;
298 self
299 }
300 pub fn font(mut self, f: &'a str) -> Self {
302 self.font = f;
303 self
304 }
305 pub fn settings(mut self, s: CheckboxSettings) -> Self {
307 self.settings = Some(s);
308 self
309 }
310
311 pub(crate) fn id_label(&self) -> (&WidgetId, Option<&'a str>) {
313 (&self.id, self.label)
314 }
315
316 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) -> bool {
318 let explicit = self.settings.is_some();
319 let theme = theme_of(cx);
320 cx.with_paint(|render, widgets, visual, _tokens| {
321 let checked = if let Some(flag) = self.bind {
322 if visual.clicked(&self.id) {
323 *flag = !*flag;
324 }
325 *flag
326 } else {
327 self.checked
328 };
329 let view = CheckboxView {
330 checked,
331 label: self.label,
332 };
333 let mut settings = self.settings.unwrap_or_default();
334 if !explicit {
335 settings.theme = Box::new(theme);
336 }
337 paint_checkbox(
338 render, widgets, visual, self.id, self.rect, &view, &settings, &self.kind,
339 self.font,
340 );
341 checked
342 })
343 }
344}
345
346pub struct ToggleBuilder<'a> {
350 id: WidgetId,
351 rect: Rect,
352 toggled: bool,
353 bind: Option<&'a mut bool>,
354 label: Option<&'a str>,
355 disabled: bool,
356 kind: ToggleRenderKind<'a>,
357 settings: Option<ToggleSettings>,
358}
359
360pub fn toggle<'a>(id: impl Into<WidgetId>, rect: Rect) -> ToggleBuilder<'a> {
362 ToggleBuilder {
363 id: id.into(),
364 rect,
365 toggled: false,
366 bind: None,
367 label: None,
368 disabled: false,
369 kind: ToggleRenderKind::Switch,
370 settings: None,
371 }
372}
373
374impl<'a> ToggleBuilder<'a> {
375 pub fn toggled(mut self, on: bool) -> Self {
377 self.toggled = on;
378 self
379 }
380 pub fn bind(mut self, flag: &'a mut bool) -> Self {
382 self.bind = Some(flag);
383 self
384 }
385 pub fn label(mut self, l: &'a str) -> Self {
387 self.label = Some(l);
388 self
389 }
390 pub fn disabled(mut self, on: bool) -> Self {
392 self.disabled = on;
393 self
394 }
395 pub fn kind(mut self, k: ToggleRenderKind<'a>) -> Self {
397 self.kind = k;
398 self
399 }
400 pub fn settings(mut self, s: ToggleSettings) -> Self {
402 self.settings = Some(s);
403 self
404 }
405
406 pub(crate) fn id_label(&self) -> (&WidgetId, Option<&'a str>, bool) {
408 (&self.id, self.label, self.disabled)
409 }
410
411 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) -> bool {
413 let explicit = self.settings.is_some();
414 let theme = theme_of(cx);
415 cx.with_paint(|render, widgets, visual, _tokens| {
416 let toggled = if let Some(flag) = self.bind {
417 if visual.clicked(&self.id) && !self.disabled {
418 *flag = !*flag;
419 }
420 *flag
421 } else {
422 self.toggled
423 };
424 let view = ToggleView {
425 toggled,
426 label: self.label,
427 disabled: self.disabled,
428 };
429 let mut settings = self.settings.unwrap_or_default();
430 if !explicit {
431 settings.theme = Box::new(theme);
432 }
433 paint_toggle(
434 render, widgets, visual, self.id, self.rect, &view, &settings, &self.kind,
435 );
436 toggled
437 })
438 }
439}
440
441pub struct SeparatorBuilder {
445 id: WidgetId,
446 rect: Rect,
447 kind: SeparatorKind,
448 sep_type: SeparatorType,
449 hovered: bool,
450 dragging: bool,
451 settings: Option<SeparatorSettings>,
452}
453
454pub fn separator(id: impl Into<WidgetId>, rect: Rect) -> SeparatorBuilder {
456 SeparatorBuilder {
457 id: id.into(),
458 rect,
459 kind: SeparatorKind::Divider,
460 sep_type: SeparatorType::Divider {
461 orientation: SeparatorOrientation::Horizontal,
462 },
463 hovered: false,
464 dragging: false,
465 settings: None,
466 }
467}
468
469impl SeparatorBuilder {
470 pub fn kind(mut self, k: SeparatorKind) -> Self {
472 self.kind = k;
473 self
474 }
475 pub fn sep_type(mut self, t: SeparatorType) -> Self {
477 self.sep_type = t;
478 self
479 }
480 pub fn hovered(mut self, on: bool) -> Self {
482 self.hovered = on;
483 self
484 }
485 pub fn dragging(mut self, on: bool) -> Self {
487 self.dragging = on;
488 self
489 }
490 pub fn settings(mut self, s: SeparatorSettings) -> Self {
492 self.settings = Some(s);
493 self
494 }
495
496 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
498 let theme = theme_of(cx);
499 cx.with_paint(|render, widgets, _visual, _tokens| {
500 let view = SeparatorView {
501 kind: self.sep_type,
502 hovered: self.hovered,
503 dragging: self.dragging,
504 };
505 let settings = match self.settings {
506 Some(settings) => settings,
507 None => SeparatorSettings {
508 theme: Box::new(theme),
509 style: Box::new(DefaultSeparatorStyle),
510 },
511 };
512 paint_separator(
513 render, widgets, self.id, self.rect, self.kind, &view, &settings,
514 );
515 });
516 }
517}
518
519pub struct ChevronBuilder<'a> {
523 id: WidgetId,
524 rect: Rect,
525 view: ChevronView,
526 settings: Option<ChevronSettings>,
527 on_click: Option<Box<dyn FnOnce() + 'a>>,
528}
529
530pub fn chevron<'a>(id: impl Into<WidgetId>, rect: Rect) -> ChevronBuilder<'a> {
532 ChevronBuilder {
533 id: id.into(),
534 rect,
535 view: ChevronView {
536 direction: ChevronDirection::Down,
537 use_case: ChevronUseCase::PureButton,
538 visibility: VisibilityPolicy::Always,
539 placement: PlacementPolicy::Standalone,
540 hit_area: HitAreaPolicy::Visual,
541 visual_kind: ChevronVisualKind::Stroked,
542 hovered: false,
543 pressed: false,
544 disabled: false,
545 active: false,
546 glyph_override: None,
547 },
548 settings: None,
549 on_click: None,
550 }
551}
552
553impl<'a> ChevronBuilder<'a> {
554 pub fn direction(mut self, d: ChevronDirection) -> Self {
556 self.view.direction = d;
557 self
558 }
559 pub fn hovered(mut self, on: bool) -> Self {
561 self.view.hovered = on;
562 self
563 }
564 pub fn pressed(mut self, on: bool) -> Self {
566 self.view.pressed = on;
567 self
568 }
569 pub fn disabled(mut self, on: bool) -> Self {
571 self.view.disabled = on;
572 self
573 }
574 pub fn on_click(mut self, f: impl FnOnce() + 'a) -> Self {
576 self.on_click = Some(Box::new(f));
577 self
578 }
579 pub fn settings(mut self, s: ChevronSettings) -> Self {
581 self.settings = Some(s);
582 self
583 }
584
585 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
587 let theme = theme_of(cx);
588 cx.with_paint(|render, widgets, visual, _tokens| {
589 if !self.view.disabled && visual.clicked(&self.id) {
590 if let Some(cb) = self.on_click {
591 cb();
592 }
593 }
594 let settings = match self.settings {
595 Some(settings) => settings,
596 None => ChevronSettings {
597 theme: Box::new(theme),
598 style: Box::new(DefaultChevronStyle),
599 },
600 };
601 paint_chevron(render, widgets, self.id, self.rect, &self.view, &settings);
602 });
603 }
604}
605
606pub struct TooltipBuilder {
610 id: WidgetId,
611 rect: Rect,
612 text: String,
613 anchor: Rect,
614 position: TooltipPosition,
615 alpha: f64,
616 settings: Option<TooltipSettings>,
617}
618
619pub fn tooltip(id: impl Into<WidgetId>, rect: Rect) -> TooltipBuilder {
621 TooltipBuilder {
622 id: id.into(),
623 rect,
624 text: String::new(),
625 anchor: Rect::new(0.0, 0.0, 0.0, 0.0),
626 position: TooltipPosition::Above,
627 alpha: 1.0,
628 settings: None,
629 }
630}
631
632impl TooltipBuilder {
633 pub fn text(mut self, t: impl Into<String>) -> Self {
635 self.text = t.into();
636 self
637 }
638 pub fn anchor(mut self, r: Rect) -> Self {
640 self.anchor = r;
641 self
642 }
643 pub fn position(mut self, p: TooltipPosition) -> Self {
645 self.position = p;
646 self
647 }
648 pub fn alpha(mut self, a: f64) -> Self {
650 self.alpha = a;
651 self
652 }
653 pub fn settings(mut self, s: TooltipSettings) -> Self {
655 self.settings = Some(s);
656 self
657 }
658
659 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
661 let theme = theme_of(cx);
662 cx.with_paint(|render, widgets, _visual, _tokens| {
663 let config = TooltipConfig {
664 text: self.text,
665 lines: None,
666 anchor: self.anchor,
667 position: self.position,
668 };
669 let settings = match self.settings {
670 Some(settings) => settings,
671 None => TooltipSettings {
672 theme: Box::new(theme),
673 style: Box::new(DefaultTooltipStyle),
674 },
675 };
676 paint_tooltip(
677 render, widgets, self.id, self.rect, &config, self.alpha, &settings,
678 );
679 });
680 }
681}
682
683pub struct ChromeBuilder<'a> {
687 id: &'a str,
688 rect: Option<Rect>,
689 tabs: &'a [ChromeTabConfig<'a>],
690 active_tab_id: Option<&'a str>,
691 show_new_tab_btn: bool,
692 show_menu_btn: bool,
693 show_new_window_btn: bool,
694 show_close_window_btn: bool,
695 is_maximized: bool,
696 menu_left: bool,
697 show_maximize: bool,
698 cursor_x: f64,
699 cursor_y: f64,
700 time_ms: f64,
701 kind: ChromeRenderKind,
702 settings: Option<ChromeSettings>,
703}
704
705pub fn chrome<'a>() -> ChromeBuilder<'a> {
707 ChromeBuilder {
708 id: "chrome",
709 rect: None,
710 tabs: &[],
711 active_tab_id: None,
712 show_new_tab_btn: false,
713 show_menu_btn: false,
714 show_new_window_btn: false,
715 show_close_window_btn: false,
716 is_maximized: false,
717 menu_left: false,
718 show_maximize: true,
719 cursor_x: 0.0,
720 cursor_y: 0.0,
721 time_ms: 0.0,
722 kind: ChromeRenderKind::Default,
723 settings: None,
724 }
725}
726
727impl<'a> ChromeBuilder<'a> {
728 pub fn widget_id(mut self, id: &'a str) -> Self {
730 self.id = id;
731 self
732 }
733 pub fn rect(mut self, r: Rect) -> Self {
735 self.rect = Some(r);
736 self
737 }
738 pub fn tabs(mut self, ts: &'a [ChromeTabConfig<'a>]) -> Self {
740 self.tabs = ts;
741 self
742 }
743 pub fn active_tab(mut self, id: &'a str) -> Self {
745 self.active_tab_id = Some(id);
746 self
747 }
748 pub fn show_new_tab_btn(mut self, on: bool) -> Self {
750 self.show_new_tab_btn = on;
751 self
752 }
753 pub fn show_menu_btn(mut self, on: bool) -> Self {
755 self.show_menu_btn = on;
756 self
757 }
758 pub fn show_new_window_btn(mut self, on: bool) -> Self {
760 self.show_new_window_btn = on;
761 self
762 }
763 pub fn cursor(mut self, pos: (f64, f64)) -> Self {
765 self.cursor_x = pos.0;
766 self.cursor_y = pos.1;
767 self
768 }
769 pub fn time_ms(mut self, t: f64) -> Self {
771 self.time_ms = t;
772 self
773 }
774 pub fn kind(mut self, k: ChromeRenderKind) -> Self {
777 self.kind = k;
778 self
779 }
780
781 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
783 let theme = theme_of(cx);
784 cx.with_paint(|render, widgets, _visual, _tokens| {
785 let view = ChromeView {
786 tabs: self.tabs,
787 active_tab_id: self.active_tab_id,
788 show_new_tab_btn: self.show_new_tab_btn,
789 show_menu_btn: self.show_menu_btn,
790 show_new_window_btn: self.show_new_window_btn,
791 show_close_window_btn: self.show_close_window_btn,
792 is_maximized: self.is_maximized,
793 menu_left: self.menu_left,
794 show_maximize: self.show_maximize,
795 cursor_x: self.cursor_x,
796 cursor_y: self.cursor_y,
797 time_ms: self.time_ms,
798 };
799 let settings = self.settings.unwrap_or_else(|| ChromeSettings {
800 theme: Box::new(theme),
801 style: Box::<DefaultChromeStyle>::default(),
802 });
803 let mut state = ChromeState::new();
804 let ids: Vec<&str> = self.tabs.iter().map(|t| t.id).collect();
805 state.sync_tabs(&ids);
806 let rect = self.rect.unwrap_or_else(|| {
807 let (w, h) = measure_chrome(&view, &state, &settings, &self.kind);
808 Rect::new(0.0, 0.0, w, h)
809 });
810 super::composites::chrome_paint(
811 render,
812 widgets,
813 WidgetId::from(self.id),
814 rect,
815 &state,
816 &view,
817 &settings,
818 &self.kind,
819 );
820 });
821 }
822}