Keyboard: add Chinese pinyin input-method (#9843)

reviewable/pr9857/r1
weijiuqiao 1 year ago committed by GitHub
parent cc53ceb039
commit 02c9f26f6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -318,6 +318,10 @@ function IME:tweak_case(new_candi, old_imex, new_stroke_upper)
end
end
function IME:hasCandidates()
return #(_stack[#_stack].candi) > 0
end
function IME:wrappedDelChar(inputbox)
local imex = _stack[#_stack]
-- stepped deletion
@ -364,6 +368,24 @@ function IME:wrappedAddChars(inputbox, char, orig_char)
return
end
self:refreshHintChars(inputbox)
elseif char == self.switch_char_prev then
if self.W and imex.code:find(self.W) then
if #imex.candi == 0 then
return
elseif imex.index <= self.last_index + 1 then
return
else
imex.index = imex.index - 1
imex.char = imex.candi[imex.index - self.last_index]
end
elseif #imex.candi > 1 then
imex.index = math.max(imex.index-1, 1)
local remainder = imex.index % #imex.candi
imex.char = imex.candi[remainder==0 and #imex.candi or remainder]
else
return
end
self:refreshHintChars(inputbox)
elseif char == self.separator or
_stack[1].code ~= "" and self.partial_separators and util.arrayContains(self.partial_separators, char) then
self:separate(inputbox)

@ -0,0 +1,159 @@
local IME = require("frontend/ui/data/keyboardlayouts/generic_ime")
local util = require("util")
local _ = require("gettext")
-- Start with the english keyboard layout (deep copy, to not alter it)
local py_keyboard = require("util").tableDeepCopy(require("ui/data/keyboardlayouts/en_keyboard"))
local SETTING_NAME = "keyboard_chinese_pinyin_settings"
local code_map = require("frontend/ui/data/keyboardlayouts/zh_pinyin_data")
local settings = G_reader_settings:readSetting(SETTING_NAME, {show_candi=true})
local ime = IME:new {
code_map = code_map,
partial_separators = {" "},
show_candi_callback = function()
return settings.show_candi
end,
switch_char = "",
switch_char_prev = "",
}
py_keyboard.keys[4][3][2].alt_label = nil
py_keyboard.keys[4][3][1].alt_label = nil
py_keyboard.keys[3][10][2] = {
"",
north = "",
alt_label = "",
northeast = "",
northwest = "",
east = "",
west = "",
south = ",",
southeast = "",
southwest = "",
"{",
"[",
";"
}
py_keyboard.keys[5][3][2] = {
"",
north = "",
alt_label = "",
northeast = "",
northwest = "",
east = "",
west = "",
south = ".",
southeast = "",
southwest = "",
"}",
"]",
":"
}
py_keyboard.keys[1][2][3] = { alt_label = "", north = "", "" }
py_keyboard.keys[1][3][3] = { alt_label = "", north = "", "" }
py_keyboard.keys[1][1][4] = { alt_label = "!", north = "!", ""}
py_keyboard.keys[2][1][4] = { alt_label = "?", north = "?", ""}
py_keyboard.keys[1][2][4] = ""
py_keyboard.keys[2][2][4] = "——"
py_keyboard.keys[1][4][3] = { alt_label = "", north = "", "" }
py_keyboard.keys[1][5][3] = { alt_label = "", north = "", "" }
py_keyboard.keys[1][4][4] = { alt_label = "¥", north = "¥", "_" }
py_keyboard.keys[3][3][4] = ""
py_keyboard.keys[3][4][4] = ""
py_keyboard.keys[4][4][3] = ""
py_keyboard.keys[4][5][3] = ""
local genMenuItems = function(self)
return {
{
text = _("Show character candidates"),
checked_func = function()
return settings.show_candi
end,
callback = function()
settings.show_candi = not settings.show_candi
G_reader_settings:saveSetting(SETTING_NAME, settings)
end
}
}
end
local wrappedAddChars = function(inputbox, char)
ime:wrappedAddChars(inputbox, char)
end
local wrappedRightChar = function(inputbox)
if ime:hasCandidates() then
ime:wrappedAddChars(inputbox, "")
else
ime:separate(inputbox)
inputbox.rightChar:raw_method_call()
end
end
local wrappedLeftChar = function(inputbox)
if ime:hasCandidates() then
ime:wrappedAddChars(inputbox, "")
else
ime:separate(inputbox)
inputbox.leftChar:raw_method_call()
end
end
local function separate(inputbox)
ime:separate(inputbox)
end
local function wrappedDelChar(inputbox)
ime:wrappedDelChar(inputbox)
end
local function clear_stack()
ime:clear_stack()
end
local wrapInputBox = function(inputbox)
if inputbox._py_wrapped == nil then
inputbox._py_wrapped = true
local wrappers = {}
-- Wrap all of the navigation and non-single-character-input keys with
-- a callback to finish (separate) the input status, but pass through to the
-- original function.
-- -- Delete text.
table.insert(wrappers, util.wrapMethod(inputbox, "delChar", wrappedDelChar, nil))
table.insert(wrappers, util.wrapMethod(inputbox, "delToStartOfLine", nil, clear_stack))
table.insert(wrappers, util.wrapMethod(inputbox, "clear", nil, clear_stack))
-- -- Navigation.
table.insert(wrappers, util.wrapMethod(inputbox, "upLine", nil, separate))
table.insert(wrappers, util.wrapMethod(inputbox, "downLine", nil, separate))
-- -- Move to other input box.
table.insert(wrappers, util.wrapMethod(inputbox, "unfocus", nil, separate))
table.insert(wrappers, util.wrapMethod(inputbox, "onCloseKeyboard", nil, separate))
-- -- Gestures to move cursor.
table.insert(wrappers, util.wrapMethod(inputbox, "onTapTextBox", nil, separate))
table.insert(wrappers, util.wrapMethod(inputbox, "onHoldTextBox", nil, separate))
table.insert(wrappers, util.wrapMethod(inputbox, "onSwipeTextBox", nil, separate))
table.insert(wrappers, util.wrapMethod(inputbox, "addChars", wrappedAddChars, nil))
table.insert(wrappers, util.wrapMethod(inputbox, "leftChar", wrappedLeftChar, nil))
table.insert(wrappers, util.wrapMethod(inputbox, "rightChar", wrappedRightChar, nil))
return function()
if inputbox._py_wrapped then
for _, wrapper in ipairs(wrappers) do
wrapper:revert()
end
inputbox._py_wrapped = nil
end
end
end
end
py_keyboard.wrapInputBox = wrapInputBox
py_keyboard.genMenuItems = genMenuItems
py_keyboard.keys[5][4].label = "空格"
return py_keyboard

File diff suppressed because one or more lines are too long

@ -799,11 +799,13 @@ local VirtualKeyboard = FocusManager:extend{
tr = "tr_keyboard",
vi = "vi_keyboard",
zh = "zh_keyboard",
zh_CN = "zh_CN_keyboard",
},
lang_has_submenu = {
ja = true,
zh = true,
zh_CN = true,
},
}

Loading…
Cancel
Save