зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 00:15:50 +03:00
convert backends' key/mouse events into Key
Этот коммит содержится в:
@@ -4,7 +4,7 @@ use crate::crossterm::event::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
impl From<Event> for Input {
|
impl From<Event> for Input {
|
||||||
/// Convert [`crossterm::event::Event`] to [`Input`].
|
/// Convert [`crossterm::event::Event`] into [`Input`].
|
||||||
fn from(event: Event) -> Self {
|
fn from(event: Event) -> Self {
|
||||||
match event {
|
match event {
|
||||||
Event::Key(key) => Self::from(key),
|
Event::Key(key) => Self::from(key),
|
||||||
@@ -14,18 +14,10 @@ impl From<Event> for Input {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<KeyEvent> for Input {
|
impl From<KeyCode> for Key {
|
||||||
/// Convert [`crossterm::event::KeyEvent`] to [`Input`].
|
/// Convert [`crossterm::event::KeyCode`] into [`Key`].
|
||||||
fn from(key: KeyEvent) -> Self {
|
fn from(code: KeyCode) -> Self {
|
||||||
if key.kind == KeyEventKind::Release {
|
match code {
|
||||||
// On Windows or when `crossterm::event::PushKeyboardEnhancementFlags` is set,
|
|
||||||
// key release event can be reported. Ignore it. (#14)
|
|
||||||
return Self::default();
|
|
||||||
}
|
|
||||||
|
|
||||||
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
|
|
||||||
let alt = key.modifiers.contains(KeyModifiers::ALT);
|
|
||||||
let key = match key.code {
|
|
||||||
KeyCode::Char(c) => Key::Char(c),
|
KeyCode::Char(c) => Key::Char(c),
|
||||||
KeyCode::Backspace => Key::Backspace,
|
KeyCode::Backspace => Key::Backspace,
|
||||||
KeyCode::Enter => Key::Enter,
|
KeyCode::Enter => Key::Enter,
|
||||||
@@ -42,20 +34,42 @@ impl From<KeyEvent> for Input {
|
|||||||
KeyCode::Esc => Key::Esc,
|
KeyCode::Esc => Key::Esc,
|
||||||
KeyCode::F(x) => Key::F(x),
|
KeyCode::F(x) => Key::F(x),
|
||||||
_ => Key::Null,
|
_ => Key::Null,
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<KeyEvent> for Input {
|
||||||
|
/// Convert [`crossterm::event::KeyEvent`] into [`Input`].
|
||||||
|
fn from(key: KeyEvent) -> Self {
|
||||||
|
if key.kind == KeyEventKind::Release {
|
||||||
|
// On Windows or when `crossterm::event::PushKeyboardEnhancementFlags` is set,
|
||||||
|
// key release event can be reported. Ignore it. (#14)
|
||||||
|
return Self::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
|
||||||
|
let alt = key.modifiers.contains(KeyModifiers::ALT);
|
||||||
|
let key = Key::from(key.code);
|
||||||
|
|
||||||
Self { key, ctrl, alt }
|
Self { key, ctrl, alt }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<MouseEvent> for Input {
|
impl From<MouseEventKind> for Key {
|
||||||
/// Convert [`crossterm::event::MouseEvent`] to [`Input`].
|
/// Convert [`crossterm::event::MouseEventKind`] into [`Key`].
|
||||||
fn from(mouse: MouseEvent) -> Self {
|
fn from(kind: MouseEventKind) -> Self {
|
||||||
let key = match mouse.kind {
|
match kind {
|
||||||
MouseEventKind::ScrollDown => Key::MouseScrollDown,
|
MouseEventKind::ScrollDown => Key::MouseScrollDown,
|
||||||
MouseEventKind::ScrollUp => Key::MouseScrollUp,
|
MouseEventKind::ScrollUp => Key::MouseScrollUp,
|
||||||
_ => Key::Null,
|
_ => Key::Null,
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<MouseEvent> for Input {
|
||||||
|
/// Convert [`crossterm::event::MouseEvent`] into [`Input`].
|
||||||
|
fn from(mouse: MouseEvent) -> Self {
|
||||||
|
let key = Key::from(mouse.kind);
|
||||||
let ctrl = mouse.modifiers.contains(KeyModifiers::CONTROL);
|
let ctrl = mouse.modifiers.contains(KeyModifiers::CONTROL);
|
||||||
let alt = mouse.modifiers.contains(KeyModifiers::ALT);
|
let alt = mouse.modifiers.contains(KeyModifiers::ALT);
|
||||||
Self { key, ctrl, alt }
|
Self { key, ctrl, alt }
|
||||||
@@ -172,9 +186,9 @@ mod tests {
|
|||||||
// Regression for https://github.com/rhysd/tui-textarea/issues/14
|
// Regression for https://github.com/rhysd/tui-textarea/issues/14
|
||||||
#[test]
|
#[test]
|
||||||
fn ignore_key_release_event() {
|
fn ignore_key_release_event() {
|
||||||
let mut k = key_event(KeyCode::Char('a'), KeyModifiers::empty());
|
let mut from = key_event(KeyCode::Char('a'), KeyModifiers::empty());
|
||||||
k.kind = KeyEventKind::Release;
|
from.kind = KeyEventKind::Release;
|
||||||
let want = input(Key::Null, false, false);
|
let to = input(Key::Null, false, false);
|
||||||
assert_eq!(Input::from(k.clone()), want, "{:?} -> {:?}", k, want);
|
assert_eq!(Input::from(from), to, "{:?} -> {:?}", from, to);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use super::{Input, Key};
|
|||||||
use termion::event::{Event, Key as KeyEvent, MouseButton, MouseEvent};
|
use termion::event::{Event, Key as KeyEvent, MouseButton, MouseEvent};
|
||||||
|
|
||||||
impl From<Event> for Input {
|
impl From<Event> for Input {
|
||||||
/// Convert [`termion::event::Event`] to [`Input`].
|
/// Convert [`termion::event::Event`] into [`Input`].
|
||||||
fn from(event: Event) -> Self {
|
fn from(event: Event) -> Self {
|
||||||
match event {
|
match event {
|
||||||
Event::Key(key) => Self::from(key),
|
Event::Key(key) => Self::from(key),
|
||||||
@@ -13,7 +13,7 @@ impl From<Event> for Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<KeyEvent> for Input {
|
impl From<KeyEvent> for Input {
|
||||||
/// Convert [`termion::event::Key`] to [`Input`].
|
/// Convert [`termion::event::Key`] into [`Input`].
|
||||||
fn from(key: KeyEvent) -> Self {
|
fn from(key: KeyEvent) -> Self {
|
||||||
let mut ctrl = false;
|
let mut ctrl = false;
|
||||||
let mut alt = false;
|
let mut alt = false;
|
||||||
@@ -48,13 +48,24 @@ impl From<KeyEvent> for Input {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<MouseButton> for Key {
|
||||||
|
/// Convert [`termion::event::MouseButton`] into [`Key`].
|
||||||
|
fn from(button: MouseButton) -> Self {
|
||||||
|
match button {
|
||||||
|
MouseButton::WheelUp => Key::MouseScrollUp,
|
||||||
|
MouseButton::WheelDown => Key::MouseScrollDown,
|
||||||
|
_ => Key::Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<MouseEvent> for Input {
|
impl From<MouseEvent> for Input {
|
||||||
/// Convert [`termion::event::MouseEvent`] to [`Input`].
|
/// Convert [`termion::event::MouseEvent`] into [`Input`].
|
||||||
fn from(mouse: MouseEvent) -> Self {
|
fn from(mouse: MouseEvent) -> Self {
|
||||||
let key = match mouse {
|
let key = if let MouseEvent::Press(button, ..) = mouse {
|
||||||
MouseEvent::Press(MouseButton::WheelUp, ..) => Key::MouseScrollUp,
|
Key::from(button)
|
||||||
MouseEvent::Press(MouseButton::WheelDown, ..) => Key::MouseScrollDown,
|
} else {
|
||||||
_ => return Self::default(),
|
Key::Null
|
||||||
};
|
};
|
||||||
Self {
|
Self {
|
||||||
key,
|
key,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use termwiz::input::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
impl From<InputEvent> for Input {
|
impl From<InputEvent> for Input {
|
||||||
/// Convert [`termwiz::input::InputEvent`] to [`Input`].
|
/// Convert [`termwiz::input::InputEvent`] into [`Input`].
|
||||||
fn from(input: InputEvent) -> Self {
|
fn from(input: InputEvent) -> Self {
|
||||||
match input {
|
match input {
|
||||||
InputEvent::Key(key) => Self::from(key),
|
InputEvent::Key(key) => Self::from(key),
|
||||||
@@ -15,11 +15,10 @@ impl From<InputEvent> for Input {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<KeyEvent> for Input {
|
impl From<KeyCode> for Key {
|
||||||
/// Convert [`termwiz::input::KeyEvent`] to [`Input`].
|
/// Convert [`termwiz::input::KeyCode`] into [`Key`].
|
||||||
fn from(key: KeyEvent) -> Self {
|
fn from(key: KeyCode) -> Self {
|
||||||
let KeyEvent { key, modifiers } = key;
|
match key {
|
||||||
let key = match key {
|
|
||||||
KeyCode::Char(c) => Key::Char(c),
|
KeyCode::Char(c) => Key::Char(c),
|
||||||
KeyCode::Backspace => Key::Backspace,
|
KeyCode::Backspace => Key::Backspace,
|
||||||
KeyCode::Tab => Key::Tab,
|
KeyCode::Tab => Key::Tab,
|
||||||
@@ -36,7 +35,15 @@ impl From<KeyEvent> for Input {
|
|||||||
KeyCode::Delete => Key::Delete,
|
KeyCode::Delete => Key::Delete,
|
||||||
KeyCode::Function(x) => Key::F(x),
|
KeyCode::Function(x) => Key::F(x),
|
||||||
_ => Key::Null,
|
_ => Key::Null,
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<KeyEvent> for Input {
|
||||||
|
/// Convert [`termwiz::input::KeyEvent`] into [`Input`].
|
||||||
|
fn from(key: KeyEvent) -> Self {
|
||||||
|
let KeyEvent { key, modifiers } = key;
|
||||||
|
let key = Key::from(key);
|
||||||
let ctrl = modifiers.contains(Modifiers::CTRL);
|
let ctrl = modifiers.contains(Modifiers::CTRL);
|
||||||
let alt = modifiers.contains(Modifiers::ALT);
|
let alt = modifiers.contains(Modifiers::ALT);
|
||||||
|
|
||||||
@@ -45,7 +52,7 @@ impl From<KeyEvent> for Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<MouseButtons> for Key {
|
impl From<MouseButtons> for Key {
|
||||||
/// Convert [`termwiz::input::MouseButtons`] to [`Key`].
|
/// Convert [`termwiz::input::MouseButtons`] into [`Key`].
|
||||||
fn from(buttons: MouseButtons) -> Self {
|
fn from(buttons: MouseButtons) -> Self {
|
||||||
if buttons.contains(MouseButtons::VERT_WHEEL) {
|
if buttons.contains(MouseButtons::VERT_WHEEL) {
|
||||||
if buttons.contains(MouseButtons::WHEEL_POSITIVE) {
|
if buttons.contains(MouseButtons::WHEEL_POSITIVE) {
|
||||||
@@ -60,7 +67,7 @@ impl From<MouseButtons> for Key {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<MouseEvent> for Input {
|
impl From<MouseEvent> for Input {
|
||||||
/// Convert [`termwiz::input::MouseEvent`] to [`Input`].
|
/// Convert [`termwiz::input::MouseEvent`] into [`Input`].
|
||||||
fn from(mouse: MouseEvent) -> Self {
|
fn from(mouse: MouseEvent) -> Self {
|
||||||
let MouseEvent {
|
let MouseEvent {
|
||||||
mouse_buttons,
|
mouse_buttons,
|
||||||
@@ -76,13 +83,14 @@ impl From<MouseEvent> for Input {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<PixelMouseEvent> for Input {
|
impl From<PixelMouseEvent> for Input {
|
||||||
/// Convert [`termwiz::input::PixelMouseEvent`] to [`Input`].
|
/// Convert [`termwiz::input::PixelMouseEvent`] into [`Input`].
|
||||||
fn from(mouse: PixelMouseEvent) -> Self {
|
fn from(mouse: PixelMouseEvent) -> Self {
|
||||||
let PixelMouseEvent {
|
let PixelMouseEvent {
|
||||||
mouse_buttons,
|
mouse_buttons,
|
||||||
modifiers,
|
modifiers,
|
||||||
..
|
..
|
||||||
} = mouse;
|
} = mouse;
|
||||||
|
|
||||||
let key = Key::from(mouse_buttons);
|
let key = Key::from(mouse_buttons);
|
||||||
let ctrl = modifiers.contains(Modifiers::CTRL);
|
let ctrl = modifiers.contains(Modifiers::CTRL);
|
||||||
let alt = modifiers.contains(Modifiers::ALT);
|
let alt = modifiers.contains(Modifiers::ALT);
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user