4
4
//
5
5
// 2011-10-23 GONG Chen <chen.sst@gmail.com>
6
6
//
7
- #include < cctype>
8
7
#include < rime/common.h>
9
- #include < rime/composition.h>
10
8
#include < rime/config.h>
11
9
#include < rime/context.h>
12
10
#include < rime/engine.h>
13
11
#include < rime/schema.h>
14
12
#include < rime/key_table.h>
15
13
#include < rime/gear/editor.h>
14
+ #include < rime/gear/key_binding_processor.h>
16
15
#include < rime/gear/translator_commons.h>
17
16
18
17
namespace rime {
19
18
20
- static struct EditorActionDef {
21
- const char * name;
22
- Editor::HandlerPtr action;
23
- } editor_action_definitions[] = {
19
+ static Editor::ActionDef editor_action_definitions[] = {
24
20
{ " confirm" , &Editor::Confirm },
25
21
{ " toggle_selection" , &Editor::ToggleSelection },
26
22
{ " commit_comment" , &Editor::CommitComment },
@@ -33,7 +29,7 @@ static struct EditorActionDef {
33
29
{ " delete_candidate" , &Editor::DeleteCandidate },
34
30
{ " delete" , &Editor::DeleteChar },
35
31
{ " cancel" , &Editor::CancelComposition },
36
- { " noop " , nullptr }
32
+ Editor:: kActionNoop
37
33
};
38
34
39
35
static struct EditorCharHandlerDef {
@@ -45,7 +41,8 @@ static struct EditorCharHandlerDef {
45
41
{ " noop" , nullptr }
46
42
};
47
43
48
- Editor::Editor (const Ticket& ticket, bool auto_commit) : Processor(ticket) {
44
+ Editor::Editor (const Ticket& ticket, bool auto_commit)
45
+ : Processor(ticket), KeyBindingProcessor(editor_action_definitions) {
49
46
engine_->context ()->set_option (" _auto_commit" , auto_commit);
50
47
}
51
48
@@ -55,27 +52,9 @@ ProcessResult Editor::ProcessKeyEvent(const KeyEvent& key_event) {
55
52
int ch = key_event.keycode ();
56
53
Context* ctx = engine_->context ();
57
54
if (ctx->IsComposing ()) {
58
- if (Accept (key_event)) {
59
- return kAccepted ;
60
- }
61
- if (key_event.ctrl () || key_event.alt ()) {
62
- return kNoop ;
63
- }
64
- if (key_event.shift ()) {
65
- KeyEvent shift_as_ctrl{
66
- key_event.keycode (),
67
- (key_event.modifier () & ~kShiftMask ) | kControlMask
68
- };
69
- if (Accept (shift_as_ctrl)) {
70
- return kAccepted ;
71
- }
72
- KeyEvent remove_shift{
73
- key_event.keycode (),
74
- key_event.modifier () & ~kShiftMask
75
- };
76
- if (Accept (remove_shift)) {
77
- return kAccepted ;
78
- }
55
+ auto result = KeyBindingProcessor::ProcessKeyEvent (key_event, ctx);
56
+ if (result != kNoop ) {
57
+ return result;
79
58
}
80
59
}
81
60
if (char_handler_ &&
@@ -89,53 +68,12 @@ ProcessResult Editor::ProcessKeyEvent(const KeyEvent& key_event) {
89
68
return kNoop ;
90
69
}
91
70
92
- bool Editor::Accept (const KeyEvent& key_event) {
93
- auto binding = key_bindings_.find (key_event);
94
- if (binding != key_bindings_.end ()) {
95
- auto action = binding->second ;
96
- RIME_THIS_CALL (action)(engine_->context ());
97
- DLOG (INFO) << " editor action key accepted: " << key_event.repr ();
98
- return true ;
99
- }
100
- return false ;
101
- }
102
-
103
- void Editor::Bind (KeyEvent key_event, HandlerPtr action) {
104
- if (action) {
105
- key_bindings_[key_event] = action;
106
- }
107
- else {
108
- key_bindings_.erase (key_event);
109
- }
110
- }
111
-
112
71
void Editor::LoadConfig () {
113
72
if (!engine_) {
114
73
return ;
115
74
}
116
75
Config* config = engine_->schema ()->config ();
117
- if (auto bindings = config->GetMap (" editor/bindings" )) {
118
- for (auto it = bindings->begin (); it != bindings->end (); ++it) {
119
- auto value = As<ConfigValue>(it->second );
120
- if (!value) {
121
- continue ;
122
- }
123
- auto * p = editor_action_definitions;
124
- while (p->action && p->name != value->str ()) {
125
- ++p;
126
- }
127
- if (!p->action && p->name != value->str ()) {
128
- LOG (WARNING) << " invalid editor action: " << value->str ();
129
- continue ;
130
- }
131
- KeyEvent ke;
132
- if (!ke.Parse (it->first )) {
133
- LOG (WARNING) << " invalid edit key: " << it->first ;
134
- continue ;
135
- }
136
- Bind (ke, p->action );
137
- }
138
- }
76
+ KeyBindingProcessor::LoadConfig (config, " editor" );
139
77
if (auto value = config->GetValue (" editor/char_handler" )) {
140
78
auto * p = editor_char_handler_definitions;
141
79
while (p->action && p->name != value->str ()) {
0 commit comments