1use std::collections::BTreeMap;
22
23use super::{DrawList, FontAtlas, Rect, Rgba, Theme};
24
25pub fn snap(r: Rect) -> Rect {
28 let x = r.x.floor();
29 let y = r.y.floor();
30 let right = r.right().ceil();
31 let bottom = r.bottom().ceil();
32 Rect::new(x, y, (right - x).max(0.0), (bottom - y).max(0.0))
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
37pub struct Scrub {
38 pub dragging: bool,
40 pub ended: bool,
42 pub delta: f32,
44}
45
46pub const SCRUB_THRESHOLD_PX: f32 = 4.0;
48
49pub const SCRUB_SHIFT_SPEED: f32 = 10.0;
52
53pub const SCRUB_CTRL_SPEED: f32 = 0.1;
55
56#[derive(Clone, Copy, Debug, Default, PartialEq)]
58pub struct DragInfo {
59 pub claimed: bool,
61 pub moved: bool,
63 pub ended: bool,
65 pub dx: f32,
67 pub dy: f32,
68}
69
70#[derive(Clone, Debug, Default)]
73pub struct UiInput {
74 pub pointer: Option<[f32; 2]>,
76 pub shift: bool,
78 pub ctrl: bool,
80 pub pressed: bool,
82 pub released: bool,
84 pub scroll: f32,
86 pub chars: String,
88 pub backspace: bool,
89 pub enter: bool,
90}
91
92#[derive(Clone, Debug, Default)]
94pub struct UiState {
95 active: Option<u64>,
96 focus: Option<u64>,
97 scroll: BTreeMap<u64, f32>,
98 open: BTreeMap<u64, bool>,
99 drag_origin: Option<[f32; 2]>,
101 drag_last: Option<[f32; 2]>,
104 focus_rect: Option<Rect>,
107}
108
109impl UiState {
110 pub fn has_focus(&self) -> bool {
113 self.focus.is_some()
114 }
115}
116
117#[derive(Clone, Copy, Debug, Default, PartialEq)]
119pub struct TreeResponse {
120 pub clicked: bool,
121 pub toggled: bool,
122}
123
124fn hash_id(key: &str) -> u64 {
126 let mut h: u64 = 0xcbf2_9ce4_8422_2325;
127 for b in key.as_bytes() {
128 h ^= *b as u64;
129 h = h.wrapping_mul(0x0000_0100_0000_01b3);
130 }
131 h
132}
133
134pub fn fit(atlas: &FontAtlas, font: usize, text: &str, max_w: f32) -> String {
136 if atlas.measure(font, text, 0.0)[0] <= max_w {
137 return text.to_string();
138 }
139 let mut best = String::new();
140 for ch in text.chars() {
141 best.push(ch);
142 if atlas.measure(font, &format!("{best}..."), 0.0)[0] > max_w {
143 best.pop();
144 break;
145 }
146 }
147 best.push_str("...");
148 best
149}
150
151pub struct Ui<'a> {
155 pub draw: DrawList,
156 pub theme: &'a Theme,
157 atlas: &'a FontAtlas,
158 small: Option<&'a FontAtlas>,
161 state: &'a mut UiState,
162 input: &'a UiInput,
163 x: f32,
164 y: f32,
165 w: f32,
166 indent: f32,
167}
168
169impl<'a> Ui<'a> {
170 pub fn new(
171 area: Rect,
172 theme: &'a Theme,
173 atlas: &'a FontAtlas,
174 small: Option<&'a FontAtlas>,
175 state: &'a mut UiState,
176 input: &'a UiInput,
177 ) -> Self {
178 Self {
179 draw: DrawList::new(area),
180 theme,
181 atlas,
182 small,
183 state,
184 input,
185 x: area.x,
186 y: area.y,
187 w: area.w,
188 indent: 0.0,
189 }
190 }
191
192 pub fn end(&mut self) {
194 if self.input.released {
195 self.state.active = None;
196 self.state.drag_origin = None;
197 self.state.drag_last = None;
198 }
199 }
200
201 pub fn row(&mut self, h: f32) -> Rect {
205 let r = snap(Rect::new(
206 self.x + self.indent,
207 self.y,
208 (self.w - self.indent).max(0.0),
209 h,
210 ));
211 self.y = r.bottom();
212 r
213 }
214
215 pub fn space(&mut self, h: f32) {
217 self.y += h;
218 }
219
220 pub fn push_indent(&mut self, px: f32) {
222 self.indent += px;
223 }
224
225 pub fn pop_indent(&mut self, px: f32) {
226 self.indent = (self.indent - px).max(0.0);
227 }
228
229 fn interact(&mut self, id: &str, r: Rect) -> (bool, bool) {
236 let hid = hash_id(id);
237 let under = self.input.pointer.is_some_and(|p| r.contains(p[0], p[1]));
238 if under && self.input.pressed && self.state.active.is_none() && self.state.focus.is_none()
239 {
240 self.state.active = Some(hid);
241 }
242 let clicked = self.input.released && self.state.active == Some(hid) && under;
243 let hovered = under || self.state.active == Some(hid);
244 (hovered, clicked)
245 }
246
247 fn is_active(&self, id: &str) -> bool {
248 self.state.active == Some(hash_id(id))
249 }
250
251 pub fn pointer(&self) -> Option<[f32; 2]> {
255 self.input.pointer
256 }
257
258 pub fn state(&self) -> &UiState {
260 self.state
261 }
262
263 pub fn is_held(&self, id: &str) -> bool {
265 self.state.active == Some(hash_id(id))
266 }
267
268 pub fn drag(&mut self, id: &str, grab: Rect, min_px: f32) -> DragInfo {
278 let hid = hash_id(id);
279 let p = self.input.pointer;
280 if self.state.active == Some(hid) {
281 let total = self
282 .state
283 .drag_origin
284 .and_then(|o| p.map(|c| (c[0] - o[0]).abs() + (c[1] - o[1]).abs()))
285 .unwrap_or(0.0);
286 let (dx, dy) = match (self.state.drag_last, p) {
287 (Some(a), Some(b)) => (b[0] - a[0], b[1] - a[1]),
288 _ => (0.0, 0.0),
289 };
290 self.state.drag_last = p;
291 DragInfo {
292 claimed: true,
293 moved: total >= min_px.max(0.0),
294 ended: self.input.released,
295 dx,
296 dy,
297 }
298 } else {
299 let under = p.is_some_and(|c| grab.contains(c[0], c[1]));
300 let claim = under && self.input.pressed && self.state.active.is_none();
301 if claim {
302 if self.state.focus.is_some()
303 && !self
304 .state
305 .focus_rect
306 .is_some_and(|r| p.is_some_and(|c| r.contains(c[0], c[1])))
307 {
308 self.state.focus = None;
309 self.state.focus_rect = None;
310 }
311 self.state.active = Some(hid);
312 self.state.drag_origin = p;
313 self.state.drag_last = p;
314 DragInfo {
315 claimed: true,
316 moved: min_px <= 0.0,
317 ended: self.input.released,
318 dx: 0.0,
319 dy: 0.0,
320 }
321 } else {
322 DragInfo::default()
323 }
324 }
325 }
326
327 pub fn scrub(&mut self, id: &str, grab: Rect, step_per_px: f32) -> Scrub {
336 let d = self.drag(id, grab, SCRUB_THRESHOLD_PX);
337 if !d.claimed || !d.moved {
338 return Scrub::default();
339 }
340 let speed = if self.input.ctrl {
341 SCRUB_CTRL_SPEED
342 } else if self.input.shift {
343 SCRUB_SHIFT_SPEED
344 } else {
345 1.0
346 };
347 Scrub {
348 dragging: !d.ended,
349 ended: d.ended,
350 delta: d.dx * step_per_px * speed,
351 }
352 }
353
354 fn pick(&self, small: bool, bold: bool) -> (&'a FontAtlas, usize) {
357 let atlas = if small {
358 self.small.unwrap_or(self.atlas)
359 } else {
360 self.atlas
361 };
362 let font = if bold && atlas.font_count() > 1 { 1 } else { 0 };
363 (atlas, font)
364 }
365
366 fn text_centered(&mut self, r: Rect, text: &str, color: Rgba, small: bool, bold: bool) {
369 let (atlas, font) = self.pick(small, bold);
370 let line_h = atlas.measure(font, "Ag", 0.0)[1];
371 let [tw, th] = atlas.measure(font, text, line_h);
372 let x = r.x + ((r.w - tw) * 0.5).max(0.0);
373 let y = r.y + ((r.h - th) * 0.5).max(0.0);
374 self.draw.text(atlas, font, text, x, y, line_h, color);
375 }
376
377 fn text_left(&mut self, r: Rect, text: &str, color: Rgba, small: bool, bold: bool) {
379 let (atlas, font) = self.pick(small, bold);
380 let line_h = atlas.measure(font, "Ag", 0.0)[1];
381 let shown = fit(atlas, font, text, r.w);
382 let th = atlas.measure(font, &shown, line_h)[1];
383 let y = r.y + ((r.h - th) * 0.5).max(0.0);
384 self.draw.text(atlas, font, &shown, r.x, y, line_h, color);
385 }
386
387 fn triangle(&mut self, r: Rect, down: bool, color: Rgba) {
392 let r = snap(r);
393 if down {
394 let n = (r.h as i32).max(1);
395 for i in 0..n {
396 let t = 1.0 - i as f32 / n as f32;
398 let cx = (r.x + r.w * 0.5).round();
399 let hw = (r.w * 0.5 * t).round();
400 self.draw
401 .line(cx - hw, r.y + i as f32, cx + hw, r.y + i as f32, 1.0, color);
402 }
403 } else {
404 let n = (r.w as i32).max(1);
405 for i in 0..n {
406 let t = 1.0 - i as f32 / n as f32;
407 let cy = (r.y + r.h * 0.5).round();
408 let hh = (r.h * t).round();
409 let (y0, y1) = ((cy - hh * 0.5).round(), (cy + hh * 0.5).round());
410 self.draw
411 .line(r.x + i as f32, y0, r.x + i as f32, y1, 1.0, color);
412 }
413 }
414 }
415
416 pub fn label(&mut self, r: Rect, text: &str) {
420 let p = self.theme.metrics.pad;
421 let c = self.theme.colors.text;
422 self.text_left(
423 Rect::new(r.x + p, r.y, (r.w - p * 2.0).max(0.0), r.h),
424 text,
425 c,
426 false,
427 false,
428 );
429 }
430
431 pub fn button(&mut self, id: &str, r: Rect, text: &str) -> bool {
433 let r = snap(r);
434 let (hovered, clicked) = self.interact(id, r);
435 let th = self.theme;
436 let bg = if self.is_active(id) && hovered {
437 th.colors.select
438 } else if hovered {
439 th.colors.hover
440 } else {
441 th.colors.header
442 };
443 self.draw.rect(r, bg);
444 self.draw.stroke(r, 1.0, th.colors.border);
445 self.text_centered(r, text, th.colors.text, false, false);
446 clicked
447 }
448
449 pub fn selectable(&mut self, id: &str, r: Rect, text: &str, selected: bool) -> bool {
451 let r = snap(r);
452 let (hovered, clicked) = self.interact(id, r);
453 let th = self.theme;
454 if selected {
455 self.draw.rect(r, th.colors.select);
456 } else if hovered {
457 self.draw.rect(r, th.colors.hover);
458 }
459 let p = th.metrics.pad;
460 self.text_left(
461 Rect::new(r.x + p, r.y, (r.w - p * 2.0).max(0.0), r.h),
462 text,
463 th.colors.text,
464 false,
465 false,
466 );
467 clicked
468 }
469
470 pub fn tree_row(
474 &mut self,
475 id: &str,
476 r: Rect,
477 text: &str,
478 depth: usize,
479 selected: bool,
480 has_children: bool,
481 ) -> TreeResponse {
482 let r = snap(r);
483 let th = self.theme;
484 let step = th.metrics.pad * 2.0;
485 let x0 = r.x + depth as f32 * step;
486 let mut toggled = false;
490 let mut arrow: Option<(Rect, bool, bool)> = None;
491 let mut row_x = x0;
492 if has_children {
493 let a = Rect::new(x0, r.y, step, r.h);
494 let (ah, ac) = self.interact(&format!("{id}/pfeil"), a);
495 toggled = ac;
496 if ac {
497 self.toggle(id, true);
498 }
499 arrow = Some((a, ah, self.is_open(id, true)));
500 row_x = a.right();
501 }
502 let (hovered, clicked) =
503 self.interact(id, Rect::new(row_x, r.y, (r.right() - row_x).max(0.0), r.h));
504 let mut tx = x0 + th.metrics.pad;
505 if let Some((a, _, _)) = arrow {
506 tx = a.right();
507 }
508 if selected {
511 self.draw.rect(r, th.colors.select);
512 } else if hovered {
513 self.draw.rect(r, th.colors.hover);
514 }
515 if let Some((a, ah, open)) = arrow {
516 let color = if ah || hovered {
517 th.colors.text
518 } else {
519 th.colors.text_dim
520 };
521 self.triangle(
522 Rect::new(a.x + 3.0, a.y + a.h * 0.5 - 4.0, 8.0, 8.0),
523 open,
524 color,
525 );
526 }
527 self.text_left(
528 Rect::new(tx, r.y, (r.right() - tx - th.metrics.pad).max(0.0), r.h),
529 text,
530 th.colors.text,
531 false,
532 false,
533 );
534 TreeResponse { clicked, toggled }
535 }
536
537 pub fn is_open(&mut self, id: &str, default: bool) -> bool {
539 *self.state.open.entry(hash_id(id)).or_insert(default)
540 }
541
542 pub fn toggle(&mut self, id: &str, default: bool) {
544 let cur = *self.state.open.get(&hash_id(id)).unwrap_or(&default);
545 self.state.open.insert(hash_id(id), !cur);
546 }
547
548 pub fn header(&mut self, id: &str, r: Rect, text: &str) -> bool {
551 let r = snap(r);
552 let (hovered, clicked) = self.interact(id, r);
553 if clicked {
554 self.toggle(id, true);
555 }
556 let th = self.theme;
557 self.draw.rect(r, th.colors.header);
558 if hovered {
559 self.draw.rect(r, th.colors.hover);
560 }
561 let open = self.is_open(id, true);
562 self.triangle(
563 Rect::new(r.x + th.metrics.pad, r.y + r.h * 0.5 - 4.0, 8.0, 8.0),
564 open,
565 th.colors.text_dim,
566 );
567 let tx = r.x + th.metrics.pad + 8.0 + th.metrics.pad * 0.5;
568 self.text_left(
569 Rect::new(tx, r.y, (r.right() - tx - th.metrics.pad).max(0.0), r.h),
570 text,
571 th.colors.text,
572 false,
573 true,
574 );
575 open
576 }
577
578 pub fn tab(&mut self, id: &str, r: Rect, text: &str, active: bool) -> bool {
581 let r = snap(r);
582 let (hovered, clicked) = self.interact(id, r);
583 let th = self.theme;
584 self.draw.rect(
585 r,
586 if active {
587 th.colors.panel
588 } else {
589 th.colors.background
590 },
591 );
592 if hovered && !active {
593 self.draw.rect(r, th.colors.hover);
594 }
595 if active {
596 self.draw
597 .rect(Rect::new(r.x, r.bottom() - 2.0, r.w, 2.0), th.colors.accent);
598 }
599 self.text_centered(
600 r,
601 text,
602 if active {
603 th.colors.text
604 } else {
605 th.colors.text_dim
606 },
607 true,
608 active,
609 );
610 clicked
611 }
612
613 pub fn tab_drag(
619 &mut self,
620 id: &str,
621 r: Rect,
622 text: &str,
623 active: bool,
624 min_px: f32,
625 ) -> (bool, DragInfo) {
626 let r = snap(r);
627 let d = self.drag(id, r, min_px);
628 let under = self.input.pointer.is_some_and(|p| r.contains(p[0], p[1]));
629 let clicked = d.claimed && d.ended && !d.moved && under;
630 let th = self.theme;
631 self.draw.rect(
632 r,
633 if active {
634 th.colors.panel
635 } else {
636 th.colors.background
637 },
638 );
639 if under && !d.moved && !active {
640 self.draw.rect(r, th.colors.hover);
641 }
642 if active {
643 self.draw
644 .rect(Rect::new(r.x, r.bottom() - 2.0, r.w, 2.0), th.colors.accent);
645 }
646 self.text_centered(
647 r,
648 text,
649 if active {
650 th.colors.text
651 } else {
652 th.colors.text_dim
653 },
654 true,
655 active,
656 );
657 (clicked, d)
658 }
659
660 pub fn checkbox(&mut self, id: &str, r: Rect, checked: bool, text: &str) -> bool {
663 let r = snap(r);
664 let (hovered, clicked) = self.interact(id, r);
665 let th = self.theme;
666 if hovered {
667 self.draw.rect(r, th.colors.hover);
668 }
669 let s = th.metrics.icon_px.min(r.h - 4.0).max(4.0);
670 let bx = snap(Rect::new(r.x + th.metrics.pad, r.y + (r.h - s) * 0.5, s, s));
671 self.draw.rect(bx, th.colors.input);
672 self.draw.stroke(bx, 1.0, th.colors.border);
673 if checked {
674 self.draw.rect(
675 Rect::new(
676 bx.x + 2.0,
677 bx.y + 2.0,
678 (bx.w - 4.0).max(1.0),
679 (bx.h - 4.0).max(1.0),
680 ),
681 th.colors.accent,
682 );
683 }
684 let tx = bx.right() + th.metrics.pad * 0.5;
685 self.text_left(
686 Rect::new(tx, r.y, (r.right() - tx - th.metrics.pad).max(0.0), r.h),
687 text,
688 th.colors.text,
689 false,
690 false,
691 );
692 clicked
693 }
694
695 pub fn text_field(&mut self, id: &str, r: Rect, value: &mut String) -> bool {
698 let r = snap(r);
699 let hid = hash_id(id);
700 let under = self.input.pointer.is_some_and(|p| r.contains(p[0], p[1]));
701 if under && self.input.pressed {
702 self.state.focus = Some(hid);
703 self.state.focus_rect = Some(r);
704 } else if self.input.pressed && self.state.focus == Some(hid) {
705 self.state.focus = None;
706 self.state.focus_rect = None;
707 }
708 let focused = self.state.focus == Some(hid);
709 if focused {
710 self.state.focus_rect = Some(r);
711 }
712 let mut edited = false;
713 if focused {
714 if self.input.backspace && !value.is_empty() {
715 value.pop();
716 edited = true;
717 }
718 for ch in self.input.chars.chars() {
719 if !ch.is_control() {
720 value.push(ch);
721 edited = true;
722 }
723 }
724 if self.input.enter {
725 self.state.focus = None;
726 self.state.focus_rect = None;
727 }
728 }
729 let th = self.theme;
730 self.draw.rect(r, th.colors.input);
731 self.draw.stroke(
732 r,
733 1.0,
734 if focused {
735 th.colors.accent
736 } else {
737 th.colors.border
738 },
739 );
740 let p = th.metrics.pad;
741 let inner = Rect::new(r.x + p, r.y, (r.w - p * 2.0).max(0.0), r.h);
742 let (atlas, font) = self.pick(false, false);
743 let shown = fit(atlas, font, value, inner.w);
744 let line_h = atlas.measure(font, "Ag", 0.0)[1];
745 let ty = r.y + ((r.h - line_h) * 0.5).max(0.0);
746 self.draw
747 .text(atlas, font, &shown, inner.x, ty, line_h, th.colors.text);
748 if focused {
749 let w = atlas.measure(font, &shown, line_h)[0];
750 let cx = (inner.x + w + 1.0).round();
751 self.draw.rect(
752 Rect::new(cx, r.y + 3.0, 1.0, (r.h - 6.0).max(1.0)),
753 th.colors.text,
754 );
755 }
756 edited
757 }
758
759 pub fn scroll(&mut self, id: &str, r: Rect, content_h: f32) -> f32 {
764 let hid = hash_id(id);
765 let th = self.theme;
766 let max = (content_h - r.h).max(0.0);
767 let under = self.input.pointer.is_some_and(|p| r.contains(p[0], p[1]));
768 let off = self.state.scroll.entry(hid).or_insert(0.0);
769 if under {
770 *off = (*off + self.input.scroll).clamp(0.0, max);
771 } else {
772 *off = off.clamp(0.0, max);
773 }
774 if max > 0.0 {
775 let hh = (r.h * r.h / content_h).max(20.0);
776 let ty = r.y + (r.h - hh) * (*off / max);
777 self.draw.rect(
778 snap(Rect::new(r.right() - 5.0, ty, 4.0, hh)),
779 th.colors.border,
780 );
781 }
782 *off
783 }
784
785 pub fn separator(&mut self, r: Rect) {
787 let c = self.theme.colors.border;
788 let y = (r.y + r.h * 0.5).floor();
789 self.draw.line(r.x.floor(), y, r.right().ceil(), y, 1.0, c);
790 }
791}
792
793#[cfg(test)]
796mod tests {
797 use super::*;
798
799 const REGULAR: &[u8] = include_bytes!("../../../../assets/fonts/inter/Inter-Regular.ttf");
800 const BOLD: &[u8] = include_bytes!("../../../../assets/fonts/inter/Inter-Bold.ttf");
801
802 fn atlas() -> FontAtlas {
803 FontAtlas::build(&[REGULAR, BOLD], 15.0, "äöüß", 512, 512).expect("Atlas")
804 }
805
806 fn inp(pointer: Option<[f32; 2]>, pressed: bool, released: bool) -> UiInput {
807 UiInput {
808 pointer,
809 pressed,
810 released,
811 ..Default::default()
812 }
813 }
814
815 fn area() -> Rect {
816 Rect::new(0.0, 0.0, 200.0, 120.0)
817 }
818
819 #[test]
820 fn button_klickt_erst_beim_release_innerhalb() {
821 let a = atlas();
822 let t = Theme::default();
823 let mut st = UiState::default();
824 let r = Rect::new(0.0, 0.0, 50.0, 20.0);
825 {
826 let i = inp(Some([10.0, 10.0]), true, false);
827 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
828 assert!(!ui.button("ok", r, "OK"), "Press allein klickt nicht");
829 ui.end();
830 }
831 {
832 let i = inp(Some([10.0, 10.0]), false, true);
833 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
834 assert!(ui.button("ok", r, "OK"), "Release innerhalb klickt");
835 ui.end();
836 }
837 {
838 let i = inp(Some([10.0, 10.0]), true, false);
840 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
841 ui.button("ok", r, "OK");
842 ui.end();
843 let i = inp(Some([150.0, 100.0]), false, true);
844 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
845 assert!(!ui.button("ok", r, "OK"), "Release außerhalb bricht ab");
846 ui.end();
847 }
848 }
849
850 #[test]
851 fn tab_drag_klick_ohne_weg_aktiviert_mit_weg_dragt() {
852 let a = atlas();
853 let t = Theme::default();
854 let mut st = UiState::default();
855 let r = Rect::new(0.0, 0.0, 90.0, 24.0);
856 {
858 let i = inp(Some([10.0, 10.0]), true, false);
859 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
860 let (clicked, d) = ui.tab_drag("tab/x", r, "X", false, 6.0);
861 assert!(
862 d.claimed && !d.moved && !d.ended && !clicked,
863 "Press allein noch nicht"
864 );
865 ui.end();
866 }
867 {
868 let i = inp(Some([12.0, 11.0]), false, true);
869 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
870 let (clicked, d) = ui.tab_drag("tab/x", r, "X", false, 6.0);
871 assert!(d.ended && !d.moved && clicked, "Klick mit Mini-Wackeln");
872 ui.end();
873 }
874 {
876 let i = inp(Some([10.0, 10.0]), true, false);
877 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
878 ui.tab_drag("tab/y", r, "Y", false, 6.0);
879 ui.end();
880 }
881 {
882 let i = inp(Some([90.0, 60.0]), false, false);
883 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
884 let (clicked, d) = ui.tab_drag("tab/y", r, "Y", false, 6.0);
885 assert!(d.claimed && d.moved && !d.ended && !clicked, "Drag läuft");
886 ui.end();
887 }
888 {
889 let i = inp(Some([90.0, 60.0]), false, true);
890 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
891 let (clicked, d) = ui.tab_drag("tab/y", r, "Y", false, 6.0);
892 assert!(d.ended && d.moved && !clicked, "Drag endet ohne Klick");
893 ui.end();
894 }
895 }
896
897 #[test]
898 fn laufender_drag_wehrt_andere_widgets_am_klick_ab() {
899 let a = atlas();
900 let t = Theme::default();
901 let mut st = UiState::default();
902 let grab = Rect::new(0.0, 0.0, 6.0, 100.0);
903 let btn = Rect::new(100.0, 0.0, 60.0, 24.0);
904 {
905 let i = inp(Some([2.0, 50.0]), true, false);
906 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
907 assert!(ui.drag("split", grab, 0.0).claimed, "Splitter greift");
908 assert!(!ui.button("ok", btn, "OK"));
909 ui.end();
910 }
911 {
912 let i = inp(Some([120.0, 10.0]), false, true);
915 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
916 let d = ui.drag("split", grab, 0.0);
917 assert!(d.claimed && d.moved && d.ended);
918 assert!(!ui.button("ok", btn, "OK"), "Release klickt nicht durch");
919 ui.end();
920 }
921 }
922
923 #[test]
924 fn auswahl_und_hover_malen_theme_farben() {
925 let a = atlas();
926 let t = Theme::default();
927 let mut st = UiState::default();
928 let i = inp(Some([10.0, 30.0]), false, false);
929 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
930 ui.selectable("z", Rect::new(0.0, 0.0, 100.0, 24.0), "Zeile", true);
931 ui.selectable("x", Rect::new(0.0, 24.0, 100.0, 24.0), "Andere", false);
932 let farben: Vec<Rgba> = ui
933 .draw
934 .cmds()
935 .iter()
936 .filter(|c| !c.textured)
937 .flat_map(|c| c.vertices.iter())
938 .map(|v| v.color)
939 .collect();
940 assert!(
941 farben.contains(&t.colors.select),
942 "Auswahl nutzt select-Farbe"
943 );
944 assert!(farben.contains(&t.colors.hover), "Hover nutzt hover-Farbe");
945 }
946
947 #[test]
948 fn baum_pfeil_klappt_um_ohne_zeilenklick() {
949 let a = atlas();
950 let t = Theme::default();
951 let mut st = UiState::default();
952 let r = Rect::new(0.0, 0.0, 150.0, 24.0);
953 {
955 let i = inp(Some([6.0, 12.0]), true, false);
956 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
957 let ant = ui.tree_row("ordner", r, "Content", 0, false, true);
958 assert!(!ant.toggled, "Press allein kippt nicht");
959 ui.end();
960 }
961 let ant = {
963 let i = inp(Some([6.0, 12.0]), false, true);
964 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
965 let ant = ui.tree_row("ordner", r, "Content", 0, false, true);
966 ui.end();
967 ant
968 };
969 assert!(ant.toggled && !ant.clicked, "Dreieck kippt nur");
970 assert!(!st.open[&hash_id("ordner")], "offen → zu");
971 {
973 let i = inp(Some([80.0, 12.0]), true, false);
974 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
975 ui.tree_row("ordner", r, "Content", 0, false, true);
976 ui.end();
977 }
978 let ant = {
979 let i = inp(Some([80.0, 12.0]), false, true);
980 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
981 let ant = ui.tree_row("ordner", r, "Content", 0, false, true);
982 ui.end();
983 ant
984 };
985 assert!(ant.clicked && !ant.toggled, "Zeile klickt nur");
986 assert!(!st.open[&hash_id("ordner")], "Zustand bleibt");
987 }
988
989 #[test]
990 fn scroll_klemmt_und_reagiert_nur_unter_der_flaeche() {
991 let a = atlas();
992 let t = Theme::default();
993 let mut st = UiState::default();
994 let r = Rect::new(0.0, 0.0, 100.0, 100.0);
995 {
996 let mut i = inp(Some([50.0, 50.0]), false, false);
998 i.scroll = 350.0;
999 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1000 assert_eq!(ui.scroll("liste", r, 400.0), 300.0);
1001 ui.end();
1002 }
1003 {
1004 let mut i = inp(Some([150.0, 110.0]), false, false);
1006 i.scroll = -100.0;
1007 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1008 assert_eq!(ui.scroll("liste", r, 400.0), 300.0);
1009 ui.end();
1010 }
1011 {
1012 let mut i = inp(Some([50.0, 50.0]), false, false);
1014 i.scroll = -500.0;
1015 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1016 assert_eq!(ui.scroll("liste", r, 400.0), 0.0);
1017 ui.end();
1018 }
1019 }
1020
1021 #[test]
1022 fn textfeld_fokus_eingabe_enter() {
1023 let a = atlas();
1024 let t = Theme::default();
1025 let mut st = UiState::default();
1026 let mut wert = String::new();
1027 let r = Rect::new(0.0, 0.0, 120.0, 24.0);
1028 {
1029 let i = inp(Some([10.0, 10.0]), true, true);
1030 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1031 ui.text_field("pfad", r, &mut wert);
1032 ui.end();
1033 }
1034 {
1035 let mut i = inp(Some([10.0, 10.0]), false, false);
1036 i.chars = "hi".into();
1037 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1038 assert!(ui.text_field("pfad", r, &mut wert));
1039 ui.end();
1040 }
1041 assert_eq!(wert, "hi");
1042 {
1043 let mut i = inp(Some([10.0, 10.0]), false, false);
1044 i.backspace = true;
1045 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1046 assert!(ui.text_field("pfad", r, &mut wert));
1047 ui.end();
1048 }
1049 assert_eq!(wert, "h");
1050 {
1051 let mut i = inp(Some([10.0, 10.0]), false, false);
1052 i.enter = true;
1053 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1054 ui.text_field("pfad", r, &mut wert);
1055 ui.end();
1056 }
1057 assert_eq!(wert, "h");
1058 assert_eq!(st.focus, None, "Enter verliert den Fokus");
1059 {
1060 let mut i = inp(Some([10.0, 10.0]), false, false);
1062 i.chars = "x".into();
1063 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1064 assert!(!ui.text_field("pfad", r, &mut wert));
1065 ui.end();
1066 }
1067 assert_eq!(wert, "h", "fokussloses Tippen ändert nichts");
1068 }
1069
1070 #[test]
1071 fn fit_kuerzt_mit_drei_punkten() {
1072 let a = atlas();
1073 let lang = "Ein sehr langer Panelname fuer die Kuerzung";
1074 let kurz = fit(&a, 0, lang, 60.0);
1075 assert!(kurz.ends_with("..."), "KI: {kurz}");
1076 assert!(a.measure(0, &kurz, 0.0)[0] <= 60.0, "muss in 60 px passen");
1077 assert_eq!(fit(&a, 0, "kurz", 60.0), "kurz");
1078 }
1079
1080 #[test]
1081 fn alle_widget_kanten_rasten_auf_pixel() {
1082 let a = atlas();
1083 let t = Theme::default();
1084 let mut st = UiState::default();
1085 let mut wert = String::from("Test");
1086 let i = inp(Some([7.3, 9.7]), false, false);
1087 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1088 ui.header("k", Rect::new(0.4, 0.2, 199.1, 23.7), "Kategorie");
1090 ui.button("b", Rect::new(10.5, 30.1, 44.3, 19.8), "OK");
1091 ui.tab("t", Rect::new(60.9, 0.5, 50.2, 25.4), "Tab", true);
1092 ui.checkbox("c", Rect::new(10.2, 55.6, 90.4, 22.3), true, "Aktiv");
1093 ui.text_field("f", Rect::new(10.8, 80.9, 100.1, 23.2), &mut wert);
1094 ui.scroll("s", Rect::new(0.0, 0.0, 100.0, 50.0), 400.0);
1095 ui.separator(Rect::new(0.3, 110.7, 199.4, 0.0));
1096 for cmd in ui.draw.cmds() {
1097 if cmd.textured {
1098 continue; }
1100 for v in &cmd.vertices {
1101 assert_eq!(
1102 v.pos[0].fract(),
1103 0.0,
1104 "x-Kante {} nicht pixelgesnapst",
1105 v.pos[0]
1106 );
1107 assert_eq!(
1108 v.pos[1].fract(),
1109 0.0,
1110 "y-Kante {} nicht pixelgesnapst",
1111 v.pos[1]
1112 );
1113 }
1114 }
1115 }
1116
1117 #[test]
1118 fn frame_mit_widgets_rasterisiert_sichtbar() {
1119 let a = atlas();
1120 let t = Theme::default();
1121 let mut st = UiState::default();
1122 let i = inp(Some([30.0, 34.0]), false, false);
1123 let (w, h) = (320u32, 200u32);
1124 let mut ui = Ui::new(
1125 Rect::new(0.0, 0.0, w as f32, h as f32),
1126 &t,
1127 &a,
1128 None,
1129 &mut st,
1130 &i,
1131 );
1132 let kopf = ui.row(24.0);
1133 ui.header("k", kopf, "Outliner");
1134 let z1 = ui.row(24.0);
1135 ui.tree_row("a", z1, "Welt", 0, false, true);
1136 let z2 = ui.row(24.0);
1137 ui.tree_row("b", z2, "Licht", 1, true, false);
1138 let z3 = ui.row(24.0);
1139 ui.tree_row("c", z3, "Ein sehr langer Szenenobjektname", 1, false, false);
1140 let tab = ui.row(26.0);
1141 ui.tab("t", Rect::new(0.0, tab.y, 90.0, tab.h), "Details", true);
1142 ui.tab("t2", Rect::new(90.0, tab.y, 90.0, tab.h), "Konsole", false);
1143 let mut wert = String::from("r.ScreenPercentage 50");
1144 ui.text_field("f", Rect::new(6.0, 140.0, 150.0, 24.0), &mut wert);
1145 ui.button("ok", Rect::new(162.0, 140.0, 60.0, 24.0), "Ausführen");
1146 ui.separator(Rect::new(0.0, 175.0, w as f32, 1.0));
1147 let px = |c: Rgba| {
1148 [
1149 (c[0] * 255.0).round() as u8,
1150 (c[1] * 255.0).round() as u8,
1151 (c[2] * 255.0).round() as u8,
1152 255,
1153 ]
1154 };
1155 let bild = super::super::rasterize(&ui.draw, Some(&a), w, h, t.colors.background);
1156 let hat = |c: Rgba| bild.chunks_exact(4).any(|p| p == px(c));
1157 assert!(hat(t.colors.header), "Kopfzeile gemalt");
1158 assert!(hat(t.colors.select), "Auswahl gemalt");
1159 assert!(hat(t.colors.text), "Text gemalt");
1160 assert!(hat(t.colors.accent), "Tab-Akzent gemalt");
1161 if let Ok(p) = std::env::var("RE_WIDGETS_PNG") {
1162 use image::RgbaImage;
1163 RgbaImage::from_raw(w, h, bild)
1164 .expect("puffer")
1165 .save(&p)
1166 .expect("png");
1167 }
1168 }
1169
1170 #[test]
1171 fn scrubbt_erst_ab_der_schwelle_zaehlt_deltas() {
1172 let a = atlas();
1173 let t = Theme::default();
1174 let mut st = UiState::default();
1175 let grab = Rect::new(4.0, 4.0, 100.0, 24.0);
1176 let scrub_frame = |pointer: Option<[f32; 2]>,
1177 pressed: bool,
1178 released: bool,
1179 shift: bool,
1180 ctrl: bool,
1181 st: &mut UiState| {
1182 let i = UiInput {
1183 pointer,
1184 pressed,
1185 released,
1186 shift,
1187 ctrl,
1188 ..Default::default()
1189 };
1190 let mut ui = Ui::new(area(), &t, &a, None, st, &i);
1191 let s = ui.scrub("wert", grab, 0.1);
1192 ui.end();
1193 s
1194 };
1195 let s = scrub_frame(Some([10.0, 10.0]), true, false, false, false, &mut st);
1196 assert!(!s.dragging && s.delta == 0.0, "Press allein scrubbt nicht");
1197 let s = scrub_frame(Some([12.0, 10.0]), false, false, false, false, &mut st);
1198 assert!(!s.dragging && s.delta == 0.0, "2 px unter der Schwelle");
1199 let s = scrub_frame(Some([16.0, 10.0]), false, false, false, false, &mut st);
1200 assert!(s.dragging, "ab 4 px Weg wird geschrubbt");
1201 assert!(
1202 (s.delta - 0.4).abs() < 1e-6,
1203 "4 px x 0.1 = 0.4, war {}",
1204 s.delta
1205 );
1206 let s = scrub_frame(Some([16.0, 40.0]), false, false, false, false, &mut st);
1208 assert!(
1209 s.dragging && s.delta == 0.0,
1210 "senkrechter Weg ist null, war {}",
1211 s.delta
1212 );
1213 let s = scrub_frame(Some([16.0, 10.0]), false, true, false, false, &mut st);
1214 assert!(
1215 s.ended && !s.dragging && s.delta == 0.0,
1216 "Loslass-Frame ohne Bewegung meldet ende"
1217 );
1218 scrub_frame(Some([10.0, 10.0]), true, false, true, false, &mut st);
1220 let s = scrub_frame(Some([20.0, 10.0]), false, false, true, false, &mut st);
1221 assert!(
1222 s.dragging && (s.delta - 10.0).abs() < 1e-5,
1223 "shift beschleunigt, war {}",
1224 s.delta
1225 );
1226 scrub_frame(Some([10.0, 10.0]), true, false, false, true, &mut st);
1228 let s = scrub_frame(Some([20.0, 10.0]), false, false, false, true, &mut st);
1229 assert!(
1230 s.dragging && (s.delta - 0.1).abs() < 1e-6,
1231 "strg verfeinert, war {}",
1232 s.delta
1233 );
1234 }
1235
1236 #[test]
1237 fn scrub_drueckgabe_entlaesst_fokus_ausserhalb() {
1238 let a = atlas();
1239 let t = Theme::default();
1240 let mut st = UiState::default();
1241 let feld = Rect::new(6.0, 40.0, 120.0, 24.0);
1242 let griff = Rect::new(6.0, 8.0, 120.0, 24.0);
1243 {
1244 let i = inp(Some([10.0, 50.0]), true, false);
1245 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1246 let mut text = String::from("1");
1247 ui.text_field("feld", feld, &mut text);
1248 ui.end();
1249 }
1250 assert!(st.has_focus(), "Feld hat Fokus");
1251 {
1252 let i = inp(Some([10.0, 16.0]), true, false);
1255 let mut ui = Ui::new(area(), &t, &a, None, &mut st, &i);
1256 let mut text = String::from("1");
1257 ui.text_field("feld", feld, &mut text);
1258 let s = ui.scrub("wert", griff, 0.1);
1259 ui.end();
1260 assert!(ui.is_held("wert"), "Zieh-Fläche hält trotz Feld-Fokus");
1261 assert!(!ui.state().has_focus(), "Feld verliert den Fokus");
1262 assert!(!s.dragging, "ohne Weg wird nicht geschrubbt");
1263 }
1264 }
1265}