1use crossterm::event::{Event, KeyCode};
2use flux_tui::canvas::Canvas;
3use flux_tui::common::*;
4use flux_tui::components::*;
5use flux_tui::tui::Tui;
6use flux_tui::window::*;
7use std::cell::RefCell;
8use std::rc::Rc;
9use vector2d::Vector2D;
10
11fn main() {
12 let control = Rc::new(RefCell::new(Control::default()));
13 let mut tui = Tui::new(control.clone()).unwrap();
14 tui.render().unwrap();
15 while let Ok(()) = tui.handle_next_event() {
16 if !control.borrow().running {
17 return;
18 }
19 tui.render().unwrap();
20 }
21}
22
23#[derive(Clone)]
24struct ExampleRow {
25 value_a: String,
26 value_b: i32,
27 value_c: bool,
28 value_d: f32,
29}
30
31impl ExampleRow {
32 fn new<S: ToString>(a: S, b: i32, c: bool, d: f32) -> Self {
33 Self {
34 value_a: a.to_string(),
35 value_b: b,
36 value_c: c,
37 value_d: d,
38 }
39 }
40}
41
42impl DataRow for ExampleRow {
43 fn get_data<'a>(&'a self, column: usize) -> Option<DataValue<'a>> {
44 match column {
45 0 => Some(DataValue::String(&self.value_a)),
46 1 => Some(DataValue::Integer(self.value_b)),
47 2 => Some(DataValue::Float(self.value_d)),
48 3 => Some(DataValue::Boolean(self.value_c)),
49 _ => None,
50 }
51 }
52}
53
54struct ExampleDataSource {
55 rows: [ExampleRow; 12],
56 defs: [ColumnDefiniton; 4],
57}
58
59impl Default for ExampleDataSource {
60 fn default() -> Self {
61 Self {
62 rows: [
63 ExampleRow::new("a", 1, true, 4.5),
64 ExampleRow::new("bb", 31, false, 42.25),
65 ExampleRow::new("ccc", 123, true, 4.265),
66 ExampleRow::new("d", 1311, true, 24.125),
67 ExampleRow::new("aa", 122, true, 4.5),
68 ExampleRow::new("bbbb", 3281, false, 42.25),
69 ExampleRow::new("cccc", 123, false, 4.265),
70 ExampleRow::new("dd", 13, true, 34.125),
71 ExampleRow::new("aaa", 19, false, 9.5),
72 ExampleRow::new("bbb", 341, false, 11.25),
73 ExampleRow::new("cccccc", 1223, true, 7.265),
74 ExampleRow::new("ddd", 13111, true, 12.125),
75 ],
76 defs: [
77 ColumnDefiniton::new("A"),
78 ColumnDefiniton::new("B"),
79 ColumnDefiniton::new("C"),
80 ColumnDefiniton::new("D"),
81 ],
82 }
83 }
84}
85
86impl DataSource for ExampleDataSource {
87 fn column_definitions(&self) -> &[ColumnDefiniton] {
88 &self.defs
89 }
90
91 fn rows(&self) -> usize {
92 self.rows.len()
93 }
94
95 fn get_row(&self, row: usize) -> &dyn DataRow {
96 &self.rows[row]
97 }
98}
99
100struct Control {
101 running: bool,
102 uid: WindowUID,
103 datagrid: Rc<RefCell<DataGrid>>,
104}
105
106impl Default for Control {
107 fn default() -> Self {
108 let datagrid = WindowFactory::create::<DataGrid>();
109 {
110 let mut brw = datagrid.borrow_mut();
111 brw.set_data_source(Box::new(ExampleDataSource::default()));
112 brw.set_selection_color(Some(Color::Cyan));
114 }
115
116 Self {
117 uid: WindowUID::new(),
118 datagrid,
119 running: true,
120 }
121 }
122}
123
124impl Window for Control {
125 fn render(&self, _: &mut Canvas) {}
126
127 fn handle_event(&mut self, event: &mut WindowEvent) {
128 if let Event::Key(k) = event.raw() {
129 if k.code == KeyCode::Esc {
130 self.running = false;
131 }
132 }
133 }
134
135 fn children(&mut self, mut builder: SubWindowBuilder) -> SubWindows {
136 builder.add_pair(self.datagrid.clone(), None).ok();
137 builder.build().unwrap()
138 }
139
140 fn focus(&self) -> Option<WindowRef> {
141 Some(self.datagrid.clone())
142 }
143}
144
145impl HasWindowUID for Control {
146 fn uid(&self) -> WindowUID {
147 self.uid
148 }
149}
150
151impl WindowLayout for Control {
152 fn desired_size(&self, available_size: Vector2D<u16>) -> Vector2D<u16> {
153 available_size / 2
154 }
155}