集成悬浮按钮功能

1. 在虚拟键盘入口管理其中继承悬浮按钮管理器

2. 为虚拟键盘托盘图标添加右键菜单功能
This commit is contained in:
liulinsong 2023-04-10 10:24:29 +08:00 committed by hanteng
parent 126bb24d7f
commit 6cd85b5e85
12 changed files with 194 additions and 12 deletions

View File

Before

Width:  |  Height:  |  Size: 476 B

After

Width:  |  Height:  |  Size: 476 B

View File

Before

Width:  |  Height:  |  Size: 479 B

After

Width:  |  Height:  |  Size: 479 B

View File

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

Before

Width:  |  Height:  |  Size: 5.4 KiB

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@ -25,6 +25,7 @@ SOURCES += \
src/virtualkeyboard/virtualkeyboardview.cpp \
src/virtualkeyboardentry/floatbutton.cpp \
src/virtualkeyboardentry/floatbuttonmanager.cpp \
src/virtualkeyboardentry/virtualkeyboardentrymanager.cpp \
src/virtualkeyboardentry/virtualkeyboardtrayicon.cpp
TRANSLATIONS = translations/translation.ts \
@ -70,6 +71,7 @@ HEADERS += \
src/virtualkeyboardentry/floatbutton.h \
src/virtualkeyboardentry/floatbuttonmanager.h \
src/virtualkeyboardentry/floatbuttonstrategy.h \
src/virtualkeyboardentry/virtualkeyboardentrymanager.h \
src/virtualkeyboardentry/virtualkeyboardtrayicon.h
DISTFILES +=

View File

@ -5,7 +5,7 @@
#include "ipc/dbusservice.h"
#include "ipc/fcitxvirtualkeyboardserviceproxy.h"
#include "virtualkeyboard/virtualkeyboardmanager.h"
#include "virtualkeyboardentry/virtualkeyboardtrayicon.h"
#include "virtualkeyboardentry/virtualkeyboardentrymanager.h"
int main(int argc, char *argv[]) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
@ -19,8 +19,8 @@ int main(int argc, char *argv[]) {
VirtualKeyboardManager virtualKeyboardManager;
FcitxVirtualKeyboardServiceProxy virtualKeyboardService;
VirtualKeyboardTrayIcon trayIcon(virtualKeyboardManager,
virtualKeyboardService);
VirtualKeyboardEntryManager entryManager(virtualKeyboardManager,
virtualKeyboardService);
DBusService dbusService(&virtualKeyboardManager);

View File

@ -131,9 +131,6 @@ void FloatButtonManager::connectFloatButtonSignals() {
showFloatButton();
}
});
connect(floatButtonView_.get(), &QPushButton::clicked, this,
[this]() { fcitxVirtualKeyboardService_.showVirtualKeyboard(); });
}
void FloatButtonManager::updateFloatButtonEnabled(bool enabled) {

View File

@ -0,0 +1,87 @@
#include "virtualkeyboardentry/virtualkeyboardentrymanager.h"
#include <QAction>
#include <QIcon>
#include "geometrymanager/geometrymanager.h"
#include "virtualkeyboardentry/floatbuttonstrategy.h"
VirtualKeyboardEntryManager::VirtualKeyboardEntryManager(
VirtualKeyboardManager &virtualKeyboardManager,
const FcitxVirtualKeyboardService &fcitxVirtualKeyboardService)
: virtualKeyboardManager_(virtualKeyboardManager),
floatButtonManager_(new FloatButtonManager(virtualKeyboardManager,
fcitxVirtualKeyboardService)) {
initFloatButtonEnabledContextMenu();
initFloatButtonDisabledContextMenu();
initTrayIcon(fcitxVirtualKeyboardService);
connectSignals();
floatButtonManager_->loadFloatButtonAvailability();
}
VirtualKeyboardEntryManager::~VirtualKeyboardEntryManager() = default;
void VirtualKeyboardEntryManager::initTrayIcon(
const FcitxVirtualKeyboardService &fcitxVirtualKeyboardService) {
trayIconEntry_.reset(new VirtualKeyboardTrayIcon(
virtualKeyboardManager_, fcitxVirtualKeyboardService));
}
void VirtualKeyboardEntryManager::connectSignals() {
connect(floatButtonManager_.get(), &FloatButtonManager::floatButtonEnabled,
this, [this]() {
trayIconEntry_->setContextMenu(floatButtonEnabledContextMenu_);
});
connect(floatButtonManager_.get(), &FloatButtonManager::floatButtonDisabled,
this, [this]() {
trayIconEntry_->setContextMenu(floatButtonDisabledContextMenu_);
});
connect(&virtualKeyboardManager_,
&VirtualKeyboardManager::virtualKeyboardVisibiltyChanged, this,
[this](bool visible) {
if (!visible) {
return;
}
trayIconEntry_->hideContextMenu();
});
}
QMenu *VirtualKeyboardEntryManager::createFloatButtonContextMenu(
const QString &icon, const QString &text, MenuTriggeredCallback callback) {
QMenu *menu = new QMenu();
QAction *action = new QAction(menu);
action->setIcon(QIcon(icon));
action->setText(text);
menu->addAction(action);
connect(action, &QAction::triggered, this, [callback](bool) {
if (!callback) {
return;
}
callback();
});
return menu;
}
void VirtualKeyboardEntryManager::initFloatButtonEnabledContextMenu() {
floatButtonEnabledContextMenu_.reset(createFloatButtonContextMenu(
":/floatbutton/img/disablefloatbutton.svg",
tr("Disable the float button"),
[this]() { floatButtonManager_->disableFloatButton(); }));
}
void VirtualKeyboardEntryManager::initFloatButtonDisabledContextMenu() {
floatButtonDisabledContextMenu_.reset(createFloatButtonContextMenu(
":/floatbutton/img/enablefloatbutton.svg",
tr("Enable the float button"),
[this]() { floatButtonManager_->enableFloatButton(); }));
}

View File

@ -0,0 +1,48 @@
#ifndef VIRTUALKEYBOARDENTRYMANAGER_H
#define VIRTUALKEYBOARDENTRYMANAGER_H
#include <functional>
#include <memory>
#include <QMenu>
#include <QObject>
#include "ipc/fcitxvirtualkeyboardserviceproxy.h"
#include "virtualkeyboard/virtualkeyboardmanager.h"
#include "virtualkeyboardentry/floatbuttonmanager.h"
#include "virtualkeyboardentry/virtualkeyboardtrayicon.h"
class VirtualKeyboardEntryManager : public QObject {
Q_OBJECT
public:
VirtualKeyboardEntryManager(
VirtualKeyboardManager &virtualKeyboardManager,
const FcitxVirtualKeyboardService &fcitxVirtualKeyboardService);
~VirtualKeyboardEntryManager() override;
private:
using MenuTriggeredCallback = std::function<void()>;
private:
void connectSignals();
QMenu *createFloatButtonContextMenu(const QString &icon,
const QString &text,
MenuTriggeredCallback callback);
void initFloatButtonEnabledContextMenu();
void initFloatButtonDisabledContextMenu();
void initTrayIcon(
const FcitxVirtualKeyboardService &fcitxVirtualKeyboardService);
private:
VirtualKeyboardManager &virtualKeyboardManager_;
std::unique_ptr<FloatButtonManager> floatButtonManager_ = nullptr;
std::unique_ptr<VirtualKeyboardTrayIcon> trayIconEntry_ = nullptr;
std::shared_ptr<QMenu> floatButtonEnabledContextMenu_ = nullptr;
std::shared_ptr<QMenu> floatButtonDisabledContextMenu_ = nullptr;
};
#endif // VIRTUALKEYBOARDENTRYMANAGER_H

View File

@ -1,8 +1,9 @@
#include <QIcon>
#include "virtualkeyboard/virtualkeyboardmanager.h"
#include "virtualkeyboardentry/virtualkeyboardtrayicon.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QIcon>
VirtualKeyboardTrayIcon::VirtualKeyboardTrayIcon(
VirtualKeyboardManager &virtualKeyboardManager,
const FcitxVirtualKeyboardService &fcitxVirtualKeyboardService)
@ -11,6 +12,13 @@ VirtualKeyboardTrayIcon::VirtualKeyboardTrayIcon(
initTrayIcon();
}
void VirtualKeyboardTrayIcon::setContextMenu(
std::shared_ptr<QMenu> contextMenu) {
contextMenu_ = contextMenu;
}
void VirtualKeyboardTrayIcon::hideContextMenu() { contextMenu_->hide(); }
void VirtualKeyboardTrayIcon::initTrayIcon() {
trayIcon_ = new QSystemTrayIcon(this);
trayIcon_->setIcon(QIcon::fromTheme("input-keyboard-symbolic"));
@ -20,13 +28,40 @@ void VirtualKeyboardTrayIcon::initTrayIcon() {
trayIcon_->setVisible(true);
}
void VirtualKeyboardTrayIcon::toggleVirtualKeyboard() {
if (virtualKeyboardManager_.isVirtualKeyboardVisible()) {
fcitxVirtualKeyboardService_.hideVirtualKeyboard();
} else {
fcitxVirtualKeyboardService_.showVirtualKeyboard();
}
}
void VirtualKeyboardTrayIcon::ensuareVirtualKeyboardInvisible() {
if (!virtualKeyboardManager_.isVirtualKeyboardVisible()) {
return;
}
virtualKeyboardManager_.hideVirtualKeyboard();
}
void VirtualKeyboardTrayIcon::showContextMenu() {
QSize menuSize = contextMenu_->sizeHint();
QPoint point = QCursor::pos();
QRect deskRect = QApplication::desktop()->availableGeometry();
contextMenu_->move(point.x(), deskRect.height() - menuSize.height());
contextMenu_->show();
}
void VirtualKeyboardTrayIcon::onTrayIconActivated(
QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::Trigger: {
virtualKeyboardManager_.isVirtualKeyboardVisible()
? fcitxVirtualKeyboardService_.hideVirtualKeyboard()
: fcitxVirtualKeyboardService_.showVirtualKeyboard();
toggleVirtualKeyboard();
break;
};
case QSystemTrayIcon::Context: {
ensuareVirtualKeyboardInvisible();
showContextMenu();
break;
};
default:

View File

@ -1,6 +1,9 @@
#ifndef VIRTUALKEYBOARDTRAYICON_H
#define VIRTUALKEYBOARDTRAYICON_H
#include <memory>
#include <QMenu>
#include <QObject>
#include <QSystemTrayIcon>
@ -17,14 +20,24 @@ public:
~VirtualKeyboardTrayIcon() override = default;
void setContextMenu(std::shared_ptr<QMenu> contextMenu);
void hideContextMenu();
private:
void initTrayIcon();
void toggleVirtualKeyboard();
void ensuareVirtualKeyboardInvisible();
void showContextMenu();
private slots:
void onTrayIconActivated(QSystemTrayIcon::ActivationReason reason);
private:
QSystemTrayIcon *trayIcon_ = nullptr;
std::shared_ptr<QMenu> contextMenu_ = nullptr;
VirtualKeyboardManager &virtualKeyboardManager_;
const FcitxVirtualKeyboardService &fcitxVirtualKeyboardService_;
};