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 comment property to CandidateWord #1004

Merged
merged 6 commits into from
Apr 2, 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.6.0)
project(fcitx VERSION 5.1.8)
project(fcitx VERSION 5.1.9)
set(FCITX_VERSION ${PROJECT_VERSION})

find_package(ECM REQUIRED 1.0.0)
Expand Down
13 changes: 6 additions & 7 deletions src/frontend/dbusfrontend/dbusfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,10 @@ class DBusInputContext1 : public InputContext,
}

void updateClientSideUIImpl() override {
auto preedit =
im_->instance()->outputFilter(this, inputPanel().preedit());
auto auxUp = im_->instance()->outputFilter(this, inputPanel().auxUp());
auto auxDown =
im_->instance()->outputFilter(this, inputPanel().auxDown());
auto instance = im_->instance();
auto preedit = instance->outputFilter(this, inputPanel().preedit());
auto auxUp = instance->outputFilter(this, inputPanel().auxUp());
auto auxDown = instance->outputFilter(this, inputPanel().auxDown());
auto candidateList = inputPanel().candidateList();
int cursorIndex = 0;

Expand All @@ -220,9 +219,9 @@ class DBusInputContext1 : public InputContext,
Text labelText = candidate.hasCustomLabel()
? candidate.customLabel()
: candidateList->label(i);
labelText = im_->instance()->outputFilter(this, labelText);
labelText = instance->outputFilter(this, labelText);
Text candidateText =
im_->instance()->outputFilter(this, candidate.text());
instance->outputFilter(this, candidate.textWithComment());
candidates.emplace_back(std::make_tuple(
labelText.toString(), candidateText.toString()));
}
Expand Down
21 changes: 21 additions & 0 deletions src/lib/fcitx/candidatelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class CandidateWordPrivate {
bool isPlaceHolder_ = false;
Text customLabel_;
bool hasCustomLabel_ = false;
Text comment_;
};

CandidateWord::CandidateWord(Text text)
Expand All @@ -110,6 +111,26 @@ void CandidateWord::setText(Text text) {
d->text_ = std::move(text);
}

const Text &CandidateWord::comment() const {
FCITX_D();
return d->comment_;
}

void CandidateWord::setComment(Text comment) {
FCITX_D();
d->comment_ = std::move(comment);
}

Text CandidateWord::textWithComment(std::string separator) const {
FCITX_D();
auto text = d->text_;
if (!d->comment_.empty()) {
text.append(std::move(separator));
text.append(d->comment_);
}
return text;
}

bool CandidateWord::isPlaceHolder() const {
FCITX_D();
return d->isPlaceHolder_;
Expand Down
16 changes: 16 additions & 0 deletions src/lib/fcitx/candidatelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,28 @@ class FCITXCORE_EXPORT CandidateWord {
bool isPlaceHolder() const;
bool hasCustomLabel() const;
const Text &customLabel() const;
/**
* Return comment corresponding to the candidate.
*
* @return value of comment.
* @since 5.1.9
*/
const Text &comment() const;
eagleoflqj marked this conversation as resolved.
Show resolved Hide resolved
/**
* Return text with comment.
*
* @param separator separator between text and comment.
* @return value of comment.
* @since 5.1.9
*/
Text textWithComment(std::string separator = " ") const;

protected:
void setText(Text text);
void setPlaceHolder(bool placeHolder);
void resetCustomLabel();
void setCustomLabel(Text text);
void setComment(Text comment);

private:
std::unique_ptr<CandidateWordPrivate> d_ptr;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/fcitx/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

#include "text.h"
#include <iterator>
#include <stdexcept>
#include <tuple>
#include <vector>
Expand Down Expand Up @@ -55,6 +56,13 @@ void Text::append(std::string str, TextFormatFlags flag) {
d->texts_.emplace_back(std::move(str), flag);
}

void Text::append(Text text) {
FCITX_D();
std::copy(std::make_move_iterator(text.d_ptr->texts_.begin()),
std::make_move_iterator(text.d_ptr->texts_.end()),
std::back_inserter(d->texts_));
}

const std::string &Text::stringAt(int idx) const {
FCITX_D();
return std::get<std::string>(d->texts_[idx]);
Expand Down
7 changes: 7 additions & 0 deletions src/lib/fcitx/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class FCITXCORE_EXPORT Text {
void setCursor(int pos = -1);
void clear();
void append(std::string str, TextFormatFlags flag = TextFormatFlag::NoFlag);
/**
* Append another text.
*
eagleoflqj marked this conversation as resolved.
Show resolved Hide resolved
* @param text text to append
* @since 5.1.9
*/
void append(Text text);
const std::string &stringAt(int idx) const;
TextFormatFlags formatAt(int idx) const;
size_t size() const;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/classic/inputwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ std::pair<int, int> InputWindow::update(InputContext *inputContext) {
labelText = instance->outputFilter(inputContext, labelText);
setTextToMultilineLayout(inputContext, labelLayouts_[localIndex],
labelText);
auto candidateText =
instance->outputFilter(inputContext, candidate.text());
auto candidateText = instance->outputFilter(
inputContext, candidate.textWithComment());
setTextToMultilineLayout(
inputContext, candidateLayouts_[localIndex], candidateText);
localIndex++;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/kimpanel/kimpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ void Kimpanel::updateInputPanel(InputContext *inputContext) {

labelText = instance->outputFilter(inputContext, labelText);
labels.push_back(labelText.toString());
auto candidateText =
instance->outputFilter(inputContext, candidate.text());
auto candidateText = instance->outputFilter(
inputContext, candidate.textWithComment());
texts.push_back(candidateText.toString());
attrs.emplace_back("");
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/virtualkeyboard/virtualkeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ std::vector<std::string> VirtualKeyboard::makeCandidateTextList(
}

auto candidateText =
instance_->outputFilter(inputContext, candidate.text());
instance_->outputFilter(inputContext, candidate.textWithComment());
candidateTextList.push_back(candidateText.toString());
}

Expand All @@ -446,7 +446,7 @@ std::vector<std::string> VirtualKeyboard::makeBulkCandidateTextList(
continue;
}

candidateText = candidate.text();
candidateText = candidate.textWithComment();
} catch (...) {
break;
}
Expand Down
14 changes: 12 additions & 2 deletions test/testcandidatelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ int selected = 0;

class TestCandidateWord : public CandidateWord {
public:
TestCandidateWord(int number)
: CandidateWord(Text(std::to_string(number))), number_(number) {}
TestCandidateWord(int number, Text comment = {})
: CandidateWord(Text(std::to_string(number))), number_(number) {
setComment(std::move(comment));
}
void select(InputContext *) const override { selected = number_; }

private:
Expand Down Expand Up @@ -303,9 +305,17 @@ void test_label() {
FCITX_ASSERT(candidatelist.label(9).toString() == ",. ");
}

void test_comment() {
TestCandidateWord candidate(1, Text("comment"));
FCITX_ASSERT(candidate.text().toString() == "1");
FCITX_ASSERT(candidate.comment().toString() == "comment");
FCITX_ASSERT(candidate.textWithComment().toString() == "1 comment");
}

int main() {
test_basic();
test_faulty_placeholder();
test_label();
test_comment();
return 0;
}
9 changes: 9 additions & 0 deletions test/testtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#include <fcitx-utils/log.h>
#include <fcitx/text.h>
#include <fcitx-utils/textformatflags.h>
using namespace fcitx;

void test_basic() {
Expand Down Expand Up @@ -33,6 +34,14 @@ void test_basic() {
FCITX_ASSERT(lines[2].toStringForCommit() == "");
FCITX_ASSERT(lines[3].toStringForCommit() == "Z");
FCITX_ASSERT(lines[4].toStringForCommit() == "1");

text = Text("ABC");
Text another = Text("DEF", TextFormatFlag::Bold);
another.append("GHI", TextFormatFlag::Italic);
text.append(another);
FCITX_ASSERT(text.toString() == "ABCDEFGHI");
FCITX_ASSERT(text.formatAt(1) == TextFormatFlag::Bold);
FCITX_ASSERT(text.formatAt(2) == TextFormatFlag::Italic);
}

void test_normalize() {
Expand Down
Loading