2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121# SOFTWARE.
2222
23+ from __future__ import annotations
24+
2325__all__ = ['TTkShortcut' ]
2426
27+ from typing import TYPE_CHECKING ,Dict ,List ,Union ,Optional
28+
2529from TermTk .TTkCore .log import TTkLog
2630from TermTk .TTkCore .constant import TTkK
2731from TermTk .TTkCore .helper import TTkHelper
2832from TermTk .TTkCore .signal import pyTTkSlot , pyTTkSignal
2933from TermTk .TTkCore .TTkTerm .inputkey import TTkKeyEvent
3034
31- class TTkStandardKey ():
35+ if TYPE_CHECKING :
36+ from TermTk .TTkWidgets .widget import TTkWidget
37+
38+ class _TTkStandardKey ():
3239 pass
3340
34- class TTkKeySequence ():
41+ class _TTkKeySequence ():
3542 __slots__ = ('_key' )
3643 def __init__ (self , key :int ):
37- self ._key = key
3844 mod = (
3945 ( TTkK .ControlModifier if key & TTkK .CTRL else 0 ) |
4046 ( TTkK .AltModifier if key & TTkK .ALT else 0 ) |
@@ -47,27 +53,27 @@ def __init__(self, key:int):
4753 else :
4854 self ._key = TTkKeyEvent (mod = mod , code = "" , type = TTkK .Character , key = chr (key ) )
4955
50-
51-
5256 def __hash__ (self ) -> int :
5357 return self ._key .__hash__ ()
5458
55- def __eq__ (self , __value : object ) -> bool :
56- pass
5759
5860class TTkShortcut ():
5961 '''TTkShortcut'''
60- _shortcuts = {}
62+ _shortcuts : Dict [ TTkKeyEvent , List [ TTkShortcut ]] = {}
6163 __slots__ = (
62- '_key' , ' _parent' , '_shortcutContext' ,
64+ '_parent' , '_shortcutContext' ,
6365 # Signals
6466 'activated' )
6567 def __init__ (self ,
66- key :int , parent = None ,
68+ key :Union [ int ,TTkKeyEvent ], parent : Optional [ 'TTkWidget' ] = None ,
6769 shortcutContext : TTkK .ShortcutContext = TTkK .ShortcutContext .WindowShortcut ):
6870 if type (key ) == int :
69- key = TTkKeySequence (key )._key
70- self ._key = key
71+ key = _TTkKeySequence (key )._key
72+ elif isinstance (key , TTkKeyEvent ):
73+ key = key
74+ else :
75+ raise TypeError (f"{ key = } is not int or TTkKeyEvent" )
76+
7177 self ._parent = parent
7278 self ._shortcutContext = shortcutContext
7379 # Signals
@@ -77,24 +83,17 @@ def __init__(self,
7783 TTkShortcut ._shortcuts [key ].append (self )
7884
7985 @staticmethod
80- def processKey (key , focusWidget ):
81- # TTkLog.debug(f"{str(key)=}")
82- # for k in TTkShortcut._shortcuts:
83- # TTkLog.debug(f"{str(k)=} - {key==k=}")
84- if key in TTkShortcut ._shortcuts :
85- for sc in TTkShortcut ._shortcuts [key ]:
86- # if sc._parent:
87- # TTkLog.debug(f"{focusWidget=} {sc._parent=} {sc._parent._parent=}")
88- # else:
89- # TTkLog.debug(f"{focusWidget=} {sc._parent=}")
90- if ( ( sc ._shortcutContext == TTkK .WidgetShortcut
91- and focusWidget == sc ._parent )
92- or ( sc ._shortcutContext == TTkK .WidgetWithChildrenShortcut
93- and ( focusWidget == sc ._parent
94- or TTkHelper .isParent (sc ._parent ,focusWidget ) ) )
95- or ( sc ._shortcutContext == TTkK .WindowShortcut )
96- or ( sc ._shortcutContext == TTkK .ApplicationShortcut )):
97- if sc .activated ._connected_slots :
98- sc .activated .emit ()
99- return True
86+ def processKey (key :TTkKeyEvent , focusWidget :'TTkWidget' ) -> bool :
87+ for sc in TTkShortcut ._shortcuts .get (key ,[]):
88+ if ( ( sc ._shortcutContext == TTkK .WidgetShortcut
89+ and focusWidget == sc ._parent )
90+ or ( sc ._shortcutContext == TTkK .WidgetWithChildrenShortcut
91+ and sc ._parent
92+ and ( focusWidget == sc ._parent
93+ or TTkHelper .isParent (sc ._parent ,focusWidget ) ) )
94+ or ( sc ._shortcutContext == TTkK .WindowShortcut )
95+ or ( sc ._shortcutContext == TTkK .ApplicationShortcut )):
96+ if sc .activated ._connected_slots :
97+ sc .activated .emit ()
98+ return True
10099 return False
0 commit comments