docs/src/explaination/framework.md
126 lines · 5498 bytes
1# Framework
2
3At the core of this TUI, is the framework from the <a href="https://crates.io/crates/tui-additions" target=_blank>`tui-additions`</a> crate, it allows the program to *render* onto the screen, *take inputs* (mouse and keyboard) in an orderly fashion.
4
5A <a href="https://docs.rs/tui-additions/latest/tui_additions/framework/struct.Framework.html" target=_blank>Framework</a> holds a bunch of other structs, a simplified version would looks something like this.
6
7```rs
8struct Framework {
9 state: Vec<Vec<Item>>,
10 cursor: CursorState,
11 selectables: Vec<Vec<(usize, usize)>>,
12 data: FrameworkData,
13 history: FrameworkHistory,
14}
15```
16
17## State
18
19Holds a 2D array of items, but that is just the simplified version.
20
21The `State` struct also holds the width of the items, heigh of the rows, and other details such as *if the row should be centered or not*.
22
23## Cursor
24
25The cursor can be in one of these states.
26
27- Selected - where an item is selected.
28- Hover - when its being moved around.
29- None - when the page first loads, the cursor does not have a state.
30
31## Selectables
32
33This is an interesting one. As [mentioned below](#selectableself---bool), some items can be hovered when other cannot. Each `selectables[x][y]` maps to a selectable (hoverable) coordinate.
34
35Instead of using a `HashMap<(usize, usize)>`, using a 2D array ensures that all *x: 0..range* and *y: 0..range* are mapped to a selectable item. And moving the cursor left is as easy as increasing x by 1, without having to worry about selectability check and stuff.
36
37## Data
38
39There are 2 sets of data available - *global* and *state*.
40
41**Global data** are global across all pages, this is mainly used to store config structs, as they are meant to be applied globally.
42
43**State data** are exclusive to each page, this is done to ensure that each page will get the correct data.
44
45> For example, if state data is not used, the next page will overwrite data in the previous page, making going back in history impossible.
46
47## History
48
49This stores the past states and data of the struct, pretty self explanatory.
50
51<hr>
52
53## Items
54
55Items are placed across the screen *in rows*, each item is <u>individual</u> and changing of one should not affect others in anyway.
56
57
58
59> Diagram showing that individual items arranged in rows on screen.
60
61Each item has a set of functions from the trait <a href="https://docs.rs/tui-additions/latest/tui_additions/framework/trait.FrameworkItem.html" target=_blank>FrameworkItem</a>:
62
63### .selectable(&self) -> bool
64
65The function is ran on page load, it determines whether *the item can be hovered by cursor or not*.
66
67Most item returns true, as they can be selected and hovered.
68
69However there are some exceptions, including MessageBar and the big info display in channel home page.
70
71> PageButtons returns true in this function, as they can be hovered by cursor. Instead, `.select()` is responsible for their special behaviour.
72
73### .select(&mut self, &mut Framework) -> bool
74
75This function is ran when an item is selected (pressed enter on), it allows the item to modify its own state and the framework state. The returned boolean also determines if the selecting item will *stay selected* and capture key input.
76
77Most items returns true in this function, for example SearchBar stays selected and captures key input after being selected, until deselect.
78
79PageButtons returns false, as they don't want to say selected. This does not mean the function does nothing, they are still able to modify their own states and <u>push a LoadPage task to queue</u>.
80
81### .deselect(&mut self, &mut Framework) -> bool
82
83This function is the exact opposite of the .select() function, it is called when the *deselect keybinding* (Esc) is pressed, or the mouse clicks somewhere outside the item.
84
85All items returns true on this function.
86
87> A fun fact is that items can actually return false, and refuse to get deselected. This can be useful when waiting for a condition to be met.
88
89### .load_item(&mut self, &mut Framework, ...) -> Result
90
91This is the function responsible for the loading of items. The YouTube TUI adds an empty item to the screen, then calls this function for the item to load itself.
92
93An item that heavily rely on this function is ItemList, it loads video according to the current page, whether it's Library, History or Search.
94
95> Currently, this function can be very inefficient as items are loaded one after another (sync). This will improve once Rust implements async traits.
96
97Errors will be displayed in MessageBar in the next frame.
98
99### .render(&mut self, &mut Framework, &mut Frame, area)
100
101Items should be able to render itself within `area` of `Frame`, the area is different for each item, and should not overlap.
102
103Each frame is rendered 2 times.
104
1051. Render all normal items.
1062. Render all popups, as they need to be above normal items.
107
108Normal items should not render in the popup render, for example:
109
110```rs
111if popup_render {
112 return;
113}
114```
115
116### .key_event(&mut self, &mut Framework, KeyEvent) -> Result
117
118Pretty self explanatory, selected item takes in a key event and do some modification to itself and/or the Framework state.
119
120Errors will be displayed in MessageBar in the next frame.
121
122### .mouse_event(&mut self, &mut Framework, x, y) -> bool
123
124Similar to key\_event, this function takes in the relative `x` and `y` of the mouse click on the item.
125
126The return boolean indicates if the item has been modified by the mouse click, if true then the screen will rerender.