Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compose support #106

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/rimeengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ void RimeEngine::reset(const InputMethodEntry & /*entry*/,
auto *inputContext = event.inputContext();
auto *state = this->state(inputContext);
state->clear();
instance_->resetCompose(inputContext);
inputContext->inputPanel().reset();
inputContext->updatePreedit();
inputContext->updateUserInterface(UserInterfaceComponent::InputPanel);
Expand Down
81 changes: 62 additions & 19 deletions src/rimestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@
#include "rimestate.h"
#include "rimecandidate.h"
#include "rimeengine.h"
#include <cstdint>
#include <fcitx-utils/capabilityflags.h>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/key.h>
#include <fcitx-utils/keysym.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx-utils/textformatflags.h>
#include <fcitx-utils/utf8.h>
#include <fcitx/candidatelist.h>
#include <fcitx/event.h>
#include <fcitx/inputcontext.h>
#include <fcitx/inputpanel.h>
#include <fcitx/text.h>
#include <fcitx/userinterface.h>
#include <functional>
#include <iterator>
#include <memory>
#include <optional>
#include <rime_api.h>
#include <string>
#include <utility>

namespace fcitx {

namespace {

bool emptyExceptAux(const InputPanel &inputPanel) {

return inputPanel.preedit().size() == 0 &&
inputPanel.preedit().size() == 0 &&
(!inputPanel.candidateList() ||
inputPanel.candidateList()->size() == 0);
return inputPanel.preedit().empty() && inputPanel.preedit().empty() &&
(!inputPanel.candidateList() || inputPanel.candidateList()->empty());
}
} // namespace

Expand Down Expand Up @@ -93,7 +105,7 @@ std::string RimeState::subModeLabel() {
}

void RimeState::toggleLatinMode() {
auto api = engine_->api();
auto *api = engine_->api();
if (api->is_maintenance_mode()) {
return;
}
Expand All @@ -103,15 +115,15 @@ void RimeState::toggleLatinMode() {
}

void RimeState::setLatinMode(bool latin) {
auto api = engine_->api();
auto *api = engine_->api();
if (api->is_maintenance_mode()) {
return;
}
api->set_option(session(), RIME_ASCII_MODE, latin);
}

void RimeState::selectSchema(const std::string &schema) {
auto api = engine_->api();
auto *api = engine_->api();
if (api->is_maintenance_mode()) {
return;
}
Expand All @@ -121,7 +133,20 @@ void RimeState::selectSchema(const std::string &schema) {
}

void RimeState::keyEvent(KeyEvent &event) {
auto api = engine_->api();
auto *ic = event.inputContext();
std::optional<std::string> compose;
if (!event.key().states().testAny(
KeyStates{KeyState::Ctrl, KeyState::Super}) &&
!event.isRelease()) {
compose =
engine_->instance()->processComposeString(&ic_, event.key().sym());
if (!compose) {
event.filterAndAccept();
return;
}
}

auto *api = engine_->api();
if (api->is_maintenance_mode()) {
return;
}
Expand All @@ -143,26 +168,44 @@ void RimeState::keyEvent(KeyEvent &event) {
// IBUS_RELEASE_MASK
intStates |= (1 << 30);
}
auto result = api->process_key(session, event.rawKey().sym(), intStates);
if (!compose->empty()) {
event.filterAndAccept();
auto length = utf8::lengthValidated(*compose);
bool result = false;
if (length == 1) {
auto c = utf8::getChar(*compose);
auto sym = Key::keySymFromUnicode(c);
if (sym != FcitxKey_None) {
result = api->process_key(session, sym, intStates);
}
}
if (!result) {
commitPreedit(ic);
ic->commitString(*compose);
clear();
}
} else {
auto result =
api->process_key(session, event.rawKey().sym(), intStates);
if (result) {
event.filterAndAccept();
}
}

auto ic = event.inputContext();
RIME_STRUCT(RimeCommit, commit);
if (api->get_commit(session, &commit)) {
ic->commitString(commit.text);
api->free_commit(&commit);
engine_->instance()->resetCompose(ic);
}

updateUI(ic, event.isRelease());

if (result) {
event.filterAndAccept();
}
}

#ifndef FCITX_RIME_NO_SELECT_CANDIDATE
void RimeState::selectCandidate(InputContext *inputContext, int idx,
bool global) {
auto api = engine_->api();
auto *api = engine_->api();
if (api->is_maintenance_mode()) {
return;
}
Expand All @@ -186,7 +229,7 @@ void RimeState::selectCandidate(InputContext *inputContext, int idx,

bool RimeState::getStatus(
const std::function<void(const RimeStatus &)> &callback) {
auto api = engine_->api();
auto *api = engine_->api();
auto session = this->session();
if (!session) {
return false;
Expand Down Expand Up @@ -298,7 +341,7 @@ void RimeState::updateUI(InputContext *ic, bool keyRelease) {
bool oldEmptyExceptAux = emptyExceptAux(inputPanel);

do {
auto api = engine_->api();
auto *api = engine_->api();
if (api->is_maintenance_mode()) {
return;
}
Expand Down Expand Up @@ -348,7 +391,7 @@ void RimeState::updateUI(InputContext *ic, bool keyRelease) {
void RimeState::release() { session_.reset(); }

void RimeState::commitPreedit(InputContext *ic) {
if (auto api = engine_->api()) {
if (auto *api = engine_->api()) {
RIME_STRUCT(RimeContext, context);
auto session = this->session();
if (!api->get_context(session, &context)) {
Expand All @@ -374,7 +417,7 @@ void RimeState::snapshot() {
if (savedCurrentSchema_.empty()) {
return;
}
auto &optionActions = engine_->optionActions();
const auto &optionActions = engine_->optionActions();
auto iter = optionActions.find(savedCurrentSchema_);
if (iter == optionActions.end()) {
return;
Expand Down
Loading