Skip to content

Commit

Permalink
Merge pull request #148 from zonble/master
Browse files Browse the repository at this point in the history
Adds the shortcut to input enclosing numbers, also maps keys on the num pad.
  • Loading branch information
zonble authored Aug 1, 2024
2 parents 806528c + 1fb8596 commit b3581b5
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 45 deletions.
39 changes: 5 additions & 34 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,12 @@ name: CI
on: [push, pull_request]

jobs:
build_ubuntu_24:
build_ubuntu:
strategy:
matrix:
os: ["ubuntu:22.04", "ubuntu:24.04"]
runs-on: ubuntu-latest
container: ubuntu:24.04

steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt install -y pkg-config
apt install -y clang
apt install -y cmake extra-cmake-modules gettext libfmt-dev
apt install -y fcitx5 libfcitx5core-dev libfcitx5config-dev libfcitx5utils-dev fcitx5-modules-dev
apt install -y libjson-c-dev
- name: Build
run: |
mkdir -p build
cd build
cmake ../ -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug
make -j
cd ../
mkdir -p src/Engine/build
cd src/Engine/build
cmake ../
make -j
- name: Test
run: |
cd build
ctest --output-on-failure
build_ubuntu_22:
runs-on: ubuntu-latest
container: ubuntu:22.04

container: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Install dependencies
Expand Down
10 changes: 10 additions & 0 deletions src/InputState.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ struct AssociatedPhrasesPlain : InputState {
const std::vector<ChoosingCandidate::Candidate> candidates;
};

struct EnclosingNumber : InputState {
EnclosingNumber(std::string number = "") : number(std::move(number)) {}
EnclosingNumber(EnclosingNumber const& number) : number(number.number) {}
std::string composingBuffer() const { return "[標題數字] " + number; }
std::string number;
};

struct ChineseNumber : InputState {
ChineseNumber(std::string number, ChineseNumberStyle style)
: number(std::move(number)), style(style) {}
Expand Down Expand Up @@ -286,6 +293,9 @@ struct SelectingFeature : InputState {
features.emplace_back("日期與時間", [this]() {
return std::make_unique<SelectingDateMacro>(this->converter);
});
features.emplace_back("標題數字", []() {
return std::make_unique<EnclosingNumber>();
});
features.emplace_back("中文數字", []() {
return std::make_unique<ChineseNumber>("", ChineseNumberStyle::LOWER);
});
Expand Down
17 changes: 11 additions & 6 deletions src/Key.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,24 @@ struct Key {
// set, since `ascii` alone is not sufficient to represent the key.
const bool shiftPressed;
const bool ctrlPressed;
const bool isFromNumberPad;

explicit Key(char c = 0, KeyName n = KeyName::UNKNOWN, bool isShift = false,
bool isCtrl = false)
: ascii(c), name(n), shiftPressed(isShift), ctrlPressed(isCtrl) {}
bool isCtrl = false, bool isFromNumberPad = false)
: ascii(c),
name(n),
shiftPressed(isShift),
ctrlPressed(isCtrl),
isFromNumberPad(isFromNumberPad) {}

static Key asciiKey(char c, bool shiftPressed = false,
bool ctrlPressed = false) {
return Key(c, KeyName::ASCII, shiftPressed, ctrlPressed);
bool ctrlPressed = false, bool isFromNumberPad = false) {
return Key(c, KeyName::ASCII, shiftPressed, ctrlPressed, isFromNumberPad);
}

static Key namedKey(KeyName name, bool shiftPressed = false,
bool ctrlPressed = false) {
return Key(0, name, shiftPressed, ctrlPressed);
bool ctrlPressed = false, bool isFromNumberPad = false) {
return Key(0, name, shiftPressed, ctrlPressed, isFromNumberPad);
}

// Regardless of the shift state.
Expand Down
75 changes: 74 additions & 1 deletion src/KeyHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,17 @@ bool KeyHandler::handle(Key key, McBopomofo::InputState* state,
errorCallback);
}

auto* enclosingNumber = dynamic_cast<InputStates::EnclosingNumber*>(state);
if (enclosingNumber != nullptr) {
return handleEnclosingNumber(key, enclosingNumber, stateCallback,
errorCallback);
}

// From Key's definition, if shiftPressed is true, it can't be a simple key
// that can be represented by ASCII.
char simpleAscii = (key.ctrlPressed || key.shiftPressed) ? '\0' : key.ascii;
char simpleAscii =
(key.ctrlPressed || key.shiftPressed || key.isFromNumberPad) ? '\0'
: key.ascii;

// See if it's valid BPMF reading.
bool keyConsumedByReading = false;
Expand Down Expand Up @@ -1056,6 +1064,71 @@ bool KeyHandler::handleChineseNumber(
return true;
}

bool KeyHandler::handleEnclosingNumber(
Key key, McBopomofo::InputStates::EnclosingNumber* state,
StateCallback stateCallback, KeyHandler::ErrorCallback errorCallback) {
if (key.ascii == Key::ESC) {
stateCallback(std::make_unique<InputStates::EmptyIgnoringPrevious>());
return true;
}
if (key.isDeleteKeys()) {
std::string number = state->number;
if (!number.empty()) {
number = number.substr(0, number.length() - 1);
} else {
errorCallback();
return true;
}
auto newState = std::make_unique<InputStates::EnclosingNumber>(number);
stateCallback(std::move(newState));
return true;
}
if (key.ascii == Key::RETURN || key.ascii == Key::SPACE) {
if (state->number.empty()) {
stateCallback(std::make_unique<InputStates::Empty>());
return true;
}
std::string unigramKey = "_number_" + state->number;
if (!lm_->hasUnigrams(unigramKey)) {
errorCallback();
return true;
}
auto unigrams = lm_->getUnigrams(unigramKey);
if (unigrams.size() == 1) {
auto firstUnigram = unigrams[0];
std::string value = firstUnigram.value();
stateCallback(std::make_unique<InputStates::Committing>(value));
stateCallback(std::make_unique<InputStates::Empty>());
return true;
}

grid_.insertReading(unigramKey);
walk();
size_t originalCursor = grid_.cursor();
if (selectPhraseAfterCursorAsCandidate_) {
grid_.setCursor(originalCursor - 1);
}
auto inputtingState = buildInputtingState();
auto choosingCandidateState =
buildChoosingCandidateState(inputtingState.get(), originalCursor);
stateCallback(std::move(inputtingState));
stateCallback(std::move(choosingCandidateState));
return true;
}
if (key.ascii >= '0' && key.ascii <= '9') {
if (state->number.length() > 2) {
errorCallback();
return true;
}
std::string newNumber = state->number + key.ascii;
auto newState = std::make_unique<InputStates::EnclosingNumber>(newNumber);
stateCallback(std::move(newState));
} else {
errorCallback();
}
return true;
}

#pragma endregion Key_Handling

#pragma region Output
Expand Down
5 changes: 5 additions & 0 deletions src/KeyHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ class KeyHandler {
McBopomofo::InputStates::ChineseNumber* state,
StateCallback stateCallback,
ErrorCallback errorCallback);
bool handleEnclosingNumber(Key key,
McBopomofo::InputStates::EnclosingNumber* state,
StateCallback stateCallback,
ErrorCallback errorCallback);

bool handleTabKey(Key key, McBopomofo::InputState* state,
const StateCallback& stateCallback,
const ErrorCallback& errorCallback);
Expand Down
Loading

0 comments on commit b3581b5

Please sign in to comment.