22
22
23
23
selectingUsingMouse
24
24
selectingUsingKeyboard
25
- # selectingWordsByDobleClick
26
- # selectingAllTextByDobleClick
27
- # selectingAllTextByTripleClick
25
+ selectingWordsByDobleClick
26
+ selectingAllTextByDobleClick
27
+ selectingAllTextByTripleClick
28
28
selectingAllTextByCtrlA
29
29
# note: implement selecting all text on activation by yourself if you need it
30
30
60
60
selectionStartX* , selectionEndX* : Property[float32 ] # in pixels
61
61
# note: provided in case if you want to animate them
62
62
63
- allowedInteractions* : set [TextAreaInteraction] = {TextAreaInteraction.low.. TextAreaInteraction.high}
63
+ allowedInteractions* : set [TextAreaInteraction] =
64
+ {TextAreaInteraction.low.. TextAreaInteraction.high} -
65
+ {TextAreaInteraction.selectingAllTextByDobleClick}
64
66
65
67
undoBuffer* : seq [tuple [text: string , cursorPos: int , selectionStart, selectionEnd: int ]]
66
68
undoBufferLimit* : int = 200
67
69
redoIndex* : int
70
+
71
+ doubleClick: bool
72
+ lastDoubleClickTime: Time
68
73
69
74
m_cursorPos: int
70
75
m_selectionStart, m_selectionEnd: int
@@ -162,28 +167,29 @@ template onKeyDown*(this: TextArea, expectedKey: Key, body: untyped) =
162
167
if event.key == expectedKey: body
163
168
164
169
170
+ proc findLeftCtrlWord(this: TextArea, inBorders: bool = false ): int =
171
+ var fondLetter = inBorders
172
+ result = 0
173
+ for i in countdown(this.cursorPos[].min(this.text[].runeLen) - 1 , 0 ):
174
+ if fondLetter and not this.text[].runeAtPos(i).isAlpha:
175
+ result = i + 1
176
+ break
177
+ elif this.text[].runeAtPos(i).isAlpha:
178
+ fondLetter = true
179
+
180
+ proc findRightCtrlWord(this: TextArea, inBorders: bool = false ): int =
181
+ var fondLetter = inBorders
182
+ result = this.text[].runeLen
183
+ for i in countup(this.cursorPos[].max(0 ), this.text[].runeLen - 1 ):
184
+ if fondLetter and not this.text[].runeAtPos(i).isAlpha:
185
+ result = i
186
+ break
187
+ elif this.text[].runeAtPos(i).isAlpha:
188
+ fondLetter = true
189
+
190
+
165
191
method recieve* (this: TextArea, signal: Signal) =
166
192
procCall this.super.recieve(signal)
167
-
168
- proc findLeftCtrlWord(): int =
169
- var fondLetter = false
170
- result = 0
171
- for i in countdown(this.cursorPos[].min(this.text[].runeLen) - 1 , 0 ):
172
- if fondLetter and not this.text[].runeAtPos(i).isAlpha:
173
- result = i + 1
174
- break
175
- elif this.text[].runeAtPos(i).isAlpha:
176
- fondLetter = true
177
-
178
- proc findRightCtrlWord(): int =
179
- var fondLetter = false
180
- result = this.text[].runeLen
181
- for i in countup(this.cursorPos[].max(0 ), this.text[].runeLen - 1 ):
182
- if fondLetter and not this.text[].runeAtPos(i).isAlpha:
183
- result = i
184
- break
185
- elif this.text[].runeAtPos(i).isAlpha:
186
- fondLetter = true
187
193
188
194
case signal
189
195
of of WindowEvent(event: @ ea is of KeyEvent(), handled: false ):
@@ -208,14 +214,14 @@ method recieve*(this: TextArea, signal: Signal) =
208
214
case key
209
215
of Key.left:
210
216
if Key.lcontrol in window.keyboard.pressed or Key.rcontrol in window.keyboard.pressed:
211
- this.cursorPos[] = findLeftCtrlWord()
217
+ this.cursorPos[] = this. findLeftCtrlWord()
212
218
else :
213
219
this.cursorPos[] = this.cursorPos[] - 1
214
220
handleSelection()
215
221
216
222
of Key.right:
217
223
if Key.lcontrol in window.keyboard.pressed or Key.rcontrol in window.keyboard.pressed:
218
- this.cursorPos[] = findRightCtrlWord()
224
+ this.cursorPos[] = this. findRightCtrlWord()
219
225
else :
220
226
this.cursorPos[] = this.cursorPos[] + 1
221
227
handleSelection()
@@ -300,7 +306,7 @@ method recieve*(this: TextArea, signal: Signal) =
300
306
elif this.cursorPos[] > 0 :
301
307
if window.keyboard.pressed.containsControl():
302
308
# delete whole word
303
- let i = findLeftCtrlWord()
309
+ let i = this. findLeftCtrlWord()
304
310
let offset = this.text[].runeOffset(i)
305
311
let offset2 =
306
312
if this.cursorPos[] == this.text[].runeLen:
@@ -329,7 +335,7 @@ method recieve*(this: TextArea, signal: Signal) =
329
335
elif this.cursorPos[] < this.text[].runeLen:
330
336
if window.keyboard.pressed.containsControl():
331
337
# delete whole word
332
- let i = findRightCtrlWord()
338
+ let i = this. findRightCtrlWord()
333
339
let offset = this.text[].runeOffset(this.cursorPos[])
334
340
let offset2 =
335
341
if i == this.text[].runeLen:
@@ -440,10 +446,38 @@ method init*(this: TextArea) =
440
446
let e = (ref MouseButtonEvent)ea
441
447
if e.pressed: root.active[] = false
442
448
449
+
450
+ this.clicked.connectTo root, e:
451
+ if selectingWordsByDobleClick in root.allowedInteractions and e.double:
452
+ root.selectionStart[] = root.findLeftCtrlWord(inBorders= true )
453
+ root.selectionEnd[] = root.findRightCtrlWord(inBorders= true )
454
+ root.cursorPos[] = root.selectionEnd[]
455
+ root.doubleClick = true
456
+
457
+ if selectingAllTextByDobleClick in root.allowedInteractions and e.double:
458
+ root.selectionStart[] = 0
459
+ root.selectionEnd[] = root.text[].runeLen
460
+ root.cursorPos[] = root.selectionEnd[]
461
+ root.doubleClick = true
462
+
463
+ if getTime() - root.lastDoubleClickTime <= initDuration(milliseconds= 300 ):
464
+ if selectingAllTextByTripleClick in root.allowedInteractions:
465
+ root.selectionStart[] = 0
466
+ root.selectionEnd[] = root.text[].runeLen
467
+ root.cursorPos[] = root.selectionEnd[]
468
+
469
+ if e.double:
470
+ root.lastDoubleClickTime = getTime()
471
+
472
+
443
473
this.pressed.changed.connectTo root, pressed:
474
+ if not pressed:
475
+ root.doubleClick = false
476
+
444
477
if activatingUsingMouse in root.allowedInteractions and pressed:
445
478
root.active[] = true
446
- if navigationUsingMouse in root.allowedInteractions and pressed:
479
+
480
+ if navigationUsingMouse in root.allowedInteractions and pressed and (not root.doubleClick):
447
481
root.cursorPos[] = characterAtPosition(root.textObj{}.arrangement[], this.mouseX[] - root.offset[])
448
482
449
483
if selectingUsingMouse in root.allowedInteractions:
@@ -452,7 +486,8 @@ method init*(this: TextArea) =
452
486
else :
453
487
root.selectionStart[] = root.cursorPos[]
454
488
root.selectionEnd[] = root.cursorPos[]
455
-
489
+
490
+
456
491
this.mouseX.changed.connectTo root, mouseX:
457
492
if navigationUsingMouse in root.allowedInteractions and this.pressed[]:
458
493
root.cursorPos[] = characterAtPosition(root.textObj{}.arrangement[], this.mouseX[] - root.offset[])
0 commit comments