Fix control character usage

This commit is contained in:
Jeremy Soller 2022-10-19 11:33:35 -06:00
parent 07a832efd4
commit edc0631df6
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
3 changed files with 82 additions and 74 deletions

View file

@ -231,14 +231,6 @@ where
buffer.action(TextAction::Down);
return Status::Captured;
},
KeyCode::Backspace => {
buffer.action(TextAction::Backspace);
return Status::Captured;
},
KeyCode::Delete => {
buffer.action(TextAction::Delete);
return Status::Captured;
},
KeyCode::Home => {
buffer.action(TextAction::Home);
return Status::Captured;
@ -255,6 +247,18 @@ where
buffer.action(TextAction::PageDown);
return Status::Captured;
},
KeyCode::Enter => {
buffer.action(TextAction::Enter);
return Status::Captured;
},
KeyCode::Backspace => {
buffer.action(TextAction::Backspace);
return Status::Captured;
},
KeyCode::Delete => {
buffer.action(TextAction::Delete);
return Status::Captured;
},
_ => ()
},
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {

View file

@ -164,12 +164,13 @@ fn main() {
orbclient::K_RIGHT if event.pressed => buffer.action(TextAction::Right),
orbclient::K_UP if event.pressed => buffer.action(TextAction::Up),
orbclient::K_DOWN if event.pressed => buffer.action(TextAction::Down),
orbclient::K_BKSP if event.pressed => buffer.action(TextAction::Backspace),
orbclient::K_DEL if event.pressed => buffer.action(TextAction::Delete),
orbclient::K_HOME if event.pressed => buffer.action(TextAction::Home),
orbclient::K_END if event.pressed => buffer.action(TextAction::End),
orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp),
orbclient::K_PGDN if event.pressed => buffer.action(TextAction::PageDown),
orbclient::K_ENTER if event.pressed => buffer.action(TextAction::Enter),
orbclient::K_BKSP if event.pressed => buffer.action(TextAction::Backspace),
orbclient::K_DEL if event.pressed => buffer.action(TextAction::Delete),
orbclient::K_0 if event.pressed && ctrl_pressed => {
font_size_i = font_size_default;
buffer.set_metrics(font_sizes[font_size_i]);

View file

@ -17,10 +17,6 @@ pub enum TextAction {
Up,
/// Move cursor down
Down,
/// Delete text behind cursor
Backspace,
/// Delete text in front of cursor
Delete,
/// Move cursor to start of line
Home,
/// Move cursor to end of line
@ -31,6 +27,12 @@ pub enum TextAction {
PageDown,
/// Insert character at cursor
Insert(char),
/// Create new line
Enter,
/// Delete text behind cursor
Backspace,
/// Delete text in front of cursor
Delete,
/// Mouse click at specified position
Click { x: i32, y: i32 },
/// Mouse drag to specified position
@ -363,29 +365,6 @@ impl<'a> TextBuffer<'a> {
}
}
},
TextAction::Backspace => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph > line.glyphs.len() {
self.cursor.glyph = line.glyphs.len();
self.redraw = true;
}
if self.cursor.glyph > 0 {
self.cursor.glyph -= 1;
let glyph = &line.glyphs[self.cursor.glyph];
let text_line = &mut self.text_lines[line.line_i.get()];
text_line.remove(glyph.start);
self.reshape_line(line.line_i);
}
},
TextAction::Delete => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph < line.glyphs.len() {
let glyph = &line.glyphs[self.cursor.glyph];
let text_line = &mut self.text_lines[line.line_i.get()];
text_line.remove(glyph.start);
self.reshape_line(line.line_i);
}
},
TextAction::Home => {
if self.cursor.glyph > 0 {
self.cursor.glyph = 0;
@ -409,50 +388,74 @@ impl<'a> TextBuffer<'a> {
self.redraw = true;
self.shape_until_scroll();
},
TextAction::Insert(character) => match character {
'\r' | '\n' => {
{
let line = &self.layout_lines[self.cursor.line];
let new_line = if self.cursor.glyph >= line.glyphs.len() {
String::new()
} else {
let glyph = &line.glyphs[self.cursor.glyph];
self.text_lines[line.line_i.get()].split_off(glyph.start)
};
self.text_lines.insert(line.line_i.get() + 1, new_line);
// Reshape all lines after new line
//TODO: improve performance
self.shape_lines.truncate(line.line_i.get());
self.relayout();
self.shape_until_scroll();
TextAction::Insert(character) => if character.is_control() {
// Filter out special chars, use TextAction instead
log::debug!("Refusing to insert control character {:?}", character);
} else {
let line = &self.layout_lines[self.cursor.line];
let insert_i = if self.cursor.glyph >= line.glyphs.len() {
match line.glyphs.last() {
Some(glyph) => glyph.end,
None => self.text_lines[line.line_i.get()].len()
}
} else {
line.glyphs[self.cursor.glyph].start
};
self.cursor.glyph = 0;
self.cursor.line += 1;
let lines = self.lines();
if (self.cursor.line as i32) < self.scroll
|| (self.cursor.line as i32) >= self.scroll + lines
{
self.scroll = self.cursor.line as i32 - (lines - 1);
self.shape_until_scroll();
}
},
_ => {
let text_line = &mut self.text_lines[line.line_i.get()];
text_line.insert(insert_i, character);
self.cursor.glyph += 1;
self.reshape_line(line.line_i);
},
TextAction::Enter => {
{
let line = &self.layout_lines[self.cursor.line];
let insert_i = if self.cursor.glyph >= line.glyphs.len() {
match line.glyphs.last() {
Some(glyph) => glyph.end,
None => self.text_lines[line.line_i.get()].len()
}
let new_line = if self.cursor.glyph >= line.glyphs.len() {
String::new()
} else {
line.glyphs[self.cursor.glyph].start
let glyph = &line.glyphs[self.cursor.glyph];
self.text_lines[line.line_i.get()].split_off(glyph.start)
};
self.text_lines.insert(line.line_i.get() + 1, new_line);
// Reshape all lines after new line
//TODO: improve performance
self.shape_lines.truncate(line.line_i.get());
self.relayout();
self.shape_until_scroll();
}
self.cursor.glyph = 0;
self.cursor.line += 1;
let lines = self.lines();
if (self.cursor.line as i32) < self.scroll
|| (self.cursor.line as i32) >= self.scroll + lines
{
self.scroll = self.cursor.line as i32 - (lines - 1);
self.shape_until_scroll();
}
},
TextAction::Backspace => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph > line.glyphs.len() {
self.cursor.glyph = line.glyphs.len();
self.redraw = true;
}
if self.cursor.glyph > 0 {
self.cursor.glyph -= 1;
let glyph = &line.glyphs[self.cursor.glyph];
let text_line = &mut self.text_lines[line.line_i.get()];
text_line.insert(insert_i, character);
self.cursor.glyph += 1;
text_line.remove(glyph.start);
self.reshape_line(line.line_i);
}
},
TextAction::Delete => {
let line = &self.layout_lines[self.cursor.line];
if self.cursor.glyph < line.glyphs.len() {
let glyph = &line.glyphs[self.cursor.glyph];
let text_line = &mut self.text_lines[line.line_i.get()];
text_line.remove(glyph.start);
self.reshape_line(line.line_i);
}
},