Added SetHotKey method to Popup interface

This commit is contained in:
Alexei Anoshenko 2026-05-25 15:31:45 +03:00
parent ebed7a209d
commit fd424f56c1
3 changed files with 60 additions and 20 deletions

View File

@ -4,7 +4,7 @@
* Added GoogleFonts field to AppParams * Added GoogleFonts field to AppParams
* Added functions: GetWhiteSpace, GetWordBreak, ScrollIntoViewIfNeeded * Added functions: GetWhiteSpace, GetWordBreak, ScrollIntoViewIfNeeded
* Added PopupShowAnimation and SetPopupShowAnimation methods to Session interface * Added PopupShowAnimation and SetPopupShowAnimation methods to Session interface
* Added DismissWithoutAnimation methos to Popup interface * Added DismissWithoutAnimation add SetHotKey methods to Popup interface
* Added ToBoundsProperty method to Bounds struct * Added ToBoundsProperty method to Bounds struct
# v0.20.0 # v0.20.0

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2021-2022 Alexei Anoshenko Copyright (c) 2021-2026 Alexei Anoshenko
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -299,6 +299,10 @@ type Popup interface {
// DismissWithoutAnimation closes a popup without animation // DismissWithoutAnimation closes a popup without animation
DismissWithoutAnimation() DismissWithoutAnimation()
// 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(Popup))
onDismiss() onDismiss()
html(buffer *strings.Builder) html(buffer *strings.Builder)
viewByHTMLID(id string) View viewByHTMLID(id string) View
@ -332,6 +336,7 @@ type popupData struct {
popupView GridLayout popupView GridLayout
contentContainer ColumnLayout contentContainer ColumnLayout
contentView View contentView View
hotkeys map[string]func(Popup)
} }
type popupManager struct { type popupManager struct {
@ -935,6 +940,16 @@ func (popup *popupData) cancel() {
popup.Dismiss() popup.Dismiss()
} }
func (popup *popupData) SetHotKey(keyCode KeyCode, controlKeys ControlKeyMask, fn func(Popup)) {
hotkey := hotkeyCode(keyCode, controlKeys)
if fn == nil {
delete(popup.hotkeys, hotkey)
} else {
popup.hotkeys[hotkey] = fn
}
}
func (popup *popupData) Dismiss() { func (popup *popupData) Dismiss() {
popup.Session().popupManager().dismissPopup(popup, true) popup.Session().popupManager().dismissPopup(popup, true)
} }
@ -1032,7 +1047,22 @@ func (popup *popupData) onDismiss() {
} }
func (popup *popupData) keyEvent(event KeyEvent) bool { func (popup *popupData) keyEvent(event KeyEvent) bool {
if !event.AltKey && !event.CtrlKey && !event.ShiftKey && !event.MetaKey {
var controlKeys ControlKeyMask = 0
if event.AltKey {
controlKeys |= AltKey
}
if event.CtrlKey {
controlKeys |= CtrlKey
}
if event.MetaKey {
controlKeys |= MetaKey
}
if event.ShiftKey {
controlKeys |= ShiftKey
}
if controlKeys == 0 {
switch event.Code { switch event.Code {
case EnterKey: case EnterKey:
for _, button := range popup.buttons() { for _, button := range popup.buttons() {
@ -1043,28 +1073,36 @@ func (popup *popupData) keyEvent(event KeyEvent) bool {
} }
case EscapeKey: case EscapeKey:
cancelable := func() bool { if popup.isCancelable() {
if closeButton, _ := boolProperty(popup, CloseButton, popup.session); closeButton {
return true
}
if outsideClose, _ := boolProperty(popup, OutsideClose, popup.session); outsideClose {
return true
}
for _, button := range popup.buttons() {
if button.buttonType == CancelButton {
return true
}
}
return false
}
if cancelable() {
popup.cancel() popup.cancel()
return true return true
} }
} }
} }
key := hotkeyCode(KeyCode(event.Code), controlKeys)
if fn, ok := popup.hotkeys[key]; ok && fn != nil {
fn(popup)
return true
}
return false
}
func (popup *popupData) isCancelable() bool {
if closeButton, _ := boolProperty(popup, CloseButton, popup.session); closeButton {
return true
}
if outsideClose, _ := boolProperty(popup, OutsideClose, popup.session); outsideClose {
return true
}
for _, button := range popup.buttons() {
if button.buttonType == CancelButton {
return true
}
}
return false return false
} }
@ -1327,6 +1365,7 @@ func NewPopup(view View, param Params) Popup {
popup.session = view.Session() popup.session = view.Session()
popup.contentView = view popup.contentView = view
popup.properties = map[PropertyName]any{} popup.properties = map[PropertyName]any{}
popup.hotkeys = map[string]func(Popup){}
defaultTransform, defaultOpacity, duration, timing := popup.session.PopupShowAnimation() defaultTransform, defaultOpacity, duration, timing := popup.session.PopupShowAnimation()
@ -1390,6 +1429,7 @@ func CreatePopupFromObject(session Session, object DataObject, binding any) Popu
popup := new(popupData) popup := new(popupData)
popup.session = session popup.session = session
popup.properties = map[PropertyName]any{} popup.properties = map[PropertyName]any{}
popup.hotkeys = map[string]func(Popup){}
for key, value := range object.ToParams() { for key, value := range object.ToParams() {
popup.Set(key, value) popup.Set(key, value)