1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See https://factorcode.org/license.txt for BSD license.
4 USING: accessors alien alien.c-types alien.data alien.strings
5 arrays assocs classes cocoa cocoa.application cocoa.classes
6 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.touchbar
7 cocoa.types cocoa.views combinators continuations
8 core-foundation.strings core-graphics core-graphics.types
9 core-text debugger io.encodings.string io.encodings.utf16
10 io.encodings.utf8 kernel literals math math.order math.parser
11 math.rectangles math.vectors namespaces opengl sequences
12 splitting threads ui.backend.cocoa.input-methods ui.commands
13 ui.gadgets ui.gadgets.editors ui.gadgets.line-support
14 ui.gadgets.private ui.gadgets.worlds ui.gestures ui.private
15 ui.theme ui.theme.switching words ;
17 IN: ui.backend.cocoa.views
21 : send-mouse-moved ( view event -- )
22 [ mouse-location ] [ drop window ] 2bi
23 [ move-hand fire-motion yield ] [ drop ] if* ;
26 : button ( event -- n )
27 ! Cocoa -> Factor UI button mapping
35 CONSTANT: NSAlphaShiftKeyMask 0x10000
36 CONSTANT: NSShiftKeyMask 0x20000
37 CONSTANT: NSControlKeyMask 0x40000
38 CONSTANT: NSAlternateKeyMask 0x80000
39 CONSTANT: NSCommandKeyMask 0x100000
40 CONSTANT: NSNumericPadKeyMask 0x200000
41 CONSTANT: NSHelpKeyMask 0x400000
42 CONSTANT: NSFunctionKeyMask 0x800000
45 { S+ $ NSShiftKeyMask }
46 { C+ $ NSControlKeyMask }
47 { M+ $ NSCommandKeyMask }
48 { A+ $ NSAlternateKeyMask }
78 : key-code ( event -- string ? )
79 dup -> keyCode key-codes at
80 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
82 : event-modifiers ( event -- modifiers )
83 -> modifierFlags modifiers modifier ;
85 : key-event>gesture ( event -- modifiers keycode action? )
86 [ event-modifiers ] [ key-code ] bi ;
88 : send-key-event ( view gesture -- )
89 swap window [ propagate-key-gesture ] [ drop ] if* ;
91 : interpret-key-event ( view event -- )
92 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
94 : send-key-down-event ( view event -- )
95 [ key-event>gesture <key-down> send-key-event ]
96 [ interpret-key-event ]
99 : send-key-up-event ( view event -- )
100 key-event>gesture <key-up> send-key-event ;
102 : mouse-event>gesture ( event -- modifiers button )
103 [ event-modifiers ] [ button ] bi ;
105 : send-button-down$ ( view event -- )
106 [ nip mouse-event>gesture <button-down> ]
110 [ send-button-down ] [ 2drop ] if* ;
112 : send-button-up$ ( view event -- )
113 [ nip mouse-event>gesture <button-up> ]
117 [ send-button-up ] [ 2drop ] if* ;
119 : send-scroll$ ( view event -- )
120 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
124 [ send-scroll ] [ 2drop ] if* ;
126 : send-action$ ( view event gesture -- )
127 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
129 : string-or-nil? ( NSString -- ? )
130 [ CF>string NSStringPboardType = ] [ t ] if* ;
132 : valid-service? ( gadget send-type return-type -- ? )
133 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
134 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
136 : NSRect>rect ( NSRect world -- rect )
137 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
138 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
141 : rect>NSRect ( rect world -- NSRect )
142 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
143 [ drop dim>> first2 ]
146 CONSTANT: selector>action H{
147 { "undo:" undo-action }
148 { "redo:" redo-action }
149 { "cut:" cut-action }
150 { "copy:" copy-action }
151 { "paste:" paste-action }
152 { "delete:" delete-action }
153 { "selectAll:" select-all-action }
154 { "newDocument:" new-action }
155 { "openDocument:" open-action }
156 { "saveDocument:" save-action }
157 { "saveDocumentAs:" save-as-action }
158 { "revertDocumentToSaved:" revert-action }
161 : validate-action ( world selector -- ? validated? )
163 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
165 : touchbar-commands ( -- commands/f gadget )
168 class-of "commands" word-prop
169 "touchbar" of dup [ commands>> ] when
173 TUPLE: send-touchbar-command target command ;
175 M: send-touchbar-command send-queued-gesture
176 [ target>> ] [ command>> ] bi invoke-command ;
178 : touchbar-invoke-command ( n -- )
179 [ touchbar-commands ] dip over [
181 send-touchbar-command queue-gesture notify-ui-thread
185 IMPORT: NSAttributedString
189 :: >codepoint-index ( str utf16-index -- codepoint-index )
190 0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
192 :: >utf16-index ( str codepoint-index -- utf16-index )
193 0 codepoint-index str subseq utf16n encode length 2 /i ;
195 :: earlier-caret/mark ( editor -- loc )
196 editor editor-caret :> caret
197 editor editor-mark :> mark
198 caret first mark first = [
199 caret second mark second < [ caret ] [ mark ] if
201 caret first mark first < [ caret ] [ mark ] if
204 :: make-preedit-underlines ( gadget text range -- underlines )
205 f gadget preedit-selection-mode?<<
206 { } clone :> underlines!
207 text -> length :> text-length
208 0 0 <NSRange> :> effective-range
209 text -> string CF>string :> str
210 str utf16n encode :> byte-16n
212 "NSMarkedClauseSegment" <NSString> :> segment-attr
213 [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
216 effective-range [ location>> ] [ length>> ] bi +
217 effective-range >c-ptr
218 -> attribute:atIndex:effectiveRange: drop
220 range location>> effective-range location>> = [
222 t gadget preedit-selection-mode?<<
225 effective-range [ location>> ] [ length>> ] bi over +
226 [ str swap >codepoint-index ] bi@ swap - :> len
227 cp-loc cp-loc len + dup cp-loc!
228 2array thickness 2array
231 underlines length 1 = [
232 underlines first first 2 2array 1array ! thickness: 2
233 ] [ underlines ] if ;
235 :: update-marked-text ( gadget str selectedRange replacementRange -- )
236 replacementRange location>> NSNotFound = [
237 gadget editor-caret first
238 dup gadget editor-line
240 replacementRange location>>
242 2array gadget set-caret
244 replacementRange [ location>> ] [ length>> ] bi +
246 2array gadget set-mark
248 gadget earlier-caret/mark dup
249 gadget preedit-start<<
250 0 1 2array v+ gadget preedit-end<<
254 gadget remove-preedit-text
257 gadget earlier-caret/mark dup
258 gadget preedit-start<<
259 0 str length 2array v+ gadget preedit-end<<
260 str gadget temp-im-input drop
261 gadget preedit-start>>
262 0 str selectedRange location>> >codepoint-index 2array v+
263 dup gadget preedit-selected-start<<
265 selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
266 [ str swap >codepoint-index ] bi@ -
268 dup gadget preedit-selected-end<<
269 dup gadget set-caret gadget set-mark
270 gadget preedit-start>> gadget preedit-end>> = [
271 gadget remove-preedit-info
274 : set-scale-factor ( n -- )
275 [ 1.0 > ] keep f ? gl-scale-factor set-global
276 cached-lines get-global clear-assoc ;
280 <CLASS: FactorView < NSOpenGLView
281 COCOA-PROTOCOL: NSTextInputClient
283 METHOD: void prepareOpenGL [
284 self -> backingScaleFactor set-scale-factor
288 METHOD: void reshape [
289 self window :> window
291 self view-dim window dim<<
296 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
297 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
298 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
299 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
300 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
301 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
302 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
303 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
305 METHOD: id makeTouchBar [
306 touchbar-commands drop [
307 length 8 min <iota> [ number>string ] map
308 ] [ { } ] if* self make-touchbar
311 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
312 touchbar-commands drop [
313 [ self string CF>string dup string>number ] dip nth
314 second name>> "com-" ?head drop over
315 "touchBarCommand" prepend make-NSTouchBar-button
320 METHOD: void drawRect: NSRect rect [
322 [ draw-world yield ] [ print-error drop ] recover
328 METHOD: void viewDidChangeEffectiveAppearance [
329 self -> effectiveAppearance -> name [
330 CF>string "NSAppearanceNameDarkAqua" =
331 dark-theme light-theme ? switch-theme-if-default
336 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
338 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
340 METHOD: void mouseExited: id event [ forget-rollover ] ;
342 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
344 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
346 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
348 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
350 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
352 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
354 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
356 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
358 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
360 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
362 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
364 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
366 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
368 METHOD: char validateUserInterfaceItem: id event
370 self window :> window
372 window world-focus :> gadget
374 gadget preedit? not [
375 window event -> action utf8 alien>string validate-action
376 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
382 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
384 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
386 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
388 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
390 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
392 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
394 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
396 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
398 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
400 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
402 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
404 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
406 ! Multi-touch gestures
407 METHOD: void magnifyWithEvent: id event
411 { 1 [ zoom-in-action send-action$ ] }
412 { -1 [ zoom-out-action send-action$ ] }
417 METHOD: void swipeWithEvent: id event
421 { 1 [ left-action send-action$ ] }
422 { -1 [ right-action send-action$ ] }
426 { 1 [ up-action send-action$ ] }
427 { -1 [ down-action send-action$ ] }
435 METHOD: char acceptsFirstResponder [ 1 ] ;
438 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
440 ! We return either self or nil
442 world-focus sendType returnType
443 valid-service? [ self ] [ f ] if
447 METHOD: char writeSelectionToPasteboard: id pboard types: id types
449 NSStringPboardType types CF>string-array member? [
451 world-focus gadget-selection
452 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
457 METHOD: char readSelectionFromPasteboard: id pboard
459 self window :> window
461 pboard pasteboard-string
462 [ window user-input 1 ] [ 0 ] if*
467 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
468 self window :> window
471 text NSString -> class -> isKindOfClass: 0 = not [
474 text -> string CF>string str!
476 window world-focus :> gadget
478 gadget support-input-methods? [
479 replacementRange location>> NSNotFound = [
480 gadget editor-caret first
481 dup gadget editor-line
483 replacementRange location>> >codepoint-index
484 2array gadget set-caret
486 replacementRange [ location>> ] [ length>> ] bi +
488 2array gadget set-mark
492 gadget remove-preedit-text
493 gadget remove-preedit-info
494 str gadget user-input* drop
495 f gadget preedit-selection-mode?<<
497 str window user-input
500 str window user-input
506 METHOD: char hasMarkedText [
507 self window :> window
509 window world-focus :> gadget
511 gadget preedit? 1 0 ?
516 METHOD: NSRange markedRange [
517 self window :> window
519 window world-focus :> gadget
522 gadget preedit-start>> second
523 gadget preedit-end>> second < [
524 gadget preedit-start>> first gadget editor-line :> str
525 gadget preedit-start>> second ! location
526 gadget preedit-end>> second
527 [ str swap >utf16-index ] bi@ over - ! length
528 ] [ NSNotFound 0 ] if
529 ] [ NSNotFound 0 ] if
530 ] [ NSNotFound 0 ] if
531 ] [ NSNotFound 0 ] if
535 METHOD: NSRange selectedRange [
536 self window :> window
538 window world-focus :> gadget
540 gadget support-input-methods? [
541 gadget editor-caret first gadget editor-line :> str
544 gadget preedit-selected-start>> second
545 gadget preedit-start>> second
546 - >utf16-index ! location
547 gadget preedit-selected-end>> second
548 gadget preedit-selected-start>> second
549 [ str swap >utf16-index ] bi@ - ! length
551 str gadget editor-caret second >utf16-index 0
559 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
560 replacementRange: NSRange replacementRange [
561 self window :> window
563 window world-focus :> gadget
565 { } clone :> underlines!
567 text NSString -> class -> isKindOfClass: 0 = not [
570 text -> string CF>string str!
571 gadget support-input-methods? [
572 gadget text selectedRange make-preedit-underlines underlines!
575 gadget support-input-methods? [
576 gadget str selectedRange replacementRange update-marked-text
577 underlines gadget preedit-underlines<<
583 METHOD: void unmarkText [
584 self window :> window
586 window world-focus :> gadget
588 gadget support-input-methods? [
591 [ preedit-start>> second ]
592 [ preedit-end>> second ]
593 [ preedit-start>> first ]
596 gadget remove-preedit-text
597 gadget remove-preedit-info
598 gadget user-input* drop
600 f gadget preedit-selection-mode?<<
606 METHOD: id validAttributesForMarkedText [
607 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
610 METHOD: id attributedSubstringForProposedRange: NSRange aRange
611 actualRange: id actualRange [ f ] ;
613 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
615 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
616 actualRange: NSRange actualRange [
617 self window :> window
619 window world-focus :> gadget
621 gadget support-input-methods? [
622 gadget editor-caret first gadget editor-line :> str
623 str aRange location>> >codepoint-index :> start-pos
624 gadget editor-caret first start-pos 2array gadget loc>x
625 gadget caret-loc second gadget caret-dim second +
626 2array ! character pos
627 gadget screen-loc v+ ! + gadget pos
629 window handle>> window>> dup -> frame -> contentRectForFrameRect:
630 CGRect-top-left 2array v+ ! + window pos
631 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
638 METHOD: void doCommandBySelector: SEL selector [ ] ;
641 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
643 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
646 METHOD: char isOpaque [ 0 ] ;
655 : sync-refresh-to-screen ( GLView -- )
656 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
657 CGLSetParameter drop ;
659 : <FactorView> ( dim pixel-format -- view )
660 [ FactorView ] 2dip <GLView>
661 [ -> backingScaleFactor set-scale-factor ] keep
662 [ sync-refresh-to-screen ] keep ;
664 : save-position ( world window -- )
665 -> frame CGRect-top-left 2array >>window-loc drop ;
667 <CLASS: FactorWindowDelegate < NSObject
669 METHOD: void windowDidMove: id notification
671 notification -> object -> contentView window
672 [ notification -> object save-position ] when*
675 METHOD: void windowDidBecomeKey: id notification
677 notification -> object -> contentView window
678 [ focus-world ] when*
681 METHOD: void windowDidResignKey: id notification
684 notification -> object -> contentView :> view
685 view window :> window
687 view -> isInFullScreenMode 0 =
688 [ window unfocus-world ] when
692 METHOD: char windowShouldClose: id notification [ 1 ] ;
694 METHOD: void windowWillClose: id notification
696 notification -> object -> contentView
697 [ window ungraft ] [ unregister-window ] bi
700 METHOD: void windowDidChangeBackingProperties: id notification
702 notification -> object -> backingScaleFactor set-scale-factor
706 : install-window-delegate ( window -- )
707 FactorWindowDelegate install-delegate ;