Skip to content

Commit

Permalink
Add new APIs, peek selection and change page
Browse files Browse the repository at this point in the history
Context:Peek
  • Loading branch information
LEOYoon-Tsaw committed Feb 20, 2023
1 parent d0d227c commit 9aa83e6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/rime/context.cc
Expand Up @@ -110,17 +110,33 @@ void Context::Clear() {
}

bool Context::Select(size_t index) {
if (composition_.empty())
bool result = Peek(index);
if (result)
select_notifier_(this);
return result;
}

bool Context::Peek(size_t index) {
if (composition_.empty() || !composition_.back().menu)
return false;
Segment& seg(composition_.back());
if (auto cand = seg.GetCandidateAt(index)) {
seg.selected_index = index;
seg.status = Segment::kSelected;
DLOG(INFO) << "Selected: '" << cand->text() << "', index = " << index;
select_notifier_(this);
return true;
size_t new_index = index;
if (index < 0) {
DLOG(INFO) << "selection index < 0, fallback to 0";
new_index = 0;
} else {
size_t candidate_count = seg.menu->Prepare(index + 1);
if (index >= candidate_count) {
DLOG(INFO) << "selection index exceed candidate pool, fallback to last";
new_index = candidate_count - 1;
}
}
return false;
size_t previous_index = seg.selected_index;
seg.selected_index = new_index;
update_notifier_(this);

DLOG(INFO) << "Selection changed from: " << previous_index << " to: " << new_index;
return true;
}

bool Context::DeleteCandidate(
Expand Down
1 change: 1 addition & 0 deletions src/rime/context.h
Expand Up @@ -45,6 +45,7 @@ class Context {

// return false if there is no candidate at index
bool Select(size_t index);
bool Peek(size_t index);
bool DeleteCandidate(size_t index);
// return false if there's no candidate for current segment
bool ConfirmCurrentSelection();
Expand Down
35 changes: 35 additions & 0 deletions src/rime_api.cc
Expand Up @@ -970,11 +970,43 @@ static bool do_with_candidate_on_current_page(
return (ctx->*verb)(page_start + index);
}

Bool RimeChangePage(RimeSessionId session_id, Bool previous) {
an<Session> session(Service::instance().GetSession(session_id));
if (!session)
return False;
Context *ctx = session->context();
if (!ctx || !ctx->HasMenu())
return False;
Schema *schema = session->schema();
if (!schema)
return False;
size_t page_size = (size_t)schema->page_size();
const auto& seg(ctx->composition().back());
size_t selected_index = seg.selected_index;
ctx->composition().back().tags.insert("paging");
if (previous) {
size_t index = selected_index <= page_size ? 0 : selected_index - page_size;
DLOG(INFO) << "Current selection: " << selected_index << ", Previous page, Peek at " << index;
return ctx->Peek(index);
} else {
size_t index = selected_index + page_size;
DLOG(INFO) << "Current selection: " << selected_index << ", Next page, Peek at " << index;
return ctx->Peek(index);
}
}

Bool RimePeekCandidate(RimeSessionId session_id, size_t index) {
return do_with_candidate(session_id, index, &Context::Peek);
}

Bool RimeSelectCandidate(RimeSessionId session_id, size_t index) {
return do_with_candidate(session_id, index, &Context::Select);
}

Bool RimePeekCandidateOnCurrentPage(RimeSessionId session_id, size_t index) {
return do_with_candidate_on_current_page(session_id, index, &Context::Peek);
}

Bool RimeSelectCandidateOnCurrentPage(RimeSessionId session_id, size_t index) {
return do_with_candidate_on_current_page(session_id, index, &Context::Select);
}
Expand Down Expand Up @@ -1116,6 +1148,9 @@ RIME_API RimeApi* rime_get_api() {
s_api.get_state_label = &RimeGetStateLabel;
s_api.delete_candidate = &RimeDeleteCandidate;
s_api.delete_candidate_on_current_page = &RimeDeleteCandidateOnCurrentPage;
s_api.peek_candidate = &RimePeekCandidate;
s_api.peek_candidate_on_current_page = &RimePeekCandidateOnCurrentPage;
s_api.change_page = &RimeChangePage;
s_api.get_state_label_abbreviated = &RimeGetStateLabelAbbreviated;
}
return &s_api;
Expand Down
6 changes: 6 additions & 0 deletions src/rime_api.h
Expand Up @@ -524,6 +524,8 @@ typedef struct rime_api_t {

//! select a candidate at the given index in candidate list.
Bool (*select_candidate)(RimeSessionId session_id, size_t index);
//! peek a selection without commiting to it
Bool (*peek_candidate)(RimeSessionId session_id, size_t index);

//! get the version of librime
const char* (*get_version)(void);
Expand All @@ -533,6 +535,10 @@ typedef struct rime_api_t {

//! select a candidate from current page.
Bool (*select_candidate_on_current_page)(RimeSessionId session_id, size_t index);
//! peek a selection without commiting to it
Bool (*peek_candidate_on_current_page)(RimeSessionId session_id, size_t index);

Bool (*change_page)(RimeSessionId session_id, Bool previous);

//! access candidate list.
Bool (*candidate_list_begin)(RimeSessionId session_id, RimeCandidateListIterator* iterator);
Expand Down

0 comments on commit 9aa83e6

Please sign in to comment.