Skip to content

Commit 6760d76

Browse files
committed
show the current input method name in the tray icon
1 parent 9989e72 commit 6760d76

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/modules/notificationitem/notificationitem.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ class StatusNotifierItem : public dbus::ObjectVTable<StatusNotifierItem> {
7272

7373
std::string label() { return ""; }
7474

75+
std::string title() {
76+
const InputMethodEntry *imEntry = nullptr;
77+
if (auto *ic = parent_->menu()->lastRelevantIc()) {
78+
imEntry = parent_->instance()->inputMethodEntry(ic);
79+
}
80+
return imEntry == nullptr ? _("Input Method") : imEntry->name();
81+
}
82+
7583
static dbus::DBusStruct<
7684
std::string,
7785
std::vector<dbus::DBusStruct<int32_t, int32_t, std::vector<uint8_t>>>,
@@ -104,6 +112,15 @@ class StatusNotifierItem : public dbus::ObjectVTable<StatusNotifierItem> {
104112
lastLabel_ = std::move(label);
105113
}
106114

115+
void notifyNewTitle() {
116+
std::string currentTitle = title();
117+
if (currentTitle.empty() || lastTitle_ == currentTitle) {
118+
return;
119+
}
120+
newTitle();
121+
lastTitle_ = std::move(currentTitle);
122+
}
123+
107124
void reset() {
108125
releaseSlot();
109126
lastIconName_.clear();
@@ -138,8 +155,7 @@ class StatusNotifierItem : public dbus::ObjectVTable<StatusNotifierItem> {
138155
FCITX_OBJECT_VTABLE_PROPERTY(category, "Category", "s",
139156
[]() { return "SystemServices"; });
140157
FCITX_OBJECT_VTABLE_PROPERTY(id, "Id", "s", []() { return "Fcitx"; });
141-
FCITX_OBJECT_VTABLE_PROPERTY(title, "Title", "s",
142-
[]() { return _("Input Method"); });
158+
FCITX_OBJECT_VTABLE_PROPERTY(title, "Title", "s", [this]() { return title(); });
143159
FCITX_OBJECT_VTABLE_PROPERTY(status, "Status", "s",
144160
[]() { return "Active"; });
145161
FCITX_OBJECT_VTABLE_PROPERTY(windowId, "WindowId", "i", []() { return 0; });
@@ -238,6 +254,7 @@ class StatusNotifierItem : public dbus::ObjectVTable<StatusNotifierItem> {
238254
std::string cachedLabel_;
239255
std::vector<dbus::DBusStruct<int, int, std::vector<uint8_t>>>
240256
cachedLabelIcon_;
257+
std::string lastTitle_;
241258
};
242259

243260
NotificationItem::NotificationItem(Instance *instance)
@@ -302,6 +319,16 @@ void NotificationItem::setRegistered(bool registered) {
302319
updateIcon(event);
303320
}
304321
}));
322+
eventHandlers_.emplace_back(instance_->watchEvent(
323+
EventType::InputContextSwitchInputMethod,
324+
EventWatcherPhase::Default, [this](Event &event) {
325+
if (auto *ic = dynamic_cast<InputContextEvent &>(event)
326+
.inputContext()) {
327+
if (ic->hasFocus()) {
328+
newTitle();
329+
}
330+
}
331+
}));
305332
}
306333

307334
for (auto &handler : handlers_.view()) {
@@ -406,6 +433,13 @@ void NotificationItem::newIcon() {
406433
// sni_->xayatanaNewLabel(sni_->label(), sni_->label());
407434
}
408435

436+
void NotificationItem::newTitle() {
437+
if (!sni_->isRegistered()) {
438+
return;
439+
}
440+
sni_->notifyNewTitle();
441+
}
442+
409443
class NotificationItemFactory : public AddonFactory {
410444
AddonInstance *create(AddonManager *manager) override {
411445
return new NotificationItem(manager->instance());

src/modules/notificationitem/notificationitem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class NotificationItem : public AddonInstance {
4949

5050
void maybeScheduleRegister();
5151
void cleanUp();
52+
void newTitle();
5253

5354
Instance *instance_;
5455
std::unique_ptr<dbus::ServiceWatcher> watcher_;

0 commit comments

Comments
 (0)