Skip to content

Commit

Permalink
Merge pull request #67 from fcitx/Issue#66_pretty_format
Browse files Browse the repository at this point in the history
Issue#66 pretty format
  • Loading branch information
naokiri authored Nov 4, 2022
2 parents fbf6cfc + fa42fb2 commit 6e0e655
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 10 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.13)
cmake_policy(SET CMP0063 NEW)
project(fcitx5-cskk VERSION 0.8.2)
project(fcitx5-cskk VERSION 1.0.0)
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_STANDARD 17)
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
Expand All @@ -21,7 +21,8 @@ find_package(PkgConfig REQUIRED)
find_package(Fcitx5Core 5.0.6 REQUIRED)
find_package(Fcitx5Utils 5.0.6 REQUIRED)

pkg_check_modules(LIBCSKK REQUIRED IMPORTED_TARGET "cskk>=0.10.0")
# GITHUB_ACTION_BUILD_CSKK_VERSION=1.0.1
pkg_check_modules(LIBCSKK REQUIRED IMPORTED_TARGET "cskk>=1.0")

option(ENABLE_QT "Enable Qt for GUI configuration" On)

Expand Down
2 changes: 1 addition & 1 deletion bin/cskk_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Run from repository root and output cskk version from CMakeLists.txt
# Helper for release automation
set -e
cat CMakeLists.txt | sed -n -e "s/pkg_check_modules(LIBCSKK REQUIRED IMPORTED_TARGET \"cskk>=\(.*\)\")/\1/p"
cat CMakeLists.txt | sed -n -e "s/# GITHUB_ACTION_BUILD_CSKK_VERSION=\(.*\)/\1/p"
1 change: 1 addition & 0 deletions org.fcitx.Fcitx5.Addon.Cskk.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<url type="bugtracker">https://github.com/naokiri/fcitx5-cskk/issues</url>
<project_group>Fcitx</project_group>
<releases>
<release version="1.0.0" date="2022-11-04"/>
<release version="0.8.2" date="2022-10-29"/>
<release version="0.8.1" date="2022-10-15"/>
<release version="0.8.0" date="2022-09-24"/>
Expand Down
141 changes: 134 additions & 7 deletions src/cskk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,10 @@ void FcitxCskkContext::updateUI() {
}

// Preedit
auto preedit = skk_context_get_preedit(context_);
Text preeditText;
// FIXME: Pretty text format someday.
preeditText.append(std::string(preedit));
preeditText.setCursor(static_cast<int>(strlen(preedit)));
uint32_t stateStackLen;
auto preeditDetail = skk_context_get_preedit_detail(context_, &stateStackLen);
auto [mainPreedit, supplementPreedit] =
FcitxCskkContext::formatPreedit(preeditDetail, stateStackLen);

// CandidateList
int currentCursorPosition =
Expand Down Expand Up @@ -383,10 +382,11 @@ void FcitxCskkContext::updateUI() {
}

if (ic_->capabilityFlags().test(CapabilityFlag::Preedit)) {
inputPanel.setClientPreedit(preeditText);
inputPanel.setClientPreedit(mainPreedit);
inputPanel.setPreedit(supplementPreedit);
ic_->updatePreedit();
} else {
inputPanel.setPreedit(preeditText);
inputPanel.setPreedit(mainPreedit);
}

// StatusArea for status icon
Expand Down Expand Up @@ -432,6 +432,133 @@ int FcitxCskkContext::getInputMode() {
}
return skk_context_get_input_mode(context_);
}
/**
* format preedit state into Text.
* Returns tuple of <Main content Text, Supplement content Text>
* Main content is something you always want to show, supplement content is
* something you may show when you have space.
*/
std::tuple<Text, Text>
FcitxCskkContext::formatPreedit(CskkStateInfoFfi *cskkStateInfoArray,
uint32_t stateLen) {
std::string precomposition_marker = "";
std::string selection_marker = "";
Text mainContent = Text(""), supplementContent = Text("");
size_t mainCursorIdx = 0;
for (uint32_t i = 0; i < stateLen; i++) {
auto cskkStateInfo = cskkStateInfoArray[i];
switch (cskkStateInfo.tag) {
case DirectStateInfo: {
auto directStateInfo = cskkStateInfo.direct_state_info;
if (directStateInfo.confirmed) {
mainCursorIdx += strlen(directStateInfo.confirmed);
mainContent.append(directStateInfo.confirmed, TextFormatFlag::NoFlag);
}
if (directStateInfo.unconverted) {
mainCursorIdx += strlen(directStateInfo.unconverted);
mainContent.append(directStateInfo.unconverted,
TextFormatFlag::Underline);
}
} break;
case PreCompositionStateInfo: {
auto precompositionStateInfo = cskkStateInfo.pre_composition_state_info;
mainCursorIdx += precomposition_marker.length();
mainContent.append(precomposition_marker, TextFormatFlag::DontCommit);
if (precompositionStateInfo.confirmed) {
mainCursorIdx += strlen(precompositionStateInfo.confirmed);
mainContent.append(precompositionStateInfo.confirmed,
TextFormatFlag::NoFlag);
}
if (precompositionStateInfo.kana_to_composite) {
mainCursorIdx += strlen(precompositionStateInfo.kana_to_composite);
mainContent.append(precompositionStateInfo.kana_to_composite,
TextFormatFlag::Underline);
}
if (precompositionStateInfo.unconverted) {
mainCursorIdx += strlen(precompositionStateInfo.unconverted);
mainContent.append(precompositionStateInfo.unconverted,
TextFormatFlag::Underline);
}
} break;
case PreCompositionOkuriganaStateInfo: {
auto precompositionOkuriganaStateInfo =
cskkStateInfo.pre_composition_okurigana_state_info;
mainContent.append(precomposition_marker, TextFormatFlag::DontCommit);
mainCursorIdx += precomposition_marker.length();
if (precompositionOkuriganaStateInfo.confirmed) {
mainCursorIdx += strlen(precompositionOkuriganaStateInfo.confirmed);
mainContent.append(precompositionOkuriganaStateInfo.confirmed,
TextFormatFlag::NoFlag);
}
if (precompositionOkuriganaStateInfo.kana_to_composite) {
mainCursorIdx +=
strlen(precompositionOkuriganaStateInfo.kana_to_composite);
mainContent.append(precompositionOkuriganaStateInfo.kana_to_composite,
TextFormatFlag::Underline);
}
if (precompositionOkuriganaStateInfo.unconverted) {
mainContent.append("*", TextFormatFlags{TextFormatFlag::Underline,
TextFormatFlag::DontCommit});
mainCursorIdx +=
strlen(precompositionOkuriganaStateInfo.unconverted) + 1;
mainContent.append(precompositionOkuriganaStateInfo.unconverted,
TextFormatFlag::Underline);
}
} break;
case CompositionSelectionStateInfo: {
auto compositionSelectionStateInfo =
cskkStateInfo.composition_selection_state_info;
mainContent.append(selection_marker, TextFormatFlag::DontCommit);
mainCursorIdx += selection_marker.length();
std::string tmpContentString;
if (compositionSelectionStateInfo.composited) {
mainCursorIdx += strlen(compositionSelectionStateInfo.composited);
tmpContentString.append(compositionSelectionStateInfo.composited);
}
if (compositionSelectionStateInfo.okuri) {
mainCursorIdx += strlen(compositionSelectionStateInfo.okuri);
tmpContentString.append(compositionSelectionStateInfo.okuri);
}
mainContent.append(tmpContentString, TextFormatFlag::Underline);

if (compositionSelectionStateInfo.annotation) {
supplementContent.clear();
supplementContent.append(compositionSelectionStateInfo.annotation,
TextFormatFlag::DontCommit);
}
} break;
case RegisterStateInfo: {
auto registerStateInfo = cskkStateInfo.register_state_info;
mainContent.append(selection_marker, TextFormatFlag::DontCommit);
mainCursorIdx += selection_marker.length();
if (registerStateInfo.confirmed) {
mainCursorIdx += strlen(registerStateInfo.confirmed);
mainContent.append(registerStateInfo.confirmed,
TextFormatFlag::DontCommit);
}
if (registerStateInfo.kana_to_composite) {
mainCursorIdx += strlen(registerStateInfo.kana_to_composite);
mainContent.append(registerStateInfo.kana_to_composite,
TextFormatFlag::DontCommit);
}
if (registerStateInfo.okuri) {
mainCursorIdx += strlen(registerStateInfo.okuri);
mainContent.append(registerStateInfo.okuri, TextFormatFlag::DontCommit);
}
mainCursorIdx += strlen("");
mainContent.append("", TextFormatFlag::DontCommit);
} break;
}
}
// FIXME: Silently assuming length is less than int_max here. May fail when
// length is over UINT_MAX. very unlikely, not high priority.
mainContent.setCursor((int)mainCursorIdx);
for (uint32_t i = 1; i < stateLen; i++) {
mainContent.append("", TextFormatFlag::DontCommit);
}

return {mainContent, supplementContent};
}

/*******************************************************************************
* FcitxCskkFactory
Expand Down
2 changes: 2 additions & 0 deletions src/cskk.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class FcitxCskkContext final : public InputContextProperty {
bool handleCandidateSelection(
const std::shared_ptr<FcitxCskkCandidateList> &candidateList,
KeyEvent &keyEvent);
static std::tuple<Text, Text> formatPreedit(CskkStateInfoFfi *cskkStateInfo,
uint32_t stateLen);
};

class FcitxCskkFactory final : public AddonFactory {
Expand Down

0 comments on commit 6e0e655

Please sign in to comment.