1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.strings
4 arrays assocs classes cocoa cocoa.application cocoa.classes
5 cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.touchbar
6 cocoa.types cocoa.views combinators core-foundation.strings
7 core-graphics core-graphics.types core-text io.encodings.utf8
8 kernel literals locals math math.order math.parser
9 math.rectangles namespaces opengl sequences splitting threads
10 ui.commands ui.gadgets ui.gadgets.private ui.gadgets.worlds
11 ui.gestures ui.private words ;
12 IN: ui.backend.cocoa.views
14 : send-mouse-moved ( view event -- )
15 [ mouse-location ] [ drop window ] 2bi
16 [ move-hand fire-motion yield ] [ drop ] if* ;
19 : button ( event -- n )
20 ! Cocoa -> Factor UI button mapping
28 CONSTANT: NSAlphaShiftKeyMask 0x10000
29 CONSTANT: NSShiftKeyMask 0x20000
30 CONSTANT: NSControlKeyMask 0x40000
31 CONSTANT: NSAlternateKeyMask 0x80000
32 CONSTANT: NSCommandKeyMask 0x100000
33 CONSTANT: NSNumericPadKeyMask 0x200000
34 CONSTANT: NSHelpKeyMask 0x400000
35 CONSTANT: NSFunctionKeyMask 0x800000
38 { S+ $ NSShiftKeyMask }
39 { C+ $ NSControlKeyMask }
40 { A+ $ NSCommandKeyMask }
41 { M+ $ NSAlternateKeyMask }
71 : key-code ( event -- string ? )
72 dup -> keyCode key-codes at
73 [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
75 : event-modifiers ( event -- modifiers )
76 -> modifierFlags modifiers modifier ;
78 : key-event>gesture ( event -- modifiers keycode action? )
79 [ event-modifiers ] [ key-code ] bi ;
81 : send-key-event ( view gesture -- )
82 swap window [ propagate-key-gesture ] [ drop ] if* ;
84 : interpret-key-event ( view event -- )
85 NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
87 : send-key-down-event ( view event -- )
88 [ key-event>gesture <key-down> send-key-event ]
89 [ interpret-key-event ]
92 : send-key-up-event ( view event -- )
93 key-event>gesture <key-up> send-key-event ;
95 : mouse-event>gesture ( event -- modifiers button )
96 [ event-modifiers ] [ button ] bi ;
98 : send-button-down$ ( view event -- )
99 [ nip mouse-event>gesture <button-down> ]
103 [ send-button-down ] [ 2drop ] if* ;
105 : send-button-up$ ( view event -- )
106 [ nip mouse-event>gesture <button-up> ]
110 [ send-button-up ] [ 2drop ] if* ;
112 : send-scroll$ ( view event -- )
113 [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
117 [ send-scroll ] [ 2drop ] if* ;
119 : send-action$ ( view event gesture -- )
120 [ drop window ] dip over [ send-action ] [ 2drop ] if ;
122 : add-resize-observer ( observer object -- )
124 "updateFactorGadgetSize:"
125 "NSViewFrameDidChangeNotification" <NSString>
128 : string-or-nil? ( NSString -- ? )
129 [ CF>string NSStringPboardType = ] [ t ] if* ;
131 : valid-service? ( gadget send-type return-type -- ? )
132 2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
133 [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
135 : NSRect>rect ( NSRect world -- rect )
136 [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
137 [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
140 : rect>NSRect ( rect world -- NSRect )
141 [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
142 [ drop dim>> first2 ]
145 CONSTANT: selector>action H{
146 { "undo:" undo-action }
147 { "redo:" redo-action }
148 { "cut:" cut-action }
149 { "copy:" copy-action }
150 { "paste:" paste-action }
151 { "delete:" delete-action }
152 { "selectAll:" select-all-action }
153 { "newDocument:" new-action }
154 { "openDocument:" open-action }
155 { "saveDocument:" save-action }
156 { "saveDocumentAs:" save-as-action }
157 { "revertDocumentToSaved:" revert-action }
160 : validate-action ( world selector -- ? validated? )
162 [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
164 : touchbar-commands ( -- commands/f gadget )
167 class-of "commands" word-prop
168 "touchbar" of dup [ commands>> ] when
172 TUPLE: send-touchbar-command target command ;
174 M: send-touchbar-command send-queued-gesture
175 [ target>> ] [ command>> ] bi invoke-command ;
177 : touchbar-invoke-command ( n -- )
178 [ touchbar-commands ] dip over [
180 send-touchbar-command queue-gesture notify-ui-thread
184 <CLASS: FactorView < NSOpenGLView
185 COCOA-PROTOCOL: NSTextInput
187 METHOD: void prepareOpenGL [
189 self SEL: setWantsBestResolutionOpenGLSurface:
190 -> respondsToSelector: c-bool> [
192 self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
194 self { double { id SEL } } ?-> backingScaleFactor
197 gl-scale-factor set-global t retina? set-global
198 cached-lines get-global clear-assoc
206 METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
207 METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
208 METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
209 METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
210 METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
211 METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
212 METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
213 METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
215 METHOD: id makeTouchBar [
216 touchbar-commands drop [
217 length 8 min <iota> [ number>string ] map
218 ] [ { } ] if* self make-touchbar
221 METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
222 touchbar-commands drop [
223 [ self string CF>string dup string>number ] dip nth
224 second name>> "com-" ?head drop over
225 "touchBarCommand" prepend make-NSTouchBar-button
230 METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
233 METHOD: char acceptsFirstMouse: id event [ 0 ] ;
235 METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
237 METHOD: void mouseExited: id event [ forget-rollover ] ;
239 METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
241 METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
243 METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
245 METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
247 METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
249 METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
251 METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
253 METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
255 METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
257 METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
259 METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
261 METHOD: void keyDown: id event [ self event send-key-down-event ] ;
263 METHOD: void keyUp: id event [ self event send-key-up-event ] ;
265 METHOD: char validateUserInterfaceItem: id event
268 event -> action utf8 alien>string validate-action
269 [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
273 METHOD: void undo: id event [ self event undo-action send-action$ ] ;
275 METHOD: void redo: id event [ self event redo-action send-action$ ] ;
277 METHOD: void cut: id event [ self event cut-action send-action$ ] ;
279 METHOD: void copy: id event [ self event copy-action send-action$ ] ;
281 METHOD: void paste: id event [ self event paste-action send-action$ ] ;
283 METHOD: void delete: id event [ self event delete-action send-action$ ] ;
285 METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
287 METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
289 METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
291 METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
293 METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
295 METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
297 ! Multi-touch gestures
298 METHOD: void magnifyWithEvent: id event
302 { 1 [ zoom-in-action send-action$ ] }
303 { -1 [ zoom-out-action send-action$ ] }
308 METHOD: void swipeWithEvent: id event
312 { 1 [ left-action send-action$ ] }
313 { -1 [ right-action send-action$ ] }
317 { 1 [ up-action send-action$ ] }
318 { -1 [ down-action send-action$ ] }
326 METHOD: char acceptsFirstResponder [ 1 ] ;
329 METHOD: id validRequestorForSendType: id sendType returnType: id returnType
331 ! We return either self or nil
333 world-focus sendType returnType
334 valid-service? [ self ] [ f ] if
338 METHOD: char writeSelectionToPasteboard: id pboard types: id types
340 NSStringPboardType types CF>string-array member? [
342 world-focus gadget-selection
343 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
348 METHOD: char readSelectionFromPasteboard: id pboard
350 self window :> window
352 pboard pasteboard-string
353 [ window user-input 1 ] [ 0 ] if*
358 METHOD: void insertText: id text
360 self window :> window
362 text CF>string window user-input
366 METHOD: char hasMarkedText [ 0 ] ;
368 METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
370 METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
372 METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
374 METHOD: void unmarkText [ ] ;
376 METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
378 METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
380 METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
382 METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
384 METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
387 METHOD: void updateFactorGadgetSize: id notification
389 self window :> window
391 self view-dim window dim<< yield
395 METHOD: void doCommandBySelector: SEL selector [ ] ;
397 METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
399 self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
400 dup dup add-resize-observer
403 METHOD: char isOpaque [ 0 ] ;
412 : sync-refresh-to-screen ( GLView -- )
413 -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
414 CGLSetParameter drop ;
416 : <FactorView> ( dim pixel-format -- view )
417 [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
419 : save-position ( world window -- )
420 -> frame CGRect-top-left 2array >>window-loc drop ;
422 <CLASS: FactorWindowDelegate < NSObject
424 METHOD: void windowDidMove: id notification
426 notification -> object -> contentView window
427 [ notification -> object save-position ] when*
430 METHOD: void windowDidBecomeKey: id notification
432 notification -> object -> contentView window
433 [ focus-world ] when*
436 METHOD: void windowDidResignKey: id notification
439 notification -> object -> contentView :> view
440 view window :> window
442 view -> isInFullScreenMode 0 =
443 [ window unfocus-world ] when
447 METHOD: char windowShouldClose: id notification [ 1 ] ;
449 METHOD: void windowWillClose: id notification
451 notification -> object -> contentView
452 [ window ungraft ] [ unregister-window ] bi
455 METHOD: void windowDidChangeBackingProperties: id notification
458 notification -> object dup SEL: backingScaleFactor
459 -> respondsToSelector: c-bool> [
460 { double { id SEL } } ?-> backingScaleFactor
462 [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
463 [ 1.0 > retina? set-global ] bi
468 : install-window-delegate ( window -- )
469 FactorWindowDelegate install-delegate ;