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 new APIs, peek selection and change page #620

Closed
wants to merge 5 commits into from
Closed
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
34 changes: 26 additions & 8 deletions src/rime/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,35 @@ void Context::Clear() {
}

bool Context::Select(size_t index) {
if (composition_.empty())
bool result = Peek(index);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback mechanism of Peek does not work for Select. See my other comment below.

Also, this creates a difference in the signaled notifications: an extra update_notifier_ is triggered before select_notifier_.

I think we'd better keep the two functions separate.

if (result) {
composition_.back().status = Segment::kSelected;
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index < 0 does not happen with size_t.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback feature is convenient in highlighting the candidate of the same label number in the next page.

But when called by Select(index), the given index means a definite candidate, and fallback is unexpected.

}
}
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -1117,6 +1149,9 @@ RIME_API RimeApi* rime_get_api() {
s_api.delete_candidate = &RimeDeleteCandidate;
s_api.delete_candidate_on_current_page = &RimeDeleteCandidateOnCurrentPage;
s_api.get_state_label_abbreviated = &RimeGetStateLabelAbbreviated;
s_api.peek_candidate = &RimePeekCandidate;
s_api.peek_candidate_on_current_page = &RimePeekCandidateOnCurrentPage;
s_api.change_page = &RimeChangePage;
}
return &s_api;
}
7 changes: 7 additions & 0 deletions src/rime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ typedef struct rime_api_t {
const char *option_name,
Bool state,
Bool abbreviated);

//! peek a selection without commiting to it
Bool (*peek_candidate)(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);
} RimeApi;

//! API entry
Expand Down
2 changes: 1 addition & 1 deletion xcode.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif
export SDKROOT ?= $(shell xcrun --sdk macosx --show-sdk-path)

# https://cmake.org/cmake/help/latest/envvar/MACOSX_DEPLOYMENT_TARGET.html
export MACOSX_DEPLOYMENT_TARGET ?= $$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)
export MACOSX_DEPLOYMENT_TARGET ?= $(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)

ifdef BUILD_UNIVERSAL
# https://cmake.org/cmake/help/latest/envvar/CMAKE_OSX_ARCHITECTURES.html
Expand Down