diff options
author | Alexander Neumann <an2048@gmail.com> | 2019-05-06 11:14:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-06 11:14:53 +0200 |
commit | caf449c9afd814376c15fcb981620994cc2928d3 (patch) | |
tree | 38d65199a678a51f0d68409133acaa4476ba3411 | |
parent | Merge pull request #507 from zx2c4-forks/jd/tooltip-mixed-toolbutton (diff) | |
parent | action: allow menu actions to have MFS_DEFAULT specified (diff) | |
download | wireguard-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.go | 58 | ||||
-rw-r--r-- | menu.go | 24 |
2 files changed, 82 insertions, 0 deletions
@@ -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 } @@ -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 |