@@ -29,6 +29,9 @@ final class SquirrelPanel: NSPanel {
29
29
private var cursorIndex : Int = 0
30
30
private var scrollDirection : CGVector = . zero
31
31
private var scrollTime : Date = . distantPast
32
+ private var page : Int = 0
33
+ private var lastPage : Bool = true
34
+ private var pagingUp : Bool ?
32
35
33
36
init ( position: NSRect ) {
34
37
self . position = position
@@ -68,33 +71,45 @@ final class SquirrelPanel: NSPanel {
68
71
override func sendEvent( _ event: NSEvent ) {
69
72
switch event. type {
70
73
case . leftMouseDown:
71
- let ( index, _) = view. click ( at: mousePosition ( ) )
72
- if let index = index, index >= 0 && index < candidates. count {
74
+ let ( index, _, pagingUp) = view. click ( at: mousePosition ( ) )
75
+ if let pagingUp {
76
+ self . pagingUp = pagingUp
77
+ } else {
78
+ self . pagingUp = nil
79
+ }
80
+ if let index, index >= 0 && index < candidates. count {
73
81
self . index = index
74
82
}
75
83
case . leftMouseUp:
76
- let ( index, preeditIndex) = view. click ( at: mousePosition ( ) )
77
- if let preeditIndex = preeditIndex, preeditIndex >= 0 && preeditIndex < preedit. utf16. count {
84
+ let ( index, preeditIndex, pagingUp) = view. click ( at: mousePosition ( ) )
85
+
86
+ if let pagingUp, pagingUp == self . pagingUp {
87
+ _ = inputController? . page ( up: pagingUp)
88
+ } else {
89
+ self . pagingUp = nil
90
+ }
91
+ if let preeditIndex, preeditIndex >= 0 && preeditIndex < preedit. utf16. count {
78
92
if preeditIndex < caretPos {
79
93
_ = inputController? . moveCaret ( forward: true )
80
94
} else if preeditIndex > caretPos {
81
95
_ = inputController? . moveCaret ( forward: false )
82
96
}
83
97
}
84
- if let index = index , index == self . index && index >= 0 && index < candidates. count {
98
+ if let index, index == self . index && index >= 0 && index < candidates. count {
85
99
_ = inputController? . selectCandidate ( index)
86
100
}
87
101
case . mouseEntered:
88
102
acceptsMouseMovedEvents = true
89
103
case . mouseExited:
90
104
acceptsMouseMovedEvents = false
91
105
if cursorIndex != index {
92
- update ( preedit: preedit, selRange: selRange, caretPos: caretPos, candidates: candidates, comments: comments, labels: labels, highlighted: index, update: false )
106
+ update ( preedit: preedit, selRange: selRange, caretPos: caretPos, candidates: candidates, comments: comments, labels: labels, highlighted: index, page : page , lastPage : lastPage , update: false )
93
107
}
108
+ pagingUp = nil
94
109
case . mouseMoved:
95
- let ( index, _) = view. click ( at: mousePosition ( ) )
110
+ let ( index, _, _ ) = view. click ( at: mousePosition ( ) )
96
111
if let index = index, cursorIndex != index && index >= 0 && index < candidates. count {
97
- update ( preedit: preedit, selRange: selRange, caretPos: caretPos, candidates: candidates, comments: comments, labels: labels, highlighted: index, update: false )
112
+ update ( preedit: preedit, selRange: selRange, caretPos: caretPos, candidates: candidates, comments: comments, labels: labels, highlighted: index, page : page , lastPage : lastPage , update: false )
98
113
}
99
114
case . scrollWheel:
100
115
if event. phase == . began {
@@ -104,7 +119,7 @@ final class SquirrelPanel: NSPanel {
104
119
if abs ( scrollDirection. dx) > abs ( scrollDirection. dy) && abs ( scrollDirection. dx) > 10 {
105
120
_ = inputController? . page ( up: ( scrollDirection. dx < 0 ) == vertical)
106
121
} else if abs ( scrollDirection. dx) < abs ( scrollDirection. dy) && abs ( scrollDirection. dy) > 10 {
107
- _ = inputController? . page ( up: scrollDirection. dx > 0 )
122
+ _ = inputController? . page ( up: scrollDirection. dy > 0 )
108
123
}
109
124
scrollDirection = . zero
110
125
// Mouse scroll wheel
@@ -141,7 +156,7 @@ final class SquirrelPanel: NSPanel {
141
156
142
157
// Main function to add attributes to text output from librime
143
158
// swiftlint:disable:next cyclomatic_complexity function_parameter_count
144
- func update( preedit: String , selRange: NSRange , caretPos: Int , candidates: [ String ] , comments: [ String ] , labels: [ String ] , highlighted index: Int , update: Bool ) {
159
+ func update( preedit: String , selRange: NSRange , caretPos: Int , candidates: [ String ] , comments: [ String ] , labels: [ String ] , highlighted index: Int , page : Int , lastPage : Bool , update: Bool ) {
145
160
if update {
146
161
self . preedit = preedit
147
162
self . selRange = selRange
@@ -150,6 +165,8 @@ final class SquirrelPanel: NSPanel {
150
165
self . comments = comments
151
166
self . labels = labels
152
167
self . index = index
168
+ self . page = page
169
+ self . lastPage = lastPage
153
170
}
154
171
cursorIndex = index
155
172
@@ -266,7 +283,7 @@ final class SquirrelPanel: NSPanel {
266
283
// text done!
267
284
view. textView. textContentStorage? . attributedString = text
268
285
view. textView. setLayoutOrientation ( vertical ? . vertical : . horizontal)
269
- view. drawView ( candidateRanges: candidateRanges, hilightedIndex: index, preeditRange: preeditRange, highlightedPreeditRange: highlightedPreeditRange)
286
+ view. drawView ( candidateRanges: candidateRanges, hilightedIndex: index, preeditRange: preeditRange, highlightedPreeditRange: highlightedPreeditRange, canPageUp : page > 0 , canPageDown : !lastPage )
270
287
show ( )
271
288
}
272
289
@@ -359,11 +376,12 @@ private extension SquirrelPanel {
359
376
360
377
if vertical {
361
378
panelRect. size = NSSize ( width: min ( 0.95 * screenRect. width, contentRect. height + theme. edgeInset. height * 2 ) ,
362
- height: min ( 0.95 * screenRect. height, contentRect. width + theme. edgeInset. width * 2 ) )
379
+ height: min ( 0.95 * screenRect. height, contentRect. width + theme. edgeInset. width * 2 ) + theme. pagingOffset)
380
+
363
381
// To avoid jumping up and down while typing, use the lower screen when
364
382
// typing on upper, and vice versa
365
383
if position. midY / screenRect. height >= 0.5 {
366
- panelRect. origin. y = position. minY - SquirrelTheme. offsetHeight - panelRect. height
384
+ panelRect. origin. y = position. minY - SquirrelTheme. offsetHeight - panelRect. height + theme . pagingOffset
367
385
} else {
368
386
panelRect. origin. y = position. maxY + SquirrelTheme. offsetHeight
369
387
}
@@ -376,7 +394,8 @@ private extension SquirrelPanel {
376
394
} else {
377
395
panelRect. size = NSSize ( width: min ( 0.95 * screenRect. width, contentRect. width + theme. edgeInset. width * 2 ) ,
378
396
height: min ( 0.95 * screenRect. height, contentRect. height + theme. edgeInset. height * 2 ) )
379
- panelRect. origin = NSPoint ( x: position. minX, y: position. minY - SquirrelTheme. offsetHeight - panelRect. height)
397
+ panelRect. size. width += theme. pagingOffset
398
+ panelRect. origin = NSPoint ( x: position. minX - theme. pagingOffset, y: position. minY - SquirrelTheme. offsetHeight - panelRect. height)
380
399
}
381
400
if panelRect. maxX > screenRect. maxX {
382
401
panelRect. origin. x = screenRect. maxX - panelRect. width
@@ -412,10 +431,13 @@ private extension SquirrelPanel {
412
431
413
432
view. frame = contentView!. bounds
414
433
view. textView. frame = contentView!. bounds
434
+ view. textView. frame. size. width -= theme. pagingOffset
435
+ view. textView. frame. origin. x += theme. pagingOffset
415
436
view. textView. textContainerInset = theme. edgeInset
416
437
417
438
if theme. translucency {
418
439
back. frame = contentView!. bounds
440
+ back. frame. size. width += theme. pagingOffset
419
441
back. appearance = NSApp . effectiveAppearance
420
442
back. isHidden = false
421
443
} else {
@@ -434,7 +456,7 @@ private extension SquirrelPanel {
434
456
view. textContentStorage. attributedString = text
435
457
view. textView. setLayoutOrientation ( vertical ? . vertical : . horizontal)
436
458
view. drawView ( candidateRanges: [ NSRange ( location: 0 , length: text. length) ] , hilightedIndex: - 1 ,
437
- preeditRange: . empty, highlightedPreeditRange: . empty)
459
+ preeditRange: . empty, highlightedPreeditRange: . empty, canPageUp : false , canPageDown : false )
438
460
show ( )
439
461
440
462
statusTimer? . invalidate ( )
0 commit comments