Skip to content

Commit

Permalink
Added accelerators
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Dec 30, 2017
1 parent 3584fa3 commit 7d7d7a3
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ document.addEventListener('astilectron-ready', function() {
- [x] tray
- [x] bundler
- [x] session
- [x] accelerators (shortcuts)
- [ ] loader
- [ ] accelerators (shortcuts)
- [ ] file methods (drag & drop, ...)
- [ ] clipboard methods
- [ ] power monitor events (suspend, resume, ...)
Expand Down
32 changes: 32 additions & 0 deletions accelerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package astilectron

import (
"strings"
)

// Accelerator separator
const acceleratorSeparator = "+"

// Accelerator represents an accelerator
// https://github.com/electron/electron/blob/v1.8.1/docs/api/accelerator.md
type Accelerator []string

// NewAccelerator creates a new accelerator
func NewAccelerator(items ...string) (a *Accelerator) {
a = &Accelerator{}
for _, i := range items {
*a = append(*a, i)
}
return
}

// MarshalText implements the encoding.TextMarshaler interface
func (a *Accelerator) MarshalText() ([]byte, error) {
return []byte(strings.Join(*a, acceleratorSeparator)), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface
func (a *Accelerator) UnmarshalText(b []byte) error {
*a = strings.Split(string(b), acceleratorSeparator)
return nil
}
18 changes: 18 additions & 0 deletions accelerator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package astilectron

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestAccelerator(t *testing.T) {
var tb = []byte("1+2+3")
var a Accelerator
err := a.UnmarshalText(tb)
assert.NoError(t, err)
assert.Equal(t, Accelerator{"1", "2", "3"}, a)
b, err := a.MarshalText()
assert.NoError(t, err)
assert.Equal(t, tb, b)
}
2 changes: 1 addition & 1 deletion dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
// MessageBoxOptions represents message box options
// We must use pointers since GO doesn't handle optional fields whereas NodeJS does. Use PtrBool, PtrInt or PtrStr
// to fill the struct
// https://github.com/electron/electron/blob/v1.6.5/docs/api/dialog.md#dialogshowmessageboxbrowserwindow-options-callback
// https://github.com/electron/electron/blob/v1.8.1/docs/api/dialog.md#dialogshowmessageboxbrowserwindow-options-callback
type MessageBoxOptions struct {
Buttons []string `json:"buttons,omitempty"`
CancelID *int `json:"cancelId,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions display.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const (
)

// Display represents a display
// https://github.com/electron/electron/blob/v1.6.5/docs/api/structures/display.md
// https://github.com/electron/electron/blob/v1.8.1/docs/api/structures/display.md
type Display struct {
o *DisplayOptions
primary bool
}

// DisplayOptions represents display options
// https://github.com/electron/electron/blob/v1.6.5/docs/api/structures/display.md
// https://github.com/electron/electron/blob/v1.8.1/docs/api/structures/display.md
type DisplayOptions struct {
Bounds *RectangleOptions `json:"bounds,omitempty"`
ID *int64 `json:"id,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

// Menu represents a menu
// https://github.com/electron/electron/blob/v1.6.5/docs/api/menu.md
// https://github.com/electron/electron/blob/v1.8.1/docs/api/menu.md
type Menu struct {
*subMenu
}
Expand Down
26 changes: 13 additions & 13 deletions menu_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,20 @@ type MenuItem struct {
// MenuItemOptions represents menu item options
// We must use pointers since GO doesn't handle optional fields whereas NodeJS does. Use PtrBool, PtrInt or PtrStr
// to fill the struct
// https://github.com/electron/electron/blob/v1.6.5/docs/api/menu-item.md
// https://github.com/electron/electron/blob/v1.8.1/docs/api/menu-item.md
type MenuItemOptions struct {
// TODO Accelerator *string `json:"label,omitempty"`
Checked *bool `json:"checked,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Icon *string `json:"icon,omitempty"`
Label *string `json:"label,omitempty"`
OnClick Listener `json:"-"`
Position *string `json:"position,omitempty"`
Role *string `json:"role,omitempty"`
SubLabel *string `json:"sublabel,omitempty"`
SubMenu []*MenuItemOptions `json:"-"`
Type *string `json:"type,omitempty"`
Visible *bool `json:"visible,omitempty"`
Accelerator *Accelerator `json:"accelerator,omitempty"`
Checked *bool `json:"checked,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Icon *string `json:"icon,omitempty"`
Label *string `json:"label,omitempty"`
OnClick Listener `json:"-"`
Position *string `json:"position,omitempty"`
Role *string `json:"role,omitempty"`
SubLabel *string `json:"sublabel,omitempty"`
SubMenu []*MenuItemOptions `json:"-"`
Type *string `json:"type,omitempty"`
Visible *bool `json:"visible,omitempty"`
}

// newMenu creates a new menu item
Expand Down
2 changes: 1 addition & 1 deletion provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (p *defaultProvisioner) provisionPackage(ctx context.Context, paths Paths,
}

// provisionElectronFinishDarwin finishes provisioning electron for Darwin systems
// https://github.com/electron/electron/blob/v1.6.5/docs/tutorial/application-distribution.md#macos
// https://github.com/electron/electron/blob/v1.8.1/docs/tutorial/application-distribution.md#macos
func (p *defaultProvisioner) provisionElectronFinishDarwin(appName string, paths Paths) (err error) {
// Log
astilog.Debug("Finishing provisioning electron for darwin system")
Expand Down
2 changes: 1 addition & 1 deletion tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Tray struct {
// TrayOptions represents tray options
// We must use pointers since GO doesn't handle optional fields whereas NodeJS does. Use PtrBool, PtrInt or PtrStr
// to fill the struct
// https://github.com/electron/electron/blob/v1.6.5/docs/api/tray.md
// https://github.com/electron/electron/blob/v1.8.1/docs/api/tray.md
type TrayOptions struct {
Image *string `json:"image,omitempty"`
Tooltip *string `json:"tooltip,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion window.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Window struct {
// WindowOptions represents window options
// We must use pointers since GO doesn't handle optional fields whereas NodeJS does. Use PtrBool, PtrInt or PtrStr
// to fill the struct
// https://github.com/electron/electron/blob/v1.6.5/docs/api/browser-window.md
// https://github.com/electron/electron/blob/v1.8.1/docs/api/browser-window.md
type WindowOptions struct {
// Custom
MessageBoxOnClose *MessageBoxOptions `json:"messageBoxOnClose,omitempty"`
Expand Down

0 comments on commit 7d7d7a3

Please sign in to comment.