summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAlexander Neumann <an2048@gmail.com>2019-05-06 11:14:53 +0200
committerGitHub <noreply@github.com>2019-05-06 11:14:53 +0200
commitcaf449c9afd814376c15fcb981620994cc2928d3 (patch)
tree38d65199a678a51f0d68409133acaa4476ba3411
parentMerge pull request #507 from zx2c4-forks/jd/tooltip-mixed-toolbutton (diff)
parentaction: allow menu actions to have MFS_DEFAULT specified (diff)
downloadwireguard-windows-caf449c9afd814376c15fcb981620994cc2928d3.tar.xz
wireguard-windows-caf449c9afd814376c15fcb981620994cc2928d3.zip
Merge pull request #502 from zx2c4-forks/jd/defawlt-yes-i-know-how-to-spell-but-default-is-a-reserved-keyword-in-golang
action: allow menu actions to have MFS_DEFAULT specified
-rw-r--r--action.go58
-rw-r--r--menu.go24
2 files changed, 82 insertions, 0 deletions
diff --git a/action.go b/action.go
index 554cdc3f..30404a7d 100644
--- a/action.go
+++ b/action.go
@@ -29,6 +29,8 @@ type Action struct {
image *Bitmap
checkedCondition Condition
checkedConditionChangedHandle int
+ defaultCondition Condition
+ defaultConditionChangedHandle int
enabledCondition Condition
enabledConditionChangedHandle int
visibleCondition Condition
@@ -39,6 +41,7 @@ type Action struct {
visible bool
checkable bool
checked bool
+ defawlt bool
exclusive bool
id uint16
}
@@ -166,6 +169,61 @@ func (a *Action) SetCheckedCondition(c Condition) {
a.raiseChanged()
}
+func (a *Action) Default() bool {
+ return a.defawlt
+}
+
+func (a *Action) SetDefault(value bool) (err error) {
+ if a.defaultCondition != nil {
+ if bp, ok := a.defaultCondition.(*boolProperty); ok {
+ if err := bp.Set(value); err != nil {
+ return err
+ }
+ } else {
+ return newError("DefaultCondition != nil")
+ }
+ }
+
+ if value != a.defawlt {
+ old := a.defawlt
+
+ a.defawlt = value
+
+ if err = a.raiseChanged(); err != nil {
+ a.defawlt = old
+ a.raiseChanged()
+ }
+ }
+
+ return
+}
+
+func (a *Action) DefaultCondition() Condition {
+ return a.defaultCondition
+}
+
+func (a *Action) SetDefaultCondition(c Condition) {
+ if a.defaultCondition != nil {
+ a.defaultCondition.Changed().Detach(a.defaultConditionChangedHandle)
+ }
+
+ a.defaultCondition = c
+
+ if c != nil {
+ a.defawlt = c.Satisfied()
+
+ a.defaultConditionChangedHandle = c.Changed().Attach(func() {
+ if a.defawlt != c.Satisfied() {
+ a.defawlt = !a.defawlt
+
+ a.raiseChanged()
+ }
+ })
+ }
+
+ a.raiseChanged()
+}
+
func (a *Action) Enabled() bool {
return a.enabled
}
diff --git a/menu.go b/menu.go
index 58493c00..1345cc22 100644
--- a/menu.go
+++ b/menu.go
@@ -118,7 +118,21 @@ func (m *Menu) initMenuItemInfoFromAction(mii *win.MENUITEMINFO, action *Action)
}
}
+func (m *Menu) handleDefaultState(action *Action) {
+ if action.Default() {
+ // Unset other default actions before we set this one. Otherwise insertion fails.
+ win.SetMenuDefaultItem(m.hMenu, ^uint32(0), false)
+ for _, otherAction := range m.actions.actions {
+ if otherAction != action {
+ otherAction.SetDefault(false)
+ }
+ }
+ }
+}
+
func (m *Menu) onActionChanged(action *Action) error {
+ m.handleDefaultState(action)
+
if !action.Visible() {
return nil
}
@@ -131,6 +145,10 @@ func (m *Menu) onActionChanged(action *Action) error {
return newError("SetMenuItemInfo failed")
}
+ if action.Default() {
+ win.SetMenuDefaultItem(m.hMenu, uint32(m.actions.indexInObserver(action)), true)
+ }
+
if action.Exclusive() && action.Checked() {
var first, last int
@@ -171,6 +189,8 @@ func (m *Menu) onActionVisibleChanged(action *Action) error {
}
func (m *Menu) insertAction(action *Action, visibleChanged bool) (err error) {
+ m.handleDefaultState(action)
+
if !visibleChanged {
action.addChangedHandler(m)
defer func() {
@@ -194,6 +214,10 @@ func (m *Menu) insertAction(action *Action, visibleChanged bool) (err error) {
return newError("InsertMenuItem failed")
}
+ if action.Default() {
+ win.SetMenuDefaultItem(m.hMenu, uint32(m.actions.indexInObserver(action)), true)
+ }
+
menu := action.menu
if menu != nil {
menu.hWnd = m.hWnd