Skip to content

Commit f299e60

Browse files
committed
Change the notification mechanism to allow list mode
Only allow notification if: 1. action triggered by user, either via action, or shortcut, or setSubConfig. 2. error on start up.
1 parent 9155bf3 commit f299e60

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/rimeaction.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ void ToggleAction::activate(InputContext *ic) {
5353
if (!state) {
5454
return;
5555
}
56-
// Do not send notification since user is explicitly select it.
57-
engine_->blockNotificationFor(30000);
5856
auto session = state->session();
5957
Bool oldValue = api->get_option(session, option_.c_str());
6058
api->set_option(session, option_.c_str(), !oldValue);

src/rimeengine.cpp

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fcitx-config/iniparser.h>
1717
#include <fcitx-config/rawconfig.h>
1818
#include <fcitx-utils/event.h>
19+
#include <fcitx-utils/eventloopinterface.h>
1920
#include <fcitx-utils/fs.h>
2021
#include <fcitx-utils/i18n.h>
2122
#include <fcitx-utils/log.h>
@@ -53,6 +54,9 @@ namespace fcitx::rime {
5354

5455
namespace {
5556

57+
// Allow notification for 60secs.
58+
constexpr uint64_t NotificationTimeout = 60000000;
59+
5660
std::unordered_map<std::string, std::unordered_map<std::string, bool>>
5761
parseAppOptions(rime_api_t *api, RimeConfig *config) {
5862
std::unordered_map<std::string, std::unordered_map<std::string, bool>>
@@ -210,7 +214,7 @@ RimeEngine::RimeEngine(Instance *instance)
210214
syncAction_.setShortText(_("Synchronize"));
211215

212216
syncAction_.connect<SimpleAction::Activated>([this](InputContext *ic) {
213-
sync();
217+
sync(/*userTriggered=*/true);
214218
auto *state = this->state(ic);
215219
if (state && ic->hasFocus()) {
216220
state->updateUI(ic, false);
@@ -224,6 +228,8 @@ RimeEngine::RimeEngine(Instance *instance)
224228
globalConfigReloadHandle_ = instance_->watchEvent(
225229
EventType::GlobalConfigReloaded, EventWatcherPhase::Default,
226230
[this](Event &) { refreshSessionPoolPolicy(); });
231+
232+
allowNotification("failure");
227233
reloadConfig();
228234
constructed_ = true;
229235
}
@@ -335,7 +341,7 @@ void RimeEngine::setSubConfig(const std::string &path,
335341
if (path == "deploy") {
336342
deploy();
337343
} else if (path == "sync") {
338-
sync();
344+
sync(/*userTriggered=*/true);
339345
}
340346
}
341347

@@ -504,7 +510,6 @@ void RimeEngine::deactivate(const InputMethodEntry &entry,
504510

505511
void RimeEngine::keyEvent(const InputMethodEntry &entry, KeyEvent &event) {
506512
FCITX_UNUSED(entry);
507-
lastKeyEventTime_ = now(CLOCK_MONOTONIC);
508513
RIME_DEBUG() << "Rime receive key: " << event.rawKey() << " "
509514
<< event.isRelease();
510515
auto *inputContext = event.inputContext();
@@ -513,7 +518,7 @@ void RimeEngine::keyEvent(const InputMethodEntry &entry, KeyEvent &event) {
513518
deploy();
514519
return event.filterAndAccept();
515520
} else if (event.key().checkKeyList(*config_.synchronize)) {
516-
sync();
521+
sync(/*userTriggered=*/true);
517522
return event.filterAndAccept();
518523
}
519524
}
@@ -534,15 +539,12 @@ void RimeEngine::reset(const InputMethodEntry & /*entry*/,
534539
inputContext->updateUserInterface(UserInterfaceComponent::InputPanel);
535540
}
536541

537-
void RimeEngine::blockNotificationFor(uint64_t usec) {
538-
blockNotificationBefore_ = now(CLOCK_MONOTONIC) + usec;
542+
void RimeEngine::allowNotification(std::string type) {
543+
allowNotificationUntil_ = now(CLOCK_MONOTONIC) + NotificationTimeout;
544+
allowNotificationType_ = std::move(type);
539545
}
540546

541-
void RimeEngine::save() {
542-
// Block notification for 5 sec.
543-
blockNotificationFor(5000000);
544-
sync();
545-
}
547+
void RimeEngine::save() { sync(/*userTriggered=*/false); }
546548

547549
void RimeEngine::rimeNotificationHandler(void *context, RimeSessionId session,
548550
const char *messageType,
@@ -578,7 +580,7 @@ void RimeEngine::notify(RimeSessionId session, const std::string &messageType,
578580
const char *message = nullptr;
579581
const char *icon = "";
580582
const char *tipId = "";
581-
int timeout = 3000;
583+
const int timeout = 3000;
582584
bool blockMessage = false;
583585
if (messageType == "deploy") {
584586
tipId = "fcitx-rime-deploy";
@@ -612,14 +614,17 @@ void RimeEngine::notify(RimeSessionId session, const std::string &messageType,
612614
}
613615

614616
auto *notifications = this->notifications();
615-
if (message && notifications &&
616-
now(CLOCK_MONOTONIC) > blockNotificationBefore_) {
617+
const auto current = now(CLOCK_MONOTONIC);
618+
if (message && notifications && current > silenceNotificationUntil_ &&
619+
(current < allowNotificationUntil_ &&
620+
(allowNotificationType_.empty() ||
621+
messageType == allowNotificationType_))) {
617622
notifications->call<INotifications::showTip>(
618623
tipId, _("Rime"), icon, _("Rime"), message, timeout);
619624
}
620625
// Block message after error / success.
621626
if (blockMessage) {
622-
blockNotificationFor(30000);
627+
silenceNotificationUntil_ = current + 30000;
623628
}
624629
}
625630

@@ -683,12 +688,16 @@ void RimeEngine::deploy() {
683688
RIME_DEBUG() << "Rime Deploy";
684689
releaseAllSession(true);
685690
api_->finalize();
691+
allowNotification();
686692
rimeStart(true);
687693
}
688694

689-
void RimeEngine::sync() {
695+
void RimeEngine::sync(bool userTriggered) {
690696
RIME_DEBUG() << "Rime Sync user data";
691697
releaseAllSession(true);
698+
if (userTriggered) {
699+
allowNotification();
700+
}
692701
api_->sync_user_data();
693702
}
694703

@@ -757,7 +766,6 @@ void RimeEngine::updateSchemaMenu() {
757766
schemaAction.connect<SimpleAction::Activated>(
758767
[this, schemaId](InputContext *ic) {
759768
auto *state = this->state(ic);
760-
blockNotificationFor(30000);
761769
state->selectSchema(schemaId);
762770
imAction_->update(ic);
763771
});

src/rimeengine.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class RimeEngine final : public InputMethodEngineV2 {
167167
FCITX_ADDON_DEPENDENCY_LOADER(dbus, instance_->addonManager());
168168
#endif
169169

170-
void blockNotificationFor(uint64_t usec);
170+
void allowNotification(std::string type = "");
171171
const auto &schemas() const { return schemas_; }
172172
const auto &optionActions() const { return optionActions_; };
173173

@@ -177,7 +177,7 @@ class RimeEngine final : public InputMethodEngineV2 {
177177
const char *messageValue);
178178

179179
void deploy();
180-
void sync();
180+
void sync(bool userTriggered);
181181
void updateSchemaMenu();
182182
void updateActionsForSchema(const std::string &schema);
183183
void notifyImmediately(RimeSessionId session, std::string_view type,
@@ -199,8 +199,9 @@ class RimeEngine final : public InputMethodEngineV2 {
199199
EventDispatcher eventDispatcher_;
200200
rime_api_t *api_;
201201
static bool firstRun_;
202-
uint64_t blockNotificationBefore_ = 0;
203-
uint64_t lastKeyEventTime_ = 0;
202+
uint64_t silenceNotificationUntil_ = 0;
203+
uint64_t allowNotificationUntil_ = 0;
204+
std::string allowNotificationType_;
204205
FactoryFor<RimeState> factory_;
205206
bool needRefreshAppOption_ = false;
206207

0 commit comments

Comments
 (0)