]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/views/views.factor
ui.backend.cocoa.views: use queued-gesture for touchbar commands.
[factor.git] / basis / ui / backend / cocoa / views / views.factor
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
13
14 : send-mouse-moved ( view event -- )
15     [ mouse-location ] [ drop window ] 2bi
16     [ move-hand fire-motion yield ] [ drop ] if* ;
17
18 ! Issue #1453
19 : button ( event -- n )
20     ! Cocoa -> Factor UI button mapping
21     -> buttonNumber {
22         { 0 [ 1 ] }
23         { 1 [ 3 ] }
24         { 2 [ 2 ] }
25         [ ]
26     } case ;
27
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
36
37 CONSTANT: modifiers {
38         { S+ $ NSShiftKeyMask }
39         { C+ $ NSControlKeyMask }
40         { A+ $ NSCommandKeyMask }
41         { M+ $ NSAlternateKeyMask }
42     }
43
44 CONSTANT: key-codes
45     H{
46         { 71 "CLEAR" }
47         { 36 "RET" }
48         { 76 "ENTER" }
49         { 53 "ESC" }
50         { 48 "TAB" }
51         { 51 "BACKSPACE" }
52         { 115 "HOME" }
53         { 117 "DELETE" }
54         { 119 "END" }
55         { 122 "F1" }
56         { 120 "F2" }
57         { 99 "F3" }
58         { 118 "F4" }
59         { 96 "F5" }
60         { 97 "F6" }
61         { 98 "F7" }
62         { 100 "F8" }
63         { 123 "LEFT" }
64         { 124 "RIGHT" }
65         { 125 "DOWN" }
66         { 126 "UP" }
67         { 116 "PAGE_UP" }
68         { 121 "PAGE_DOWN" }
69     }
70
71 : key-code ( event -- string ? )
72     dup -> keyCode key-codes at
73     [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
74
75 : event-modifiers ( event -- modifiers )
76     -> modifierFlags modifiers modifier ;
77
78 : key-event>gesture ( event -- modifiers keycode action? )
79     [ event-modifiers ] [ key-code ] bi ;
80
81 : send-key-event ( view gesture -- )
82     swap window [ propagate-key-gesture ] [ drop ] if* ;
83
84 : interpret-key-event ( view event -- )
85     NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
86
87 : send-key-down-event ( view event -- )
88     [ key-event>gesture <key-down> send-key-event ]
89     [ interpret-key-event ]
90     2bi ;
91
92 : send-key-up-event ( view event -- )
93     key-event>gesture <key-up> send-key-event ;
94
95 : mouse-event>gesture ( event -- modifiers button )
96     [ event-modifiers ] [ button ] bi ;
97
98 : send-button-down$ ( view event -- )
99     [ nip mouse-event>gesture <button-down> ]
100     [ mouse-location ]
101     [ drop window ]
102     2tri
103     [ send-button-down ] [ 2drop ] if* ;
104
105 : send-button-up$ ( view event -- )
106     [ nip mouse-event>gesture <button-up> ]
107     [ mouse-location ]
108     [ drop window ]
109     2tri
110     [ send-button-up ] [ 2drop ] if* ;
111
112 : send-scroll$ ( view event -- )
113     [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
114     [ mouse-location ]
115     [ drop window ]
116     2tri
117     [ send-scroll ] [ 2drop ] if* ;
118
119 : send-action$ ( view event gesture -- )
120     [ drop window ] dip over [ send-action ] [ 2drop ] if ;
121
122 : add-resize-observer ( observer object -- )
123     [
124         "updateFactorGadgetSize:"
125         "NSViewFrameDidChangeNotification" <NSString>
126     ] dip add-observer ;
127
128 : string-or-nil? ( NSString -- ? )
129     [ CF>string NSStringPboardType = ] [ t ] if* ;
130
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 ;
134
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 ]
138     2bi <rect> ;
139
140 : rect>NSRect ( rect world -- NSRect )
141     [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
142     [ drop dim>> first2 ]
143     2bi <CGRect> ;
144
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 }
158 }
159
160 : validate-action ( world selector -- ? validated? )
161     selector>action at
162     [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
163
164 : touchbar-commands ( -- commands/f gadget )
165     world get [
166         children>> [
167             class-of "commands" word-prop
168             "touchbar" of dup [ commands>> ] when
169         ] map-find
170     ] [ f f ] if* ;
171
172 TUPLE: send-touchbar-command target command ;
173
174 M: send-touchbar-command send-queued-gesture
175     [ target>> ] [ command>> ] bi invoke-command ;
176
177 : touchbar-invoke-command ( n -- )
178     [ touchbar-commands ] dip over [
179         rot nth second
180         send-touchbar-command queue-gesture notify-ui-thread
181         yield
182     ] [ 3drop ] if ;
183
184 <CLASS: FactorView < NSOpenGLView
185     COCOA-PROTOCOL: NSTextInput
186
187     METHOD: void prepareOpenGL [
188
189         self SEL: setWantsBestResolutionOpenGLSurface:
190         -> respondsToSelector: c-bool> [
191
192             self SEL: setWantsBestResolutionOpenGLSurface: 1
193             void f "objc_msgSend" { id SEL char } f alien-invoke
194
195             self SEL: backingScaleFactor
196             double f "objc_msgSend" { id SEL } f alien-invoke
197
198             dup 1.0 > [
199                 gl-scale-factor set-global t retina? set-global
200                 cached-lines get-global clear-assoc
201             ] [ drop ] if
202
203         ] when
204     ] ;
205
206     ! TouchBar
207     METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
208     METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
209     METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
210     METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
211     METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
212     METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
213     METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
214     METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
215
216     METHOD: Class makeTouchBar [
217         touchbar-commands drop [
218             length 8 min <iota> [ number>string ] map
219         ] [ { } ] if* self make-touchbar
220     ] ;
221
222     METHOD: Class touchBar: Class touchbar makeItemForIdentifier: Class string [
223         touchbar-commands drop [
224             [ self string CF>string dup string>number ] dip nth
225             second name>> "com-" ?head drop over
226             "touchBarCommand" prepend make-NSTouchBar-button
227         ] [ f ] if*
228     ] ;
229
230     ! Rendering
231     METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
232
233     ! Events
234     METHOD: char acceptsFirstMouse: id event [ 0 ] ;
235
236     METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
237
238     METHOD: void mouseExited: id event [ forget-rollover ] ;
239
240     METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
241
242     METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
243
244     METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
245
246     METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
247
248     METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
249
250     METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
251
252     METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
253
254     METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
255
256     METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
257
258     METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
259
260     METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
261
262     METHOD: void keyDown: id event [ self event send-key-down-event ] ;
263
264     METHOD: void keyUp: id event [ self event send-key-up-event ] ;
265
266     METHOD: char validateUserInterfaceItem: id event
267     [
268         self window [
269             event -> action utf8 alien>string validate-action
270             [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
271         ] [ 0 ] if*
272     ] ;
273
274     METHOD: id undo: id event [ self event undo-action send-action$ f ] ;
275
276     METHOD: id redo: id event [ self event redo-action send-action$ f ] ;
277
278     METHOD: id cut: id event [ self event cut-action send-action$ f ] ;
279
280     METHOD: id copy: id event [ self event copy-action send-action$ f ] ;
281
282     METHOD: id paste: id event [ self event paste-action send-action$ f ] ;
283
284     METHOD: id delete: id event [ self event delete-action send-action$ f ] ;
285
286     METHOD: id selectAll: id event [ self event select-all-action send-action$ f ] ;
287
288     METHOD: id newDocument: id event [ self event new-action send-action$ f ] ;
289
290     METHOD: id openDocument: id event [ self event open-action send-action$ f ] ;
291
292     METHOD: id saveDocument: id event [ self event save-action send-action$ f ] ;
293
294     METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ] ;
295
296     METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ] ;
297
298     ! Multi-touch gestures
299     METHOD: void magnifyWithEvent: id event
300     [
301         self event
302         dup -> deltaZ sgn {
303             {  1 [ zoom-in-action send-action$ ] }
304             { -1 [ zoom-out-action send-action$ ] }
305             {  0 [ 2drop ] }
306         } case
307     ] ;
308
309     METHOD: void swipeWithEvent: id event
310     [
311         self event
312         dup -> deltaX sgn {
313             {  1 [ left-action send-action$ ] }
314             { -1 [ right-action send-action$ ] }
315             {  0
316                 [
317                     dup -> deltaY sgn {
318                         {  1 [ up-action send-action$ ] }
319                         { -1 [ down-action send-action$ ] }
320                         {  0 [ 2drop ] }
321                     } case
322                 ]
323             }
324         } case
325     ] ;
326
327     METHOD: char acceptsFirstResponder [ 1 ] ;
328
329     ! Services
330     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
331     [
332         ! We return either self or nil
333         self window [
334             world-focus sendType returnType
335             valid-service? [ self ] [ f ] if
336         ] [ f ] if*
337     ] ;
338
339     METHOD: char writeSelectionToPasteboard: id pboard types: id types
340     [
341         NSStringPboardType types CF>string-array member? [
342             self window [
343                 world-focus gadget-selection
344                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
345             ] [ 0 ] if*
346         ] [ 0 ] if
347     ] ;
348
349     METHOD: char readSelectionFromPasteboard: id pboard
350     [
351         self window :> window
352         window [
353             pboard pasteboard-string
354             [ window user-input 1 ] [ 0 ] if*
355         ] [ 0 ] if
356     ] ;
357
358     ! Text input
359     METHOD: void insertText: id text
360     [
361         self window :> window
362         window [
363             text CF>string window user-input
364         ] when
365     ] ;
366
367     METHOD: char hasMarkedText [ 0 ] ;
368
369     METHOD: NSRange markedRange [ 0 0 <NSRange> ] ;
370
371     METHOD: NSRange selectedRange [ 0 0 <NSRange> ] ;
372
373     METHOD: void setMarkedText: id text selectedRange: NSRange range [ ] ;
374
375     METHOD: void unmarkText [ ] ;
376
377     METHOD: id validAttributesForMarkedText [ NSArray -> array ] ;
378
379     METHOD: id attributedSubstringFromRange: NSRange range [ f ] ;
380
381     METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
382
383     METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ] ;
384
385     METHOD: NSInteger conversationIdentifier [ self alien-address ] ;
386
387     ! Initialization
388     METHOD: void updateFactorGadgetSize: id notification
389     [
390         self window :> window
391         window [
392             self view-dim window dim<< yield
393         ] when
394     ] ;
395
396     METHOD: void doCommandBySelector: SEL selector [ ] ;
397
398     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
399     [
400         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
401         dup dup add-resize-observer
402     ] ;
403
404     METHOD: char isOpaque [ 0 ] ;
405
406     METHOD: void dealloc
407     [
408         self remove-observer
409         self SUPER-> dealloc
410     ] ;
411 ;CLASS>
412
413 : sync-refresh-to-screen ( GLView -- )
414     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
415     CGLSetParameter drop ;
416
417 : <FactorView> ( dim pixel-format -- view )
418     [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
419
420 : save-position ( world window -- )
421     -> frame CGRect-top-left 2array >>window-loc drop ;
422
423 <CLASS: FactorWindowDelegate < NSObject
424
425     METHOD: void windowDidMove: id notification
426     [
427         notification -> object -> contentView window
428         [ notification -> object save-position ] when*
429     ] ;
430
431     METHOD: void windowDidBecomeKey: id notification
432     [
433         notification -> object -> contentView window
434         [ focus-world ] when*
435     ] ;
436
437     METHOD: void windowDidResignKey: id notification
438     [
439         forget-rollover
440         notification -> object -> contentView :> view
441         view window :> window
442         window [
443             view -> isInFullScreenMode 0 =
444             [ window unfocus-world ] when
445         ] when
446     ] ;
447
448     METHOD: char windowShouldClose: id notification [ 1 ] ;
449
450     METHOD: void windowWillClose: id notification
451     [
452         notification -> object -> contentView
453         [ window ungraft ] [ unregister-window ] bi
454     ] ;
455
456     METHOD: void windowDidChangeBackingProperties: id notification
457     [
458
459         notification -> object dup SEL: backingScaleFactor
460         -> respondsToSelector: c-bool> [
461             { double { id SEL } } ?-> backingScaleFactor
462
463             [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
464             [ 1.0 > retina? set-global ] bi
465         ] [ drop ] if
466     ] ;
467 ;CLASS>
468
469 : install-window-delegate ( window -- )
470     FactorWindowDelegate install-delegate ;