forked from mbk-lab/rui_orig
2
0
Fork 0

Added SetHotKey function to Session interface

This commit is contained in:
anoshenko 2023-05-15 16:19:33 +03:00
parent ab421b4c32
commit dc2ea14cac
5 changed files with 176 additions and 106 deletions

View File

@ -1,5 +1,6 @@
# v0.13.0 # v0.13.0
* Added SetHotKey function to Session interface
* Added ViewIndex function to ViewsContainer interface * Added ViewIndex function to ViewsContainer interface
* Added ReloadCell function to TableView interface * Added ReloadCell function to TableView interface
* Added ReloadTableViewCell function * Added ReloadTableViewCell function

View File

@ -5217,6 +5217,8 @@ Safari и Firefox.
* DownloadFileData(filename string, data []byte) - загружает (сохраняет) на стороне клиента файл с заданным именем и * DownloadFileData(filename string, data []byte) - загружает (сохраняет) на стороне клиента файл с заданным именем и
заданным содержимым. Обычно используется для передачи файла сгенерированного в памяти сервера. заданным содержимым. Обычно используется для передачи файла сгенерированного в памяти сервера.
* SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Session)) - устанавливает функцию которая будет вызываться при нажатии заданной горячей клавиши.
## Формат описания ресурсов ## Формат описания ресурсов
Ресурсы приложения (темы, View, переводы) могут быть описаны в виде текста (utf-8). Данный текст помещается Ресурсы приложения (темы, View, переводы) могут быть описаны в виде текста (utf-8). Данный текст помещается

View File

@ -5171,6 +5171,9 @@ It is used when the client needs to transfer a file from the server.
* DownloadFileData(filename string, data [] byte) downloads (saves) on the client side a file * DownloadFileData(filename string, data [] byte) downloads (saves) on the client side a file
with a specified name and specified content. Typically used to transfer a file generated in server memory. with a specified name and specified content. Typically used to transfer a file generated in server memory.
* SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Session)) - sets the function that will be called
when the given hotkey is pressed.
## Resource description format ## Resource description format
Application resources (themes, views, translations) can be described as text (utf-8). Application resources (themes, views, translations) can be described as text (utf-8).

View File

@ -18,117 +18,122 @@ const (
// The additional listener formats: // The additional listener formats:
// func(KeyEvent), func(View), and func(). // func(KeyEvent), func(View), and func().
KeyUpEvent = "key-up-event" KeyUpEvent = "key-up-event"
)
type ControlKeyMask int
type KeyCode string
const (
// AltKey is the mask of the "alt" key // AltKey is the mask of the "alt" key
AltKey = 1 AltKey ControlKeyMask = 1
// CtrlKey is the mask of the "ctrl" key // CtrlKey is the mask of the "ctrl" key
CtrlKey = 2 CtrlKey ControlKeyMask = 2
// ShiftKey is the mask of the "shift" key // ShiftKey is the mask of the "shift" key
ShiftKey = 4 ShiftKey ControlKeyMask = 4
// MetaKey is the mask of the "meta" key // MetaKey is the mask of the "meta" key
MetaKey = 8 MetaKey ControlKeyMask = 8
KeyA = "KeyA" KeyA KeyCode = "KeyA"
KeyB = "KeyB" KeyB KeyCode = "KeyB"
KeyC = "KeyC" KeyC KeyCode = "KeyC"
KeyD = "KeyD" KeyD KeyCode = "KeyD"
KeyE = "KeyE" KeyE KeyCode = "KeyE"
KeyF = "KeyF" KeyF KeyCode = "KeyF"
KeyG = "KeyG" KeyG KeyCode = "KeyG"
KeyH = "KeyH" KeyH KeyCode = "KeyH"
KeyI = "KeyI" KeyI KeyCode = "KeyI"
KeyJ = "KeyJ" KeyJ KeyCode = "KeyJ"
KeyK = "KeyK" KeyK KeyCode = "KeyK"
KeyL = "KeyL" KeyL KeyCode = "KeyL"
KeyM = "KeyM" KeyM KeyCode = "KeyM"
KeyN = "KeyN" KeyN KeyCode = "KeyN"
KeyO = "KeyO" KeyO KeyCode = "KeyO"
KeyP = "KeyP" KeyP KeyCode = "KeyP"
KeyQ = "KeyQ" KeyQ KeyCode = "KeyQ"
KeyR = "KeyR" KeyR KeyCode = "KeyR"
KeyS = "KeyS" KeyS KeyCode = "KeyS"
KeyT = "KeyT" KeyT KeyCode = "KeyT"
KeyU = "KeyU" KeyU KeyCode = "KeyU"
KeyV = "KeyV" KeyV KeyCode = "KeyV"
KeyW = "KeyW" KeyW KeyCode = "KeyW"
KeyX = "KeyX" KeyX KeyCode = "KeyX"
KeyY = "KeyY" KeyY KeyCode = "KeyY"
KeyZ = "KeyZ" KeyZ KeyCode = "KeyZ"
Digit0Key = "Digit0" Digit0Key KeyCode = "Digit0"
Digit1Key = "Digit1" Digit1Key KeyCode = "Digit1"
Digit2Key = "Digit2" Digit2Key KeyCode = "Digit2"
Digit3Key = "Digit3" Digit3Key KeyCode = "Digit3"
Digit4Key = "Digit4" Digit4Key KeyCode = "Digit4"
Digit5Key = "Digit5" Digit5Key KeyCode = "Digit5"
Digit6Key = "Digit6" Digit6Key KeyCode = "Digit6"
Digit7Key = "Digit7" Digit7Key KeyCode = "Digit7"
Digit8Key = "Digit8" Digit8Key KeyCode = "Digit8"
Digit9Key = "Digit9" Digit9Key KeyCode = "Digit9"
SpaceKey = "Space" SpaceKey KeyCode = "Space"
MinusKey = "Minus" MinusKey KeyCode = "Minus"
EqualKey = "Equal" EqualKey KeyCode = "Equal"
IntlBackslashKey = "IntlBackslash" IntlBackslashKey KeyCode = "IntlBackslash"
BracketLeftKey = "BracketLeft" BracketLeftKey KeyCode = "BracketLeft"
BracketRightKey = "BracketRight" BracketRightKey KeyCode = "BracketRight"
SemicolonKey = "Semicolon" SemicolonKey KeyCode = "Semicolon"
CommaKey = "Comma" CommaKey KeyCode = "Comma"
PeriodKey = "Period" PeriodKey KeyCode = "Period"
QuoteKey = "Quote" QuoteKey KeyCode = "Quote"
BackquoteKey = "Backquote" BackquoteKey KeyCode = "Backquote"
SlashKey = "Slash" SlashKey KeyCode = "Slash"
EscapeKey = "Escape" EscapeKey KeyCode = "Escape"
EnterKey = "Enter" EnterKey KeyCode = "Enter"
TabKey = "Tab" TabKey KeyCode = "Tab"
CapsLockKey = "CapsLock" CapsLockKey KeyCode = "CapsLock"
DeleteKey = "Delete" DeleteKey KeyCode = "Delete"
HelpKey = "Help" HelpKey KeyCode = "Help"
BackspaceKey = "Backspace" BackspaceKey KeyCode = "Backspace"
ArrowLeftKey = "ArrowLeft" ArrowLeftKey KeyCode = "ArrowLeft"
ArrowRightKey = "ArrowRight" ArrowRightKey KeyCode = "ArrowRight"
ArrowUpKey = "ArrowUp" ArrowUpKey KeyCode = "ArrowUp"
ArrowDownKey = "ArrowDown" ArrowDownKey KeyCode = "ArrowDown"
HomeKey = "Home" HomeKey KeyCode = "Home"
EndKey = "End" EndKey KeyCode = "End"
PageUpKey = "PageUp" PageUpKey KeyCode = "PageUp"
PageDownKey = "PageDown" PageDownKey KeyCode = "PageDown"
F1Key = "F1" F1Key KeyCode = "F1"
F2Key = "F2" F2Key KeyCode = "F2"
F3Key = "F3" F3Key KeyCode = "F3"
F4Key = "F4" F4Key KeyCode = "F4"
F5Key = "F5" F5Key KeyCode = "F5"
F6Key = "F6" F6Key KeyCode = "F6"
F7Key = "F7" F7Key KeyCode = "F7"
F8Key = "F8" F8Key KeyCode = "F8"
F9Key = "F9" F9Key KeyCode = "F9"
F10Key = "F10" F10Key KeyCode = "F10"
F11Key = "F11" F11Key KeyCode = "F11"
F12Key = "F12" F12Key KeyCode = "F12"
F13Key = "F13" F13Key KeyCode = "F13"
NumLockKey = "NumLock" NumLockKey KeyCode = "NumLock"
NumpadKey0 = "Numpad0" NumpadKey0 KeyCode = "Numpad0"
NumpadKey1 = "Numpad1" NumpadKey1 KeyCode = "Numpad1"
NumpadKey2 = "Numpad2" NumpadKey2 KeyCode = "Numpad2"
NumpadKey3 = "Numpad3" NumpadKey3 KeyCode = "Numpad3"
NumpadKey4 = "Numpad4" NumpadKey4 KeyCode = "Numpad4"
NumpadKey5 = "Numpad5" NumpadKey5 KeyCode = "Numpad5"
NumpadKey6 = "Numpad6" NumpadKey6 KeyCode = "Numpad6"
NumpadKey7 = "Numpad7" NumpadKey7 KeyCode = "Numpad7"
NumpadKey8 = "Numpad8" NumpadKey8 KeyCode = "Numpad8"
NumpadKey9 = "Numpad9" NumpadKey9 KeyCode = "Numpad9"
NumpadDecimalKey = "NumpadDecimal" NumpadDecimalKey KeyCode = "NumpadDecimal"
NumpadEnterKey = "NumpadEnter" NumpadEnterKey KeyCode = "NumpadEnter"
NumpadAddKey = "NumpadAdd" NumpadAddKey KeyCode = "NumpadAdd"
NumpadSubtractKey = "NumpadSubtract" NumpadSubtractKey KeyCode = "NumpadSubtract"
NumpadMultiplyKey = "NumpadMultiply" NumpadMultiplyKey KeyCode = "NumpadMultiply"
NumpadDivideKey = "NumpadDivide" NumpadDivideKey KeyCode = "NumpadDivide"
ShiftLeftKey = "ShiftLeft" ShiftLeftKey KeyCode = "ShiftLeft"
ShiftRightKey = "ShiftRight" ShiftRightKey KeyCode = "ShiftRight"
ControlLeftKey = "ControlLeft" ControlLeftKey KeyCode = "ControlLeft"
ControlRightKey = "ControlRight" ControlRightKey KeyCode = "ControlRight"
AltLeftKey = "AltLeft" AltLeftKey KeyCode = "AltLeft"
AltRightKey = "AltRight" AltRightKey KeyCode = "AltRight"
MetaLeftKey = "MetaLeft" MetaLeftKey KeyCode = "MetaLeft"
MetaRightKey = "MetaRight" MetaRightKey KeyCode = "MetaRight"
) )
type KeyEvent struct { type KeyEvent struct {

View File

@ -100,10 +100,17 @@ type Session interface {
// OpenURL opens the url in the new browser tab // OpenURL opens the url in the new browser tab
OpenURL(url string) OpenURL(url string)
// ClientItem reads value by key from the client-side storage
ClientItem(key string) (string, bool) ClientItem(key string) (string, bool)
// SetClientItem stores a key-value pair in the client-side storage
SetClientItem(key, value string) SetClientItem(key, value string)
// RemoveAllClientItems removes all key-value pair from the client-side storage
RemoveAllClientItems() RemoveAllClientItems()
// SetHotKey sets the function that will be called when the given hotkey is pressed.
// Invoke SetHotKey(..., ..., nil) for remove hotkey function.
SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Session))
getCurrentTheme() Theme getCurrentTheme() Theme
registerAnimation(props []AnimatedProperty) string registerAnimation(props []AnimatedProperty) string
@ -188,6 +195,7 @@ type sessionData struct {
animationCSS string animationCSS string
updateScripts map[string]*strings.Builder updateScripts map[string]*strings.Builder
clientStorage map[string]string clientStorage map[string]string
hotkeys map[string]func(Session)
} }
func newSession(app Application, id int, customTheme string, params DataObject) Session { func newSession(app Application, id int, customTheme string, params DataObject) Session {
@ -205,6 +213,7 @@ func newSession(app Application, id int, customTheme string, params DataObject)
session.animationCSS = "" session.animationCSS = ""
session.updateScripts = map[string]*strings.Builder{} session.updateScripts = map[string]*strings.Builder{}
session.clientStorage = map[string]string{} session.clientStorage = map[string]string{}
session.hotkeys = map[string]func(Session){}
if customTheme != "" { if customTheme != "" {
if theme, ok := CreateThemeFromText(customTheme); ok { if theme, ok := CreateThemeFromText(customTheme); ok {
@ -672,7 +681,57 @@ func (session *sessionData) hotKey(event KeyEvent) {
return return
} }
} }
// TODO
var controlKeys ControlKeyMask = 0
if event.AltKey {
controlKeys |= AltKey
}
if event.CtrlKey {
controlKeys |= CtrlKey
}
if event.MetaKey {
controlKeys |= MetaKey
}
if event.ShiftKey {
controlKeys |= ShiftKey
}
key := hotkeyCode(KeyCode(event.Code), controlKeys)
if fn, ok := session.hotkeys[key]; ok && fn != nil {
fn(session)
}
}
func hotkeyCode(keyCode KeyCode, controlKeys ControlKeyMask) string {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
buffer.WriteString(strings.ToLower(string(keyCode)))
if controlKeys != 0 {
buffer.WriteRune('-')
if controlKeys&AltKey != 0 {
buffer.WriteRune('a')
}
if controlKeys&CtrlKey != 0 {
buffer.WriteRune('c')
}
if controlKeys&MetaKey != 0 {
buffer.WriteRune('m')
}
if controlKeys&ShiftKey != 0 {
buffer.WriteRune('s')
}
}
return buffer.String()
}
func (session *sessionData) SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Session)) {
hotkey := hotkeyCode(keyCode, controlKeys)
if fn == nil {
delete(session.hotkeys, hotkey)
} else {
session.hotkeys[hotkey] = fn
}
} }
func (session *sessionData) SetTitle(title string) { func (session *sessionData) SetTitle(title string) {