Skip to content

Commit

Permalink
i don't fucking know anymore fuck this shit
Browse files Browse the repository at this point in the history
  • Loading branch information
GaZaTu committed Oct 21, 2023
1 parent c144ed9 commit 970c364
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
22 changes: 13 additions & 9 deletions src/EmojiPickerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ void moveQWidgetToCenter(QWidget* window) {
}

QPoint createPointInScreen(QWidget* window, QRect newPoint) {
QPoint result{newPoint.x(), newPoint.y()};
result.setX(result.x() + newPoint.width());
result.setY(result.y() + newPoint.height());
QPoint result{0, 0};
result.setX(newPoint.x() + newPoint.width());
result.setY(newPoint.y() + newPoint.height());

QRect windowRect = window->geometry();

Expand Down Expand Up @@ -80,17 +80,19 @@ std::function<void()> resetInputMethodEngine = []() {
ThreadsafeQueue<std::shared_ptr<EmojiCommand>> emojiCommandQueue;

EmojiPickerWindow::EmojiPickerWindow() : QMainWindow() {
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_ShowWithoutActivating);
setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::WindowDoesNotAcceptFocus);
setWindowIcon(QIcon(":/res/im-emoji-picker_72x72.png"));
setWindowOpacity(_settings.windowOpacity());
setFocusPolicy(Qt::NoFocus);
setAttribute(Qt::WA_ShowWithoutActivating);
setWindowTitle("im-emoji-picker");
setFixedSize(340, 190);

_searchContainerWidget->setLayout(_searchContainerLayout);
_searchContainerLayout->setStackingMode(QStackedLayout::StackAll);

_searchCompletion->setFocusPolicy(Qt::NoFocus);
_searchCompletion->setAttribute(Qt::WA_ShowWithoutActivating);
_searchCompletion->setReadOnly(true);
if (_settings.useSystemQtTheme()) {
_searchCompletion->setStyleSheet(_searchCompletion->styleSheet() + QString("background: #00000000;"));
Expand Down Expand Up @@ -574,7 +576,7 @@ void EmojiPickerWindow::setCursorLocation(const QRect* rect) {
newRect.setY((double)rect->y() / pixelRatio);
newRect.setWidth((double)rect->width() / pixelRatio);
newRect.setHeight((double)rect->height() / pixelRatio);
newRect.setWidth(0);
newRect.setWidth(0); // ????

QPoint newPoint = createPointInScreen(this, newRect);
// newPoint.setX((double)rect->x() / pixelRatio);
Expand Down Expand Up @@ -733,8 +735,10 @@ void EmojiPickerWindow::commitEmoji(const Emoji& emoji, bool isRealEmoji, bool c
}
}

void EmojiPickerWindow::processKeyEvent(const QKeyEvent* event) {
EmojiAction action = getEmojiActionForQKeyEvent(event);
void EmojiPickerWindow::processKeyEvent(const QKeyEvent* event, EmojiAction action) {
if (action == EmojiAction::INVALID) {
action = getEmojiActionForQKeyEvent(event);
}

switch (action) {
case EmojiAction::INVALID:
Expand Down Expand Up @@ -945,7 +949,7 @@ void gui_main(int argc, char** argv) {
window.setCursorLocation(&*command->rect);
}
if (auto command = std::dynamic_pointer_cast<EmojiCommandProcessKeyEvent>(_command)) {
window.processKeyEvent(&*command->keyEvent);
window.processKeyEvent(&*command->keyEvent, command->action);
}
}
});
Expand Down
48 changes: 25 additions & 23 deletions src/EmojiPickerWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@

extern std::function<void()> resetInputMethodEngine;

enum class EmojiAction {
INVALID,
SELECT_ALL_IN_SEARCH,
COPY_SELECTED_EMOJI,
DISABLE,
COMMIT_EMOJI,
SWITCH_VIEW_MODE,
UP,
DOWN,
LEFT,
RIGHT,
PAGE_UP,
PAGE_DOWN,
OPEN_SETTINGS,
CUT_SELECTION_IN_SEARCH,
CLEAR_SEARCH,
REMOVE_CHAR_IN_SEARCH,
INSERT_CHAR_IN_SEARCH,
};

struct EmojiCommand {
public:
virtual ~EmojiCommand() {
Expand All @@ -34,7 +54,7 @@ struct EmojiCommandEnable : public EmojiCommand {

bool resetPosition;

EmojiCommandEnable(std::function<void(const std::string&)> commitText, bool resetPosition = true) : EmojiCommand(), commitText{std::move(commitText)}, resetPosition{resetPosition} {
EmojiCommandEnable(std::function<void(const std::string&)>&& commitText, bool resetPosition = true) : EmojiCommand(), commitText{std::move(commitText)}, resetPosition{resetPosition} {
}
};

Expand Down Expand Up @@ -62,32 +82,14 @@ struct EmojiCommandProcessKeyEvent : public EmojiCommand {
public:
std::shared_ptr<QKeyEvent> keyEvent;

EmojiCommandProcessKeyEvent(QKeyEvent* keyEvent) : EmojiCommand(), keyEvent{keyEvent} {
EmojiAction action;

EmojiCommandProcessKeyEvent(QKeyEvent* keyEvent, EmojiAction action = EmojiAction::INVALID) : EmojiCommand(), keyEvent{keyEvent}, action{action} {
}
};

extern ThreadsafeQueue<std::shared_ptr<EmojiCommand>> emojiCommandQueue;

enum class EmojiAction {
INVALID,
SELECT_ALL_IN_SEARCH,
COPY_SELECTED_EMOJI,
DISABLE,
COMMIT_EMOJI,
SWITCH_VIEW_MODE,
UP,
DOWN,
LEFT,
RIGHT,
PAGE_UP,
PAGE_DOWN,
OPEN_SETTINGS,
CUT_SELECTION_IN_SEARCH,
CLEAR_SEARCH,
REMOVE_CHAR_IN_SEARCH,
INSERT_CHAR_IN_SEARCH,
};

QKeyEvent* createKeyEventWithUserPreferences(QEvent::Type _type, int _key, Qt::KeyboardModifiers _modifiers, const QString& _text);

EmojiAction getEmojiActionForQKeyEvent(const QKeyEvent* event);
Expand All @@ -112,7 +114,7 @@ public Q_SLOTS:
void enable(bool resetPosition = true);
void disable();
void setCursorLocation(const QRect* rect);
void processKeyEvent(const QKeyEvent* event);
void processKeyEvent(const QKeyEvent* event, EmojiAction action = EmojiAction::INVALID);

protected:
void changeEvent(QEvent* event) override;
Expand Down
13 changes: 9 additions & 4 deletions src/Fcitx5ImEmojiPickerModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ Fcitx5ImEmojiPickerModule::Fcitx5ImEmojiPickerModule(fcitx::Instance* instance)
_eventHandlers.emplace_back(_instance->watchEvent(fcitx::EventType::InputContextFocusOut, fcitx::EventWatcherPhase::Default, [this](fcitx::Event& _event) {
auto& event = static_cast<fcitx::InputContextEvent&>(_event);

log_printf("[debug] Fcitx5ImEmojiPickerModule::_eventHandlers[InputContextFocusOut]\n");

deactivate(event);
}));

_eventHandlers.emplace_back(_instance->watchEvent(fcitx::EventType::InputContextReset, fcitx::EventWatcherPhase::Default, [this](fcitx::Event& _event) {
auto& event = static_cast<fcitx::InputContextEvent&>(_event);

log_printf("[debug] Fcitx5ImEmojiPickerModule::_eventHandlers[InputContextReset]\n");

deactivate(event);
}));

_eventHandlers.emplace_back(_instance->watchEvent(fcitx::EventType::InputContextSwitchInputMethod, fcitx::EventWatcherPhase::Default, [this](fcitx::Event& _event) {
auto& event = static_cast<fcitx::InputContextEvent&>(_event);

log_printf("[debug] Fcitx5ImEmojiPickerModule::_eventHandlers[InputContextSwitchInputMethod]\n");

deactivate(event);
}));

Expand Down Expand Up @@ -156,7 +162,7 @@ void Fcitx5ImEmojiPickerModule::keyEvent(fcitx::KeyEvent& keyEvent) {
EmojiAction action = getEmojiActionForQKeyEvent(qevent);

if (action != EmojiAction::INVALID) {
emojiCommandQueue.push(std::make_shared<EmojiCommandProcessKeyEvent>(qevent));
emojiCommandQueue.push(std::make_shared<EmojiCommandProcessKeyEvent>(qevent, action));

keyEvent.accept();
} else {
Expand All @@ -182,10 +188,9 @@ void Fcitx5ImEmojiPickerModule::activate(fcitx::InputContextEvent& event) {
sendCursorLocation(event);

emojiCommandQueue.push(std::make_shared<EmojiCommandEnable>([this, inputContext{event.inputContext()}](const std::string& text) {
inputContext->commitString(text);
log_printf("[debug] Fcitx5ImEmojiPickerModule::(lambda) commitString:%s program:%s\n", text.data(), inputContext->program().data());

// usleep(10000);
// sendCursorLocation(inputContext);
inputContext->commitString(text);
}, false));
}

Expand Down

0 comments on commit 970c364

Please sign in to comment.