forked from linebender/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_input.rs
163 lines (152 loc) · 5.96 KB
/
text_input.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2019 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Map input to `EditAction`s
use super::Movement;
use crate::{HotKey, KbKey, KeyEvent, Modifiers, SysMods};
// This following enumerations are heavily inspired by xi-editors enumerations found at
// https://github.com/xi-editor/xi-editor/blob/e2589974fc4050beb33af82481aa71b258358e48/rust/core-lib/src/edit_types.rs
// This is done with the goal of eventually being able to easily switch
// to a xi-based implementation of our Events.
/// An enum that represents actions in a text buffer.
#[derive(Debug, PartialEq, Clone)]
pub enum EditAction {
Move(Movement),
ModifySelection(Movement),
SelectAll,
Click(MouseAction),
Drag(MouseAction),
Delete,
Backspace,
JumpDelete(Movement),
JumpBackspace(Movement),
Insert(String),
Paste(String),
}
/// Extra information related to mouse actions
#[derive(PartialEq, Debug, Clone)]
pub struct MouseAction {
pub row: usize,
pub column: usize,
pub mods: Modifiers,
}
pub trait TextInput {
/// Handle a key event and return an edit action to be executed
/// for the key event
fn handle_event(&self, event: &KeyEvent) -> Option<EditAction>;
}
/// Handles key events and returns actions that are applicable to
/// single line textboxes
#[derive(Default)]
pub struct BasicTextInput {}
impl BasicTextInput {
pub fn new() -> Self {
Self {}
}
}
impl TextInput for BasicTextInput {
fn handle_event(&self, event: &KeyEvent) -> Option<EditAction> {
let action = match event {
// Select left word (Shift+Ctrl+ArrowLeft || Shift+Cmd+ArrowLeft)
k_e if (HotKey::new(SysMods::CmdShift, KbKey::ArrowLeft)).matches(k_e) => {
EditAction::ModifySelection(Movement::LeftWord)
}
// Select right word (Shift+Ctrl+ArrowRight || Shift+Cmd+ArrowRight)
k_e if (HotKey::new(SysMods::CmdShift, KbKey::ArrowRight)).matches(k_e) => {
EditAction::ModifySelection(Movement::RightWord)
}
// Select to home (Shift+Home)
k_e if (HotKey::new(SysMods::Shift, KbKey::Home)).matches(k_e) => {
EditAction::ModifySelection(Movement::LeftOfLine)
}
// Select to end (Shift+End)
k_e if (HotKey::new(SysMods::Shift, KbKey::End)).matches(k_e) => {
EditAction::ModifySelection(Movement::RightOfLine)
}
// Select left (Shift+ArrowLeft)
k_e if (HotKey::new(SysMods::Shift, KbKey::ArrowLeft)).matches(k_e) => {
EditAction::ModifySelection(Movement::Left)
}
// Select right (Shift+ArrowRight)
k_e if (HotKey::new(SysMods::Shift, KbKey::ArrowRight)).matches(k_e) => {
EditAction::ModifySelection(Movement::Right)
}
// Select all (Ctrl+A || Cmd+A)
k_e if (HotKey::new(SysMods::Cmd, "a")).matches(k_e) => EditAction::SelectAll,
// Left word (Ctrl+ArrowLeft || Cmd+ArrowLeft)
k_e if (HotKey::new(SysMods::Cmd, KbKey::ArrowLeft)).matches(k_e) => {
EditAction::Move(Movement::LeftWord)
}
// Right word (Ctrl+ArrowRight || Cmd+ArrowRight)
k_e if (HotKey::new(SysMods::Cmd, KbKey::ArrowRight)).matches(k_e) => {
EditAction::Move(Movement::RightWord)
}
// Move left (ArrowLeft)
k_e if (HotKey::new(None, KbKey::ArrowLeft)).matches(k_e) => {
EditAction::Move(Movement::Left)
}
// Move right (ArrowRight)
k_e if (HotKey::new(None, KbKey::ArrowRight)).matches(k_e) => {
EditAction::Move(Movement::Right)
}
// Delete left word
k_e if (HotKey::new(SysMods::Cmd, KbKey::Backspace)).matches(k_e) => {
EditAction::JumpBackspace(Movement::LeftWord)
}
// Delete right word
k_e if (HotKey::new(SysMods::Cmd, KbKey::Delete)).matches(k_e) => {
EditAction::JumpDelete(Movement::RightWord)
}
// Backspace
k_e if (HotKey::new(None, KbKey::Backspace)).matches(k_e) => EditAction::Backspace,
// Delete
k_e if (HotKey::new(None, KbKey::Delete)).matches(k_e) => EditAction::Delete,
// Home
k_e if (HotKey::new(None, KbKey::Home)).matches(k_e) => {
EditAction::Move(Movement::LeftOfLine)
}
// End
k_e if (HotKey::new(None, KbKey::End)).matches(k_e) => {
EditAction::Move(Movement::RightOfLine)
}
// Actual typing
k_e if key_event_is_printable(k_e) => {
if let KbKey::Character(chars) = &k_e.key {
EditAction::Insert(chars.to_owned())
} else {
return None;
}
}
_ => return None,
};
Some(action)
}
}
/// Determine whether a keyboard event contains insertable text.
fn key_event_is_printable(event: &KeyEvent) -> bool {
if let KbKey::Character(_) = &event.key {
if event.mods.ctrl() || event.mods.meta() {
return false;
}
// On mac, Alt functions more like AltGr.
#[cfg(not(target_os = "macos"))]
{
if event.mods.alt() {
return false;
}
}
true
} else {
false
}
}