1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See http://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.utf16n
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
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
276 <CLASS: FactorView < NSOpenGLView
277 COCOA-PROTOCOL: NSTextInputClient
279 METHOD: void prepareOpenGL [
281 self -> backingScaleFactor
282 [ 1.0 > ] keep f ? gl-scale-factor set-global
284 cached-lines get-global clear-assoc
289 METHOD: void reshape [
290 self window :> window
292 self view-dim window dim<<
297 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
298 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
299 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
300 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
301 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
302 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
303 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
304 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
306 METHOD: id makeTouchBar [
307 touchbar-commands drop [
308 length 8 min <iota> [ number>string ] map
309 ] [ { } ] if* self make-touchbar
312 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
313 touchbar-commands drop [
314 [ self string CF>string dup string>number ] dip nth
315 second name>> "com-" ?head drop over
316 "touchBarCommand" prepend make-NSTouchBar-button
321 METHOD: void drawRect: NSRect rect [
323 [ draw-world yield ] [ print-error drop ] recover
329 ! METHOD: void viewDidChangeEffectiveAppearance [
330 ! self -> effectiveAppearance -> name [
331 ! CF>string "NSAppearanceNameDarkAqua" =
332 ! dark-theme light-theme ? switch-theme-if-default
337 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
339 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
341 METHOD: void mouseExited: id event [ forget-rollover ] ;
343 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
345 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
347 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
349 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
351 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
353 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
355 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
357 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
359 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
361 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
363 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
365 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
367 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
369 METHOD: char validateUserInterfaceItem: id event
371 self window :> window
373 window world-focus :> gadget
375 gadget preedit? not [
376 window event -> action utf8 alien>string validate-action
377 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
383 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
385 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
387 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
389 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
391 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
393 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
395 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
397 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
399 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
401 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
403 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
405 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
407 ! Multi-touch gestures
408 METHOD: void magnifyWithEvent: id event
412 { 1 [ zoom-in-action send-action$ ] }
413 { -1 [ zoom-out-action send-action$ ] }
418 METHOD: void swipeWithEvent: id event
422 { 1 [ left-action send-action$ ] }
423 { -1 [ right-action send-action$ ] }
427 { 1 [ up-action send-action$ ] }
428 { -1 [ down-action send-action$ ] }
436 METHOD: char acceptsFirstResponder [ 1 ] ;
439 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
441 ! We return either self or nil
443 world-focus sendType returnType
444 valid-service? [ self ] [ f ] if
448 METHOD: char writeSelectionToPasteboard: id pboard types: id types
450 NSStringPboardType types CF>string-array member? [
452 world-focus gadget-selection
453 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
458 METHOD: char readSelectionFromPasteboard: id pboard
460 self window :> window
462 pboard pasteboard-string
463 [ window user-input 1 ] [ 0 ] if*
468 METHOD: void insertText: id text replacementRange: NSRange replacementRange [
469 self window :> window
472 text NSString -> class -> isKindOfClass: 0 = not [
475 text -> string CF>string str!
477 window world-focus :> gadget
479 gadget support-input-methods? [
480 replacementRange location>> NSNotFound = [
481 gadget editor-caret first
482 dup gadget editor-line
484 replacementRange location>> >codepoint-index
485 2array gadget set-caret
487 replacementRange [ location>> ] [ length>> ] bi +
489 2array gadget set-mark
493 gadget remove-preedit-text
494 gadget remove-preedit-info
495 str gadget user-input* drop
496 f gadget preedit-selection-mode?<<
498 str window user-input
501 str window user-input
507 METHOD: char hasMarkedText [
508 self window :> window
510 window world-focus :> gadget
512 gadget preedit? 1 0 ?
517 METHOD: NSRange markedRange [
518 self window :> window
520 window world-focus :> gadget
523 gadget preedit-start>> second
524 gadget preedit-end>> second < [
525 gadget preedit-start>> first gadget editor-line :> str
526 gadget preedit-start>> second ! location
527 gadget preedit-end>> second
528 [ str swap >utf16-index ] bi@ over - ! length
529 ] [ NSNotFound 0 ] if
530 ] [ NSNotFound 0 ] if
531 ] [ NSNotFound 0 ] if
532 ] [ NSNotFound 0 ] if
536 METHOD: NSRange selectedRange [
537 self window :> window
539 window world-focus :> gadget
541 gadget support-input-methods? [
542 gadget editor-caret first gadget editor-line :> str
545 gadget preedit-selected-start>> second
546 gadget preedit-start>> second
547 - >utf16-index ! location
548 gadget preedit-selected-end>> second
549 gadget preedit-selected-start>> second
550 [ str swap >utf16-index ] bi@ - ! length
552 str gadget editor-caret second >utf16-index 0
560 METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
561 replacementRange: NSRange replacementRange [
562 self window :> window
564 window world-focus :> gadget
566 { } clone :> underlines!
568 text NSString -> class -> isKindOfClass: 0 = not [
571 text -> string CF>string str!
572 gadget support-input-methods? [
573 gadget text selectedRange make-preedit-underlines underlines!
576 gadget support-input-methods? [
577 gadget str selectedRange replacementRange update-marked-text
578 underlines gadget preedit-underlines<<
584 METHOD: void unmarkText [
585 self window :> window
587 window world-focus :> gadget
589 gadget support-input-methods? [
592 [ preedit-start>> second ]
593 [ preedit-end>> second ]
594 [ preedit-start>> first ]
597 gadget remove-preedit-text
598 gadget remove-preedit-info
599 gadget user-input* drop
601 f gadget preedit-selection-mode?<<
607 METHOD: id validAttributesForMarkedText [
608 NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
611 METHOD: id attributedSubstringForProposedRange: NSRange aRange
612 actualRange: id actualRange [ f ] ;
614 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
616 METHOD: NSRect firstRectForCharacterRange: NSRange aRange
617 actualRange: NSRange actualRange [
618 self window :> window
620 window world-focus :> gadget
622 gadget support-input-methods? [
623 gadget editor-caret first gadget editor-line :> str
624 str aRange location>> >codepoint-index :> start-pos
625 gadget editor-caret first start-pos 2array gadget loc>x
626 gadget caret-loc second gadget caret-dim second +
627 2array ! character pos
628 gadget screen-loc v+ ! + gadget pos
630 window handle>> window>> dup -> frame -> contentRectForFrameRect:
631 CGRect-top-left 2array v+ ! + window pos
632 first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
639 METHOD: void doCommandBySelector: SEL selector [ ] ;
642 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
644 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
647 METHOD: char isOpaque [ 0 ] ;
656 : sync-refresh-to-screen ( GLView -- )
657 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
658 CGLSetParameter drop ;
660 : <FactorView> ( dim pixel-format -- view )
661 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
663 : save-position ( world window -- )
664 -> frame CGRect-top-left 2array >>window-loc drop ;
666 <CLASS: FactorWindowDelegate < NSObject
668 METHOD: void windowDidMove: id notification
670 notification -> object -> contentView window
671 [ notification -> object save-position ] when*
674 METHOD: void windowDidBecomeKey: id notification
676 notification -> object -> contentView window
677 [ focus-world ] when*
680 METHOD: void windowDidResignKey: id notification
683 notification -> object -> contentView :> view
684 view window :> window
686 view -> isInFullScreenMode 0 =
687 [ window unfocus-world ] when
691 METHOD: char windowShouldClose: id notification [ 1 ] ;
693 METHOD: void windowWillClose: id notification
695 notification -> object -> contentView
696 [ window ungraft ] [ unregister-window ] bi
699 METHOD: void windowDidChangeBackingProperties: id notification
701 notification -> object -> backingScaleFactor
702 [ 1.0 > ] keep f ? gl-scale-factor set-global
706 : install-window-delegate ( window -- )
707 FactorWindowDelegate install-delegate ;