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 fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
124 let theme = theme_of(cx);
125 cx.with_paint(|render, widgets, visual, tokens| {
126 if !self.disabled && visual.clicked(&self.id) {
127 if let Some(cb) = self.on_click {
128 cb();
129 }
130 if let Some(n) = self.bind_count {
131 *n = n.wrapping_add(1);
132 }
133 }
134 let view = ButtonView {
135 icon: self.icon,
136 text: self.text,
137 active: self.active,
138 disabled: self.disabled,
139 active_border: self.active_border,
140 hover_chevron: self.hover_chevron,
141 };
142 let settings = self.settings.unwrap_or_else(|| {
143 let mut s = button_settings(tokens);
144 s.theme = Box::new(theme);
145 s
146 });
147 paint_button(
148 render, widgets, visual, self.id, self.rect, &view, &settings,
149 );
150 });
151 }
152}
153
154pub struct TextBuilder<'a> {
158 id: WidgetId,
159 rect: Rect,
160 text: &'a str,
161 color: Option<&'a str>,
162 align: TextAlign,
163 baseline: TextBaseline,
164 font: Option<&'a str>,
165 overflow: TextOverflow,
166 hovered: bool,
167 settings: Option<TextSettings>,
168}
169
170pub fn text<'a>(id: impl Into<WidgetId>, rect: Rect, text: &'a str) -> TextBuilder<'a> {
172 TextBuilder {
173 id: id.into(),
174 rect,
175 text,
176 color: None,
177 align: TextAlign::Left,
178 baseline: TextBaseline::Middle,
179 font: None,
180 overflow: TextOverflow::Clip,
181 hovered: false,
182 settings: None,
183 }
184}
185
186impl<'a> TextBuilder<'a> {
187 pub fn color(mut self, c: &'a str) -> Self {
189 self.color = Some(c);
190 self
191 }
192 pub fn align(mut self, a: TextAlign) -> Self {
194 self.align = a;
195 self
196 }
197 pub fn baseline(mut self, b: TextBaseline) -> Self {
199 self.baseline = b;
200 self
201 }
202 pub fn font(mut self, f: &'a str) -> Self {
204 self.font = Some(f);
205 self
206 }
207 pub fn overflow(mut self, o: TextOverflow) -> Self {
209 self.overflow = o;
210 self
211 }
212 pub fn hovered(mut self, on: bool) -> Self {
214 self.hovered = on;
215 self
216 }
217 pub fn settings(mut self, s: TextSettings) -> Self {
219 self.settings = Some(s);
220 self
221 }
222
223 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
225 let explicit = self.settings.is_some();
226 let theme = theme_of(cx);
227 cx.with_paint(|render, widgets, _visual, _tokens| {
228 let view = TextView {
229 text: self.text,
230 color: self.color,
231 align: self.align,
232 baseline: self.baseline,
233 font: self.font,
234 overflow: self.overflow,
235 hovered: self.hovered,
236 };
237 let mut settings = self.settings.unwrap_or_default();
238 if !explicit {
239 settings.theme = Box::new(theme);
240 }
241 paint_text(render, widgets, self.id, self.rect, &view, &settings);
242 });
243 }
244}
245
246pub struct CheckboxBuilder<'a> {
250 id: WidgetId,
251 rect: Rect,
252 checked: bool,
253 bind: Option<&'a mut bool>,
254 label: Option<&'a str>,
255 kind: CheckboxRenderKind<'a>,
256 font: &'a str,
257 settings: Option<CheckboxSettings>,
258}
259
260pub fn checkbox<'a>(id: impl Into<WidgetId>, rect: Rect) -> CheckboxBuilder<'a> {
262 CheckboxBuilder {
263 id: id.into(),
264 rect,
265 checked: false,
266 bind: None,
267 label: None,
268 kind: CheckboxRenderKind::Standard,
269 font: "13px sans-serif",
270 settings: None,
271 }
272}
273
274impl<'a> CheckboxBuilder<'a> {
275 pub fn checked(mut self, on: bool) -> Self {
277 self.checked = on;
278 self
279 }
280 pub fn bind(mut self, flag: &'a mut bool) -> Self {
282 self.bind = Some(flag);
283 self
284 }
285 pub fn label(mut self, l: &'a str) -> Self {
287 self.label = Some(l);
288 self
289 }
290 pub fn kind(mut self, k: CheckboxRenderKind<'a>) -> Self {
292 self.kind = k;
293 self
294 }
295 pub fn font(mut self, f: &'a str) -> Self {
297 self.font = f;
298 self
299 }
300 pub fn settings(mut self, s: CheckboxSettings) -> Self {
302 self.settings = Some(s);
303 self
304 }
305
306 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
308 let explicit = self.settings.is_some();
309 let theme = theme_of(cx);
310 cx.with_paint(|render, widgets, visual, _tokens| {
311 let checked = if let Some(flag) = self.bind {
312 if visual.clicked(&self.id) {
313 *flag = !*flag;
314 }
315 *flag
316 } else {
317 self.checked
318 };
319 let view = CheckboxView {
320 checked,
321 label: self.label,
322 };
323 let mut settings = self.settings.unwrap_or_default();
324 if !explicit {
325 settings.theme = Box::new(theme);
326 }
327 paint_checkbox(
328 render, widgets, visual, self.id, self.rect, &view, &settings, &self.kind,
329 self.font,
330 );
331 });
332 }
333}
334
335pub struct ToggleBuilder<'a> {
339 id: WidgetId,
340 rect: Rect,
341 toggled: bool,
342 bind: Option<&'a mut bool>,
343 label: Option<&'a str>,
344 disabled: bool,
345 kind: ToggleRenderKind<'a>,
346 settings: Option<ToggleSettings>,
347}
348
349pub fn toggle<'a>(id: impl Into<WidgetId>, rect: Rect) -> ToggleBuilder<'a> {
351 ToggleBuilder {
352 id: id.into(),
353 rect,
354 toggled: false,
355 bind: None,
356 label: None,
357 disabled: false,
358 kind: ToggleRenderKind::Switch,
359 settings: None,
360 }
361}
362
363impl<'a> ToggleBuilder<'a> {
364 pub fn toggled(mut self, on: bool) -> Self {
366 self.toggled = on;
367 self
368 }
369 pub fn bind(mut self, flag: &'a mut bool) -> Self {
371 self.bind = Some(flag);
372 self
373 }
374 pub fn label(mut self, l: &'a str) -> Self {
376 self.label = Some(l);
377 self
378 }
379 pub fn disabled(mut self, on: bool) -> Self {
381 self.disabled = on;
382 self
383 }
384 pub fn kind(mut self, k: ToggleRenderKind<'a>) -> Self {
386 self.kind = k;
387 self
388 }
389 pub fn settings(mut self, s: ToggleSettings) -> Self {
391 self.settings = Some(s);
392 self
393 }
394
395 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
397 let explicit = self.settings.is_some();
398 let theme = theme_of(cx);
399 cx.with_paint(|render, widgets, visual, _tokens| {
400 let toggled = if let Some(flag) = self.bind {
401 if visual.clicked(&self.id) && !self.disabled {
402 *flag = !*flag;
403 }
404 *flag
405 } else {
406 self.toggled
407 };
408 let view = ToggleView {
409 toggled,
410 label: self.label,
411 disabled: self.disabled,
412 };
413 let mut settings = self.settings.unwrap_or_default();
414 if !explicit {
415 settings.theme = Box::new(theme);
416 }
417 paint_toggle(
418 render, widgets, visual, self.id, self.rect, &view, &settings, &self.kind,
419 );
420 });
421 }
422}
423
424pub struct SeparatorBuilder {
428 id: WidgetId,
429 rect: Rect,
430 kind: SeparatorKind,
431 sep_type: SeparatorType,
432 hovered: bool,
433 dragging: bool,
434 settings: Option<SeparatorSettings>,
435}
436
437pub fn separator(id: impl Into<WidgetId>, rect: Rect) -> SeparatorBuilder {
439 SeparatorBuilder {
440 id: id.into(),
441 rect,
442 kind: SeparatorKind::Divider,
443 sep_type: SeparatorType::Divider {
444 orientation: SeparatorOrientation::Horizontal,
445 },
446 hovered: false,
447 dragging: false,
448 settings: None,
449 }
450}
451
452impl SeparatorBuilder {
453 pub fn kind(mut self, k: SeparatorKind) -> Self {
455 self.kind = k;
456 self
457 }
458 pub fn sep_type(mut self, t: SeparatorType) -> Self {
460 self.sep_type = t;
461 self
462 }
463 pub fn hovered(mut self, on: bool) -> Self {
465 self.hovered = on;
466 self
467 }
468 pub fn dragging(mut self, on: bool) -> Self {
470 self.dragging = on;
471 self
472 }
473 pub fn settings(mut self, s: SeparatorSettings) -> Self {
475 self.settings = Some(s);
476 self
477 }
478
479 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
481 let theme = theme_of(cx);
482 cx.with_paint(|render, widgets, _visual, _tokens| {
483 let view = SeparatorView {
484 kind: self.sep_type,
485 hovered: self.hovered,
486 dragging: self.dragging,
487 };
488 let settings = match self.settings {
489 Some(settings) => settings,
490 None => SeparatorSettings {
491 theme: Box::new(theme),
492 style: Box::new(DefaultSeparatorStyle),
493 },
494 };
495 paint_separator(
496 render, widgets, self.id, self.rect, self.kind, &view, &settings,
497 );
498 });
499 }
500}
501
502pub struct ChevronBuilder<'a> {
506 id: WidgetId,
507 rect: Rect,
508 view: ChevronView,
509 settings: Option<ChevronSettings>,
510 on_click: Option<Box<dyn FnOnce() + 'a>>,
511}
512
513pub fn chevron<'a>(id: impl Into<WidgetId>, rect: Rect) -> ChevronBuilder<'a> {
515 ChevronBuilder {
516 id: id.into(),
517 rect,
518 view: ChevronView {
519 direction: ChevronDirection::Down,
520 use_case: ChevronUseCase::PureButton,
521 visibility: VisibilityPolicy::Always,
522 placement: PlacementPolicy::Standalone,
523 hit_area: HitAreaPolicy::Visual,
524 visual_kind: ChevronVisualKind::Stroked,
525 hovered: false,
526 pressed: false,
527 disabled: false,
528 active: false,
529 glyph_override: None,
530 },
531 settings: None,
532 on_click: None,
533 }
534}
535
536impl<'a> ChevronBuilder<'a> {
537 pub fn direction(mut self, d: ChevronDirection) -> Self {
539 self.view.direction = d;
540 self
541 }
542 pub fn hovered(mut self, on: bool) -> Self {
544 self.view.hovered = on;
545 self
546 }
547 pub fn pressed(mut self, on: bool) -> Self {
549 self.view.pressed = on;
550 self
551 }
552 pub fn disabled(mut self, on: bool) -> Self {
554 self.view.disabled = on;
555 self
556 }
557 pub fn on_click(mut self, f: impl FnOnce() + 'a) -> Self {
559 self.on_click = Some(Box::new(f));
560 self
561 }
562 pub fn settings(mut self, s: ChevronSettings) -> Self {
564 self.settings = Some(s);
565 self
566 }
567
568 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
570 let theme = theme_of(cx);
571 cx.with_paint(|render, widgets, visual, _tokens| {
572 if !self.view.disabled && visual.clicked(&self.id) {
573 if let Some(cb) = self.on_click {
574 cb();
575 }
576 }
577 let settings = match self.settings {
578 Some(settings) => settings,
579 None => ChevronSettings {
580 theme: Box::new(theme),
581 style: Box::new(DefaultChevronStyle),
582 },
583 };
584 paint_chevron(render, widgets, self.id, self.rect, &self.view, &settings);
585 });
586 }
587}
588
589pub struct TooltipBuilder {
593 id: WidgetId,
594 rect: Rect,
595 text: String,
596 anchor: Rect,
597 position: TooltipPosition,
598 alpha: f64,
599 settings: Option<TooltipSettings>,
600}
601
602pub fn tooltip(id: impl Into<WidgetId>, rect: Rect) -> TooltipBuilder {
604 TooltipBuilder {
605 id: id.into(),
606 rect,
607 text: String::new(),
608 anchor: Rect::new(0.0, 0.0, 0.0, 0.0),
609 position: TooltipPosition::Above,
610 alpha: 1.0,
611 settings: None,
612 }
613}
614
615impl TooltipBuilder {
616 pub fn text(mut self, t: impl Into<String>) -> Self {
618 self.text = t.into();
619 self
620 }
621 pub fn anchor(mut self, r: Rect) -> Self {
623 self.anchor = r;
624 self
625 }
626 pub fn position(mut self, p: TooltipPosition) -> Self {
628 self.position = p;
629 self
630 }
631 pub fn alpha(mut self, a: f64) -> Self {
633 self.alpha = a;
634 self
635 }
636 pub fn settings(mut self, s: TooltipSettings) -> Self {
638 self.settings = Some(s);
639 self
640 }
641
642 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
644 let theme = theme_of(cx);
645 cx.with_paint(|render, widgets, _visual, _tokens| {
646 let config = TooltipConfig {
647 text: self.text,
648 lines: None,
649 anchor: self.anchor,
650 position: self.position,
651 };
652 let settings = match self.settings {
653 Some(settings) => settings,
654 None => TooltipSettings {
655 theme: Box::new(theme),
656 style: Box::new(DefaultTooltipStyle),
657 },
658 };
659 paint_tooltip(
660 render, widgets, self.id, self.rect, &config, self.alpha, &settings,
661 );
662 });
663 }
664}
665
666pub struct ChromeBuilder<'a> {
670 id: &'a str,
671 rect: Option<Rect>,
672 tabs: &'a [ChromeTabConfig<'a>],
673 active_tab_id: Option<&'a str>,
674 show_new_tab_btn: bool,
675 show_menu_btn: bool,
676 show_new_window_btn: bool,
677 show_close_window_btn: bool,
678 is_maximized: bool,
679 menu_left: bool,
680 show_maximize: bool,
681 cursor_x: f64,
682 cursor_y: f64,
683 time_ms: f64,
684 kind: ChromeRenderKind,
685 settings: Option<ChromeSettings>,
686}
687
688pub fn chrome<'a>() -> ChromeBuilder<'a> {
690 ChromeBuilder {
691 id: "chrome",
692 rect: None,
693 tabs: &[],
694 active_tab_id: None,
695 show_new_tab_btn: false,
696 show_menu_btn: false,
697 show_new_window_btn: false,
698 show_close_window_btn: false,
699 is_maximized: false,
700 menu_left: false,
701 show_maximize: true,
702 cursor_x: 0.0,
703 cursor_y: 0.0,
704 time_ms: 0.0,
705 kind: ChromeRenderKind::Default,
706 settings: None,
707 }
708}
709
710impl<'a> ChromeBuilder<'a> {
711 pub fn widget_id(mut self, id: &'a str) -> Self {
713 self.id = id;
714 self
715 }
716 pub fn rect(mut self, r: Rect) -> Self {
718 self.rect = Some(r);
719 self
720 }
721 pub fn tabs(mut self, ts: &'a [ChromeTabConfig<'a>]) -> Self {
723 self.tabs = ts;
724 self
725 }
726 pub fn active_tab(mut self, id: &'a str) -> Self {
728 self.active_tab_id = Some(id);
729 self
730 }
731 pub fn show_new_tab_btn(mut self, on: bool) -> Self {
733 self.show_new_tab_btn = on;
734 self
735 }
736 pub fn show_menu_btn(mut self, on: bool) -> Self {
738 self.show_menu_btn = on;
739 self
740 }
741 pub fn show_new_window_btn(mut self, on: bool) -> Self {
743 self.show_new_window_btn = on;
744 self
745 }
746 pub fn cursor(mut self, pos: (f64, f64)) -> Self {
748 self.cursor_x = pos.0;
749 self.cursor_y = pos.1;
750 self
751 }
752 pub fn time_ms(mut self, t: f64) -> Self {
754 self.time_ms = t;
755 self
756 }
757 pub fn kind(mut self, k: ChromeRenderKind) -> Self {
760 self.kind = k;
761 self
762 }
763
764 pub fn build<'c, C: ContentCx<'c>>(self, cx: &mut C) {
766 let theme = theme_of(cx);
767 cx.with_paint(|render, widgets, _visual, _tokens| {
768 let view = ChromeView {
769 tabs: self.tabs,
770 active_tab_id: self.active_tab_id,
771 show_new_tab_btn: self.show_new_tab_btn,
772 show_menu_btn: self.show_menu_btn,
773 show_new_window_btn: self.show_new_window_btn,
774 show_close_window_btn: self.show_close_window_btn,
775 is_maximized: self.is_maximized,
776 menu_left: self.menu_left,
777 show_maximize: self.show_maximize,
778 cursor_x: self.cursor_x,
779 cursor_y: self.cursor_y,
780 time_ms: self.time_ms,
781 };
782 let settings = self.settings.unwrap_or_else(|| ChromeSettings {
783 theme: Box::new(theme),
784 style: Box::<DefaultChromeStyle>::default(),
785 });
786 let mut state = ChromeState::new();
787 let ids: Vec<&str> = self.tabs.iter().map(|t| t.id).collect();
788 state.sync_tabs(&ids);
789 let rect = self.rect.unwrap_or_else(|| {
790 let (w, h) = measure_chrome(&view, &state, &settings, &self.kind);
791 Rect::new(0.0, 0.0, w, h)
792 });
793 super::composites::chrome_paint(
794 render,
795 widgets,
796 WidgetId::from(self.id),
797 rect,
798 &state,
799 &view,
800 &settings,
801 &self.kind,
802 );
803 });
804 }
805}