1use futtl::Futtl;
2use std::ops::{Add, Sub};
3use std::rc::Rc;
4use std::cell::Cell;
5
6pub const SINGLE_BORDER: BorderStyle = BorderStyle {
7 top: '─' as i32,
8 bottom: '─' as i32,
9 left: '│' as i32,
10 right: '│' as i32,
11 top_left: '┌' as i32,
12 top_right: '┐' as i32,
13 bottom_right: '┘' as i32,
14 bottom_left: '└' as i32,
15};
16
17pub const DOUBLE_BORDER: BorderStyle = BorderStyle {
18 top: '═' as i32,
19 bottom: '═' as i32,
20 left: '║' as i32,
21 right: '║' as i32,
22 top_left: '╔' as i32,
23 top_right: '╗' as i32,
24 bottom_right: '╝' as i32,
25 bottom_left: '╚' as i32,
26};
27
28pub const NO_BORDER: BorderStyle = BorderStyle {
29 top: '\0' as i32,
30 bottom: '\0' as i32,
31 left: '\0' as i32,
32 right: '\0' as i32,
33 top_left: '\0' as i32,
34 top_right: '\0' as i32,
35 bottom_right: '\0' as i32,
36 bottom_left: '\0' as i32,
37};
38
39pub struct IdGenerator {
40 next: u32,
41}
42
43impl Default for IdGenerator {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49impl IdGenerator {
50 pub fn new() -> Self {
51 Self { next: 0 }
52 }
53
54 pub fn next(&mut self) -> u32 {
55 let id = self.next;
56 self.next += 1;
57 id
58 }
59}
60
61#[derive(Clone)]
62pub enum Layout {
63 Vh(i32),
64 Vw(i32),
65 Percent(i32),
66 Cell(i32),
67 Auto,
68
69 Add(Box<Layout>, Box<Layout>),
70 Sub(Box<Layout>, Box<Layout>),
71}
72
73impl Add for Layout {
74 type Output = Layout;
75
76 fn add(self, rhs: Layout) -> Layout {
77 Layout::Add(Box::new(self), Box::new(rhs))
78 }
79}
80
81impl Sub for Layout {
82 type Output = Layout;
83
84 fn sub(self, rhs: Layout) -> Layout {
85 Layout::Sub(Box::new(self), Box::new(rhs))
86 }
87}
88
89#[derive(Copy, Clone)]
90pub enum Alignment {
91 Left,
92 Center,
93 Right
94}
95
96#[derive(Copy, Clone)]
97pub struct BorderStyle {
98 pub top: i32,
99 pub bottom: i32,
100 pub left: i32,
101 pub right: i32,
102 pub top_left: i32,
103 pub top_right: i32,
104 pub bottom_right: i32,
105 pub bottom_left: i32,
106}
107
108#[derive(Copy, Clone)]
109pub enum Border {
110 Single,
111 Double,
112 Custom(BorderStyle),
113 None
114}
115
116#[derive(Clone)]
117pub struct LayoutVec2 {
118 pub x: Layout,
119 pub y: Layout
120}
121
122impl LayoutVec2 {
123 pub fn new(x: Layout, y: Layout) -> Self {
124 Self {x, y}
125 }
126}
127
128#[derive(Clone)]
129pub struct Style {
130 pub position: LayoutVec2,
131 pub size: LayoutVec2,
132 pub margin: LayoutVec2,
133 pub color: futtl::ColorSet,
134 pub attributes: futtl::Attr,
135 pub widget_border: Border,
136 pub alignment_type: Alignment,
137 pub wrap_text: bool
138}
139
140pub enum WidgetCtx {
141 Box(usize),
142 None
143}
144
145pub struct Text {
146 pub text: String,
147 pub style: Style,
148 pub id: u32
149}
150
151pub struct Canvas {
152 pub ctx: Option<futtl::Container>,
153 callback: Box<dyn FnMut(&mut Canvas)>,
154 id: Option<usize>,
155 pub pos: futtl::Vec2,
156 pub size: futtl::Vec2,
157}
158
159
160pub struct Widget {
161 pub ctx: WidgetCtx,
162 pub style: Style,
163 pub children: Vec<Widget>,
164 content: Vec<Text>,
165 futtl_canvas_content: Option<Canvas>,
166 pub pos: futtl::Vec2,
167 pub id: u32,
168 callback: fn(&mut Widget),
169 size: futtl::Vec2
170}
171
172#[derive(Clone)]
173pub enum Event {
174 Key(i32),
175 Quit,
176}
177
178pub struct Win {
179 pub ctx: futtl::Win,
180 pub style: Style,
181 pub children: Vec<Widget>,
182 pub color: futtl::ColorSet,
183 ids: IdGenerator,
184 events: Vec<Event>,
185 exit_keys: Vec<i32>,
186 content: Vec<Text>,
187}
188
189impl Style {
190 pub fn new() -> Self {
191 Self {
192 position: LayoutVec2 {x: Layout::Auto, y: Layout::Auto},
193 size: LayoutVec2 {x: Layout::Auto, y: Layout::Auto},
194 margin: LayoutVec2 {x: Layout::Auto, y: Layout::Auto},
195 color: futtl::ColorSet::new_default(),
196 attributes: futtl::Attr::NONE,
197 widget_border: Border::None,
198 alignment_type: Alignment::Left,
199 wrap_text: false
200 }
201 }
202}
203
204#[derive(Copy, Clone)]
205enum Axis {
206 X,
207 Y,
208}
209
210fn evaluate_layout(term_size: futtl::Vec2, parent_size: futtl::Vec2, layout: &Layout, axis: Axis) -> i32 {
211 match layout {
212 Layout::Auto => 0,
213
214 Layout::Vw(n) => match axis {
215 Axis::X => term_size.x * n / 100,
216 Axis::Y => 0,
217 },
218
219 Layout::Vh(n) => match axis {
220 Axis::X => 0,
221 Axis::Y => term_size.y * n / 100,
222 },
223
224 Layout::Percent(n) => match axis {
225 Axis::X => parent_size.x * n / 100,
226 Axis::Y => parent_size.y * n / 100,
227 },
228
229 Layout::Cell(n) => *n,
230
231 Layout::Add(a, b) => {
232 evaluate_layout(term_size, parent_size, a, axis)
233 + evaluate_layout(term_size, parent_size, b, axis)
234 }
235
236 Layout::Sub(a, b) => {
237 evaluate_layout(term_size, parent_size, a, axis)
238 - evaluate_layout(term_size, parent_size, b, axis)
239 }
240 }
241}
242
243fn evaluate_size(term_size: futtl::Vec2, parent_size: futtl::Vec2, size: &LayoutVec2, margin: &LayoutVec2) -> futtl::Vec2 {
244 futtl::Vec2::new(
245 evaluate_layout(term_size, parent_size, &size.x, Axis::X)
246 - evaluate_layout(term_size, parent_size, &margin.x, Axis::X) * 2,
247 evaluate_layout(term_size, parent_size, &size.y, Axis::Y)
248 - evaluate_layout(term_size, parent_size, &margin.y, Axis::Y) * 2,
249 )
250}
251
252fn evaluate_pos(term_size: futtl::Vec2, parent_size: futtl::Vec2, pos: &LayoutVec2, margin: &LayoutVec2) -> futtl::Vec2 {
253 futtl::Vec2::new(
254 evaluate_layout(term_size, parent_size, &pos.x, Axis::X)
255 + evaluate_layout(term_size, parent_size, &margin.x, Axis::X),
256 evaluate_layout(term_size, parent_size, &pos.y, Axis::Y)
257 + evaluate_layout(term_size, parent_size, &margin.y, Axis::Y),
258 )
259}
260
261impl Win {
262 pub fn new_with_max_id(max_id: i32) -> Self {
263 Self {
264 ctx: futtl::Win::new(max_id, futtl::Attr::NONE),
265 style: Style::new(),
266 children: Vec::new(),
267 color: futtl::ColorSet::new(
268 futtl::Color::ANSI(futtl::AnsiColor::DefaultFG),
269 futtl::Color::ANSI(futtl::AnsiColor::DefaultBG),
270 ),
271 ids: IdGenerator::new(),
272 events: Vec::new(),
273 exit_keys: Vec::new(),
274 content: Vec::new()
275 }
276 }
277
278 pub fn new() -> Self {
279 Self::new_with_max_id(1024)
280 }
281
282 fn next_id(&mut self) -> u32 {
283 self.ids.next()
284 }
285
286 pub fn text(&mut self, text: impl Into<String>) -> &mut Text {
287 let mut style = Style::new();
288 style.color = self.style.color;
289 style.attributes = self.style.attributes;
290 style.alignment_type = self.style.alignment_type;
291 style.wrap_text = self.style.wrap_text;
292
293 self.content.push(Text {
294 text: text.into(),
295 style,
296 id: 0,
297 });
298
299 self.content.last_mut().unwrap()
300 }
301
302
303 fn assign_ids(&mut self, widget: &mut Widget) {
304 widget.id = self.next_id();
305
306 for text in &mut widget.content {
307 text.id = self.next_id();
308 }
309
310 for child in &mut widget.children {
311 self.assign_ids(child);
312 }
313 }
314
315 pub fn pack(&mut self, mut widget: Widget) {
316 self.assign_ids(&mut widget);
317 self.children.push(widget);
318 }
319
320 fn evaluate_members(&mut self) {
321 let term_size = self.ctx.get_size();
322
323 let auto_x = self.children
324 .iter()
325 .filter(|child| matches!(child.style.size.x, Layout::Auto))
326 .count();
327
328 let auto_y = self.children
329 .iter()
330 .filter(|child| matches!(child.style.size.y, Layout::Auto))
331 .count();
332
333 let mut remaining_x = term_size.x;
334 let mut remaining_y = term_size.y;
335
336 for child in &self.children {
338 let size = evaluate_size(
339 term_size,
340 term_size,
341 &child.style.size,
342 &child.style.margin,
343 );
344
345 if !matches!(child.style.size.x, Layout::Auto) {
346 remaining_x -= size.x;
347 }
348
349 if !matches!(child.style.size.y, Layout::Auto) {
350 remaining_y -= size.y;
351 }
352 }
353
354 remaining_x = remaining_x.max(0);
355 remaining_y = remaining_y.max(0);
356
357 let auto_x_size = if auto_x > 0 {
359 remaining_x / auto_x as i32
360 } else {
361 0
362 };
363
364 let auto_y_size = if auto_y > 0 {
365 remaining_y / auto_y as i32
366 } else {
367 0
368 };
369
370 let mut current_x = 0;
371 let mut current_y = 0;
372
373 for child in self.children.iter_mut() {
374 let child_size = evaluate_size(
375 term_size,
376 term_size,
377 &child.style.size,
378 &child.style.margin,
379 );
380
381 let x_size = match child.style.size.x {
382 Layout::Auto => auto_x_size,
383 _ => child_size.x.min(term_size.x),
384 };
385
386 let y_size = match child.style.size.y {
387 Layout::Auto => auto_y_size,
388 _ => child_size.y.min(term_size.y),
389 };
390
391 child.pos = evaluate_pos(
392 term_size,
393 term_size,
394 &child.style.position,
395 &child.style.margin,
396 );
397
398 if matches!(child.style.position.x, Layout::Auto) {
400 child.pos.x = current_x;
401 }
402
403 if matches!(child.style.position.y, Layout::Auto) {
404 child.pos.y = current_y;
405 }
406
407 current_x += x_size;
409 current_y += y_size;
410
411 let idx = self.ctx.create_container(x_size, y_size);
412
413 child.ctx = WidgetCtx::Box(idx);
414 }
415 }
416
417 fn reset_widget_contexts(widget: &mut Widget) {
418 widget.ctx = WidgetCtx::None;
419
420 if let Some(canvas) = &mut widget.futtl_canvas_content {
421 canvas.ctx = None;
422 canvas.id = None;
423 }
424
425 for child in &mut widget.children {
426 Self::reset_widget_contexts(child);
427 }
428 }
429
430 pub fn clear_child(&mut self, id: usize) {
431 self.children[id].clear();
432 }
433
434 pub fn get_events(&mut self) -> Vec<Event> {
435 let curr_events = self.events.clone();
436
437 self.events.clear();
438
439 curr_events
440 }
441
442 fn handle_input(&mut self) {
443 let c = self.ctx.get_in();
444
445 match c {
446 -1 => {
447
448 },
449 _ => {
450 self.events.push(Event::Key(c));
451 for key in &self.exit_keys {
452 if c == *key {
453 self.events.push(Event::Quit)
454 }
455 }
456 }
457 }
458 }
459
460 pub fn add_exit_key(&mut self, key: i32) {
461 if !self.exit_keys.contains(&key) {
462 self.exit_keys.push(key);
463 }
464 }
465
466 pub fn remove_exit_key(&mut self, key: i32) {
467 if let Some(i) = self.exit_keys.iter().position(|&x| x == key) {
468 self.exit_keys.remove(i);
469 }
470 }
471
472 fn draw_content(&mut self) {
473 for member in self.content.iter_mut() {
474 let size = self.ctx.get_size();
475 member.show(&mut self.ctx, futtl::Vec2::new(0, 0), size);
476 }
477 }
478
479 pub fn render(&mut self) {
480 for child in &mut self.children {
481 Self::reset_widget_contexts(child);
482 }
483
484 while !self.ctx.children().is_empty() {
485 assert_eq!(
486 self.ctx.remove_child(0),
487 0,
488 "failed to release the previous frame's widget context"
489 );
490 }
491
492 self.evaluate_members();
493
494 for child in self.children.iter_mut() {
495 match child.ctx {
496 WidgetCtx::None => {},
497 WidgetCtx::Box(_) => {
498 child.show(child.pos, &mut self.ctx);
499 }
500 }
501 }
502
503 self.draw_content();
504
505 futtl::flush();
506
507 self.handle_input();
508 }
509}
510
511pub trait Stylable: Sized {
512 fn get_style(&mut self) -> &mut Style;
513
514 fn foreground(mut self, col: futtl::Color) -> Self {
515 self.get_style().color.fg = col;
516 self
517 }
518
519 fn background(mut self, col: futtl::Color) -> Self {
520 self.get_style().color.bg = col;
521 self
522 }
523
524 fn position(mut self, x: Layout, y: Layout) -> Self {
525 self.get_style().position.x = x;
526 self.get_style().position.y = y;
527 self
528 }
529
530 fn position_x(mut self, x: Layout) -> Self {
531 self.get_style().position.x = x;
532 self
533 }
534
535 fn position_y(mut self, y: Layout) -> Self {
536 self.get_style().position.y = y;
537 self
538 }
539
540 fn size(mut self, x: Layout, y: Layout) -> Self {
541 self.get_style().size.x = x;
542 self.get_style().size.y = y;
543 self
544 }
545
546 fn size_x(mut self, x: Layout) -> Self {
547 self.get_style().size.x = x;
548 self
549 }
550
551 fn size_y(mut self, y: Layout) -> Self {
552 self.get_style().size.y = y;
553 self
554 }
555
556 fn margin(mut self, x: Layout, y: Layout) -> Self {
557 self.get_style().margin.x = x;
558 self.get_style().margin.y = y;
559 self
560 }
561
562 fn margin_x(mut self, x: Layout) -> Self {
563 self.get_style().margin.x = x;
564 self
565 }
566
567 fn margin_y(mut self, y: Layout) -> Self {
568 self.get_style().margin.y = y;
569 self
570 }
571
572 fn attribute(mut self, attr: futtl::Attr) -> Self {
573 self.get_style().attributes |= attr;
574 self
575 }
576
577 fn remove_attribute(mut self, attr: futtl::Attr) -> Self {
578 self.get_style().attributes &= !attr;
579 self
580 }
581
582 fn border(mut self, border: Border) -> Self {
583 self.get_style().widget_border = border;
584 self
585 }
586
587 fn color(mut self, cs: futtl::ColorSet) -> Self {
588 self.get_style().color = cs;
589 self
590 }
591
592 fn align(mut self, alignment: Alignment) -> Self {
593 self.get_style().alignment_type = alignment;
594 self
595 }
596
597 fn set_wrap(mut self, wrapping: bool) -> Self {
598 self.get_style().wrap_text = wrapping;
599 self
600 }
601
602 fn style(mut self, style: Style) -> Self {
603 *self.get_style() = style;
604 self
605 }
606}
607
608pub trait BorrowStylable {
609 fn get_style(&mut self) -> &mut Style;
610
611 fn foreground(&mut self, col: futtl::Color) -> &mut Self {
612 self.get_style().color.fg = col;
613 self
614 }
615
616 fn background(&mut self, col: futtl::Color) -> &mut Self {
617 self.get_style().color.bg = col;
618 self
619 }
620
621 fn position(&mut self, x: Layout, y: Layout) -> &mut Self {
622 self.get_style().position.x = x;
623 self.get_style().position.y = y;
624 self
625 }
626
627 fn position_x(&mut self, x: Layout) -> &mut Self {
628 self.get_style().position.x = x;
629 self
630 }
631
632 fn position_y(&mut self, y: Layout) -> &mut Self {
633 self.get_style().position.y = y;
634 self
635 }
636
637 fn size(&mut self, x: Layout, y: Layout) -> &mut Self {
638 self.get_style().size.x = x;
639 self.get_style().size.y = y;
640 self
641 }
642
643 fn size_x(&mut self, x: Layout) -> &mut Self {
644 self.get_style().size.x = x;
645 self
646 }
647
648 fn size_y(&mut self, y: Layout) -> &mut Self {
649 self.get_style().size.y = y;
650 self
651 }
652
653 fn margin(&mut self, x: Layout, y: Layout) -> &mut Self {
654 self.get_style().margin.x = x;
655 self.get_style().margin.y = y;
656 self
657 }
658
659 fn margin_x(&mut self, x: Layout) -> &mut Self {
660 self.get_style().margin.x = x;
661 self
662 }
663
664 fn margin_y(&mut self, y: Layout) -> &mut Self {
665 self.get_style().margin.y = y;
666 self
667 }
668
669 fn attribute(&mut self, attr: futtl::Attr) -> &mut Self {
670 self.get_style().attributes |= attr;
671 self
672 }
673
674 fn remove_attribute(&mut self, attr: futtl::Attr) -> &mut Self {
675 self.get_style().attributes &= !attr;
676 self
677 }
678
679 fn border(&mut self, border: Border) -> &mut Self {
680 self.get_style().widget_border = border;
681 self
682 }
683
684 fn color(&mut self, cs: futtl::ColorSet) -> &mut Self {
685 self.get_style().color = cs;
686 self
687 }
688
689 fn align(&mut self, alignment: Alignment) -> &mut Self {
690 self.get_style().alignment_type = alignment;
691 self
692 }
693
694 fn set_wrap(&mut self, wrapping: bool) -> &mut Self {
695 self.get_style().wrap_text = wrapping;
696 self
697 }
698
699 fn style(&mut self, style: Style) -> &mut Self {
700 *self.get_style() = style;
701 self
702 }
703}
704
705impl Stylable for Widget {
706 fn get_style(&mut self) -> &mut Style {
707 &mut self.style
708 }
709}
710
711impl BorrowStylable for Style {
712 fn get_style(&mut self) -> &mut Style {
713 self
714 }
715}
716
717impl BorrowStylable for Text {
718 fn get_style(&mut self) -> &mut Style {
719 &mut self.style
720 }
721}
722
723impl BorrowStylable for Win {
724 fn get_style(&mut self) -> &mut Style {
725 &mut self.style
726 }
727}
728
729
730impl Canvas {
731 pub fn new<F>(callback: F) -> Self
732 where
733 F: FnMut(&mut Self) + 'static,
734 {
735 Self {
736 ctx: None,
737 callback: Box::new(callback),
738 id: None,
739 pos: futtl::Vec2::new(0, 0),
740 size: futtl::Vec2::new(0, 0),
741 }
742 }
743
744 pub fn show(&mut self, pos: futtl::Vec2, size: futtl::Vec2) {
745 self.pos = pos;
746 self.size = size;
747
748 let mut callback = std::mem::replace(
749 &mut self.callback,
750 Box::new(|_| {}),
751 );
752
753 callback(self);
758
759 self.callback = callback;
760 }
761}
762
763impl Text {
764 pub fn text(&mut self, text: impl Into<String>) {
765 self.text = text.into();
766 }
767
768 pub fn show(&self, parent_ctx: &mut impl futtl::Futtl, offset: futtl::Vec2, size: futtl::Vec2) {
769 let pos = evaluate_pos(size, size, &self.style.position, &self.style.margin);
770
771 let text_size = futtl::Vec2::new(
772 self.text
773 .lines()
774 .map(|line| line.chars().count())
775 .max()
776 .unwrap_or(0) as i32,
777 self.text.lines().count() as i32,
778 );
779
780 let text_offset = match self.style.alignment_type {
781 Alignment::Left => futtl::Vec2::new(0, 0),
782 Alignment::Right => futtl::Vec2::new(text_size.x, 0),
783 Alignment::Center => futtl::Vec2::new(text_size.x / 2, 0),
784 };
785
786 let text_x = pos.x - text_offset.x + offset.x;
787
788 let text: String = self.text
789 .chars()
790 .skip((-text_x).max(0) as usize)
791 .collect();
792
793 parent_ctx.set_curs(
794 text_x.max(0),
795 pos.y.max(0) + offset.y,
796 );
797
798 parent_ctx.add_color(self.style.color, self.id as usize);
799 parent_ctx.set_color(self.id as usize);
800 parent_ctx.remove_attribute(futtl::Attr::ALL);
801 parent_ctx.add_attribute(self.style.attributes);
802
803 if self.style.wrap_text {
804 parent_ctx.write_wrap_str(&text);
805 } else {
806 parent_ctx.write_str(&text);
807 }
808 }
809}
810
811fn default_callback(_ctx: &mut Widget) {
812
813}
814
815impl Widget {
816 pub fn new() -> Self {
817 Self {
818 ctx: WidgetCtx::None,
819 style: Style::new(),
820 children: Vec::new(),
821 content: Vec::new(),
822 pos: futtl::Vec2::new(0, 0),
823 id: 0,
824 callback: default_callback,
825 size: futtl::Vec2::new(0, 0),
826 futtl_canvas_content: None
827 }
828 }
829
830 pub fn style(&mut self, style: Style) {
831 self.style = style;
832 }
833
834 pub fn text(&mut self, text: impl Into<String>) -> &mut Text {
835 let mut style = Style::new();
836 style.color = self.style.color;
837 style.attributes = self.style.attributes;
838 style.alignment_type = self.style.alignment_type;
839 style.wrap_text = self.style.wrap_text;
840
841 self.content.push(Text {
842 text: text.into(),
843 style,
844 id: 0,
845 });
846
847 self.content.last_mut().unwrap()
848 }
849
850 pub fn canvas<F>(mut self, callback: F) -> Self
851 where
852 F: FnMut(&mut Canvas) + 'static,
853 {
854 if self.futtl_canvas_content.is_none() {
855 self.futtl_canvas_content = Some(Canvas::new(callback));
856 }
857
858 self
859 }
860
861 fn set_border(&mut self, box_ctx: &mut futtl::Container, border: Border) {
862 let border = match border {
863 Border::Single => SINGLE_BORDER,
864 Border::Double => DOUBLE_BORDER,
865 Border::Custom(border) => border,
866 Border::None => NO_BORDER
867 };
868
869 box_ctx.add_color(self.style.color, self.id as usize);
870
871 box_ctx.set_border(
872 border.top as u32,
873 border.right as u32,
874 border.bottom as u32,
875 border.left as u32,
876 border.top_right as u32,
877 border.bottom_right as u32,
878 border.bottom_left as u32,
879 border.top_left as u32,
880 self.id as i32,
881 );
882 }
883
884 pub fn clear(&mut self) {
885 self.content.clear();
886 }
887
888 pub fn get_size(&mut self) -> futtl::Vec2 {
889 futtl::Vec2::new(0, 0)
890 }
891
892 pub fn pack(&mut self, mut widget: Widget, f_win: &mut Win) {
893 f_win.assign_ids(&mut widget);
894 self.children.push(widget);
895 }
896
897 pub fn show(&mut self, widget_pos: futtl::Vec2, f_ctx: &mut futtl::Win) {
898 match self.ctx {
899 WidgetCtx::None => {}
900
901 WidgetCtx::Box(ctx) => {
902 let parent_size = f_ctx.get_size();
903
904 let box_size = f_ctx.children()[ctx].get_size();
905
906 let size = match self.style.widget_border {
907 Border::None => box_size,
908 _ => futtl::Vec2::new(box_size.x - 2, box_size.y - 2),
909 };
910
911 self.size = size;
912
913 (self.callback)(self);
914 {
915 let box_ctx = &mut f_ctx.children()[ctx];
916
917 box_ctx.set_color(self.id as usize);
918 box_ctx.remove_attribute(futtl::Attr::ALL);
919 box_ctx.add_attribute(self.style.attributes);
920 box_ctx.clear();
921
922 self.set_border(box_ctx, self.style.widget_border);
923 }
924
925 let offset = match self.style.widget_border {
926 Border::None => futtl::Vec2::new(0, 0),
927 _ => futtl::Vec2::new(1, 1),
928 };
929
930 let box_size = f_ctx.children()[ctx].get_size();
931
932 let size = match self.style.widget_border {
933 Border::None => box_size,
934 _ => futtl::Vec2::new(box_size.x - 2, box_size.y - 2),
935 };
936
937 for item in &self.content {
938 let box_ctx = &mut f_ctx.children()[ctx];
939
940 let box_ctx_cont: &mut futtl::Container = box_ctx;
941 item.show(box_ctx_cont, offset, size);
942
943 box_ctx.remove_attribute(futtl::Attr::ALL);
944 box_ctx.add_attribute(self.style.attributes);
945 }
946
947 for child in &mut self.children {
948 let pos = evaluate_pos(
949 parent_size,
950 size,
951 &child.style.position,
952 &child.style.margin,
953 );
954
955 child.show(pos, f_ctx);
956
957 let box_ctx = &mut f_ctx.children()[ctx];
958
959 box_ctx.remove_attribute(futtl::Attr::ALL);
960 box_ctx.add_attribute(self.style.attributes);
961 }
962
963 f_ctx.children()[ctx].show(widget_pos.x, widget_pos.y);
964
965 if let Some(content) = &mut self.futtl_canvas_content {
966 let canvas_size = futtl::Vec2::new(size.x.max(0), size.y.max(0));
967 if content.size.x != canvas_size.x
968 || content.size.y != canvas_size.y
969 || content.id.is_none()
970 {
971 content.id = Some(f_ctx.create_container(canvas_size.x, canvas_size.y));
972 content.ctx = Some((*f_ctx.children()[content.id.unwrap()]).clone());
973 }
974
975 let canvas_pos = futtl::Vec2::new(
976 widget_pos.x + offset.x,
977 widget_pos.y + offset.y,
978 );
979 content.show(canvas_pos, canvas_size);
980
981 let canvas_ctx = &mut f_ctx.children()[content.id.unwrap()];
982 canvas_ctx.show(canvas_pos.x, canvas_pos.y);
983 }
984 }
985 }
986 }
987}
988
989
990impl Default for Win {
991 fn default() -> Self {
992 Self::new()
993 }
994}
995
996impl Default for Widget {
997 fn default() -> Self {
998 Self::new()
999 }
1000}
1001
1002impl Default for Style {
1003 fn default() -> Self {
1004 Self::new()
1005 }
1006}
1007
1008
1009#[cfg(test)]
1010mod tests {
1011 use super::*;
1012 use std::{cell::RefCell, rc::Rc};
1013
1014 #[test]
1015 fn canvas_callback_receives_current_geometry() {
1016 let geometry = Rc::new(RefCell::new(None));
1017 let callback_geometry = Rc::clone(&geometry);
1018 let mut canvas = Canvas::new(move |canvas| {
1019 *callback_geometry.borrow_mut() = Some((
1020 canvas.pos.x,
1021 canvas.pos.y,
1022 canvas.size.x,
1023 canvas.size.y,
1024 ));
1025 });
1026
1027 canvas.show(futtl::Vec2::new(4, 5), futtl::Vec2::new(12, 7));
1028
1029 assert_eq!(*geometry.borrow(), Some((4, 5, 12, 7)));
1030 }
1031
1032 #[test]
1033 fn reset_widget_contexts_clears_canvas_context_ids() {
1034 let mut parent = Widget::new().canvas(|_| {});
1035 parent.ctx = WidgetCtx::Box(3);
1036 parent.futtl_canvas_content.as_mut().unwrap().id = Some(4);
1037
1038 let mut child = Widget::new().canvas(|_| {});
1039 child.ctx = WidgetCtx::Box(5);
1040 child.futtl_canvas_content.as_mut().unwrap().id = Some(6);
1041 parent.children.push(child);
1042
1043 Win::reset_widget_contexts(&mut parent);
1044
1045 assert!(matches!(parent.ctx, WidgetCtx::None));
1046 assert_eq!(parent.futtl_canvas_content.as_ref().unwrap().id, None);
1047 assert!(matches!(parent.children[0].ctx, WidgetCtx::None));
1048 assert_eq!(
1049 parent.children[0].futtl_canvas_content.as_ref().unwrap().id,
1050 None
1051 );
1052 }
1053
1054 #[test]
1055 fn test() {
1056 let mut ctx = Win::new();
1057
1058 let mut text_widget = Widget::new()
1059 .position_x(Layout::Cell(0))
1060 .position_y(Layout::Cell(0))
1061 .size_x(Layout::Vw(50))
1062 .size_y(Layout::Vh(50) - Layout::Cell(2))
1063 .border(Border::Double)
1064 .foreground(futtl::Color::ANSI256(69));
1065
1066 text_widget.text("test")
1067 .position_x(Layout::Percent(50))
1068 .align(Alignment::Left);
1069
1070 ctx.pack(text_widget);
1071
1072 let frame = Rc::new(Cell::new(0));
1073 let canvas_frame = Rc::clone(&frame);
1074
1075 let animated_widget = Widget::new()
1076 .position_x(Layout::Cell(0))
1077 .position_y(Layout::Vh(50) - Layout::Cell(2))
1078 .size_x(Layout::Vw(50))
1079 .size_y(Layout::Vh(50) + Layout::Cell(2))
1080 .border(Border::Double)
1081 .canvas(move |canvas| {
1082 let frame = canvas_frame.get();
1083 let ctx = canvas.ctx.as_mut().unwrap();
1084 ctx.set_curs(0, 0);
1085
1086 let message = "Hello, World!";
1087
1088 for (i, c) in message.chars().enumerate() {
1089 if i != ((frame / 100) % message.len()) {
1090 ctx.put_char(c)
1091 }
1092 else {
1093 ctx.put_char(' ');
1094 }
1095 }
1096 });
1097
1098 ctx.pack(animated_widget);
1099
1100 let mut widget2 = Widget::new()
1101 .position_x(Layout::Vw(50))
1102 .position_y(Layout::Cell(0))
1103 .size_x(Layout::Vw(50))
1104 .size_y(Layout::Vh(100) - Layout::Cell(1))
1105 .border(Border::Single)
1106 .margin_x(Layout::Cell(2));
1107
1108 widget2.text("Left".to_string())
1109 .position_x(Layout::Percent(50))
1110 .color(futtl::ColorSet::new(
1111 futtl::Color::ANSI(futtl::AnsiColor::Red),
1112 futtl::Color::ANSI(futtl::AnsiColor::DefaultBG)
1113 ));
1114
1115 widget2.text("Center".to_string())
1116 .position(
1117 Layout::Percent(50),
1118 Layout::Cell(1)
1119 )
1120 .color(futtl::ColorSet::new(
1121 futtl::Color::ANSI(futtl::AnsiColor::Green),
1122 futtl::Color::ANSI(futtl::AnsiColor::DefaultBG)
1123 ))
1124 .align(Alignment::Center);
1125
1126 widget2.text("Right".to_string())
1127 .position(
1128 Layout::Percent(50),
1129 Layout::Cell(2)
1130 )
1131 .color(futtl::ColorSet::new(
1132 futtl::Color::ANSI(futtl::AnsiColor::Blue),
1133 futtl::Color::ANSI(futtl::AnsiColor::DefaultBG)
1134 ))
1135 .align(Alignment::Right)
1136 .attribute(futtl::Attr::UNDERLINE);
1137
1138
1139 ctx.pack(widget2);
1140
1141 ctx.add_exit_key(27);
1142 ctx.add_exit_key('q' as i32);
1143
1144 let mut events: Vec<Event> = Vec::new();
1145
1146 futtl::set_input_blocking(futtl::InputMode::NonBlocking);
1147
1148 'mainloop: loop {
1149 for event in events {
1150 match event {
1151 Event::Key(_) => {
1152
1153 },
1154 Event::Quit => {
1155 break 'mainloop;
1156 }
1157 }
1158 }
1159 ctx.render();
1160 frame.set(frame.get() + 1);
1161
1162 events = ctx.get_events();
1163 }
1164 }
1165}