diff --git a/examples/key_codes.rs b/examples/key_codes.rs index d6b6c01..6301416 100644 --- a/examples/key_codes.rs +++ b/examples/key_codes.rs @@ -36,10 +36,7 @@ impl View for KeyCodeView { } fn on_event(&mut self, event: Event) -> EventResult { - let line = match event { - Event::Char(c) => format!("Char: {}", c), - Event::Key(key) => format!("Key: {}", key), - }; + let line = format!("{:?}", event); self.history.push(line); while self.history.len() > self.size { diff --git a/src/backend/curses.rs b/src/backend/curses.rs index d91f645..d79974f 100644 --- a/src/backend/curses.rs +++ b/src/backend/curses.rs @@ -1,5 +1,5 @@ use backend; -use event; +use event::{Event, Key}; use theme; use utf8; @@ -80,16 +80,16 @@ impl backend::Backend for NcursesBackend { ncurses::mvaddstr(y as i32, x as i32, text); } - fn poll_event() -> event::Event { + fn poll_event() -> Event { let ch: i32 = ncurses::getch(); // Is it a UTF-8 starting point? if 32 <= ch && ch < 0x100 && ch != 127 { - event::Event::Char(utf8::read_char(ch as u8, + Event::Char(utf8::read_char(ch as u8, || ncurses::getch() as u8) .unwrap()) } else { - event::Event::Key(parse_ncurses_char(ch)) + parse_ncurses_char(ch) } } @@ -103,125 +103,122 @@ impl backend::Backend for NcursesBackend { } /// Returns the Key enum corresponding to the given ncurses event. -fn parse_ncurses_char(ch: i32) -> event::Key { +fn parse_ncurses_char(ch: i32) -> Event { match ch { // Values under 256 are chars and control values // // Tab is '\t' - 9 => event::Key::Tab, + 9 => Event::Key(Key::Tab), // Treat '\n' and the numpad Enter the same 10 | - ncurses::KEY_ENTER => event::Key::Enter, + ncurses::KEY_ENTER => Event::Key(Key::Enter), // This is the escape key when pressed by itself. // When used for control sequences, it should have been caught earlier. - 27 => event::Key::Esc, + 27 => Event::Key(Key::Esc), // `Backspace` sends 127, but Ctrl-H sends `Backspace` 127 | - ncurses::KEY_BACKSPACE => event::Key::Backspace, + ncurses::KEY_BACKSPACE => Event::Key(Key::Backspace), - 410 => event::Key::Resize, + 410 => Event::WindowResize, // Values 512 and above are probably extensions // Those keys don't seem to be documented... - 519 => event::Key::AltDel, - 520 => event::Key::AltShiftDel, - 521 => event::Key::CtrlDel, - 522 => event::Key::CtrlShiftDel, + 519 => Event::Alt(Key::Del), + 520 => Event::AltShift(Key::Del), + 521 => Event::Ctrl(Key::Del), + 522 => Event::CtrlShift(Key::Del), // 523: CtrlAltDel? // // 524? - 525 => event::Key::AltDown, - 526 => event::Key::AltShiftDown, - 527 => event::Key::CtrlDown, - 528 => event::Key::CtrlShiftDown, - 529 => event::Key::CtrlAltDown, + 525 => Event::Alt(Key::Down), + 526 => Event::AltShift(Key::Down), + 527 => Event::Ctrl(Key::Down), + 528 => Event::CtrlShift(Key::Down), + 529 => Event::CtrlAlt(Key::Down), - 530 => event::Key::AltEnd, - 531 => event::Key::AltShiftEnd, - 532 => event::Key::CtrlEnd, - 533 => event::Key::CtrlShiftEnd, - 534 => event::Key::CtrlAltEnd, + 530 => Event::Alt(Key::End), + 531 => Event::AltShift(Key::End), + 532 => Event::Ctrl(Key::End), + 533 => Event::CtrlShift(Key::End), + 534 => Event::CtrlAlt(Key::End), - 535 => event::Key::AltHome, - 536 => event::Key::AltShiftHome, - 537 => event::Key::CtrlHome, - 538 => event::Key::CtrlShiftHome, - 539 => event::Key::CtrlAltHome, + 535 => Event::Alt(Key::Home), + 536 => Event::AltShift(Key::Home), + 537 => Event::Ctrl(Key::Home), + 538 => Event::CtrlShift(Key::Home), + 539 => Event::CtrlAlt(Key::Home), - 540 => event::Key::AltIns, + 540 => Event::Alt(Key::Ins), // 541: AltShiftIns? - 542 => event::Key::CtrlIns, + 542 => Event::Ctrl(Key::Ins), // 543: CtrlShiftIns? - 544 => event::Key::CtrlAltIns, + 544 => Event::CtrlAlt(Key::Ins), - 545 => event::Key::AltLeft, - 546 => event::Key::AltShiftLeft, - 547 => event::Key::CtrlLeft, - 548 => event::Key::CtrlShiftLeft, - 549 => event::Key::CtrlAltLeft, + 545 => Event::Alt(Key::Left), + 546 => Event::AltShift(Key::Left), + 547 => Event::Ctrl(Key::Left), + 548 => Event::CtrlShift(Key::Left), + 549 => Event::CtrlAlt(Key::Left), - 550 => event::Key::AltPageDown, - 551 => event::Key::AltShiftPageDown, - 552 => event::Key::CtrlPageDown, - 553 => event::Key::CtrlShiftPageDown, - 554 => event::Key::CtrlAltPageDown, + 550 => Event::Alt(Key::PageDown), + 551 => Event::AltShift(Key::PageDown), + 552 => Event::Ctrl(Key::PageDown), + 553 => Event::CtrlShift(Key::PageDown), + 554 => Event::CtrlAlt(Key::PageDown), - 555 => event::Key::AltPageUp, - 556 => event::Key::AltShiftPageUp, - 557 => event::Key::CtrlPageUp, - 558 => event::Key::CtrlShiftPageUp, - 559 => event::Key::CtrlAltPageUp, + 555 => Event::Alt(Key::PageUp), + 556 => Event::AltShift(Key::PageUp), + 557 => Event::Ctrl(Key::PageUp), + 558 => Event::CtrlShift(Key::PageUp), + 559 => Event::CtrlAlt(Key::PageUp), - 560 => event::Key::AltRight, - 561 => event::Key::AltShiftRight, - 562 => event::Key::CtrlRight, - 563 => event::Key::CtrlShiftRight, - 564 => event::Key::CtrlAltRight, + 560 => Event::Alt(Key::Right), + 561 => Event::AltShift(Key::Right), + 562 => Event::Ctrl(Key::Right), + 563 => Event::CtrlShift(Key::Right), + 564 => Event::CtrlAlt(Key::Right), // 565? - 566 => event::Key::AltUp, - 567 => event::Key::AltShiftUp, - 568 => event::Key::CtrlUp, - 569 => event::Key::CtrlShiftUp, - 570 => event::Key::CtrlAltUp, + 566 => Event::Alt(Key::Up), + 567 => Event::AltShift(Key::Up), + 568 => Event::Ctrl(Key::Up), + 569 => Event::CtrlShift(Key::Up), + 570 => Event::CtrlAlt(Key::Up), - ncurses::KEY_B2 => event::Key::NumpadCenter, - ncurses::KEY_DC => event::Key::Del, - ncurses::KEY_IC => event::Key::Ins, - ncurses::KEY_BTAB => event::Key::ShiftTab, - ncurses::KEY_SLEFT => event::Key::ShiftLeft, - ncurses::KEY_SRIGHT => event::Key::ShiftRight, - ncurses::KEY_LEFT => event::Key::Left, - ncurses::KEY_RIGHT => event::Key::Right, - ncurses::KEY_UP => event::Key::Up, - ncurses::KEY_DOWN => event::Key::Down, - ncurses::KEY_SR => event::Key::ShiftUp, - ncurses::KEY_SF => event::Key::ShiftDown, - ncurses::KEY_PPAGE => event::Key::PageUp, - ncurses::KEY_NPAGE => event::Key::PageDown, - ncurses::KEY_HOME => event::Key::Home, - ncurses::KEY_END => event::Key::End, - ncurses::KEY_SHOME => event::Key::ShiftHome, - ncurses::KEY_SEND => event::Key::ShiftEnd, - ncurses::KEY_SDC => event::Key::ShiftDel, - ncurses::KEY_SNEXT => event::Key::ShiftPageDown, - ncurses::KEY_SPREVIOUS => event::Key::ShiftPageUp, + ncurses::KEY_B2 => Event::Key(Key::NumpadCenter), + ncurses::KEY_DC => Event::Key(Key::Del), + ncurses::KEY_IC => Event::Key(Key::Ins), + ncurses::KEY_BTAB => Event::Shift(Key::Tab), + ncurses::KEY_SLEFT => Event::Shift(Key::Left), + ncurses::KEY_SRIGHT => Event::Shift(Key::Right), + ncurses::KEY_LEFT => Event::Key(Key::Left), + ncurses::KEY_RIGHT => Event::Key(Key::Right), + ncurses::KEY_UP => Event::Key(Key::Up), + ncurses::KEY_DOWN => Event::Key(Key::Down), + ncurses::KEY_SR => Event::Shift(Key::Up), + ncurses::KEY_SF => Event::Shift(Key::Down), + ncurses::KEY_PPAGE => Event::Key(Key::PageUp), + ncurses::KEY_NPAGE => Event::Key(Key::PageDown), + ncurses::KEY_HOME => Event::Key(Key::Home), + ncurses::KEY_END => Event::Key(Key::End), + ncurses::KEY_SHOME => Event::Shift(Key::Home), + ncurses::KEY_SEND => Event::Shift(Key::End), + ncurses::KEY_SDC => Event::Shift(Key::Del), + ncurses::KEY_SNEXT => Event::Shift(Key::PageDown), + ncurses::KEY_SPREVIOUS => Event::Shift(Key::PageUp), // All Fn keys use the same enum with associated number f @ ncurses::KEY_F1...ncurses::KEY_F12 => { - event::Key::F((f - ncurses::KEY_F0) as u8) + Event::Key(Key::from_f((f - ncurses::KEY_F0) as u8)) } - f @ 277...288 => event::Key::ShiftF((f - 277) as u8), - f @ 289...300 => event::Key::CtrlF((f - 289) as u8), - f @ 301...312 => event::Key::CtrlShiftF((f - 300) as u8), - f @ 313...324 => event::Key::AltF((f - 313) as u8), - // Shift and Ctrl F{1-4} need escape sequences... - // - // TODO: shift and ctrl Fn keys - // Avoids 8-10 (H,I,J), they are used by other commands. - c @ 1...7 | c @ 11...25 => { - event::Key::CtrlChar((b'a' + (c - 1) as u8) as char) + f @ 277...288 => Event::Shift(Key::from_f((f - 276) as u8)), + f @ 289...300 => Event::Ctrl(Key::from_f((f - 288) as u8)), + f @ 301...312 => Event::CtrlShift(Key::from_f((f - 300) as u8)), + f @ 313...324 => Event::Alt(Key::from_f((f - 312) as u8)), + // Values 8-10 (H,I,J) are used by other commands, so we won't receive them. + c @ 1...25 => { + Event::CtrlChar((b'a' + (c - 1) as u8) as char) } - _ => event::Key::Unknown(ch), + _ => Event::Unknown(ch), } } diff --git a/src/event.rs b/src/event.rs index 2e285de..468d8e8 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,6 +1,5 @@ //! User-input events and their effects. -use std::fmt; use std::rc::Rc; use Cursive; @@ -24,250 +23,137 @@ impl EventResult { } } -/// Represents a key, or a combination of keys. -#[derive(PartialEq,Eq,Clone,Copy,Hash)] +/// A non-character key on the keyboard +#[derive(PartialEq,Eq,Clone,Copy,Hash,Debug)] pub enum Key { - /// Both Enter and numpad Enter + /// Both Enter (or Return) and numpad Enter Enter, /// Tabulation key Tab, - ShiftTab, + /// Backspace key Backspace, - - /// Indicates the window was resized - /// - /// (Not really a key) - Resize, - - /// Escape key. + /// Escape key Esc, + + /// Left arrow + Left, + /// Right arrow + Right, + /// Up arrow + Up, + /// Down arrow + Down, + + /// Insert key + Ins, + /// Delete key + Del, + /// Home key + Home, + /// End key + End, + /// Page Up key + PageUp, + /// Page Down key + PageDown, + /// The 5 in the center of the keypad, when numlock is disabled. NumpadCenter, - Left, - /// Left arrow while shift is pressed. - ShiftLeft, - AltLeft, - AltShiftLeft, - CtrlLeft, - CtrlShiftLeft, - CtrlAltLeft, - - Right, - /// Right arrow while shift is pressed. - ShiftRight, - AltRight, - AltShiftRight, - CtrlRight, - CtrlShiftRight, - CtrlAltRight, - - Up, - ShiftUp, - AltUp, - AltShiftUp, - CtrlUp, - CtrlShiftUp, - CtrlAltUp, - - Down, - ShiftDown, - AltDown, - AltShiftDown, - CtrlDown, - CtrlShiftDown, - CtrlAltDown, - - PageUp, - ShiftPageUp, - AltPageUp, - AltShiftPageUp, - CtrlPageUp, - CtrlShiftPageUp, - CtrlAltPageUp, - - PageDown, - ShiftPageDown, - AltPageDown, - AltShiftPageDown, - CtrlPageDown, - CtrlShiftPageDown, - CtrlAltPageDown, - - Home, - ShiftHome, - AltHome, - AltShiftHome, - CtrlHome, - CtrlShiftHome, - CtrlAltHome, - - End, - ShiftEnd, - AltEnd, - AltShiftEnd, - CtrlEnd, - CtrlShiftEnd, - CtrlAltEnd, - - /// Delete key - Del, - ShiftDel, - AltDel, - AltShiftDel, - CtrlDel, - CtrlShiftDel, - - /// Insert key. - Ins, - /// Insert key while ctrl is pressed. - CtrlIns, - AltIns, - CtrlAltIns, - - F(u8), - ShiftF(u8), - AltF(u8), - CtrlF(u8), - CtrlShiftF(u8), - CtrlChar(char), - Unknown(i32), + /// F1 key + F1, + /// F2 key + F2, + /// F3 key + F3, + /// F4 key + F4, + /// F5 key + F5, + /// F6 key + F6, + /// F7 key + F7, + /// F8 key + F8, + /// F9 key + F9, + /// F10 key + F10, + /// F11 key + F11, + /// F12 key + F12, } -impl fmt::Display for Key { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Key::Unknown(ch) => write!(f, "Unknown: {}", ch), - Key::CtrlChar(ch) => write!(f, "Ctrl-{}", ch), - Key::F(n) => write!(f, "F{}", n), - Key::ShiftF(n) => write!(f, "Shift-F{}", n), - Key::AltF(n) => write!(f, "Alt-F{}", n), - Key::CtrlF(n) => write!(f, "Ctrl-F{}", n), - Key::CtrlShiftF(n) => write!(f, "Ctrl-Shift-F{}", n), - key => { - write!(f, - "{}", - match key { - Key::Resize => "Screen resize", - Key::NumpadCenter => "Numpad center", - Key::Backspace => "Backspace", - Key::Enter => "Enter", - Key::Tab => "Tab", - Key::ShiftTab => "Shift-Tab", - - Key::PageUp => "PageUp", - Key::ShiftPageUp => "Shift-PageUp", - Key::AltPageUp => "Alt-PageUp", - Key::AltShiftPageUp => "Alt-Shift-PageUp", - Key::CtrlPageUp => "Ctrl-PageUp", - Key::CtrlShiftPageUp => "Ctrl-Shift-PageUp", - Key::CtrlAltPageUp => "Ctrl-Alt-PageUp", - - Key::PageDown => "PageDown", - Key::ShiftPageDown => "Shift-PageDown", - Key::AltPageDown => "Alt-PageDown", - Key::AltShiftPageDown => "Alt-Shift-PageDown", - Key::CtrlPageDown => "Ctrl-PageDown", - Key::CtrlShiftPageDown => "Ctrl-Shift-PageDown", - Key::CtrlAltPageDown => "Ctrl-Alt-PageDown", - - Key::Left => "Left", - Key::ShiftLeft => "Shift-Left", - Key::AltLeft => "Alt-Left", - Key::AltShiftLeft => "Alt-Shift-Left", - Key::CtrlLeft => "Ctrl-Left", - Key::CtrlShiftLeft => "Ctrl-Shift-Left", - Key::CtrlAltLeft => "Ctrl-Alt-Left", - - Key::Right => "Right", - Key::ShiftRight => "Shift-Right", - Key::AltRight => "Alt-Right", - Key::AltShiftRight => "Alt-Shift-Right", - Key::CtrlRight => "Ctrl-Right", - Key::CtrlShiftRight => "Ctrl-Shift-Right", - Key::CtrlAltRight => "Ctrl-Alt-Right", - - Key::Down => "Down", - Key::ShiftDown => "Shift-Down", - Key::AltDown => "Alt-Down", - Key::AltShiftDown => "Alt-Shift-Down", - Key::CtrlDown => "Ctrl-Down", - Key::CtrlShiftDown => "Ctrl-Shift-Down", - Key::CtrlAltDown => "Ctrl-Alt-Down", - - Key::Up => "Up", - Key::ShiftUp => "Shift-Up", - Key::AltUp => "Alt-Up", - Key::AltShiftUp => "Alt-Shift-Up", - Key::CtrlUp => "Ctrl-Up", - Key::CtrlShiftUp => "Ctrl-Shift-Up", - Key::CtrlAltUp => "Ctrl-Alt-Up", - - Key::Del => "Del", - Key::ShiftDel => "Shift-Del", - Key::AltDel => "Alt-Del", - Key::AltShiftDel => "Alt-Shift-Del", - Key::CtrlDel => "Ctrl-Del", - Key::CtrlShiftDel => "Ctrl-Shift-Del", - - Key::Home => "Home", - Key::ShiftHome => "Shift-Home", - Key::AltHome => "Alt-Home", - Key::AltShiftHome => "Alt-Shift-Home", - Key::CtrlHome => "Ctrl-Home", - Key::CtrlShiftHome => "Ctrl-Shift-Home", - Key::CtrlAltHome => "Ctrl-Alt-Home", - - Key::End => "End", - Key::ShiftEnd => "Shift-End", - Key::AltEnd => "Alt-End", - Key::AltShiftEnd => "Alt-Shift-End", - Key::CtrlEnd => "Ctrl-End", - Key::CtrlShiftEnd => "Ctrl-Shift-End", - Key::CtrlAltEnd => "Ctrl-Alt-End", - - Key::Ins => "Ins", - Key::AltIns => "Alt-Ins", - Key::CtrlIns => "Ctrl-Ins", - Key::CtrlAltIns => "Ctrl-Alt-Ins", - - Key::Esc => "Esc", - _ => "Missing key label", - }) - } +impl Key { + /// Returns the function key corresponding to the given number + /// + /// 1 -> F1, etc... + /// + /// # Panics + /// + /// If `n == 0 || n > 12` + pub fn from_f(n: u8) -> Key { + match n { + 1 => Key::F1, + 2 => Key::F2, + 3 => Key::F3, + 4 => Key::F4, + 5 => Key::F5, + 6 => Key::F6, + 7 => Key::F7, + 8 => Key::F8, + 9 => Key::F9, + 10 => Key::F10, + 11 => Key::F11, + 12 => Key::F12, + _ => panic!("unknown function key: F{}", n), } } } /// Represents an event as seen by the application. -/// -#[derive(PartialEq,Eq,Clone,Copy,Hash)] +#[derive(PartialEq,Eq,Clone,Copy,Hash,Debug)] pub enum Event { - /// A text character was entered. + /// Event fired when the window is resized + WindowResize, + + /// A character was entered (includes numbers, punctuation, ...) Char(char), - /// A key was pressed. + /// A character was entered with the Ctrl key pressed + CtrlChar(char), + /// A character was entered with the Alt key pressed + AltChar(char), + + /// A non-character key was pressed Key(Key), + /// A non-character key was pressed with the Shift key pressed + Shift(Key), + /// A non-character key was pressed with the Alt key pressed + Alt(Key), + /// A non-character key was pressed with the Shift and Alt keys pressed + AltShift(Key), + /// A non-character key was pressed with the Ctrl key pressed + Ctrl(Key), + /// A non-character key was pressed with the Ctrl and Shift keys pressed + CtrlShift(Key), + /// A non-character key was pressed with the Ctrl and Alt keys pressed + CtrlAlt(Key), + + + /// An unknown event was received. + Unknown(i32), } -/// Generic trait to convert a value to an event. -pub trait ToEvent { - fn to_event(self) -> Event; -} - -impl ToEvent for char { - fn to_event(self) -> Event { - Event::Char(self) +impl From for Event { + fn from(c: char) -> Event { + Event::Char(c) } } -impl ToEvent for Key { - fn to_event(self) -> Event { - Event::Key(self) - } -} - -impl ToEvent for Event { - fn to_event(self) -> Event { - self +impl From for Event { + fn from(k: Key) -> Event { + Event::Key(k) } } diff --git a/src/lib.rs b/src/lib.rs index 9faaa37..c5dc13e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,7 +65,7 @@ use printer::Printer; use view::View; use view::{Selector, StackView}; -use event::{Callback, Event, EventResult, ToEvent}; +use event::{Callback, Event, EventResult}; /// Identifies a screen in the cursive ROOT. pub type ScreenId = usize; @@ -218,10 +218,10 @@ impl Cursive { /// Adds a global callback. /// /// Will be triggered on the given key press when no view catches it. - pub fn add_global_callback(&mut self, event: E, cb: F) + pub fn add_global_callback>(&mut self, event: E, cb: F) where F: Fn(&mut Cursive) + 'static { - self.global_callbacks.insert(event.to_event(), Rc::new(cb)); + self.global_callbacks.insert(event.into(), Rc::new(cb)); } /// Convenient method to add a layer to the current screen. @@ -310,9 +310,8 @@ impl Cursive { // Wait for next event. // (If set_fps was called, this returns -1 now and then) let event = B::poll_event(); - if event == Event::Key(event::Key::Resize) { + if event == Event::WindowResize { B::clear(); - continue; } // Event dispatch order: diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 3bb0bfc..0aea424 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -226,7 +226,7 @@ impl View for Dialog { match event { Event::Key(Key::Down) | Event::Key(Key::Tab) | - Event::Key(Key::ShiftTab) => { + Event::Shift(Key::Tab) => { // Default to leftmost button when going down. self.focus = Focus::Button(0); EventResult::Consumed(None) @@ -245,7 +245,7 @@ impl View for Dialog { // Up goes back to the content Event::Key(Key::Up) | Event::Key(Key::Tab) | - Event::Key(Key::ShiftTab) => { + Event::Shift(Key::Tab) => { if self.content.take_focus() { self.focus = Focus::Content; EventResult::Consumed(None) diff --git a/src/view/edit_view.rs b/src/view/edit_view.rs index 56122aa..217b4e2 100644 --- a/src/view/edit_view.rs +++ b/src/view/edit_view.rs @@ -155,44 +155,40 @@ impl View for EditView { // Find the byte index of the char at self.cursor self.content.insert(self.cursor, ch); - // TODO: handle wide (CJK) chars self.cursor += ch.len_utf8(); } - Event::Key(key) => { - match key { - Key::Home => self.cursor = 0, - Key::End => self.cursor = self.content.len(), - Key::Left if self.cursor > 0 => { - let len = self.content[..self.cursor] - .graphemes(true) - .last() - .unwrap() - .len(); - self.cursor -= len; - } - Key::Right if self.cursor < self.content.len() => { - let len = self.content[self.cursor..] - .graphemes(true) - .next() - .unwrap() - .len(); - self.cursor += len; - } - Key::Backspace if self.cursor > 0 => { - let len = self.content[..self.cursor] - .graphemes(true) - .last() - .unwrap() - .len(); - self.cursor -= len; - self.content.remove(self.cursor); - } - Key::Del if self.cursor < self.content.len() => { - self.content.remove(self.cursor); - } - _ => return EventResult::Ignored, - } + // TODO: handle ctrl-key? + Event::Key(Key::Home) => self.cursor = 0, + Event::Key(Key::End) => self.cursor = self.content.len(), + Event::Key(Key::Left) if self.cursor > 0 => { + let len = self.content[..self.cursor] + .graphemes(true) + .last() + .unwrap() + .len(); + self.cursor -= len; } + Event::Key(Key::Right) if self.cursor < self.content.len() => { + let len = self.content[self.cursor..] + .graphemes(true) + .next() + .unwrap() + .len(); + self.cursor += len; + } + Event::Key(Key::Backspace) if self.cursor > 0 => { + let len = self.content[..self.cursor] + .graphemes(true) + .last() + .unwrap() + .len(); + self.cursor -= len; + self.content.remove(self.cursor); + } + Event::Key(Key::Del) if self.cursor < self.content.len() => { + self.content.remove(self.cursor); + } + _ => return EventResult::Ignored, } // Keep cursor in [offset, offset+last_length] by changing offset diff --git a/src/view/key_event_view.rs b/src/view/key_event_view.rs index 69e611c..e224919 100644 --- a/src/view/key_event_view.rs +++ b/src/view/key_event_view.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::rc::Rc; use Cursive; -use event::{Callback, Event, EventResult, ToEvent}; +use event::{Callback, Event, EventResult}; use super::{View, ViewWrapper}; /// A simple wrapper view that catches some ignored event from its child. @@ -23,10 +23,10 @@ impl KeyEventView { } /// Registers a callback when the given key is ignored by the child. - pub fn register(mut self, event: E, cb: F) -> Self + pub fn register>(mut self, event: E, cb: F) -> Self where F: Fn(&mut Cursive) + 'static { - self.callbacks.insert(event.to_event(), Rc::new(cb)); + self.callbacks.insert(event.into(), Rc::new(cb)); self } diff --git a/src/view/linear_layout.rs b/src/view/linear_layout.rs index dc2f833..8aede83 100644 --- a/src/view/linear_layout.rs +++ b/src/view/linear_layout.rs @@ -207,7 +207,7 @@ impl View for LinearLayout { self.focus -= 1; EventResult::Consumed(None) } - Event::Key(Key::ShiftTab) if self.focus + 1 < + Event::Shift(Key::Tab) if self.focus + 1 < self.children.len() => { self.focus += 1; EventResult::Consumed(None)