Support for Accelerators!

This commit is contained in:
Lea Anthony
2020-11-28 20:57:07 +11:00
parent a0fe2f1e13
commit fb1d4c6325
6 changed files with 245 additions and 51 deletions
+75
View File
@@ -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
View File
@@ -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,
}
}