-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathengine_hexadecimal.go
114 lines (103 loc) · 3.28 KB
/
engine_hexadecimal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Bamboo - A Vietnamese Input method editor - Hexadecimal emulator
* for Bamboo
* Copyright (C) 2021 Tran Duc Binh <binhtran432k@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"log"
"strconv"
"github.com/BambooEngine/bamboo-core"
ibus "github.com/BambooEngine/goibus"
)
func (e *IBusBambooEngine) hexadecimalProcessKeyEvent(keyVal uint32, keyCode uint32, state uint32) bool {
var rawKeyLen = e.getRawKeyLen()
if keyVal >= 0xffb0 && keyVal <= 0xffb9 {
keyVal = keyVal - 0xffb0 + 0x0030
}
var keyRune = rune(keyVal)
var mode = bamboo.EnglishMode | bamboo.FullText
var oldText = e.getProcessedString(mode)
defer e.updateLastKeyWithShift(keyVal, state)
if rawKeyLen == 0 || oldText[0] != 'u' {
e.closeHexadecimalInput()
return false
}
if keyVal == IBusEscape {
e.closeHexadecimalInput()
return true
}
if keyVal == IBusBackSpace {
if rawKeyLen > 2 {
e.preeditor.RemoveLastChar(true)
e.updateHexadecimal(e.getProcessedString(mode))
} else {
e.closeHexadecimalInput()
}
return true
}
if keyVal == IBusSpace || keyVal == IBusReturn || keyVal == 0xff8d {
if rawKeyLen > 1 {
value, err := strconv.ParseInt(oldText[1:], 16, 64)
if err != nil || value > 0x10ffff {
log.Println("Input is out of range")
} else {
log.Printf("Commit Text [%s]\n", fmt.Sprint(value))
e.CommitText(ibus.NewText(fmt.Sprint(value)))
}
}
e.closeHexadecimalInput()
return true
}
if (keyRune >= '0' && keyRune <= '9') || (keyRune >= 'A' && keyRune <= 'F') || (keyRune >= 'a' && keyRune <= 'f') {
if !isValidState(state) || !e.isValidKeyVal(keyVal) {
return true
}
e.preeditor.ProcessKey(keyRune, mode)
e.updateHexadecimal(e.getProcessedString(mode))
}
return true
}
func (e *IBusBambooEngine) setupHexadecimalProcessKeyEvent() {
var keyVal = uint32(117)
var state = uint32(0)
var keyRune = rune(keyVal)
var mode = bamboo.EnglishMode | bamboo.FullText
defer e.updateLastKeyWithShift(keyVal, state)
if e.isValidKeyVal(keyVal) {
e.preeditor.ProcessKey(keyRune, mode)
}
e.updateHexadecimal(e.getProcessedString(mode))
}
func (e *IBusBambooEngine) closeHexadecimalInput() {
e.HidePreeditText()
e.preeditor.Reset()
e.isInHexadecimal = false
}
func (e *IBusBambooEngine) updateHexadecimal(processedStr string) {
var encodedStr = e.encodeText(processedStr)
var preeditLen = uint32(len([]rune(encodedStr)))
if preeditLen == 0 {
e.HidePreeditText()
e.CommitText(ibus.NewText(""))
return
}
var ibusText = ibus.NewText(encodedStr)
ibusText.AppendAttr(ibus.IBUS_ATTR_TYPE_UNDERLINE, ibus.IBUS_ATTR_UNDERLINE_SINGLE, 0, preeditLen)
e.UpdatePreeditTextWithMode(ibusText, preeditLen, true, ibus.IBUS_ENGINE_PREEDIT_COMMIT)
}