Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide cursor based on selection, preedit, or config #263

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
22 changes: 20 additions & 2 deletions src/edit/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ pub struct Editor<'buffer> {
cursor_moved: bool,
auto_indent: bool,
change: Option<Change>,
// A preedit was specified with non-empty text but with empty cursor,
// indicating that the cursor should be hidden
has_preedit_without_cursor: bool,
// Set with `set_cursor_hidden`
cursor_hidden_by_setting: bool,
}

fn cursor_glyph_opt(cursor: &Cursor, run: &LayoutRun) -> Option<(usize, f32)> {
Expand Down Expand Up @@ -104,6 +109,8 @@ impl<'buffer> Editor<'buffer> {
cursor_moved: false,
auto_indent: false,
change: None,
has_preedit_without_cursor: false,
cursor_hidden_by_setting: false,
}
}

Expand Down Expand Up @@ -191,8 +198,14 @@ impl<'buffer> Editor<'buffer> {
}

// Draw cursor
if let Some((x, y)) = cursor_position(&self.cursor, &run) {
f(x, y, 1, line_height as u32, cursor_color);
let cursor_hidden = self.cursor_hidden_by_setting
|| self.has_preedit_without_cursor
|| self.has_selection();

if !cursor_hidden {
if let Some((x, y)) = cursor_position(&self.cursor, &run) {
f(x, y, 1, line_height as u32, cursor_color);
}
}

for glyph in run.glyphs.iter() {
Expand Down Expand Up @@ -255,6 +268,11 @@ impl<'buffer> Edit<'buffer> for Editor<'buffer> {
}
}

fn set_cursor_hidden(&mut self, hidden: bool) {
self.cursor_hidden_by_setting = hidden;
self.set_redraw(true);
}

fn selection(&self) -> Selection {
self.selection
}
Expand Down
24 changes: 24 additions & 0 deletions src/edit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,33 @@ pub trait Edit<'buffer> {
/// Get the current cursor
fn cursor(&self) -> Cursor;

/// Hide or show the cursor
///
/// This should be used to hide the cursor, for example,
/// when the editor is unfocused, when the text is not editable,
/// or to implement cursor blinking.
///
/// Note that even after `set_cursor_hidden(false)`, the editor may
/// choose to hide the cursor based on internal state, for example,
/// when there is a selection or when there is a preedit without a cursor.
fn set_cursor_hidden(&mut self, hidden: bool);

/// Set the current cursor
fn set_cursor(&mut self, cursor: Cursor);

/// Returns true if some text is selected
fn has_selection(&self) -> bool {
match self.selection() {
Selection::None => false,
Selection::Normal(selection) => {
let cursor = self.cursor();
selection.line != cursor.line || selection.index != cursor.index
}
Selection::Line(selection) => selection.line != self.cursor().line,
Selection::Word(_) => true,
}
}

/// Get the current selection position
fn selection(&self) -> Selection;

Expand Down
4 changes: 4 additions & 0 deletions src/edit/syntect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for SyntaxEditor<'syntax_system, 'bu
self.editor.selection()
}

fn set_cursor_hidden(&mut self, hidden: bool) {
self.editor.set_cursor_hidden(hidden);
}

fn set_selection(&mut self, selection: Selection) {
self.editor.set_selection(selection);
}
Expand Down
4 changes: 4 additions & 0 deletions src/edit/vi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,10 @@ impl<'syntax_system, 'buffer> Edit<'buffer> for ViEditor<'syntax_system, 'buffer
fn cursor_position(&self) -> Option<(i32, i32)> {
self.editor.cursor_position()
}

fn set_cursor_hidden(&mut self, hidden: bool) {
self.editor.set_cursor_hidden(hidden);
}
}

impl<'font_system, 'syntax_system, 'buffer>
Expand Down
Loading