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); buffer.action(TextAction::Down);
return Status::Captured; return Status::Captured;
}, },
KeyCode::Backspace => {
buffer.action(TextAction::Backspace);
return Status::Captured;
},
KeyCode::Delete => {
buffer.action(TextAction::Delete);
return Status::Captured;
},
KeyCode::Home => { KeyCode::Home => {
buffer.action(TextAction::Home); buffer.action(TextAction::Home);
return Status::Captured; return Status::Captured;
@ -255,6 +247,18 @@ where
buffer.action(TextAction::PageDown); buffer.action(TextAction::PageDown);
return Status::Captured; 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)) => { 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_RIGHT if event.pressed => buffer.action(TextAction::Right),
orbclient::K_UP if event.pressed => buffer.action(TextAction::Up), orbclient::K_UP if event.pressed => buffer.action(TextAction::Up),
orbclient::K_DOWN if event.pressed => buffer.action(TextAction::Down), 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_HOME if event.pressed => buffer.action(TextAction::Home),
orbclient::K_END if event.pressed => buffer.action(TextAction::End), orbclient::K_END if event.pressed => buffer.action(TextAction::End),
orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp), orbclient::K_PGUP if event.pressed => buffer.action(TextAction::PageUp),
orbclient::K_PGDN if event.pressed => buffer.action(TextAction::PageDown), 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 => { orbclient::K_0 if event.pressed && ctrl_pressed => {
font_size_i = font_size_default; font_size_i = font_size_default;
buffer.set_metrics(font_sizes[font_size_i]); buffer.set_metrics(font_sizes[font_size_i]);

View file

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