mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Support for Accelerators!
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package menu
|
||||
|
||||
// Modifier is actually a string
|
||||
type Modifier string
|
||||
|
||||
const (
|
||||
// CmdOrCtrl represents Command on Mac and Control on other platforms
|
||||
CmdOrCtrl Modifier = "CmdOrCtrl"
|
||||
// OptionOrAlt represents Option on Mac and Alt on other platforms
|
||||
OptionOrAlt Modifier = "OptionOrAlt"
|
||||
// Shift represents the shift key on all systems
|
||||
Shift Modifier = "Shift"
|
||||
// Super represents Command on Mac and the Windows key on the other platforms
|
||||
Super Modifier = "Super"
|
||||
// Control represents the control key on all systems
|
||||
Control Modifier = "Control"
|
||||
)
|
||||
|
||||
// Accelerator holds the keyboard shortcut for a menu item
|
||||
type Accelerator struct {
|
||||
Key string
|
||||
Modifiers []Modifier
|
||||
}
|
||||
|
||||
// CmdOrCtrlAccel creates a 'CmdOrCtrl' Accelerator
|
||||
func CmdOrCtrlAccel(key string) *Accelerator {
|
||||
return &Accelerator{
|
||||
Key: key,
|
||||
Modifiers: []Modifier{CmdOrCtrl},
|
||||
}
|
||||
}
|
||||
|
||||
// OptionOrAltAccel creates a 'OptionOrAlt' Accelerator
|
||||
func OptionOrAltAccel(key string) *Accelerator {
|
||||
return &Accelerator{
|
||||
Key: key,
|
||||
Modifiers: []Modifier{OptionOrAlt},
|
||||
}
|
||||
}
|
||||
|
||||
// ShiftAccel creates a 'Shift' Accelerator
|
||||
func ShiftAccel(key string) *Accelerator {
|
||||
return &Accelerator{
|
||||
Key: key,
|
||||
Modifiers: []Modifier{Shift},
|
||||
}
|
||||
}
|
||||
|
||||
// ControlAccel creates a 'Control' Accelerator
|
||||
func ControlAccel(key string) *Accelerator {
|
||||
return &Accelerator{
|
||||
Key: key,
|
||||
Modifiers: []Modifier{Control},
|
||||
}
|
||||
}
|
||||
|
||||
// SuperAccel creates a 'Super' Accelerator
|
||||
func SuperAccel(key string) *Accelerator {
|
||||
return &Accelerator{
|
||||
Key: key,
|
||||
Modifiers: []Modifier{Super},
|
||||
}
|
||||
}
|
||||
|
||||
// ComboAccel creates an Accelerator with multiple Modifiers
|
||||
func ComboAccel(key string, modifier1 Modifier, modifier2 Modifier, rest ...Modifier) *Accelerator {
|
||||
result := &Accelerator{
|
||||
Key: key,
|
||||
Modifiers: []Modifier{modifier1, modifier2},
|
||||
}
|
||||
for _, extra := range rest {
|
||||
result.Modifiers = append(result.Modifiers, extra)
|
||||
}
|
||||
return result
|
||||
}
|
||||
+30
-12
@@ -9,7 +9,7 @@ type MenuItem struct {
|
||||
// Role is a predefined menu type
|
||||
Role Role `json:"Role,omitempty"`
|
||||
// Accelerator holds a representation of a key binding
|
||||
Accelerator string `json:"Accelerator,omitempty"`
|
||||
Accelerator *Accelerator `json:"Accelerator,omitempty"`
|
||||
// Type of MenuItem, EG: Checkbox, Text, Separator, Radio, Submenu
|
||||
Type Type
|
||||
// Disabled makes the item unselectable
|
||||
@@ -24,10 +24,16 @@ type MenuItem struct {
|
||||
|
||||
// Text is a helper to create basic Text menu items
|
||||
func Text(label string, id string) *MenuItem {
|
||||
return TextWithAccelerator(label, id, nil)
|
||||
}
|
||||
|
||||
// TextWithAccelerator is a helper to create basic Text menu items with an accelerator
|
||||
func TextWithAccelerator(label string, id string, accelerator *Accelerator) *MenuItem {
|
||||
return &MenuItem{
|
||||
ID: id,
|
||||
Label: label,
|
||||
Type: TextType,
|
||||
ID: id,
|
||||
Label: label,
|
||||
Type: TextType,
|
||||
Accelerator: accelerator,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,21 +46,33 @@ func Separator() *MenuItem {
|
||||
|
||||
// Radio is a helper to create basic Radio menu items
|
||||
func Radio(label string, id string, selected bool) *MenuItem {
|
||||
return RadioWithAccelerator(label, id, selected, nil)
|
||||
}
|
||||
|
||||
// RadioWithAccelerator is a helper to create basic Radio menu items with an accelerator
|
||||
func RadioWithAccelerator(label string, id string, selected bool, accelerator *Accelerator) *MenuItem {
|
||||
return &MenuItem{
|
||||
ID: id,
|
||||
Label: label,
|
||||
Type: RadioType,
|
||||
Checked: selected,
|
||||
ID: id,
|
||||
Label: label,
|
||||
Type: RadioType,
|
||||
Checked: selected,
|
||||
Accelerator: accelerator,
|
||||
}
|
||||
}
|
||||
|
||||
// Checkbox is a helper to create basic Checkbox menu items
|
||||
func Checkbox(label string, id string, checked bool) *MenuItem {
|
||||
return CheckboxWithAccelerator(label, id, checked, nil)
|
||||
}
|
||||
|
||||
// CheckboxWithAccelerator is a helper to create basic Checkbox menu items with an accelerator
|
||||
func CheckboxWithAccelerator(label string, id string, checked bool, accelerator *Accelerator) *MenuItem {
|
||||
return &MenuItem{
|
||||
ID: id,
|
||||
Label: label,
|
||||
Type: CheckboxType,
|
||||
Checked: checked,
|
||||
ID: id,
|
||||
Label: label,
|
||||
Type: CheckboxType,
|
||||
Checked: checked,
|
||||
Accelerator: accelerator,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user