-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcolumn.rs
189 lines (157 loc) · 4.71 KB
/
column.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use crate::{
actionbar::TimelineOpenResult,
route::{Route, Router},
timeline::{Timeline, TimelineCache, TimelineKind},
};
use enostr::RelayPool;
use nostrdb::{Ndb, Transaction};
use notedeck::NoteCache;
use std::iter::Iterator;
use tracing::warn;
#[derive(Clone, Debug)]
pub struct Column {
router: Router<Route>,
}
impl Column {
pub fn new(routes: Vec<Route>) -> Self {
let router = Router::new(routes);
Column { router }
}
pub fn router(&self) -> &Router<Route> {
&self.router
}
pub fn router_mut(&mut self) -> &mut Router<Route> {
&mut self.router
}
}
#[derive(Default, Debug)]
pub struct Columns {
/// Columns are simply routers into settings, timelines, etc
columns: Vec<Column>,
/// The selected column for key navigation
selected: i32,
}
impl Columns {
pub fn new() -> Self {
Columns::default()
}
pub fn add_new_timeline_column(
&mut self,
timeline_cache: &mut TimelineCache,
txn: &Transaction,
ndb: &Ndb,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
kind: &TimelineKind,
) -> Option<TimelineOpenResult> {
self.columns
.push(Column::new(vec![Route::timeline(kind.to_owned())]));
timeline_cache.open(ndb, note_cache, txn, pool, kind)
}
pub fn new_column_picker(&mut self) {
self.add_column(Column::new(vec![Route::AddColumn(
crate::ui::add_column::AddColumnRoute::Base,
)]));
}
pub fn insert_intermediary_routes(
&mut self,
timeline_cache: &mut TimelineCache,
intermediary_routes: Vec<IntermediaryRoute>,
) {
let routes = intermediary_routes
.into_iter()
.map(|r| match r {
IntermediaryRoute::Timeline(timeline) => {
let route = Route::timeline(timeline.kind.clone());
timeline_cache
.timelines
.insert(timeline.kind.clone(), timeline);
route
}
IntermediaryRoute::Route(route) => route,
})
.collect();
self.columns.push(Column::new(routes));
}
pub fn add_column_at(&mut self, column: Column, index: u32) {
self.columns.insert(index as usize, column);
}
pub fn add_column(&mut self, column: Column) {
self.columns.push(column);
}
pub fn columns_mut(&mut self) -> &mut Vec<Column> {
&mut self.columns
}
pub fn num_columns(&self) -> usize {
self.columns.len()
}
// Get the first router in the columns if there are columns present.
// Otherwise, create a new column picker and return the router
pub fn get_first_router(&mut self) -> &mut Router<Route> {
if self.columns.is_empty() {
self.new_column_picker();
}
self.columns[0].router_mut()
}
pub fn column(&self, ind: usize) -> &Column {
&self.columns[ind]
}
pub fn columns(&self) -> &[Column] {
&self.columns
}
pub fn selected(&mut self) -> &mut Column {
&mut self.columns[self.selected as usize]
}
pub fn column_mut(&mut self, ind: usize) -> &mut Column {
&mut self.columns[ind]
}
pub fn select_down(&mut self) {
warn!("todo: implement select_down");
}
pub fn select_up(&mut self) {
warn!("todo: implement select_up");
}
pub fn select_left(&mut self) {
if self.selected - 1 < 0 {
return;
}
self.selected -= 1;
}
pub fn select_right(&mut self) {
if self.selected + 1 >= self.columns.len() as i32 {
return;
}
self.selected += 1;
}
#[must_use = "you must call timeline_cache.pop() for each returned value"]
pub fn delete_column(&mut self, index: usize) -> Vec<TimelineKind> {
let mut kinds_to_pop: Vec<TimelineKind> = vec![];
for route in self.columns[index].router().routes() {
if let Route::Timeline(kind) = route {
kinds_to_pop.push(kind.clone());
}
}
self.columns.remove(index);
if self.columns.is_empty() {
self.new_column_picker();
}
kinds_to_pop
}
pub fn move_col(&mut self, from_index: usize, to_index: usize) {
if from_index == to_index
|| from_index >= self.columns.len()
|| to_index >= self.columns.len()
{
return;
}
self.columns.swap(from_index, to_index);
}
}
pub enum IntermediaryRoute {
Timeline(Timeline),
Route(Route),
}
pub enum ColumnsAction {
Switch(usize, usize), // from Switch.0 to Switch.1,
Remove(usize),
}