Skip to content

Commit

Permalink
Update the interface for candidate list.
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Feb 12, 2020
1 parent 30a3cee commit 8bee2b2
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 97 deletions.
8 changes: 3 additions & 5 deletions src/im/keyboard/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void KeyboardEngine::keyEvent(const InputMethodEntry &entry, KeyEvent &event) {
int idx = event.key().keyListIndex(selectionKeys_);
if (idx >= 0 && idx < candList->size()) {
event.filterAndAccept();
candList->candidate(idx)->select(inputContext);
candList->candidate(idx).select(inputContext);
return;
}

Expand Down Expand Up @@ -483,9 +483,7 @@ std::string KeyboardEngine::preeditString(InputContext *inputContext) {
auto candidate = inputContext->inputPanel().candidateList();
std::string preedit;
if (candidate && candidate->cursorIndex() >= 0) {
return candidate->candidate(candidate->cursorIndex())
->text()
.toString();
return candidate->candidate(candidate->cursorIndex()).text().toString();
}
return state->buffer_.userInput();
}
Expand Down Expand Up @@ -530,7 +528,7 @@ void KeyboardEngine::updateCandidate(const InputMethodEntry &entry,
auto candidateList = std::make_unique<CommonCandidateList>();
auto spellType = guessSpellType(state->buffer_.userInput());
for (const auto &result : results) {
candidateList->append(new KeyboardCandidateWord(
candidateList->append(std::make_unique<KeyboardCandidateWord>(
this, Text(formatWord(result, spellType))));
}
candidateList->setSelectionKey(selectionKeys_);
Expand Down
14 changes: 6 additions & 8 deletions src/lib/fcitx-wayland/core/outputinfomation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ OutputInfomation::OutputInfomation(WlOutput *output)
d->next_.model_ = model;
d->next_.transform_ = static_cast<wl_output_transform>(transform);
});
d->modeConnection_ =
output->mode().connect([this](uint32_t flags, int32_t width,
int32_t height, int32_t refresh) {
d->modeConnection_ = output->mode().connect(
[this](uint32_t flags, int32_t width, int32_t height, int32_t refresh) {
if (!(flags & WL_OUTPUT_MODE_CURRENT)) {
return;
}
Expand All @@ -72,11 +71,10 @@ OutputInfomation::OutputInfomation(WlOutput *output)
d->next_.height_ = height;
d->next_.refreshRate_ = refresh;
});
d->scaleConnection_ =
output->scale().connect([this](int32_t scale) {
FCITX_D();
d->next_.scale_ = scale;
});
d->scaleConnection_ = output->scale().connect([this](int32_t scale) {
FCITX_D();
d->next_.scale_ = scale;
});
d->doneConnection_ = output->done().connect([this]() {
FCITX_D();
d->current_ = d->next_;
Expand Down
22 changes: 10 additions & 12 deletions src/lib/fcitx/candidatelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,10 @@ const Text &DisplayOnlyCandidateList::label(int idx) const {
return d->emptyText_;
}

std::shared_ptr<const CandidateWord>
DisplayOnlyCandidateList::candidate(int idx) const {
const CandidateWord &DisplayOnlyCandidateList::candidate(int idx) const {
FCITX_D();
d->checkIndex(idx);
return d->candidateWords_[idx];
return *d->candidateWords_[idx];
}

int DisplayOnlyCandidateList::cursorIndex() const {
Expand All @@ -224,7 +223,7 @@ class CommonCandidateListPrivate {
int pageSize_ = 5;
std::vector<Text> labels_;
// use shared_ptr for type erasure
std::vector<std::shared_ptr<CandidateWord>> candidateWord_;
std::vector<std::unique_ptr<CandidateWord>> candidateWord_;
CandidateLayoutHint layoutHint_ = CandidateLayoutHint::NotSet;
bool cursorIncludeUnselected_ = false;
bool cursorKeepInSamePage_ = false;
Expand Down Expand Up @@ -395,12 +394,11 @@ int CommonCandidateList::totalSize() const {
return d->candidateWord_.size();
}

std::shared_ptr<const CandidateWord>
CommonCandidateList::candidate(int idx) const {
const CandidateWord &CommonCandidateList::candidate(int idx) const {
FCITX_D();
d->checkIndex(idx);
auto globalIndex = d->toGlobalIndex(idx);
return d->candidateWord_[globalIndex];
return *d->candidateWord_[globalIndex];
}

const Text &CommonCandidateList::label(int idx) const {
Expand All @@ -414,14 +412,13 @@ const Text &CommonCandidateList::label(int idx) const {
return d->labels_[idx];
}

void CommonCandidateList::insert(int idx, CandidateWord *word) {
void CommonCandidateList::insert(int idx, std::unique_ptr<CandidateWord> word) {
FCITX_D();
// it's ok to insert at tail
if (idx != static_cast<int>(d->candidateWord_.size())) {
d->checkGlobalIndex(idx);
}
d->candidateWord_.insert(d->candidateWord_.begin() + idx,
std::shared_ptr<CandidateWord>(word));
d->candidateWord_.insert(d->candidateWord_.begin() + idx, std::move(word));
}

void CommonCandidateList::remove(int idx) {
Expand Down Expand Up @@ -550,9 +547,10 @@ void CommonCandidateList::setPage(int page) {
}
}

void CommonCandidateList::replace(int idx, CandidateWord *word) {
void CommonCandidateList::replace(int idx,
std::unique_ptr<CandidateWord> word) {
FCITX_D();
d->candidateWord_[idx].reset(word);
d->candidateWord_[idx] = std::move(word);
}

void CommonCandidateList::fixAfterUpdate() {
Expand Down
18 changes: 10 additions & 8 deletions src/lib/fcitx/candidatelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class FCITXCORE_EXPORT CandidateList {
virtual ~CandidateList();

virtual const Text &label(int idx) const = 0;
virtual std::shared_ptr<const CandidateWord> candidate(int idx) const = 0;
virtual const CandidateWord &candidate(int idx) const = 0;
virtual int size() const = 0;
virtual int cursorIndex() const = 0;
virtual CandidateLayoutHint layoutHint() const = 0;
Expand Down Expand Up @@ -121,12 +121,14 @@ class FCITXCORE_EXPORT BulkCandidateList {
class FCITXCORE_EXPORT ModifiableCandidateList : public BulkCandidateList {
public:
// All index used there are global index
virtual void insert(int idx, CandidateWord *word) = 0;
virtual void insert(int idx, std::unique_ptr<CandidateWord> word) = 0;
virtual void remove(int idx) = 0;
virtual void replace(int idx, CandidateWord *word) = 0;
virtual void replace(int idx, std::unique_ptr<CandidateWord> word) = 0;
virtual void move(int from, int to) = 0;

void append(CandidateWord *word) { insert(totalSize(), word); }
void append(std::unique_ptr<CandidateWord> word) {
insert(totalSize(), std::move(word));
}
};

class DisplayOnlyCandidateListPrivate;
Expand All @@ -143,7 +145,7 @@ class FCITXCORE_EXPORT DisplayOnlyCandidateList : public CandidateList {

// CandidateList
const fcitx::Text &label(int idx) const override;
std::shared_ptr<const CandidateWord> candidate(int idx) const override;
const CandidateWord &candidate(int idx) const override;
int cursorIndex() const override;
int size() const override;
CandidateLayoutHint layoutHint() const override;
Expand Down Expand Up @@ -173,7 +175,7 @@ class FCITXCORE_EXPORT CommonCandidateList : public CandidateList,

// CandidateList
const fcitx::Text &label(int idx) const override;
std::shared_ptr<const CandidateWord> candidate(int idx) const override;
const CandidateWord &candidate(int idx) const override;
int cursorIndex() const override;
int size() const override;

Expand All @@ -196,9 +198,9 @@ class FCITXCORE_EXPORT CommonCandidateList : public CandidateList,
int totalSize() const override;

// ModifiableCandidateList
void insert(int idx, CandidateWord *word) override;
void insert(int idx, std::unique_ptr<CandidateWord> word) override;
void remove(int idx) override;
void replace(int idx, CandidateWord *word) override;
void replace(int idx, std::unique_ptr<CandidateWord> word) override;
void move(int from, int to) override;

// CursorMovableCandidateList
Expand Down
5 changes: 3 additions & 2 deletions src/lib/fcitx/icontheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,9 @@ class IconThemePrivate : QPtrHolder<IconTheme> {
std::string lookupIcon(const std::string &iconname, int size, int scale,
const std::vector<std::string> &extensions) const {

auto checkDirectory = [&extensions, &iconname](const IconThemeDirectory &directory,
std::string baseDir) -> std::string {
auto checkDirectory = [&extensions,
&iconname](const IconThemeDirectory &directory,
std::string baseDir) -> std::string {
baseDir = stringutils::joinPath(baseDir, directory.path());
if (!fs::isdir(baseDir)) {
return {};
Expand Down
8 changes: 4 additions & 4 deletions src/lib/fcitx/misc_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ parseLayout(const std::string &layout) {
return {layout.substr(0, pos), layout.substr(pos + 1)};
}

static inline std::shared_ptr<const CandidateWord>
static inline const CandidateWord *
nthCandidateIgnorePlaceholder(const CandidateList &candidateList, int idx) {
int total = 0;
if (idx < 0 || idx >= candidateList.size()) {
return nullptr;
}
for (int i = 0, e = candidateList.size(); i < e; i++) {
auto candidate = candidateList.candidate(i);
if (candidate->isPlaceHolder()) {
auto &candidate = candidateList.candidate(i);
if (candidate.isPlaceHolder()) {
continue;
}
if (idx == total) {
return candidate;
return &candidate;
}
++total;
}
Expand Down
13 changes: 8 additions & 5 deletions src/modules/clipboard/clipboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ Clipboard::Clipboard(Instance *instance)
if (idx >= 0) {
keyEvent.accept();
if (idx < candidateList->size()) {
candidateList->candidate(idx)->select(inputContext);
candidateList->candidate(idx).select(inputContext);
}
return;
}
if (keyEvent.key().check(FcitxKey_space)) {
keyEvent.accept();
if (candidateList->size() > 0) {
candidateList->candidate(0)->select(inputContext);
candidateList->candidate(0).select(inputContext);
}
return;
}
Expand Down Expand Up @@ -265,7 +265,8 @@ void Clipboard::updateUI(InputContext *inputContext) {
// Append first item from history_.
auto iter = history_.begin();
if (iter != history_.end()) {
candidateList->append(new ClipboardCandidateWord(this, *iter));
candidateList->append(
std::make_unique<ClipboardCandidateWord>(this, *iter));
iter++;
}
// Append primary_, but check duplication first.
Expand All @@ -278,15 +279,17 @@ void Clipboard::updateUI(InputContext *inputContext) {
}
}
if (!dup) {
candidateList->append(new ClipboardCandidateWord(this, primary_));
candidateList->append(
std::make_unique<ClipboardCandidateWord>(this, primary_));
}
}
// If primary_ is appended, it might squeeze one space out.
for (; iter != history_.end(); iter++) {
if (candidateList->totalSize() >= config_.numOfEntries.value()) {
break;
}
candidateList->append(new ClipboardCandidateWord(this, *iter));
candidateList->append(
std::make_unique<ClipboardCandidateWord>(this, *iter));
}
candidateList->setSelectionKey(selectionKeys_);
candidateList->setLayoutHint(CandidateLayoutHint::Vertical);
Expand Down
5 changes: 2 additions & 3 deletions src/modules/notificationitem/notificationitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ class StatusNotifierItem : public dbus::ObjectVTable<StatusNotifierItem> {
[this]() { return tooltip(); });
FCITX_OBJECT_VTABLE_PROPERTY(iconThemePath, "IconThemePath", "s",
[]() { return ""; });
FCITX_OBJECT_VTABLE_PROPERTY(menu, "Menu", "o", []() {
return dbus::ObjectPath("/MenuBar");
});
FCITX_OBJECT_VTABLE_PROPERTY(menu, "Menu", "o",
[]() { return dbus::ObjectPath("/MenuBar"); });
FCITX_OBJECT_VTABLE_PROPERTY(xayatanaLabel, "XAyatanaLabel", "s",
[]() { return ""; });
FCITX_OBJECT_VTABLE_PROPERTY(XAyatanaLabelGuide, "XAyatanaLabelGuide", "s",
Expand Down
8 changes: 4 additions & 4 deletions src/modules/quickphrase/quickphrase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ QuickPhrase::QuickPhrase(Instance *instance)
if (idx >= 0) {
keyEvent.accept();
if (idx < candidateList->size()) {
candidateList->candidate(idx)->select(inputContext);
candidateList->candidate(idx).select(inputContext);
}
return;
}

if (keyEvent.key().check(FcitxKey_space) &&
candidateList->size()) {
keyEvent.accept();
candidateList->candidate(0)->select(inputContext);
candidateList->candidate(0).select(inputContext);
return;
}

Expand Down Expand Up @@ -265,8 +265,8 @@ void QuickPhrase::updateUI(InputContext *inputContext) {
text.append(start->second);
text.append(" ");
text.append(start->first.substr(state->buffer_.userInput().size()));
candidateList->append(
new QuickPhraseCandidateWord(this, std::move(text)));
candidateList->append(std::make_unique<QuickPhraseCandidateWord>(
this, std::move(text)));
}
candidateList->setSelectionKey(selectionKeys_);
inputContext->inputPanel().setCandidateList(std::move(candidateList));
Expand Down
5 changes: 3 additions & 2 deletions src/modules/unicode/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Unicode::Unicode(Instance *instance)
if (idx >= 0) {
keyEvent.accept();
if (idx < candidateList->size()) {
candidateList->candidate(idx)->select(inputContext);
candidateList->candidate(idx).select(inputContext);
}
return;
}
Expand Down Expand Up @@ -227,7 +227,8 @@ void Unicode::updateUI(InputContext *inputContext) {
candidateList->setPageSize(instance_->globalConfig().defaultPageSize());
for (auto c : result) {
if (utf8::UCS4IsValid(c)) {
candidateList->append(new UnicodeCandidateWord(this, c));
candidateList->append(
std::make_unique<UnicodeCandidateWord>(this, c));
}
}
candidateList->setSelectionKey(selectionKeys_);
Expand Down
14 changes: 7 additions & 7 deletions src/ui/classic/inputwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ void InputWindow::update(InputContext *inputContext) {
int count = 0;

for (int i = 0, e = candidateList->size(); i < e; i++) {
auto candidate = candidateList->candidate(i);
if (candidate->isPlaceHolder()) {
auto &candidate = candidateList->candidate(i);
if (candidate.isPlaceHolder()) {
continue;
}
count++;
Expand All @@ -199,14 +199,14 @@ void InputWindow::update(InputContext *inputContext) {

int localIndex = 0;
for (int i = 0, e = candidateList->size(); i < e; i++) {
auto candidate = candidateList->candidate(i);
auto &candidate = candidateList->candidate(i);
// Skip placeholder.
if (candidate->isPlaceHolder()) {
if (candidate.isPlaceHolder()) {
continue;
}

Text labelText = candidate->hasCustomLabel()
? candidate->customLabel()
Text labelText = candidate.hasCustomLabel()
? candidate.customLabel()
: candidateList->label(i);

labelText = instance->outputFilter(inputContext, labelText);
Expand All @@ -215,7 +215,7 @@ void InputWindow::update(InputContext *inputContext) {
highlightLabelAttrLists_[localIndex].get(),
{labelText});
auto candidateText =
instance->outputFilter(inputContext, candidate->text());
instance->outputFilter(inputContext, candidate.text());
setTextToLayout(candidateLayouts_[localIndex].get(),
candidateAttrLists_[localIndex].get(),
highlightCandidateAttrLists_[localIndex].get(),
Expand Down
9 changes: 4 additions & 5 deletions src/ui/classic/xcbmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,10 @@ XCBMenu *MenuPool::findOrCreateMenu(XCBUI *ui, Menu *menu) {
return &iter->second.first;
}

ScopedConnection conn =
menu->connect<ObjectDestroyed>([this](void *data) {
Menu *menu = static_cast<Menu *>(data);
pool_.erase(menu);
});
ScopedConnection conn = menu->connect<ObjectDestroyed>([this](void *data) {
Menu *menu = static_cast<Menu *>(data);
pool_.erase(menu);
});

auto result = pool_.emplace(
std::piecewise_construct, std::forward_as_tuple(menu),
Expand Down
Loading

0 comments on commit 8bee2b2

Please sign in to comment.