@@ -41,6 +41,9 @@ class QuickPhraseState : public InputContextProperty {
41
41
InputBuffer buffer_;
42
42
QuickPhrase *q_;
43
43
44
+ std::string originalBuffer_;
45
+ QuickPhraseRestoreCallback restoreCallback_;
46
+
44
47
bool typed_ = false ;
45
48
std::string text_;
46
49
std::string prefix_;
@@ -57,6 +60,8 @@ class QuickPhraseState : public InputContextProperty {
57
60
prefix_.clear ();
58
61
str_.clear ();
59
62
alt_.clear ();
63
+ originalBuffer_.clear ();
64
+ restoreCallback_ = nullptr ;
60
65
key_ = Key (FcitxKey_None);
61
66
ic->inputPanel ().reset ();
62
67
ic->updatePreedit ();
@@ -222,18 +227,29 @@ QuickPhrase::QuickPhrase(Instance *instance)
222
227
return ;
223
228
}
224
229
if (keyEvent.key ().check (FcitxKey_BackSpace)) {
230
+ keyEvent.accept ();
225
231
if (state->buffer_ .empty ()) {
226
232
state->reset (inputContext);
227
233
} else {
228
234
if (state->buffer_ .backspace ()) {
235
+ if (state->restoreCallback_ &&
236
+ state->buffer_ .cursor () == state->buffer_ .size () &&
237
+ state->buffer_ .userInput () ==
238
+ state->originalBuffer_ ) {
239
+ auto callback = std::move (state->restoreCallback_ );
240
+ auto original = std::move (state->originalBuffer_ );
241
+ state->reset (inputContext);
242
+ callback (inputContext, original);
243
+ return ;
244
+ }
245
+
229
246
if (state->buffer_ .empty ()) {
230
247
state->reset (inputContext);
231
248
} else {
232
249
updateUI (inputContext);
233
250
}
234
251
}
235
252
}
236
- keyEvent.accept ();
237
253
return ;
238
254
}
239
255
if (keyEvent.key ().check (FcitxKey_Delete)) {
@@ -256,28 +272,32 @@ QuickPhrase::QuickPhrase(Instance *instance)
256
272
if (key.check (FcitxKey_Home) || key.check (FcitxKey_KP_Home)) {
257
273
state->buffer_ .setCursor (0 );
258
274
keyEvent.accept ();
259
- return updateUI (inputContext);
275
+ updateUI (inputContext);
276
+ return ;
260
277
}
261
278
if (key.check (FcitxKey_End) || key.check (FcitxKey_KP_End)) {
262
279
state->buffer_ .setCursor (state->buffer_ .size ());
263
280
keyEvent.accept ();
264
- return updateUI (inputContext);
281
+ updateUI (inputContext);
282
+ return ;
265
283
}
266
284
if (key.check (FcitxKey_Left) || key.check (FcitxKey_KP_Left)) {
267
285
auto cursor = state->buffer_ .cursor ();
268
286
if (cursor > 0 ) {
269
287
state->buffer_ .setCursor (cursor - 1 );
270
288
}
271
289
keyEvent.accept ();
272
- return updateUI (inputContext);
290
+ updateUI (inputContext);
291
+ return ;
273
292
}
274
293
if (key.check (FcitxKey_Right) || key.check (FcitxKey_KP_Right)) {
275
294
auto cursor = state->buffer_ .cursor ();
276
295
if (cursor < state->buffer_ .size ()) {
277
296
state->buffer_ .setCursor (cursor + 1 );
278
297
}
279
298
keyEvent.accept ();
280
- return updateUI (inputContext);
299
+ updateUI (inputContext);
300
+ return ;
281
301
}
282
302
}
283
303
if (!state->typed_ && !state->str_ .empty () &&
@@ -294,7 +314,8 @@ QuickPhrase::QuickPhrase(Instance *instance)
294
314
295
315
// compose is invalid, ignore it.
296
316
if (!compose) {
297
- return event.accept ();
317
+ event.accept ();
318
+ return ;
298
319
}
299
320
300
321
if (!compose->empty ()) {
@@ -539,6 +560,20 @@ void QuickPhrase::setBuffer(InputContext *ic, const std::string &text) {
539
560
updateUI (ic);
540
561
}
541
562
563
+ void QuickPhrase::setBufferWithRestoreCallback (
564
+ InputContext *ic, const std::string &text, const std::string &original,
565
+ QuickPhraseRestoreCallback callback) {
566
+ auto *state = ic->propertyFor (&factory_);
567
+ if (!state->enabled_ ) {
568
+ return ;
569
+ }
570
+ state->buffer_ .clear ();
571
+ state->buffer_ .type (text);
572
+ state->originalBuffer_ = original;
573
+ state->restoreCallback_ = std::move (callback);
574
+ updateUI (ic);
575
+ }
576
+
542
577
class QuickPhraseModuleFactory : public AddonFactory {
543
578
AddonInstance *create (AddonManager *manager) override {
544
579
return new QuickPhrase (manager->instance ());
0 commit comments