Skip to content

Commit

Permalink
Add z scroll support - zz, zt and zb (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch authored Dec 10, 2024
1 parent cd8c66d commit 970c232
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 2 deletions.
3 changes: 3 additions & 0 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum KeyEvent {
W,
X,
Y,
Z,
CapA,
CapG,
CapI,
Expand All @@ -111,6 +112,8 @@ pub enum KeyEvent {
Enter,
Tab,
Tilde,
Dot,
Dash,
Esc,
}

Expand Down
13 changes: 13 additions & 0 deletions core/src/state/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ impl NotebookState {

format!("Note '{name}' normal mode - change inside {n}ci")
}
EditingNormalMode(VimNormalState::Scroll) => {
let name = &self.get_selected_note()?.name;

format!("Note '{name}' normal mode - scroll")
}
EditingVisualMode(VimVisualState::Idle) => {
let name = &self.get_selected_note()?.name;

Expand Down Expand Up @@ -395,6 +400,14 @@ impl NotebookState {
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimNormalState::Scroll) => {
vec![
"[z|.] Scroll to center".to_owned(),
"[t|Enter] Scroll to top".to_owned(),
"[b|-] Scroll to bottom".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingVisualMode(VimVisualState::Idle) => {
// more in the keymap
vec![
Expand Down
36 changes: 36 additions & 0 deletions core/src/state/notebook/inner_state/editing_normal_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum VimNormalState {
Change(usize),
Change2(usize, usize),
ChangeInside(usize),
Scroll,
}

pub async fn consume(
Expand All @@ -45,6 +46,7 @@ pub async fn consume(
VimNormalState::Change(n) => consume_change(state, n, event).await,
VimNormalState::Change2(n1, n2) => consume_change2(state, n1, n2, event).await,
VimNormalState::ChangeInside(n) => consume_change_inside(state, n, event).await,
VimNormalState::Scroll => consume_scroll(state, event).await,
}
}

Expand All @@ -66,6 +68,11 @@ async fn consume_idle(state: &mut NotebookState, event: Event) -> Result<Noteboo

ToggleMode.into()
}
Key(KeyEvent::Z) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Scroll);

ScrollMode.into()
}
Key(KeyEvent::P) => Paste.into(),
Key(KeyEvent::U) => Undo.into(),
Key(KeyEvent::CtrlR) => Redo.into(),
Expand Down Expand Up @@ -677,6 +684,35 @@ async fn consume_change_inside(
}
}

async fn consume_scroll(state: &mut NotebookState, event: Event) -> Result<NotebookTransition> {
use Event::*;
use NormalModeTransition::*;

match event {
Key(KeyEvent::Z | KeyEvent::Dot) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

ScrollCenter.into()
}
Key(KeyEvent::T | KeyEvent::Enter) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

ScrollTop.into()
}
Key(KeyEvent::B | KeyEvent::Dash) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

ScrollBottom.into()
}
event @ Key(_) => {
state.inner_state = InnerState::EditingNormalMode(VimNormalState::Idle);

consume_idle(state, event).await
}
_ => Err(Error::Wip("todo: Notebook::consume".to_owned())),
}
}

impl From<NormalModeTransition> for Result<NotebookTransition> {
fn from(transition: NormalModeTransition) -> Self {
Ok(NotebookTransition::EditingNormalMode(transition))
Expand Down
4 changes: 4 additions & 0 deletions core/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub enum NormalModeTransition {
DeleteInsideMode,
ChangeMode,
ChangeInsideMode,
ScrollMode,
NextTab(NoteId),
PrevTab(NoteId),
CloseTab(NoteId),
Expand All @@ -122,6 +123,9 @@ pub enum NormalModeTransition {
MoveCursorTop,
MoveCursorBottom,
MoveCursorToLine(usize),
ScrollCenter,
ScrollTop,
ScrollBottom,
InsertAtCursor,
InsertAtLineStart,
InsertAfterCursor,
Expand Down
3 changes: 3 additions & 0 deletions tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ fn to_event(input: Input) -> Option<KeyEvent> {
KeyCode::Char('w') => KeyEvent::W,
KeyCode::Char('x') => KeyEvent::X,
KeyCode::Char('y') => KeyEvent::Y,
KeyCode::Char('z') => KeyEvent::Z,
KeyCode::Char('A') => KeyEvent::CapA,
KeyCode::Char('G') => KeyEvent::CapG,
KeyCode::Char('I') => KeyEvent::CapI,
Expand All @@ -427,6 +428,8 @@ fn to_event(input: Input) -> Option<KeyEvent> {
KeyCode::Char('$') => KeyEvent::DollarSign,
KeyCode::Char('^') => KeyEvent::Caret,
KeyCode::Char('~') => KeyEvent::Tilde,
KeyCode::Char('.') => KeyEvent::Dot,
KeyCode::Char('-') => KeyEvent::Dash,
KeyCode::Left => KeyEvent::Left,
KeyCode::Right => KeyEvent::Right,
KeyCode::Up => KeyEvent::Up,
Expand Down
2 changes: 2 additions & 0 deletions tui/src/context/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct NotebookContext {
pub directory_actions_state: ListState,

// editor
pub editor_height: u16,
pub tabs: Vec<EditorTab>,
pub tab_index: Option<usize>,
pub show_line_number: bool,
Expand All @@ -96,6 +97,7 @@ impl Default for NotebookContext {
note_actions_state: ListState::default(),
directory_actions_state: ListState::default(),

editor_height: 0,
tabs: vec![],
tab_index: None,
show_line_number: true,
Expand Down
32 changes: 30 additions & 2 deletions tui/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use {
NotebookEvent,
},
std::time::SystemTime,
tui_textarea::{CursorMove, TextArea},
tui_textarea::{CursorMove, Scrolling, TextArea},
};

impl App {
Expand Down Expand Up @@ -200,7 +200,7 @@ impl App {
self.context.notebook.get_editor_mut().cancel_selection();
}
ToggleMode | NumberingMode | GatewayMode | YankMode | DeleteMode | DeleteInsideMode
| ChangeMode | ChangeInsideMode => {}
| ChangeMode | ChangeInsideMode | ScrollMode => {}
NextTab(note_id) | PrevTab(note_id) => {
let NotebookState { root, .. } = self.glues.state.get_inner().log_unwrap();

Expand Down Expand Up @@ -489,6 +489,34 @@ impl App {

self.context.notebook.mark_dirty();
}
ScrollCenter => {
let height = self.context.notebook.editor_height;
let editor = self.context.notebook.get_editor_mut();
let (row, col) = editor.cursor();
editor.scroll((i16::MIN / 2, 0));
editor.scroll(Scrolling::Delta {
rows: (row as i16 - height as i16 / 2),
cols: 0,
});
editor.move_cursor(CursorMove::Jump(row as u16, col as u16));
}
ScrollTop => {
let editor = self.context.notebook.get_editor_mut();
let (row, col) = editor.cursor();
editor.move_cursor(CursorMove::Top);
editor.scroll(Scrolling::Delta {
rows: row as i16,
cols: 0,
});
editor.move_cursor(CursorMove::Head);
editor.move_cursor(CursorMove::Jump(row as u16, col as u16));
}
ScrollBottom => {
let editor = self.context.notebook.get_editor_mut();
let (row, col) = editor.cursor();
editor.scroll((i16::MIN / 2, 0));
editor.move_cursor(CursorMove::Jump(row as u16, col as u16));
}
};
}

Expand Down
2 changes: 2 additions & 0 deletions tui/src/views/body/notebook/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use {
};

pub fn draw(frame: &mut Frame, area: Rect, context: &mut Context) {
context.notebook.editor_height = area.height - 2;

let title = if let Some(tab_index) = context.notebook.tab_index {
let mut title = vec!["[".into()];
for (i, tab) in context.notebook.tabs.iter().enumerate() {
Expand Down

0 comments on commit 970c232

Please sign in to comment.