123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- Namespace mojox
- #rem monkeydoc The Action class.
- #end
- Class Action
- #rem monkeydoc Invoked when the action is triggered.
- #end
- Field Triggered:Void()
-
- #rem monkeydoc Invoked when the [[Text]], [[Icon]] or [[Enabled]] property is changed.
- #end
- Field Modified:Void()
- #rem monkeydoc Creates a new action.
- #end
- Method New( text:String,icon:Image=Null )
-
- _text=text
- _icon=icon
- End
- #rem monkeydoc Text representintg the action.
- This is used for button or menu text.
- #end
- Property Text:String()
-
- Return _text
-
- Setter( text:String )
- If text=_text Return
-
- _text=text
-
- Modified()
- End
-
- #rem monkeydoc Icon representing the action.
-
- This is used for button or menu icon.
-
- #end
- Property Icon:Image()
-
- Return _icon
-
- Setter( icon:Image )
- If icon=_icon Return
-
- _icon=icon
-
- Modified()
- End
-
- #rem monkeydoc Action enabled state.
- #end
- Property Enabled:Bool()
-
- Return _enabled
-
- Setter( enabled:Bool )
- If enabled=_enabled Return
-
- _enabled=enabled
-
- If _enabled EnableHotKey() Else DisableHotKey()
-
- Modified()
- End
-
- #rem monkeydoc Action async flag.
-
- If true (the default), then when the action is triggered the [[Triggered]] signal is run on a new fiber.
-
- Note: Fiber are not supported in emscripten, so this property has no effect.
-
- #end
- Property Async:Bool()
-
- Return _async
-
- Setter( async:Bool )
- #If Not __WEB_TARGET__
- _async=async
- #endif
- End
-
- #rem monkeydoc Hotkey for the action.
- #end
- Property HotKey:Key()
-
- Return _hotKey
-
- Setter( hotKey:Key )
- If hotKey=_hotKey Return
-
- If _enabled DisableHotKey()
-
- _hotKey=hotKey
-
- If _enabled EnableHotKey()
- End
-
- #rem monkeydoc Hotkey modifiers for the action.
- #end
- Property HotKeyModifiers:Modifier()
-
- Return _hotKeyMods
-
- Setter( hotKeyModifiers:Modifier )
-
- _hotKeyMods=hotKeyModifiers
- End
-
- #rem monkeydoc Text representation of the action hotkey.
- #end
- Property HotKeyText:String()
- If Not _hotKey Return ""
-
- Local text:=""
- If _hotKeyMods & Modifier.Shift text+="Shift"
- If _hotKeyMods & Modifier.Control text+="+Ctrl"
- If _hotKeyMods & Modifier.Alt text+="+Alt"
- If _hotKeyMods & Modifier.Gui text+="+Cmd"
- text+="+"+Keyboard.KeyName( _hotKey )
- If text.StartsWith( "+" ) text=text.Slice( 1 )
- Return text
- End
- #rem monkeydoc Triggers the action.
- #end
- Method Trigger()
- #If Not __WEB_TARGET__
- If _async
- New Fiber( Triggered )
- Else
- Triggered()
- Endif
- #Else
- Triggered()
- #Endif
- End
-
- Private
-
- Field _enabled:Bool=True
- Field _text:String
- Field _icon:Image
- Field _hotKey:Key
- Field _hotKeyMods:Modifier
- #If __DESKTOP_TARGET__
- Field _async:Bool=True
- #Else
- Field _async:Bool=False
- #Endif
-
- Global _hotKeys:Map<Key,Stack<Action>>
-
- Method DisableHotKey()
-
- If Not _hotKey Return
-
- _hotKeys[_hotKey].Remove( Self )
- End
-
- Method EnableHotKey()
-
- If Not _hotKey Return
-
- If _hotKeyMods & Modifier.Ignore Return
-
- If Not _hotKeys
-
- _hotKeys=New Map<Key,Stack<Action>>
-
- App.KeyEventFilter+=Lambda( event:KeyEvent )
-
- If event.Eaten Return
-
- If App.ModalView Return
-
- If event.Type<>EventType.KeyDown Return
- Local actions:=_hotKeys[event.Key]
- If Not actions Return
- Local mods:=event.Modifiers & (Modifier.Shift|Modifier.Alt|Modifier.Control|Modifier.Gui)
- mods|=Cast<Modifier>( (Int(mods) & $541) Shl 1 | (Int(mods) & $a82) Shr 1 )
-
- For Local action:=Eachin actions
- If event.Key<>action._hotKey Continue
- If mods<>action._hotKeyMods Continue
- action.Trigger()
- event.Eat()
- Return
- Next
-
- End
-
- Endif
-
- Local actions:=_hotKeys[_hotKey]
- If Not actions
- actions=New Stack<Action>
- _hotKeys[_hotKey]=actions
- Endif
-
- actions.Add( Self )
- End
- End
|