Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/modalkit-ratatui/examples/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ impl TerminalCursor for EditorWindow {
EditorWindow::Listing(ls) => ls.get_term_cursor(),
}
}

fn hide_term_cursor(&self) -> bool {
match self {
EditorWindow::Text(tbox) => tbox.hide_term_cursor(),
EditorWindow::Listing(ls) => ls.hide_term_cursor(),
}
}
}

impl WindowOps<EditorInfo> for EditorWindow {
Expand Down Expand Up @@ -739,6 +746,9 @@ impl Editor {

render_cursor(f, sstate, cursor);
})?;
if sstate.hide_term_cursor() {
term.hide_cursor()?;
}

Ok(())
}
Expand Down
7 changes: 7 additions & 0 deletions crates/modalkit-ratatui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ pub trait TerminalCursor {
/// Returns the current offset of the cursor, relative to the upper left corner of the
/// terminal.
fn get_term_cursor(&self) -> Option<TermOffset>;

/// Returns whether the cursor should be invisible.
///
/// Accessibility tools like screen readers need the terminal cursor to be placed in a
/// meaningful way to be useful. Consider placing a hidden cursor to make these tools work
/// correctly.
fn hide_term_cursor(&self) -> bool;
}

/// A widget whose content can be scrolled in multiple ways.
Expand Down
16 changes: 13 additions & 3 deletions crates/modalkit-ratatui/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ where
items: Vec<T>,
cursor: ListCursor,
viewctx: ViewportContext<ListCursor>,
term_cursor: (u16, u16),

/// Tracks the jumplist for this window.
jumped: HistoryList<ListCursor>,
Expand Down Expand Up @@ -191,6 +192,7 @@ where
id,
items,
cursor: 0.into(),
term_cursor: (0, 0),
viewctx,
jumped: HistoryList::new(0.into(), 100),
}
Expand Down Expand Up @@ -1137,8 +1139,10 @@ where
I: ApplicationInfo,
{
fn get_term_cursor(&self) -> Option<(u16, u16)> {
// We highlight the selected text, but don't show the cursor.
return None;
self.term_cursor.into()
}
fn hide_term_cursor(&self) -> bool {
true
}
}

Expand All @@ -1154,6 +1158,7 @@ where
cursor: self.cursor.clone(),
viewctx: self.viewctx.clone(),
jumped: self.jumped.clone(),
term_cursor: (0, 0),
}
}

Expand Down Expand Up @@ -1236,6 +1241,7 @@ where
if state.is_empty() {
if let Some(msg) = self.empty_message {
Paragraph::new(msg).alignment(self.empty_alignment).render(area, buf);
state.term_cursor = (area.left(), area.top());
return;
}
}
Expand Down Expand Up @@ -1299,9 +1305,13 @@ where
let mut y = area.top();
let x = area.left();

for (_, _, txt) in lines.into_iter() {
for (idx, row, txt) in lines.into_iter() {
let _ = buf.set_line(x, y, &txt, area.width);

if row == 0 && idx == state.cursor.position {
state.term_cursor = (x, y);
}

y += 1;
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/modalkit-ratatui/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,19 @@ where
},
}
}

fn hide_term_cursor(&self) -> bool {
match self.focused {
CurrentFocus::Command => self.cmdbar.hide_term_cursor(),
CurrentFocus::Window => {
if let Some(w) = self.current_window() {
w.hide_term_cursor()
} else {
false
}
},
}
}
}

impl<W, C, I> Jumpable<C, I> for ScreenState<W, I>
Expand Down
4 changes: 4 additions & 0 deletions crates/modalkit-ratatui/src/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ where

self.term_cursor.into()
}

fn hide_term_cursor(&self) -> bool {
false
}
}

impl<I> WindowOps<I> for TextBoxState<I>
Expand Down
4 changes: 4 additions & 0 deletions crates/modalkit-ratatui/src/windows/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,10 @@ mod tests {
fn get_term_cursor(&self) -> Option<(u16, u16)> {
(self.term_area.left(), self.term_area.top()).into()
}

fn hide_term_cursor(&self) -> bool {
false
}
}

impl WindowOps<TestApp> for TestWindow {
Expand Down
3 changes: 3 additions & 0 deletions crates/modalkit-ratatui/src/windows/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ where
fn get_term_cursor(&self) -> Option<TermOffset> {
self.current.get_term_cursor()
}
fn hide_term_cursor(&self) -> bool {
self.current.hide_term_cursor()
}
}

impl<W, I> WindowOps<I> for WindowSlot<W>
Expand Down
Loading