Skip to content

Commit abd1ed3

Browse files
authored
Add expand(<) and shrink(>) width support to NoteTree (#125)
1 parent 24c65d6 commit abd1ed3

File tree

10 files changed

+52
-1
lines changed

10 files changed

+52
-1
lines changed

core/src/event.rs

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub enum KeyEvent {
111111
DollarSign,
112112
Caret,
113113
QuestionMark,
114+
AngleBracketOpen,
115+
AngleBracketClose,
114116
Num(NumKey),
115117
Left,
116118
Right,

core/src/state/notebook.rs

+6
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ impl NotebookState {
268268
"[k] Select previous".to_owned(),
269269
"[G] Select last".to_owned(),
270270
"[1-9] Add steps".to_owned(),
271+
"[>] Expand width".to_owned(),
272+
"[<] Shrink width".to_owned(),
271273
"[Space] Move note".to_owned(),
272274
"[m] Show more actions".to_owned(),
273275
];
@@ -287,6 +289,8 @@ impl NotebookState {
287289
"[k] Select previous".to_owned(),
288290
"[G] Select last".to_owned(),
289291
"[1-9] Add steps".to_owned(),
292+
"[>] Expand width".to_owned(),
293+
"[<] Shrink width".to_owned(),
290294
"[Space] Move directory".to_owned(),
291295
"[m] Show more actions".to_owned(),
292296
];
@@ -304,6 +308,8 @@ impl NotebookState {
304308
format!("[k] Select {n} previous"),
305309
"[G] Select last".to_owned(),
306310
"[0-9] Append steps".to_owned(),
311+
format!("[>] Expand width by {n}"),
312+
format!("[<] Shrink width by {n}"),
307313
"[Esc] Cancel".to_owned(),
308314
]
309315
}

core/src/state/notebook/inner_state/note_tree/directory_selected.rs

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ pub async fn consume(
8181
Ok(NotebookTransition::None)
8282
}
8383
Key(KeyEvent::CapG) => Ok(NotebookTransition::NoteTree(NoteTreeTransition::SelectLast)),
84+
Key(KeyEvent::AngleBracketOpen) => Ok(NotebookTransition::NoteTree(
85+
NoteTreeTransition::ShrinkWidth(1),
86+
)),
87+
Key(KeyEvent::AngleBracketClose) => Ok(NotebookTransition::NoteTree(
88+
NoteTreeTransition::ExpandWidth(1),
89+
)),
8490
Key(KeyEvent::Tab) if !state.tabs.is_empty() => tabs::focus_editor(db, state).await,
8591
event @ Key(_) => Ok(NotebookTransition::Inedible(event)),
8692
_ => Err(Error::Wip("todo: Notebook::consume".to_owned())),

core/src/state/notebook/inner_state/note_tree/note_selected.rs

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ pub async fn consume(
7070
Ok(NotebookTransition::None)
7171
}
7272
Key(KeyEvent::CapG) => Ok(NotebookTransition::NoteTree(NoteTreeTransition::SelectLast)),
73+
Key(KeyEvent::AngleBracketOpen) => Ok(NotebookTransition::NoteTree(
74+
NoteTreeTransition::ShrinkWidth(1),
75+
)),
76+
Key(KeyEvent::AngleBracketClose) => Ok(NotebookTransition::NoteTree(
77+
NoteTreeTransition::ExpandWidth(1),
78+
)),
7379
Key(KeyEvent::Tab) if !state.tabs.is_empty() => tabs::focus_editor(db, state).await,
7480
event @ Key(_) => Ok(NotebookTransition::Inedible(event)),
7581
_ => Err(Error::Wip("todo: Notebook::consume".to_owned())),

core/src/state/notebook/inner_state/note_tree/numbering.rs

+12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ pub async fn consume(
5656
reset_state(state);
5757
Ok(NotebookTransition::NoteTree(NoteTreeTransition::SelectLast))
5858
}
59+
Key(KeyEvent::AngleBracketOpen) => {
60+
reset_state(state);
61+
Ok(NotebookTransition::NoteTree(
62+
NoteTreeTransition::ShrinkWidth(n),
63+
))
64+
}
65+
Key(KeyEvent::AngleBracketClose) => {
66+
reset_state(state);
67+
Ok(NotebookTransition::NoteTree(
68+
NoteTreeTransition::ExpandWidth(n),
69+
))
70+
}
5971
event @ Key(_) => {
6072
reset_state(state);
6173
Ok(NotebookTransition::Inedible(event))

core/src/transition.rs

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ pub enum NoteTreeTransition {
9595
SelectNext(usize),
9696
SelectPrev(usize),
9797
SelectLast,
98+
ExpandWidth(usize),
99+
ShrinkWidth(usize),
98100
}
99101

100102
pub enum MoveModeTransition {

tui/src/action.rs

+2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ fn to_event(input: Input) -> Option<KeyEvent> {
433433
KeyCode::Char('^') => KeyEvent::Caret,
434434
KeyCode::Char('~') => KeyEvent::Tilde,
435435
KeyCode::Char('?') => KeyEvent::QuestionMark,
436+
KeyCode::Char('<') => KeyEvent::AngleBracketOpen,
437+
KeyCode::Char('>') => KeyEvent::AngleBracketClose,
436438
KeyCode::Char('.') => KeyEvent::Dot,
437439
KeyCode::Char('-') => KeyEvent::Dash,
438440
KeyCode::Char(' ') => KeyEvent::Space,

tui/src/context/notebook.rs

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub struct NotebookContext {
7171
// note tree
7272
pub tree_state: ListState,
7373
pub tree_items: Vec<TreeItem>,
74+
pub tree_width: u16,
7475

7576
// note actions
7677
pub note_actions_state: ListState,
@@ -101,6 +102,7 @@ impl Default for NotebookContext {
101102
state: ContextState::NoteTreeBrowsing,
102103
tree_state: ListState::default().with_selected(Some(0)),
103104
tree_items: vec![],
105+
tree_width: 45,
104106

105107
note_actions_state: ListState::default(),
106108
directory_actions_state: ListState::default(),

tui/src/transitions/notebook/note_tree.rs

+13
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ impl App {
8787
let event = get_select_event(selected);
8888
self.glues.dispatch(event).await.log_unwrap();
8989
}
90+
NoteTreeTransition::ExpandWidth(n) => {
91+
let n = n.try_into().unwrap_or_default();
92+
let width = self.context.notebook.tree_width.saturating_add(n);
93+
94+
self.context.notebook.tree_width = width;
95+
}
96+
NoteTreeTransition::ShrinkWidth(n) => {
97+
let n = n.try_into().unwrap_or_default();
98+
let width = self.context.notebook.tree_width.saturating_sub(n);
99+
let width = if width < 11 { 11 } else { width };
100+
101+
self.context.notebook.tree_width = width;
102+
}
90103
NoteTreeTransition::ShowNoteActionsDialog(_)
91104
| NoteTreeTransition::ShowDirectoryActionsDialog(_) => {}
92105
}

tui/src/views/body/notebook.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub fn draw(frame: &mut Frame, area: Rect, context: &mut Context) {
1919
return;
2020
}
2121

22-
let horizontal = Layout::horizontal([Length(45), Percentage(100)]);
22+
let horizontal = Layout::horizontal([Length(context.notebook.tree_width), Percentage(100)]);
2323
let [note_tree_area, editor_area] = horizontal.areas(area);
2424

2525
note_tree::draw(frame, note_tree_area, &mut context.notebook);

0 commit comments

Comments
 (0)