11
11
#include < rime/engine.h>
12
12
#include < rime/key_event.h>
13
13
#include < rime/key_table.h>
14
+ #include < rime/schema.h>
14
15
#include < rime/gear/navigator.h>
15
16
#include < rime/gear/translator_commons.h>
16
17
17
18
namespace rime {
18
19
20
+ static Navigator::ActionDef navigation_actions[] = {
21
+ { " rewind" , &Navigator::Rewind },
22
+ { " left_by_char" , &Navigator::LeftByChar },
23
+ { " right_by_char" , &Navigator::RightByChar },
24
+ { " left_by_syllable" , &Navigator::LeftBySyllable },
25
+ { " right_by_syllable" , &Navigator::RightBySyllable },
26
+ { " home" , &Navigator::Home },
27
+ { " end" , &Navigator::End },
28
+ Navigator::kActionNoop
29
+ };
30
+
31
+ Navigator::Navigator (const Ticket& ticket)
32
+ : Processor(ticket), KeyBindingProcessor<Navigator>(navigation_actions) {
33
+ // Default key binding.
34
+ Bind ({XK_Left, 0 }, &Navigator::Rewind);
35
+ Bind ({XK_Left, kControlMask }, &Navigator::LeftBySyllable);
36
+ Bind ({XK_KP_Left, 0 }, &Navigator::LeftByChar);
37
+ Bind ({XK_Right, 0 }, &Navigator::RightByChar);
38
+ Bind ({XK_Right, kControlMask }, &Navigator::RightBySyllable);
39
+ Bind ({XK_KP_Right, 0 }, &Navigator::RightByChar);
40
+ Bind ({XK_Home, 0 }, &Navigator::Home);
41
+ Bind ({XK_KP_Home, 0 }, &Navigator::Home);
42
+ Bind ({XK_End, 0 }, &Navigator::End);
43
+ Bind ({XK_KP_End, 0 }, &Navigator::End);
44
+
45
+ Config* config = engine_->schema ()->config ();
46
+ KeyBindingProcessor::LoadConfig (config, " navigator" );
47
+ }
48
+
19
49
ProcessResult Navigator::ProcessKeyEvent (const KeyEvent& key_event) {
20
50
if (key_event.release ())
21
51
return kNoop ;
22
52
Context* ctx = engine_->context ();
23
53
if (!ctx->IsComposing ())
24
54
return kNoop ;
25
- int ch = key_event.keycode ();
26
- if (ch == XK_Left || ch == XK_KP_Left) {
27
- BeginMove (ctx);
28
- if (key_event.ctrl () || key_event.shift ()) {
29
- size_t confirmed_pos = ctx->composition ().GetConfirmedPosition ();
30
- JumpLeft (ctx, confirmed_pos) || End (ctx);
31
- }
32
- else {
33
- // take a jump leftwards when there are multiple spans,
34
- // but not from the middle of a span.
35
- (spans_.Count () > 1 &&
36
- spans_.HasVertex (ctx->caret_pos ())
37
- ? JumpLeft (ctx) : Left (ctx)) || End (ctx);
38
- }
39
- return kAccepted ;
40
- }
41
- if (ch == XK_Right || ch == XK_KP_Right) {
42
- BeginMove (ctx);
43
- if (key_event.ctrl () || key_event.shift ()) {
44
- size_t confirmed_pos = ctx->composition ().GetConfirmedPosition ();
45
- JumpRight (ctx, confirmed_pos) || End (ctx);
46
- }
47
- else {
48
- Right (ctx) || Home (ctx);
49
- }
50
- return kAccepted ;
51
- }
52
- if (ch == XK_Home || ch == XK_KP_Home) {
53
- BeginMove (ctx);
54
- Home (ctx);
55
- return kAccepted ;
56
- }
57
- if (ch == XK_End || ch == XK_KP_End) {
58
- BeginMove (ctx);
59
- End (ctx);
60
- return kAccepted ;
61
- }
62
- // not handled
63
- return kNoop ;
55
+ return KeyBindingProcessor::ProcessKeyEvent (key_event, ctx);
56
+ }
57
+
58
+ void Navigator::LeftBySyllable (Context* ctx) {
59
+ BeginMove (ctx);
60
+ size_t confirmed_pos = ctx->composition ().GetConfirmedPosition ();
61
+ JumpLeft (ctx, confirmed_pos) || GoToEnd (ctx);
62
+ }
63
+
64
+ void Navigator::LeftByChar (Context* ctx) {
65
+ BeginMove (ctx);
66
+ MoveLeft (ctx) || GoToEnd (ctx);
67
+ }
68
+
69
+ void Navigator::Rewind (Context* ctx) {
70
+ BeginMove (ctx);
71
+ // take a jump leftwards when there are multiple spans,
72
+ // but not from the middle of a span.
73
+ (
74
+ spans_.Count () > 1 && spans_.HasVertex (ctx->caret_pos ())
75
+ ? JumpLeft (ctx) : MoveLeft (ctx)
76
+ ) || GoToEnd (ctx);
77
+ }
78
+
79
+ void Navigator::RightBySyllable (Context* ctx) {
80
+ BeginMove (ctx);
81
+ size_t confirmed_pos = ctx->composition ().GetConfirmedPosition ();
82
+ JumpRight (ctx, confirmed_pos) || GoToEnd (ctx);
83
+ }
84
+
85
+ void Navigator::RightByChar (Context* ctx) {
86
+ BeginMove (ctx);
87
+ MoveRight (ctx) || GoHome (ctx);
88
+ }
89
+
90
+ void Navigator::Home (Context* ctx) {
91
+ BeginMove (ctx);
92
+ GoHome (ctx);
93
+ }
94
+
95
+ void Navigator::End (Context* ctx) {
96
+ BeginMove (ctx);
97
+ GoToEnd (ctx);
64
98
}
65
99
66
100
void Navigator::BeginMove (Context* ctx) {
67
101
ctx->ConfirmPreviousSelection ();
68
102
// update spans
69
- size_t caret_pos = ctx->caret_pos ();
70
- if (input_ != ctx->input () || caret_pos > spans_.end ()) {
103
+ if (input_ != ctx->input () || ctx->caret_pos () > spans_.end ()) {
71
104
input_ = ctx->input ();
72
105
spans_.Clear ();
73
106
for (const auto &seg : ctx->composition ()) {
@@ -109,7 +142,7 @@ bool Navigator::JumpRight(Context* ctx, size_t start_pos) {
109
142
return false ;
110
143
}
111
144
112
- bool Navigator::Left (Context* ctx) {
145
+ bool Navigator::MoveLeft (Context* ctx) {
113
146
DLOG (INFO) << " navigate left." ;
114
147
size_t caret_pos = ctx->caret_pos ();
115
148
if (caret_pos == 0 )
@@ -118,7 +151,7 @@ bool Navigator::Left(Context* ctx) {
118
151
return true ;
119
152
}
120
153
121
- bool Navigator::Right (Context* ctx) {
154
+ bool Navigator::MoveRight (Context* ctx) {
122
155
DLOG (INFO) << " navigate right." ;
123
156
size_t caret_pos = ctx->caret_pos ();
124
157
if (caret_pos >= ctx->input ().length ())
@@ -127,7 +160,7 @@ bool Navigator::Right(Context* ctx) {
127
160
return true ;
128
161
}
129
162
130
- bool Navigator::Home (Context* ctx) {
163
+ bool Navigator::GoHome (Context* ctx) {
131
164
DLOG (INFO) << " navigate home." ;
132
165
size_t caret_pos = ctx->caret_pos ();
133
166
const Composition& comp = ctx->composition ();
@@ -151,7 +184,7 @@ bool Navigator::Home(Context* ctx) {
151
184
return false ;
152
185
}
153
186
154
- bool Navigator::End (Context* ctx) {
187
+ bool Navigator::GoToEnd (Context* ctx) {
155
188
DLOG (INFO) << " navigate end." ;
156
189
size_t end_pos = ctx->input ().length ();
157
190
if (ctx->caret_pos () != end_pos) {
0 commit comments