Skip to content

Commit

Permalink
Merge pull request #114 from ceccopierangiolieugenio/MouseMove
Browse files Browse the repository at this point in the history
Mouse Move (Tracking) and ToolTip
  • Loading branch information
ceccopierangiolieugenio authored Feb 14, 2023
2 parents 6667ee5 + 8117efa commit 0920f38
Show file tree
Hide file tree
Showing 25 changed files with 514 additions and 142 deletions.
2 changes: 1 addition & 1 deletion TermTk/TTkCore/TTkTerm/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def key_process(self, stdinRead):
y = int(m.group(3))-1
state = m.group(4)
key = TTkMouseEvent.NoButton
evt = TTkMouseEvent.NoEvent
evt = TTkMouseEvent.Move
tap = 0

def _checkTap(lastTime, tap):
Expand Down
12 changes: 9 additions & 3 deletions TermTk/TTkCore/TTkTerm/term_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,23 @@ class Sigmask():
CTRL_Q = 0x0008

title: str = "TermTk"
mouse: bool = True
width: int = 0
height: int = 0
mouse: bool = True
directMouse: bool = False

_sigWinChCb = None

@staticmethod
def init(mouse: bool = True, title: str = "TermTk", sigmask=0):
def init(mouse: bool = True, directMouse: bool = False, title: str = "TermTk", sigmask=0):
TTkTermBase.title = title
TTkTermBase.mouse = mouse
TTkTermBase.mouse = mouse | directMouse
TTkTermBase.directMouse = directMouse
TTkTermBase.push(TTkTermBase.ALT_SCREEN + TTkTermBase.CLEAR + TTkTermBase.Cursor.HIDE + TTkTermBase.escTitle(TTkTermBase.title))
if TTkTermBase.mouse:
TTkTermBase.push(TTkTermBase.Mouse.ON)
if TTkTermBase.directMouse:
TTkTermBase.push(TTkTermBase.Mouse.DIRECT_ON)
TTkTermBase.setEcho(False)
TTkTermBase.CRNL(False)
TTkTermBase.setSigmask(sigmask, False)
Expand All @@ -118,6 +122,8 @@ def cont():
TTkTermBase.push(TTkTermBase.ALT_SCREEN + TTkTermBase.CLEAR + TTkTermBase.Cursor.HIDE + TTkTermBase.escTitle(TTkTermBase.title))
if TTkTermBase.mouse:
TTkTermBase.push(TTkTermBase.Mouse.ON)
if TTkTermBase.directMouse:
TTkTermBase.push(TTkTermBase.Mouse.DIRECT_ON)
TTkTermBase.setEcho(False)
TTkTermBase.CRNL(False)

Expand Down
27 changes: 27 additions & 0 deletions TermTk/TTkCore/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,30 @@ def dndEnd():
TTkHelper._rootWidget.rootLayout().removeWidget(TTkHelper._dnd['d'].pixmap())
TTkHelper._dnd = None
TTkHelper._rootWidget.update()

# ToolTip Helper Methods
toolTipWidget = None
toolTipTrigger = lambda _: True
toolTipReset = lambda : True

@staticmethod
def toolTipShow(tt):
TTkHelper.toolTipClose()
if not TTkHelper._rootWidget: return
TTkHelper.toolTipWidget = tt
rw,rh = TTkHelper._rootWidget.size()
tw,th = tt.size()
mx,my = TTkHelper._mousePos
x = max(0, min(mx-(tw//2),rw-tw))
if my <= th: # Draw below the Mouse
y = my+1
else: # Draw above the Mouse
y = max(0,my-th)
tt.move(x,y)
TTkHelper._rootWidget.rootLayout().addWidget(tt)
tt.raiseWidget()

def toolTipClose():
TTkHelper.toolTipReset()
if TTkHelper.toolTipWidget:
TTkHelper.toolTipWidget.close()
76 changes: 35 additions & 41 deletions TermTk/TTkCore/timer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python3

# MIT License
#
# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
Expand Down Expand Up @@ -35,7 +33,7 @@ class TTkTimer():
_uid = 0

__slots__ = (
'_id', '_running',
'_id', '_running', '_timer',
'timeout', '_timerEvent',
'_delay', '_delayLock', '_quit',
'_stopTime')
Expand All @@ -44,6 +42,7 @@ def __init__(self):
# Define Signals
self.timeout = pyTTkSignal()
self._running = True
self._timer = None

self._id = TTkTimer._uid
TTkTimer._uid +=1
Expand All @@ -69,36 +68,33 @@ def pyodideQuit():
def quit(self):
pass

def run(self):
pass

@pyTTkSlot(int)
def start(self, sec=0):
@pyTTkSlot(float)
def start(self, sec=0.0):
self.stop()
if self._running:
pyodideProxy.setTimeout(int(sec*1000), self._id)
self._timer = pyodideProxy.setTimeout(int(sec*1000), self._id)
# pyodideProxy.consoleLog(f"Timer {self._timer}")

@pyTTkSlot()
def stop(self):
pass
# pyodideProxy.consoleLog(f"Timer {self._timer}")
if self._timer:
pyodideProxy.stopTimeout(self._timer)
self._timer = None
else:
class TTkTimer(threading.Thread):
_timers = []
__slots__ = (
'timeout', '_timerEvent',
'_delay', '_delayLock', '_quit',
'_stopTime')
'timeout', '_delay',
'_timer', '_quit', '_start')
def __init__(self):
# Define Signals
self.timeout = pyTTkSignal()

self._timerEvent = threading.Event()
self._quit = threading.Event()
self._stopTime = 0
self._delay=0
self._delayLock = threading.Lock()
threading.Thread.__init__(self)
self._delay = 0
self._quit = threading.Event()
self._start = threading.Event()
self._timer = threading.Event()
super().__init__()
TTkTimer._timers.append(self)
threading.Thread.start(self)

@staticmethod
def quitAll():
Expand All @@ -107,28 +103,26 @@ def quitAll():

def quit(self):
self._quit.set()
self._delay=1
self._timerEvent.set()
self._timer.set()
self._start.set()

def run(self):
while self._timerEvent.wait():
self._timerEvent.clear()
while self._delay > 0:
# self._delayLock.acquire()
delay = self._delay
self._delay = 0
# self._delayLock.release()
if self._quit.wait(delay):
return
self.timeout.emit()

@pyTTkSlot(int)
def start(self, sec=0):
self._lastTime = time.time()
while not self._quit.is_set():
self._start.wait()
self._start.clear()
if not self._timer.wait(self._delay):
self.timeout.emit()

@pyTTkSlot(float)
def start(self, sec=0.0):
self._delay = sec
self._timerEvent.set()
self._timer.set()
self._timer.clear()
self._start.set()
if not self.native_id:
super().start()

@pyTTkSlot()
def stop(self):
# TODO: Timer.stop()
self._stopTime = time.time()
self._timer.set()

18 changes: 12 additions & 6 deletions TermTk/TTkCore/ttk.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, input):
self.resize(1,1)
input.inputEvent.connect(self._mouseInput)
@pyTTkSlot(TTkKeyEvent, TTkMouseEvent)
def _mouseInput(self, kevt, mevt):
def _mouseInput(self, _, mevt):
if mevt is not None:
self._cursor = '✠'
self._color = TTkColor.RST
Expand All @@ -77,7 +77,7 @@ def paintEvent(self):
#self._canvas.drawChar((0,0),'✜')

__slots__ = (
'_input',
'_input', '_termMouse', '_termDirectMouse',
'_title',
'_showMouseCursor',
'_sigmask',
Expand All @@ -86,6 +86,8 @@ def paintEvent(self):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._termMouse = True
self._termDirectMouse = kwargs.get('mouseTrack',False)
self._input = TTkInput()
self._input.inputEvent.connect(self._processInput)
self._title = kwargs.get('title','TermTk')
Expand Down Expand Up @@ -143,7 +145,11 @@ def mainloop(self):

# Keep track of the multiTap to avoid the extra key release
self._lastMultiTap = False
TTkTerm.init(title=self._title, sigmask=self._sigmask)
TTkTerm.init(
title=self._title,
sigmask=self._sigmask,
mouse=self._termMouse,
directMouse=self._termDirectMouse )

if self._showMouseCursor:
TTkTerm.push(TTkTerm.Mouse.DIRECT_ON)
Expand Down Expand Up @@ -174,6 +180,7 @@ def _mouse_event(self, mevt):
# Upload the global mouse position
# Mainly used by the drag pixmap display
TTkHelper.setMousePos((mevt.x,mevt.y))
TTkWidget._mouseOverProcessed = False

# Avoid to broadcast a key release after a multitap event
if mevt.evt == TTkK.Release and self._lastMultiTap: return
Expand All @@ -188,12 +195,11 @@ def _mouse_event(self, mevt):

# Mouse Events forwarded straight to the Focus widget:
# - Drag
# - Move
# - Release
focusWidget = TTkHelper.getFocus()
if ( focusWidget is not None and
mevt.evt != TTkK.Press and
mevt.key != TTkK.Wheel and
( mevt.evt == TTkK.Drag or
mevt.evt == TTkK.Release ) and
not TTkHelper.isDnD() ) :
x,y = TTkHelper.absPos(focusWidget)
nmevt = mevt.clone(pos=(mevt.x-x, mevt.y-y))
Expand Down
3 changes: 2 additions & 1 deletion TermTk/TTkGui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from .textwrap1 import TTkTextWrap
from .textcursor import TTkTextCursor
from .textdocument import TTkTextDocument
from .clipboard import TTkClipboard
from .clipboard import TTkClipboard
from .tooltip import TTkToolTip
1 change: 0 additions & 1 deletion TermTk/TTkGui/drag.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class _TTkDragDisplayWidget(TTkWidget):
__slots__ = ('_pixmap')
def __init__(self, *args, **kwargs):
TTkWidget.__init__(self, *args, **kwargs)
self._name = kwargs.get('name' , '_TTkDragDisplayWidget' )
self._x, self._y = TTkHelper.mousePos()

def setPixmap(self, pixmap):
Expand Down
78 changes: 78 additions & 0 deletions TermTk/TTkGui/tooltip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# MIT License
#
# Copyright (c) 2023 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# from TermTk.TTkCore.helper import TTkHelper
from TermTk.TTkCore.log import TTkLog
from TermTk.TTkCore.canvas import TTkCanvas
from TermTk.TTkCore.color import TTkColor
from TermTk.TTkCore.timer import TTkTimer
from TermTk.TTkCore.helper import TTkHelper
from TermTk.TTkCore.string import TTkString
from TermTk.TTkWidgets.widget import TTkWidget
from TermTk.TTkCore.signal import pyTTkSlot

class _TTkToolTipDisplayWidget(TTkWidget):
__slots__ = ('_toolTip', '_x', '_y')
def __init__(self, *args, **kwargs):
TTkWidget.__init__(self, *args, **kwargs)
self._toolTip = kwargs.get('toolTip',TTkString()).split('\n')
w = 2+max([s.termWidth() for s in self._toolTip])
h = 2+len(self._toolTip)
self.resize(w,h)

def mouseEvent(self, evt): return False

def paintEvent(self):
w,h = self.size()
borderColor = TTkColor.fg("#888888")
canvas = self.getCanvas()
canvas.drawBox(pos=(0,0),size=(w,h), color=borderColor)
canvas.drawChar(pos=(0, 0), char='╭', color=borderColor)
canvas.drawChar(pos=(w-1,0), char='╮', color=borderColor)
canvas.drawChar(pos=(w-1,h-1),char='╯', color=borderColor)
canvas.drawChar(pos=(0, h-1),char='╰', color=borderColor)
for i,s in enumerate(self._toolTip,1):
canvas.drawTTkString(pos=(1,i), text=s)

class TTkToolTip():
toolTipTimer = TTkTimer()
toolTip = TTkString()

@pyTTkSlot()
@staticmethod
def _toolTipShow():
# TTkLog.debug(f"TT:{TTkToolTip.toolTip}")
TTkHelper.toolTipShow(_TTkToolTipDisplayWidget(toolTip=TTkToolTip.toolTip))

@staticmethod
def trigger(toolTip):
# TTkToolTip.toolTipTimer.stop()
TTkToolTip.toolTip = toolTip
TTkToolTip.toolTipTimer.start(1)

@staticmethod
def reset():
TTkToolTip.toolTipTimer.stop()

TTkToolTip.toolTipTimer.timeout.connect(TTkToolTip._toolTipShow)
TTkHelper.toolTipTrigger = TTkToolTip.trigger
TTkHelper.toolTipReset = TTkToolTip.reset
5 changes: 5 additions & 0 deletions TermTk/TTkTheme/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def loadTheme(theme):
buttonBorderColorFocus = TTkColor.fg("#ffff00") + TTkColor.BOLD
'''Default to **TTkColor.fg("#ffff00") + **:class:`~TermTk.TTkCore.color.TTkColor.BOLD`'''

buttonTextColorHover = TTkColor.fg("#dddd88")+TTkColor.bg("#000050")+TTkColor.BOLD
'''Default to **TTkColor.fg("#dddd88")+TTkColor.bg("#000066")+** :class:`~TermTk.TTkCore.color.TTkColor.BOLD`'''
buttonBorderColorHover = TTkColor.fg("#ffffcc") + TTkColor.BOLD
'''Default to **TTkColor.fg("#ffff88") + **:class:`~TermTk.TTkCore.color.TTkColor.BOLD`'''

buttonTextColorDisabled = textColorDisabled
'''Default to :class:`textColorDisabled`'''
buttonBorderColorDisabled= borderColorDisabled
Expand Down
2 changes: 1 addition & 1 deletion TermTk/TTkWidgets/Fancy/treewidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, *args, **kwargs):
self.setMinimumSize(1, 1)

def paintEvent(self):
if self._checked:
if self.isChecked():
self._canvas.drawText(pos=(0,0), text="▼")
else:
self._canvas.drawText(pos=(0,0), text="▶")
Expand Down
Loading

0 comments on commit 0920f38

Please sign in to comment.