forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
1647 lines (1421 loc) · 43.6 KB
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package widget
import (
"image/color"
"math"
"strings"
"unicode"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/driver/mobile"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
const (
multiLineRows = 3
doubleClickWordSeperator = "`~!@#$%^&*()-=+[{]}\\|;:'\",.<>/?"
)
// Declare conformity with interfaces
var _ fyne.Disableable = (*Entry)(nil)
var _ fyne.Draggable = (*Entry)(nil)
var _ fyne.Focusable = (*Entry)(nil)
var _ fyne.Tappable = (*Entry)(nil)
var _ fyne.Widget = (*Entry)(nil)
var _ desktop.Mouseable = (*Entry)(nil)
var _ desktop.Keyable = (*Entry)(nil)
var _ mobile.Keyboardable = (*Entry)(nil)
// Entry widget allows simple text to be input when focused.
type Entry struct {
DisableableWidget
shortcut fyne.ShortcutHandler
Text string
// Since: 2.0
TextStyle fyne.TextStyle
PlaceHolder string
OnChanged func(string) `json:"-"`
// Since: 2.0
OnSubmitted func(string) `json:"-"`
Password bool
MultiLine bool
Wrapping fyne.TextWrap
// Set a validator that this entry will check against
// Since: 1.4
Validator fyne.StringValidator
validationStatus *validationStatus
onValidationChanged func(error)
validationError error
CursorRow, CursorColumn int
OnCursorChanged func() `json:"-"`
cursorAnim *entryCursorAnimation
focused bool
text *textProvider
placeholder *textProvider
content *entryContent
scroll *widget.Scroll
// selectRow and selectColumn represent the selection start location
// The selection will span from selectRow/Column to CursorRow/Column -- note that the cursor
// position may occur before or after the select start position in the text.
selectRow, selectColumn int
// selectKeyDown indicates whether left shift or right shift is currently held down
selectKeyDown bool
// selecting indicates whether the cursor has moved since it was at the selection start location
selecting bool
popUp *PopUpMenu
// TODO: Add OnSelectChanged
// ActionItem is a small item which is displayed at the outer right of the entry (like a password revealer)
ActionItem fyne.CanvasObject
textSource binding.String
textListener binding.DataListener
}
// NewEntry creates a new single line entry widget.
func NewEntry() *Entry {
e := &Entry{Wrapping: fyne.TextTruncate}
e.ExtendBaseWidget(e)
return e
}
// NewEntryWithData returns an Entry widget connected to the specified data source.
//
// Since: 2.0
func NewEntryWithData(data binding.String) *Entry {
entry := NewEntry()
entry.Bind(data)
return entry
}
// NewMultiLineEntry creates a new entry that allows multiple lines
func NewMultiLineEntry() *Entry {
e := &Entry{MultiLine: true, Wrapping: fyne.TextTruncate}
e.ExtendBaseWidget(e)
return e
}
// NewPasswordEntry creates a new entry password widget
func NewPasswordEntry() *Entry {
e := &Entry{Password: true, Wrapping: fyne.TextTruncate}
e.ExtendBaseWidget(e)
e.ActionItem = newPasswordRevealer(e)
return e
}
// Bind connects the specified data source to this Entry.
// The current value will be displayed and any changes in the data will cause the widget to update.
// User interactions with this Entry will set the value into the data source.
//
// Since: 2.0
func (e *Entry) Bind(data binding.String) {
e.Unbind()
e.textSource = data
var convertErr error
e.Validator = func(string) error {
return convertErr
}
e.textListener = binding.NewDataListener(func() {
val, err := data.Get()
if err != nil {
convertErr = err
e.SetValidationError(e.Validate())
return
}
e.Text = val
convertErr = nil
e.Refresh()
if cache.IsRendered(e) {
e.Refresh()
}
})
data.AddListener(e.textListener)
e.OnChanged = func(s string) {
convertErr = data.Set(s)
e.SetValidationError(e.Validate())
}
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer
//
// Implements: fyne.Widget
func (e *Entry) CreateRenderer() fyne.WidgetRenderer {
e.ExtendBaseWidget(e)
// initialise
e.textProvider()
e.placeholderProvider()
box := canvas.NewRectangle(theme.InputBackgroundColor())
line := canvas.NewRectangle(theme.ShadowColor())
cursor := canvas.NewRectangle(color.Transparent)
cursor.Hide()
e.cursorAnim = newEntryCursorAnimation(cursor)
e.content = &entryContent{entry: e}
e.scroll = &widget.Scroll{}
objects := []fyne.CanvasObject{box, line}
if e.Wrapping != fyne.TextWrapOff {
e.scroll.Content = e.content
objects = append(objects, e.scroll)
} else {
e.scroll.Hide()
objects = append(objects, e.content)
}
e.content.scroll = e.scroll
if e.Password && e.ActionItem == nil {
// An entry widget has been created via struct setting manually
// the Password field to true. Going to enable the password revealer.
e.ActionItem = newPasswordRevealer(e)
}
if e.ActionItem != nil {
objects = append(objects, e.ActionItem)
}
return &entryRenderer{box, line, e.scroll, objects, e}
}
// Cursor returns the cursor type of this widget
//
// Implements: desktop.Cursorable
func (e *Entry) Cursor() desktop.Cursor {
return desktop.TextCursor
}
// Disable this widget so that it cannot be interacted with, updating any style appropriately.
//
// Implements: fyne.Disableable
func (e *Entry) Disable() {
e.DisableableWidget.Disable()
}
// Disabled returns whether the entry is disabled or read-only.
//
// Implements: fyne.Disableable
func (e *Entry) Disabled() bool {
return e.DisableableWidget.disabled
}
// DoubleTapped is called when this entry has been double tapped so we should select text below the pointer
//
// Implements: fyne.DoubleTappable
func (e *Entry) DoubleTapped(p *fyne.PointEvent) {
row := e.textProvider().row(e.CursorRow)
start, end := getTextWhitespaceRegion(row, e.CursorColumn)
if start == -1 || end == -1 {
return
}
e.setFieldsAndRefresh(func() {
if !e.selectKeyDown {
e.selectRow = e.CursorRow
e.selectColumn = start
}
// Always aim to maximise the selected region
if e.selectRow > e.CursorRow || (e.selectRow == e.CursorRow && e.selectColumn > e.CursorColumn) {
e.CursorColumn = start
} else {
e.CursorColumn = end
}
e.selecting = true
})
}
// DragEnd is called at end of a drag event.
//
// Implements: fyne.Draggable
func (e *Entry) DragEnd() {
e.propertyLock.Lock()
if e.CursorColumn == e.selectColumn && e.CursorRow == e.selectRow {
e.selecting = false
}
shouldRefresh := !e.selecting
e.propertyLock.Unlock()
if shouldRefresh {
e.Refresh()
}
}
// Dragged is called when the pointer moves while a button is held down.
// It updates the selection accordingly.
//
// Implements: fyne.Draggable
func (e *Entry) Dragged(d *fyne.DragEvent) {
pevt := d.PointEvent
// Convert the relative drag position from our Entry coordinates to be relative
// for Scroll.Content
pevt.Position = pevt.Position.Subtract(e.scroll.Offset)
if !e.selecting {
e.selectRow, e.selectColumn = e.getRowCol(&pevt)
e.selecting = true
}
e.updateMousePointer(&pevt, false)
}
// Enable this widget, updating any style or features appropriately.
//
// Implements: fyne.Disableable
func (e *Entry) Enable() {
e.DisableableWidget.Enable()
}
// ExtendBaseWidget is used by an extending widget to make use of BaseWidget functionality.
func (e *Entry) ExtendBaseWidget(wid fyne.Widget) {
impl := e.getImpl()
if impl != nil {
return
}
e.propertyLock.Lock()
defer e.propertyLock.Unlock()
e.BaseWidget.impl = wid
e.registerShortcut()
}
// FocusGained is called when the Entry has been given focus.
//
// Implements: fyne.Focusable
func (e *Entry) FocusGained() {
e.setFieldsAndRefresh(func() {
e.focused = true
})
}
// FocusLost is called when the Entry has had focus removed.
//
// Implements: fyne.Focusable
func (e *Entry) FocusLost() {
e.setFieldsAndRefresh(func() {
e.focused = false
e.selectKeyDown = false
})
}
// Hide hides the entry.
//
// Implements: fyne.Widget
func (e *Entry) Hide() {
if e.popUp != nil {
e.popUp.Hide()
e.popUp = nil
}
e.DisableableWidget.Hide()
}
// Keyboard implements the Keyboardable interface
//
// Implements: mobile.Keyboardable
func (e *Entry) Keyboard() mobile.KeyboardType {
e.propertyLock.RLock()
defer e.propertyLock.RUnlock()
if e.MultiLine {
return mobile.DefaultKeyboard
}
return mobile.SingleLineKeyboard
}
// KeyDown handler for keypress events - used to store shift modifier state for text selection
//
// Implements: desktop.Keyable
func (e *Entry) KeyDown(key *fyne.KeyEvent) {
if e.Disabled() {
return
}
// For keyboard cursor controlled selection we now need to store shift key state and selection "start"
// Note: selection start is where the highlight started (if the user moves the selection up or left then
// the selectRow/Column will not match SelectionStart)
if key.Name == desktop.KeyShiftLeft || key.Name == desktop.KeyShiftRight {
if !e.selecting {
e.selectRow = e.CursorRow
e.selectColumn = e.CursorColumn
}
e.selectKeyDown = true
}
}
// KeyUp handler for key release events - used to reset shift modifier state for text selection
//
// Implements: desktop.Keyable
func (e *Entry) KeyUp(key *fyne.KeyEvent) {
if e.Disabled() {
return
}
// Handle shift release for keyboard selection
// Note: if shift is released then the user may repress it without moving to adjust their old selection
if key.Name == desktop.KeyShiftLeft || key.Name == desktop.KeyShiftRight {
e.selectKeyDown = false
}
}
// MinSize returns the size that this widget should not shrink below.
//
// Implements: fyne.Widget
func (e *Entry) MinSize() fyne.Size {
e.ExtendBaseWidget(e)
min := e.BaseWidget.MinSize()
if e.ActionItem != nil {
min = min.Add(fyne.NewSize(theme.IconInlineSize()+theme.Padding(), 0))
}
if e.Validator != nil {
min = min.Add(fyne.NewSize(theme.IconInlineSize()+theme.Padding(), 0))
}
return min
}
// MouseDown called on mouse click, this triggers a mouse click which can move the cursor,
// update the existing selection (if shift is held), or start a selection dragging operation.
//
// Implements: desktop.Mouseable
func (e *Entry) MouseDown(m *desktop.MouseEvent) {
e.propertyLock.Lock()
if e.selectKeyDown {
e.selecting = true
}
if e.selecting && !e.selectKeyDown && m.Button == desktop.MouseButtonPrimary {
e.selecting = false
}
e.propertyLock.Unlock()
e.updateMousePointer(&m.PointEvent, m.Button == desktop.MouseButtonSecondary)
}
// MouseUp called on mouse release
// If a mouse drag event has completed then check to see if it has resulted in an empty selection,
// if so, and if a text select key isn't held, then disable selecting
//
// Implements: desktop.Mouseable
func (e *Entry) MouseUp(m *desktop.MouseEvent) {
start, _ := e.selection()
e.propertyLock.Lock()
defer e.propertyLock.Unlock()
if start == -1 && e.selecting && !e.selectKeyDown {
e.selecting = false
}
}
// SelectedText returns the text currently selected in this Entry.
// If there is no selection it will return the empty string.
func (e *Entry) SelectedText() string {
e.propertyLock.RLock()
selecting := e.selecting
e.propertyLock.RUnlock()
if !selecting {
return ""
}
start, stop := e.selection()
e.propertyLock.RLock()
defer e.propertyLock.RUnlock()
return string(e.textProvider().buffer[start:stop])
}
// SetPlaceHolder sets the text that will be displayed if the entry is otherwise empty
func (e *Entry) SetPlaceHolder(text string) {
e.propertyLock.Lock()
e.PlaceHolder = text
e.propertyLock.Unlock()
e.placeholderProvider().setText(text) // refreshes
}
// SetText manually sets the text of the Entry to the given text value.
func (e *Entry) SetText(text string) {
e.textProvider().setText(text)
e.updateText(text)
if text == "" {
e.setFieldsAndRefresh(func() {
e.CursorColumn = 0
e.CursorRow = 0
})
return
}
e.propertyLock.Lock()
defer e.propertyLock.Unlock()
if e.CursorRow >= e.textProvider().rows() {
e.CursorRow = e.textProvider().rows() - 1
}
rowLength := e.textProvider().rowLength(e.CursorRow)
if e.CursorColumn >= rowLength {
e.CursorColumn = rowLength
}
}
// Tapped is called when this entry has been tapped so we should update the cursor position.
//
// Implements: fyne.Tappable
func (e *Entry) Tapped(ev *fyne.PointEvent) {
if fyne.CurrentDevice().IsMobile() && e.selecting {
e.selecting = false
}
e.updateMousePointer(ev, false)
}
// TappedSecondary is called when right or alternative tap is invoked.
//
// Opens the PopUpMenu with `Paste` item to paste text from the clipboard.
//
// Implements: fyne.SecondaryTappable
func (e *Entry) TappedSecondary(pe *fyne.PointEvent) {
if e.Disabled() && e.concealed() {
return // no popup options for a disabled concealed field
}
cutItem := fyne.NewMenuItem("Cut", func() {
clipboard := fyne.CurrentApp().Driver().AllWindows()[0].Clipboard()
e.cutToClipboard(clipboard)
})
copyItem := fyne.NewMenuItem("Copy", func() {
clipboard := fyne.CurrentApp().Driver().AllWindows()[0].Clipboard()
e.copyToClipboard(clipboard)
})
pasteItem := fyne.NewMenuItem("Paste", func() {
clipboard := fyne.CurrentApp().Driver().AllWindows()[0].Clipboard()
e.pasteFromClipboard(clipboard)
})
selectAllItem := fyne.NewMenuItem("Select all", e.selectAll)
super := e.super()
entryPos := fyne.CurrentApp().Driver().AbsolutePositionForObject(super)
popUpPos := entryPos.Add(fyne.NewPos(pe.Position.X, pe.Position.Y))
c := fyne.CurrentApp().Driver().CanvasForObject(super)
var menu *fyne.Menu
if e.Disabled() {
menu = fyne.NewMenu("", copyItem, selectAllItem)
} else if e.concealed() {
menu = fyne.NewMenu("", pasteItem, selectAllItem)
} else {
menu = fyne.NewMenu("", cutItem, copyItem, pasteItem, selectAllItem)
}
e.popUp = NewPopUpMenu(menu, c)
e.popUp.ShowAtPosition(popUpPos)
}
// TypedKey receives key input events when the Entry widget is focused.
//
// Implements: fyne.Focusable
func (e *Entry) TypedKey(key *fyne.KeyEvent) {
if e.Disabled() {
return
}
if e.cursorAnim != nil {
e.cursorAnim.interrupt()
}
e.propertyLock.RLock()
provider := e.textProvider()
onSubmitted := e.OnSubmitted
multiLine := e.MultiLine
selectDown := e.selectKeyDown
text := e.Text
e.propertyLock.RUnlock()
if e.selectKeyDown || e.selecting {
if e.selectingKeyHandler(key) {
e.Refresh()
return
}
}
switch key.Name {
case fyne.KeyBackspace:
e.propertyLock.RLock()
isEmpty := provider.len() == 0 || (e.CursorColumn == 0 && e.CursorRow == 0)
e.propertyLock.RUnlock()
if isEmpty {
return
}
e.propertyLock.Lock()
pos := e.cursorTextPos()
provider.deleteFromTo(pos-1, pos)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos - 1)
e.propertyLock.Unlock()
case fyne.KeyDelete:
pos := e.cursorTextPos()
if provider.len() == 0 || pos == provider.len() {
return
}
e.propertyLock.Lock()
provider.deleteFromTo(pos, pos+1)
e.propertyLock.Unlock()
case fyne.KeyReturn, fyne.KeyEnter:
if !multiLine {
// Single line doesn't support newline.
// Call submitted callback, if any.
if onSubmitted != nil {
onSubmitted(text)
}
return
} else if selectDown && onSubmitted != nil {
// Multiline supports newline, unless shift is held and OnSubmitted is set.
onSubmitted(text)
return
}
e.propertyLock.Lock()
provider.insertAt(e.cursorTextPos(), []rune("\n"))
e.CursorColumn = 0
e.CursorRow++
e.propertyLock.Unlock()
case fyne.KeyTab:
e.TypedRune('\t')
case fyne.KeyUp:
if !multiLine {
return
}
e.propertyLock.Lock()
if e.CursorRow > 0 {
e.CursorRow--
}
rowLength := provider.rowLength(e.CursorRow)
if e.CursorColumn > rowLength {
e.CursorColumn = rowLength
}
e.propertyLock.Unlock()
case fyne.KeyDown:
if !multiLine {
return
}
e.propertyLock.Lock()
if e.CursorRow < provider.rows()-1 {
e.CursorRow++
}
rowLength := provider.rowLength(e.CursorRow)
if e.CursorColumn > rowLength {
e.CursorColumn = rowLength
}
e.propertyLock.Unlock()
case fyne.KeyLeft:
e.propertyLock.Lock()
if e.CursorColumn > 0 {
e.CursorColumn--
} else if e.MultiLine && e.CursorRow > 0 {
e.CursorRow--
e.CursorColumn = provider.rowLength(e.CursorRow)
}
e.propertyLock.Unlock()
case fyne.KeyRight:
e.propertyLock.Lock()
if e.MultiLine {
rowLength := provider.rowLength(e.CursorRow)
if e.CursorColumn < rowLength {
e.CursorColumn++
} else if e.CursorRow < provider.rows()-1 {
e.CursorRow++
e.CursorColumn = 0
}
} else if e.CursorColumn < provider.len() {
e.CursorColumn++
}
e.propertyLock.Unlock()
case fyne.KeyEnd:
e.propertyLock.Lock()
if e.MultiLine {
e.CursorColumn = provider.rowLength(e.CursorRow)
} else {
e.CursorColumn = provider.len()
}
e.propertyLock.Unlock()
case fyne.KeyHome:
e.propertyLock.Lock()
e.CursorColumn = 0
e.propertyLock.Unlock()
case fyne.KeyPageUp:
e.propertyLock.Lock()
if e.MultiLine {
e.CursorRow = 0
}
e.CursorColumn = 0
e.propertyLock.Unlock()
case fyne.KeyPageDown:
e.propertyLock.Lock()
if e.MultiLine {
e.CursorRow = provider.rows() - 1
e.CursorColumn = provider.rowLength(e.CursorRow)
} else {
e.CursorColumn = provider.len()
}
e.propertyLock.Unlock()
default:
return
}
e.propertyLock.Lock()
if e.CursorRow == e.selectRow && e.CursorColumn == e.selectColumn {
e.selecting = false
}
e.propertyLock.Unlock()
e.updateText(provider.String())
}
// TypedRune receives text input events when the Entry widget is focused.
//
// Implements: fyne.Focusable
func (e *Entry) TypedRune(r rune) {
if e.Disabled() {
return
}
e.propertyLock.Lock()
if e.popUp != nil {
e.popUp.Hide()
}
selecting := e.selecting
e.propertyLock.Unlock()
// if we've typed a character and we're selecting then replace the selection with the character
if selecting {
e.eraseSelection()
}
e.propertyLock.Lock()
provider := e.textProvider()
e.selecting = false
runes := []rune{r}
pos := e.cursorTextPos()
provider.insertAt(pos, runes)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos + len(runes))
content := provider.String()
e.propertyLock.Unlock()
e.updateText(content)
}
// TypedShortcut implements the Shortcutable interface
//
// Implements: fyne.Shortcutable
func (e *Entry) TypedShortcut(shortcut fyne.Shortcut) {
e.shortcut.TypedShortcut(shortcut)
}
// Unbind disconnects any configured data source from this Entry.
// The current value will remain at the last value of the data source.
//
// Since: 2.0
func (e *Entry) Unbind() {
e.OnChanged = nil
if e.textSource == nil || e.textListener == nil {
return
}
e.Validator = nil
e.textSource.RemoveListener(e.textListener)
e.textListener = nil
e.textSource = nil
}
// concealed tells the rendering textProvider if we are a concealed field
func (e *Entry) concealed() bool {
return e.Password
}
// copyToClipboard copies the current selection to a given clipboard.
// This does nothing if it is a concealed entry.
func (e *Entry) copyToClipboard(clipboard fyne.Clipboard) {
if !e.selecting || e.concealed() {
return
}
clipboard.SetContent(e.SelectedText())
}
func (e *Entry) cursorColAt(text []rune, pos fyne.Position) int {
for i := 0; i < len(text); i++ {
str := string(text[0:i])
wid := fyne.MeasureText(str, theme.TextSize(), e.textStyle()).Width
charWid := fyne.MeasureText(string(text[i]), theme.TextSize(), e.textStyle()).Width
if pos.X < theme.Padding()*2+wid+(charWid/2) {
return i
}
}
return len(text)
}
func (e *Entry) cursorTextPos() (pos int) {
return e.textPosFromRowCol(e.CursorRow, e.CursorColumn)
}
// copyToClipboard copies the current selection to a given clipboard and then removes the selected text.
// This does nothing if it is a concealed entry.
func (e *Entry) cutToClipboard(clipboard fyne.Clipboard) {
if !e.selecting || e.concealed() {
return
}
e.copyToClipboard(clipboard)
e.eraseSelection()
}
// eraseSelection removes the current selected region and moves the cursor
func (e *Entry) eraseSelection() {
if e.Disabled() {
return
}
provider := e.textProvider()
posA, posB := e.selection()
if posA == posB {
return
}
e.propertyLock.Lock()
provider.deleteFromTo(posA, posB)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(posA)
e.selectRow, e.selectColumn = e.CursorRow, e.CursorColumn
e.selecting = false
e.propertyLock.Unlock()
e.updateText(provider.String())
}
func (e *Entry) getRowCol(ev *fyne.PointEvent) (int, int) {
e.propertyLock.RLock()
defer e.propertyLock.RUnlock()
rowHeight := e.textProvider().charMinSize().Height
row := int(math.Floor(float64(ev.Position.Y+e.scroll.Offset.Y-theme.Padding()) / float64(rowHeight)))
col := 0
if row < 0 {
row = 0
} else if row >= e.textProvider().rows() {
row = e.textProvider().rows() - 1
col = 0
} else {
col = e.cursorColAt(e.textProvider().row(row), ev.Position.Add(e.scroll.Offset))
}
return row, col
}
// object returns the root object of the widget so it can be referenced
func (e *Entry) object() fyne.Widget {
return nil
}
// pasteFromClipboard inserts text from the clipboard content,
// starting from the cursor position.
func (e *Entry) pasteFromClipboard(clipboard fyne.Clipboard) {
if e.selecting {
e.eraseSelection()
}
text := clipboard.Content()
if !e.MultiLine {
// format clipboard content to be compatible with single line entry
text = strings.Replace(text, "\n", " ", -1)
}
provider := e.textProvider()
runes := []rune(text)
pos := e.cursorTextPos()
provider.insertAt(pos, runes)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos + len(runes))
e.updateText(provider.String())
e.Refresh()
}
// placeholderProvider returns the placeholder text handler for this entry
func (e *Entry) placeholderProvider() *textProvider {
if e.placeholder != nil {
return e.placeholder
}
text := newTextProvider(e.PlaceHolder, &placeholderPresenter{e})
text.ExtendBaseWidget(text)
text.extraPad = fyne.NewSize(theme.Padding(), theme.InputBorderSize())
e.placeholder = text
return e.placeholder
}
func (e *Entry) registerShortcut() {
e.shortcut.AddShortcut(&fyne.ShortcutCut{}, func(se fyne.Shortcut) {
cut := se.(*fyne.ShortcutCut)
e.cutToClipboard(cut.Clipboard)
})
e.shortcut.AddShortcut(&fyne.ShortcutCopy{}, func(se fyne.Shortcut) {
cpy := se.(*fyne.ShortcutCopy)
e.copyToClipboard(cpy.Clipboard)
})
e.shortcut.AddShortcut(&fyne.ShortcutPaste{}, func(se fyne.Shortcut) {
paste := se.(*fyne.ShortcutPaste)
e.pasteFromClipboard(paste.Clipboard)
})
e.shortcut.AddShortcut(&fyne.ShortcutSelectAll{}, func(se fyne.Shortcut) {
e.selectAll()
})
}
// Obtains row,col from a given textual position
// expects a read or write lock to be held by the caller
func (e *Entry) rowColFromTextPos(pos int) (row int, col int) {
provider := e.textProvider()
canWrap := e.Wrapping == fyne.TextWrapBreak || e.Wrapping == fyne.TextWrapWord
totalRows := provider.rows()
for i := 0; i < totalRows; i++ {
b := provider.rowBoundary(i)
if b[0] <= pos {
if b[1] < pos {
row++
}
col = pos - b[0]
if canWrap && b[0] == pos && col == 0 && pos != 0 && row < (totalRows-1) {
row++
}
} else {
break
}
}
return
}
// selectAll selects all text in entry
func (e *Entry) selectAll() {
if e.textProvider().len() == 0 {
return
}
e.setFieldsAndRefresh(func() {
e.selectRow = 0
e.selectColumn = 0
lastRow := e.textProvider().rows() - 1
e.CursorColumn = e.textProvider().rowLength(lastRow)
e.CursorRow = lastRow
e.selecting = true
})
}
// selectingKeyHandler performs keypress action in the scenario that a selection
// is either a) in progress or b) about to start
// returns true if the keypress has been fully handled
func (e *Entry) selectingKeyHandler(key *fyne.KeyEvent) bool {
if e.selectKeyDown && !e.selecting {
switch key.Name {
case fyne.KeyUp, fyne.KeyDown,
fyne.KeyLeft, fyne.KeyRight,
fyne.KeyEnd, fyne.KeyHome,
fyne.KeyPageUp, fyne.KeyPageDown:
e.selecting = true
}
}
if !e.selecting {
return false
}
switch key.Name {
case fyne.KeyBackspace, fyne.KeyDelete:
// clears the selection -- return handled
e.eraseSelection()
return true
case fyne.KeyReturn, fyne.KeyEnter:
// clear the selection -- return unhandled to add the newline
e.eraseSelection()
return false
}
if !e.selectKeyDown {
switch key.Name {
case fyne.KeyLeft:
// seek to the start of the selection -- return handled
selectStart, _ := e.selection()
e.propertyLock.Lock()
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(selectStart)
e.selecting = false
e.propertyLock.Unlock()
return true
case fyne.KeyRight:
// seek to the end of the selection -- return handled
_, selectEnd := e.selection()
e.propertyLock.Lock()
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(selectEnd)
e.selecting = false
e.propertyLock.Unlock()
return true
case fyne.KeyUp, fyne.KeyDown, fyne.KeyEnd, fyne.KeyHome, fyne.KeyPageUp, fyne.KeyPageDown:
// cursor movement without left or right shift -- clear selection and return unhandled
e.selecting = false
return false
}
}
return false
}
// selection returns the start and end text positions for the selected span of text
// Note: this functionality depends on the relationship between the selection start row/col and
// the current cursor row/column.
// eg: (whitespace for clarity, '_' denotes cursor)
// "T e s [t i]_n g" == 3, 5
// "T e s_[t i] n g" == 3, 5
// "T e_[s t i] n g" == 2, 5
func (e *Entry) selection() (int, int) {
e.propertyLock.RLock()
noSelection := !e.selecting || (e.CursorRow == e.selectRow && e.CursorColumn == e.selectColumn)
e.propertyLock.RUnlock()
if noSelection {
return -1, -1
}
e.propertyLock.Lock()
defer e.propertyLock.Unlock()
// Find the selection start
rowA, colA := e.CursorRow, e.CursorColumn
rowB, colB := e.selectRow, e.selectColumn
// Reposition if the cursors row is more than select start row, or if the row is the same and
// the cursors col is more that the select start column
if rowA > e.selectRow || (rowA == e.selectRow && colA > e.selectColumn) {
rowA, colA = e.selectRow, e.selectColumn
rowB, colB = e.CursorRow, e.CursorColumn
}