|
33 | 33 |
|
34 | 34 | #include "Big5Utils/Big5Utils.h" |
35 | 35 | #include "BopomofoBraille/Converter.h" |
| 36 | +#include "IcuTransformInputHelper.h" |
| 37 | +#include "Log.h" |
36 | 38 | #include "NumberInputHelper.h" |
37 | 39 | #include "UTF8Helper.h" |
38 | 40 |
|
@@ -137,11 +139,6 @@ bool KeyHandler::handle(Key key, McBopomofo::InputState* state, |
137 | 139 | return handleBig5(key, big5, stateCallback, errorCallback); |
138 | 140 | } |
139 | 141 |
|
140 | | - auto* iroha = dynamic_cast<InputStates::Iroha*>(state); |
141 | | - if (iroha != nullptr) { |
142 | | - return handleIroha(key, iroha, stateCallback, errorCallback); |
143 | | - } |
144 | | - |
145 | 142 | // From Key's definition, if shiftPressed is true, it can't be a simple key |
146 | 143 | // that can be represented by ASCII. |
147 | 144 | char simpleAscii = |
@@ -1234,6 +1231,61 @@ bool KeyHandler::handleNumberInput(Key key, |
1234 | 1231 | return true; |
1235 | 1232 | } |
1236 | 1233 |
|
| 1234 | +bool KeyHandler::handleIcuTransformInput(Key key, |
| 1235 | + McBopomofo::InputStates::IcuTransformInput* state, |
| 1236 | + StateCallback stateCallback, |
| 1237 | + KeyHandler::ErrorCallback errorCallback) { |
| 1238 | + if (key.ascii == Key::ESC) { |
| 1239 | + stateCallback(std::make_unique<InputStates::EmptyIgnoringPrevious>()); |
| 1240 | + return true; |
| 1241 | + } |
| 1242 | + if (key.isDeleteKeys()) { |
| 1243 | + std::string string = state->string; |
| 1244 | + if (!string.empty()) { |
| 1245 | + string = string.substr(0, string.length() - 1); |
| 1246 | + auto candidates = |
| 1247 | + IcuTransformInputHelper::FillCandidatesWithString(string); |
| 1248 | + auto newState = |
| 1249 | + std::make_unique<InputStates::IcuTransformInput>(string, candidates); |
| 1250 | + stateCallback(std::move(newState)); |
| 1251 | + return true; |
| 1252 | + } else { |
| 1253 | + errorCallback(); |
| 1254 | + return true; |
| 1255 | + } |
| 1256 | + } |
| 1257 | + |
| 1258 | + char selectionKeys[10] = {'!', '@', '#', '$', '%', '^', '&', '*', '(', ')'}; |
| 1259 | + for (size_t i = 0; i < state->candidates.size() && i < 10; ++i) { |
| 1260 | + if (key.ascii == selectionKeys[i]) { |
| 1261 | + return false; |
| 1262 | + } |
| 1263 | + } |
| 1264 | + |
| 1265 | + if (isprint(key.ascii)) { |
| 1266 | + if (state->string.length() > 100) { |
| 1267 | + errorCallback(); |
| 1268 | + return true; |
| 1269 | + } |
| 1270 | + std::string newString = state->string + key.ascii; |
| 1271 | + auto candidates = |
| 1272 | + IcuTransformInputHelper::FillCandidatesWithString(newString); |
| 1273 | + auto newState = |
| 1274 | + std::make_unique<InputStates::IcuTransformInput>(newString, candidates); |
| 1275 | + stateCallback(std::move(newState)); |
| 1276 | + return true; |
| 1277 | + } else if (!state->candidates.empty()) { |
| 1278 | + // If the candidate panel is visible, let it handle the key. |
| 1279 | + return false; |
| 1280 | + } |
| 1281 | + |
| 1282 | + // If the buffer is empty, all other keys (e.g. cursor keys) exit the state. |
| 1283 | + stateCallback(std::make_unique<InputStates::EmptyIgnoringPrevious>()); |
| 1284 | + return true; |
| 1285 | +} |
| 1286 | + |
| 1287 | + |
| 1288 | + |
1237 | 1289 | bool KeyHandler::handleBig5(Key key, McBopomofo::InputStates::Big5* state, |
1238 | 1290 | StateCallback stateCallback, |
1239 | 1291 | KeyHandler::ErrorCallback errorCallback) { |
@@ -1279,78 +1331,6 @@ bool KeyHandler::handleBig5(Key key, McBopomofo::InputStates::Big5* state, |
1279 | 1331 | return true; |
1280 | 1332 | } |
1281 | 1333 |
|
1282 | | -bool KeyHandler::handleIroha(Key key, McBopomofo::InputStates::Iroha* state, |
1283 | | - StateCallback stateCallback, |
1284 | | - KeyHandler::ErrorCallback errorCallback) { |
1285 | | - if (key.ascii == Key::ESC) { |
1286 | | - stateCallback(std::make_unique<InputStates::EmptyIgnoringPrevious>()); |
1287 | | - return true; |
1288 | | - } |
1289 | | - |
1290 | | - if (key.ascii == Key::RETURN || key.ascii == Key::SPACE) { |
1291 | | - std::string code = state->code; |
1292 | | - if (code.empty()) { |
1293 | | - stateCallback(std::make_unique<InputStates::EmptyIgnoringPrevious>()); |
1294 | | - return true; |
1295 | | - } |
1296 | | - |
1297 | | - std::string unigram = "_kana_" + code; |
1298 | | - if (lm_->hasUnigrams(unigram)) { |
1299 | | - auto unigrams = lm_->getUnigrams(unigram); |
1300 | | - if (unigrams.size() == 1) { |
1301 | | - std::string value = unigrams[0].value(); |
1302 | | - auto seq = std::make_unique<InputStates::StateSequence>(); |
1303 | | - seq->push_back(std::make_unique<InputStates::Committing>(value)); |
1304 | | - seq->push_back(std::make_unique<InputStates::Iroha>("")); |
1305 | | - stateCallback(std::move(seq)); |
1306 | | - } else { |
1307 | | - std::vector<std::string> candidates; |
1308 | | - for (const auto& unigram : unigrams) { |
1309 | | - candidates.emplace_back(unigram.value()); |
1310 | | - } |
1311 | | - auto newState = |
1312 | | - std::make_unique<InputStates::IrohaCandidate>(code, candidates); |
1313 | | - stateCallback(std::move(newState)); |
1314 | | - } |
1315 | | - return true; |
1316 | | - } else { |
1317 | | - errorCallback(); |
1318 | | - auto newState = std::make_unique<InputStates::Iroha>(""); |
1319 | | - stateCallback(std::move(newState)); |
1320 | | - return true; |
1321 | | - } |
1322 | | - } |
1323 | | - |
1324 | | - if (key.isDeleteKeys()) { |
1325 | | - std::string code = state->code; |
1326 | | - if (!code.empty()) { |
1327 | | - code = code.substr(0, code.length() - 1); |
1328 | | - auto newState = std::make_unique<InputStates::Iroha>(code); |
1329 | | - stateCallback(std::move(newState)); |
1330 | | - } else { |
1331 | | - stateCallback(std::make_unique<InputStates::EmptyIgnoringPrevious>()); |
1332 | | - } |
1333 | | - return true; |
1334 | | - } |
1335 | | - |
1336 | | - if ((key.ascii >= 'a' && key.ascii <= 'z') || |
1337 | | - (key.ascii >= 'A' && key.ascii <= 'Z')) { |
1338 | | - std::string code = state->code; |
1339 | | - if (code.length() <= 4) // Iroha code is 4 hex digits. |
1340 | | - { |
1341 | | - std::string code = |
1342 | | - state->code + static_cast<char>(std::tolower(key.ascii)); |
1343 | | - auto newState = std::make_unique<InputStates::Iroha>(code); |
1344 | | - stateCallback(std::move(newState)); |
1345 | | - } else { |
1346 | | - errorCallback(); |
1347 | | - } |
1348 | | - } else { |
1349 | | - errorCallback(); |
1350 | | - } |
1351 | | - return true; |
1352 | | -} |
1353 | | - |
1354 | 1334 | #pragma endregion Key_Handling |
1355 | 1335 |
|
1356 | 1336 | #pragma region Output |
|
0 commit comments