Skip to content

Commit 6b1d58e

Browse files
fix(textedit): trigger update when wrap size change (#508)
1 parent ffbab1e commit 6b1d58e

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

libs/pyTermTk/TermTk/TTkCore/constant.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,16 @@ class SelectionMode(IntEnum):
129129
# ContiguousSelection = SelectionMode.ContiguousSelection
130130
MultiSelection = SelectionMode.MultiSelection
131131

132-
class SelectionFormat(int):
132+
class SelectionFormat(IntEnum):
133133
'''
134134
Selection properties
135135
136136
.. autosummary::
137+
NONE
137138
FullWidthSelection
138139
'''
140+
NONE = 0
141+
'''Default value'''
139142
FullWidthSelection = 0x06000
140143
'''When set on the characterFormat of a selection, the whole width of the text will be shown selected.'''
141144

libs/pyTermTk/TermTk/TTkWidgets/texedit.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
__all__ = ['TTkTextEditView', 'TTkTextEdit', 'TTkTextEditRuler']
2424

25-
from typing import List,Optional,Union,Dict,Any
25+
from typing import List,Optional,Union,Dict,Any,TYPE_CHECKING
2626

2727
from TermTk.TTkCore.log import TTkLog
2828
from TermTk.TTkCore.cfg import TTkCfg
@@ -120,6 +120,8 @@ def addMarkRuler(self, markRuler:MarkRuler) -> None:
120120
self._wrapChanged()
121121

122122
def setTextWrap(self, tw:TTkTextWrap) -> None:
123+
if self._textWrap:
124+
self._textWrap.wrapChanged.disconnect(self._wrapChanged)
123125
self._textWrap = tw
124126
tw.wrapChanged.connect(self._wrapChanged)
125127
self._wrapChanged()
@@ -224,7 +226,7 @@ class ExtraSelection():
224226
__slots__ = ('_format', '_color', '_cursor')
225227
def __init__(self,
226228
cursor:TTkTextCursor,
227-
format:TTkK.SelectionFormat=TTkK.NONE,
229+
format:TTkK.SelectionFormat=TTkK.SelectionFormat.NONE,
228230
color:TTkColor=TTkColor.RST) -> None:
229231
self._color = color
230232
self._format = format
@@ -511,6 +513,7 @@ def _setDocument(self, document:Optional[TTkTextDocument]) -> None:
511513
self._textDocument = document
512514
self._textCursor = TTkTextCursor(document=self._textDocument)
513515
self._textWrap = TTkTextWrap(document=self._textDocument)
516+
self._textWrap.wrapChanged.connect(self.update)
514517
self._textDocument.contentsChanged.connect(self._documentChanged)
515518
self._textDocument.cursorPositionChanged.connect(self._cursorPositionChanged)
516519
self._textDocument.undoAvailable.connect(self._undoAvailable)
@@ -531,8 +534,6 @@ def setDocument(self, document:TTkTextDocument) -> None:
531534
self._textDocument.formatChanged.disconnect(self.update)
532535
self._textWrap.wrapChanged.disconnect(self.update)
533536
self._setDocument(document)
534-
# Trigger an update when the rewrap happen
535-
self._textWrap.wrapChanged.connect(self.update)
536537

537538
# forward textWrap Methods
538539
def wrapWidth(self, *args, **kwargs) -> None:
@@ -888,7 +889,7 @@ def mouseTapEvent(self, evt:TTkMouseEvent) -> bool:
888889
self.update()
889890
return True
890891

891-
def pasteEvent(self, txt:TTkStringType) -> None:
892+
def pasteEvent(self, txt:TTkStringType) -> bool:
892893
txt = TTkString(txt)
893894
if not self._multiLine:
894895
txt = TTkString().join(txt.split('\n'))
@@ -903,6 +904,7 @@ def pasteEvent(self, txt:TTkStringType) -> None:
903904
self._scrolToInclude(cx,cy)
904905
self._pushCursor()
905906
self.update()
907+
return True
906908

907909
def keyEvent(self, evt: TTkKeyEvent) -> bool:
908910
if self._readOnly:
@@ -1025,9 +1027,9 @@ def keyEvent(self, evt: TTkKeyEvent) -> bool:
10251027
return True
10261028
else: # Input char
10271029
if self._replace:
1028-
self._textCursor.replaceText(evt.key, moveCursor=True)
1030+
self._textCursor.replaceText(str(evt.key), moveCursor=True)
10291031
else:
1030-
self._textCursor.insertText(evt.key, moveCursor=True)
1032+
self._textCursor.insertText(str(evt.key), moveCursor=True)
10311033
# Scroll to align to the cursor
10321034
p = self._textCursor.position()
10331035
cx, cy = self._textWrap.dataToScreenPosition(p.line, p.pos)
@@ -1086,7 +1088,12 @@ class TTkTextEdit(TTkAbstractScrollArea):
10861088
10871089
''' + (TTkTextEditView.__doc__ if TTkTextEditView.__doc__ else '')
10881090

1089-
ExtraSelection = TTkTextEditView.ExtraSelection
1091+
if TYPE_CHECKING:
1092+
from typing import TypeAlias
1093+
ExtraSelection: TypeAlias = TTkTextEditView.ExtraSelection
1094+
else:
1095+
# TODO: remove this once Python 3.9 will disappear from the world
1096+
ExtraSelection = TTkTextEditView.ExtraSelection
10901097

10911098
_ttk_forward:_ForwardData = _ForwardData(
10921099
forwardClass=TTkTextEditView ,

0 commit comments

Comments
 (0)