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

Fix cursor not showing with empty buff #182

Merged
merged 1 commit into from
Nov 19, 2023
Merged
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
34 changes: 14 additions & 20 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,15 @@ impl From<(&KeyEvent, &SmolStr)> for TextCommand {
(ModifiersState::SUPER, "c") => Self::Copy,
(ModifiersState::SUPER, "x") => Self::Cut,
(ModifiersState::SUPER, "v") => Self::Paste,
_ => {
dbg!("Unhandled action", event.modifiers, ch);
Self::None
}
_ => Self::None,
}
#[cfg(not(target_os = "macos"))]
match (event.modifiers, ch.as_str()) {
(ModifiersState::CONTROL, "a") => Self::SelectAll,
(ModifiersState::CONTROL, "c") => Self::Copy,
(ModifiersState::CONTROL, "x") => Self::Cut,
(ModifiersState::CONTROL, "v") => Self::Paste,
_ => {
dbg!("Unhandled action", event.modifiers, ch);
Self::None
}
_ => Self::None,
}
}
}
Expand Down Expand Up @@ -294,11 +288,10 @@ impl TextInput {
}

fn get_cursor_rect(&self, node_layout: &Layout) -> Rect {
let virtual_text = self.text_buf.as_ref().unwrap();
let text_height = virtual_text.size().height;

let node_location = node_layout.location;

let text_height = self.height;

let cursor_start = Point::new(
self.cursor_x + node_location.x as f64,
node_location.y as f64,
Expand All @@ -308,7 +301,7 @@ impl TextInput {
cursor_start,
Point::new(
cursor_start.x + self.cursor_width,
cursor_start.y + text_height,
cursor_start.y + text_height as f64,
),
)
}
Expand Down Expand Up @@ -347,20 +340,24 @@ impl TextInput {

fn update_text_layout(&mut self) {
let mut text_layout = TextLayout::new();
let attrs = self.get_text_attrs();
let attrs_list = self.get_text_attrs();

self.buffer
.with_untracked(|buff| text_layout.set_text(buff, attrs.clone()));
.with_untracked(|buff| text_layout.set_text(buff, attrs_list.clone()));

self.width = APPROX_VISIBLE_CHARS * self.font_size();
self.height = self.font_size();

// determine the height of the text, even if the buff is empty
let mut tmp = TextLayout::new();
tmp.set_text("W", attrs_list.clone());
self.height = tmp.size().height as f32;

// main buff should always get updated
self.text_buf = Some(text_layout.clone());

if let Some(cr_text) = self.clipped_text.clone().as_ref() {
let mut clp_txt_lay = text_layout;
clp_txt_lay.set_text(cr_text, attrs);
clp_txt_lay.set_text(cr_text, attrs_list);

self.clip_txt_buf = Some(clp_txt_lay);
}
Expand Down Expand Up @@ -626,10 +623,7 @@ impl TextInput {

cursor_moved
}
ref key => {
dbg!("Unhandled key", key);
false
}
_ => false,
}
}

Expand Down