docs/src/explaination/framework.md
128 lines · 5738 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 pools 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[This](https://github.com/Siriusmart/tui-additions/blob/3087abbf8e121f26c0956cda5fb43efc7b862bc7/src/framework/framework.rs#LL39C4-L39C4) showed how state data stays with the history *snapshot* of the page, whereas global data does not.
48
49## History
50
51This stores the past states and data of the struct, pretty self explanatory.
52
53<hr>
54
55## Items
56
57Items are placed across the screen *in rows*, each item is <u>individual</u> and changing of one should not affect others in anyway.
58
59
60
61> Diagram showing that individual items arranged in rows on screen.
62
63Each 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>:
64
65### .selectable(&self) -> bool
66
67The function is ran on page load, it determines whether *the item can be hovered by cursor or not*.
68
69Most item returns true, as they can be selected and hovered.
70
71However there are some exceptions, including MessageBar and the big info display in channel home page.
72
73> PageButtons returns true in this function, as they can be hovered by cursor. Instead, `.select()` is responsible for their special behaviour.
74
75### .select(&mut self, &mut Framework) -> bool
76
77This 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.
78
79Most items returns true in this function, for example SearchBar stays selected and captures key input after being selected, until deselect.
80
81PageButtons 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>.
82
83### .deselect(&mut self, &mut Framework) -> bool
84
85This 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.
86
87All items returns true on this function.
88
89> 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.
90
91### .load_item(&mut self, &mut Framework, ...) -> Result
92
93This 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.
94
95An item that heavily rely on this function is ItemList, it loads video according to the current page, whether it's Library, History or Search.
96
97> Currently, this function can be very inefficient as items are loaded one after another (sync). This will improve once Rust implements async traits.
98
99Errors will be displayed in MessageBar in the next frame.
100
101### .render(&mut self, &mut Framework, &mut Frame, area)
102
103Items should be able to render itself within `area` of `Frame`, the area is different for each item, and should not overlap.
104
105Each frame is rendered 2 times.
106
1071. Render all normal items.
1082. Render all popups, as they need to be above normal items.
109
110Normal items should not render in the popup render, for example:
111
112```rs
113if popup_render {
114 return;
115}
116```
117
118### .key_event(&mut self, &mut Framework, KeyEvent) -> Result
119
120Pretty self explanatory, selected item takes in a key event and do some modification to itself and/or the Framework state.
121
122Errors will be displayed in MessageBar in the next frame.
123
124### .mouse_event(&mut self, &mut Framework, x, y) -> bool
125
126Similar to key\_event, this function takes in the relative `x` and `y` of the mouse click on the item.
127
128The return boolean indicates if the item has been modified by the mouse click, if true then the screen will rerender.