]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/views/views.factor
basis/extra: replace "/ >integer" with "/i" in a few places.
[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 sorting math.vectors
12 ui.baseline-alignment ui.gadgets.line-support
13 ui.gadgets.editors ui.backend.cocoa.input-methods
14 ui.backend.cocoa.input-methods.editors io.encodings.utf16n
15 io.encodings.string classes.struct ;
16 IN: ui.backend.cocoa.views
17
18 SLOT: window
19
20 : send-mouse-moved ( view event -- )
21     [ mouse-location ] [ drop window ] 2bi
22     [ move-hand fire-motion yield ] [ drop ] if* ;
23
24 ! Issue #1453
25 : button ( event -- n )
26     ! Cocoa -> Factor UI button mapping
27     -> buttonNumber {
28         { 0 [ 1 ] }
29         { 1 [ 3 ] }
30         { 2 [ 2 ] }
31         [ ]
32     } case ;
33
34 CONSTANT: NSAlphaShiftKeyMask 0x10000
35 CONSTANT: NSShiftKeyMask      0x20000
36 CONSTANT: NSControlKeyMask    0x40000
37 CONSTANT: NSAlternateKeyMask  0x80000
38 CONSTANT: NSCommandKeyMask    0x100000
39 CONSTANT: NSNumericPadKeyMask 0x200000
40 CONSTANT: NSHelpKeyMask       0x400000
41 CONSTANT: NSFunctionKeyMask   0x800000
42
43 CONSTANT: modifiers {
44         { S+ $ NSShiftKeyMask }
45         { C+ $ NSControlKeyMask }
46         { A+ $ NSCommandKeyMask }
47         { M+ $ NSAlternateKeyMask }
48     }
49
50 CONSTANT: key-codes
51     H{
52         { 71 "CLEAR" }
53         { 36 "RET" }
54         { 76 "ENTER" }
55         { 53 "ESC" }
56         { 48 "TAB" }
57         { 51 "BACKSPACE" }
58         { 115 "HOME" }
59         { 117 "DELETE" }
60         { 119 "END" }
61         { 122 "F1" }
62         { 120 "F2" }
63         { 99 "F3" }
64         { 118 "F4" }
65         { 96 "F5" }
66         { 97 "F6" }
67         { 98 "F7" }
68         { 100 "F8" }
69         { 123 "LEFT" }
70         { 124 "RIGHT" }
71         { 125 "DOWN" }
72         { 126 "UP" }
73         { 116 "PAGE_UP" }
74         { 121 "PAGE_DOWN" }
75     }
76
77 : key-code ( event -- string ? )
78     dup -> keyCode key-codes at
79     [ t ] [ -> charactersIgnoringModifiers CF>string f ] ?if ;
80
81 : event-modifiers ( event -- modifiers )
82     -> modifierFlags modifiers modifier ;
83
84 : key-event>gesture ( event -- modifiers keycode action? )
85     [ event-modifiers ] [ key-code ] bi ;
86
87 : send-key-event ( view gesture -- )
88     swap window [ propagate-key-gesture ] [ drop ] if* ;
89
90 : interpret-key-event ( view event -- )
91     NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
92
93 : send-key-down-event ( view event -- )
94     [ key-event>gesture <key-down> send-key-event ]
95     [ interpret-key-event ]
96     2bi ;
97
98 : send-key-up-event ( view event -- )
99     key-event>gesture <key-up> send-key-event ;
100
101 : mouse-event>gesture ( event -- modifiers button )
102     [ event-modifiers ] [ button ] bi ;
103
104 : send-button-down$ ( view event -- )
105     [ nip mouse-event>gesture <button-down> ]
106     [ mouse-location ]
107     [ drop window ]
108     2tri
109     [ send-button-down ] [ 2drop ] if* ;
110
111 : send-button-up$ ( view event -- )
112     [ nip mouse-event>gesture <button-up> ]
113     [ mouse-location ]
114     [ drop window ]
115     2tri
116     [ send-button-up ] [ 2drop ] if* ;
117
118 : send-scroll$ ( view event -- )
119     [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
120     [ mouse-location ]
121     [ drop window ]
122     2tri
123     [ send-scroll ] [ 2drop ] if* ;
124
125 : send-action$ ( view event gesture -- )
126     [ drop window ] dip over [ send-action ] [ 2drop ] if ;
127
128 : add-resize-observer ( observer object -- )
129     [
130         "updateFactorGadgetSize:"
131         "NSViewFrameDidChangeNotification" <NSString>
132     ] dip add-observer ;
133
134 : string-or-nil? ( NSString -- ? )
135     [ CF>string NSStringPboardType = ] [ t ] if* ;
136
137 : valid-service? ( gadget send-type return-type -- ? )
138     2dup [ string-or-nil? ] [ string-or-nil? ] bi* and
139     [ drop [ gadget-selection? ] [ drop t ] if ] [ 3drop f ] if ;
140
141 : NSRect>rect ( NSRect world -- rect )
142     [ [ [ CGRect-x ] [ CGRect-y ] bi ] [ dim>> second ] bi* swap - 2array ]
143     [ drop [ CGRect-w ] [ CGRect-h ] bi 2array ]
144     2bi <rect> ;
145
146 : rect>NSRect ( rect world -- NSRect )
147     [ [ loc>> first2 ] [ dim>> second ] bi* swap - ]
148     [ drop dim>> first2 ]
149     2bi <CGRect> ;
150
151 CONSTANT: selector>action H{
152     { "undo:" undo-action }
153     { "redo:" redo-action }
154     { "cut:" cut-action }
155     { "copy:" copy-action }
156     { "paste:" paste-action }
157     { "delete:" delete-action }
158     { "selectAll:" select-all-action }
159     { "newDocument:" new-action }
160     { "openDocument:" open-action }
161     { "saveDocument:" save-action }
162     { "saveDocumentAs:" save-as-action }
163     { "revertDocumentToSaved:" revert-action }
164 }
165
166 : validate-action ( world selector -- ? validated? )
167     selector>action at
168     [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
169
170 : touchbar-commands ( -- commands/f gadget )
171     world get-global [
172         children>> [
173             class-of "commands" word-prop
174             "touchbar" of dup [ commands>> ] when
175         ] map-find
176     ] [ f f ] if* ;
177
178 TUPLE: send-touchbar-command target command ;
179
180 M: send-touchbar-command send-queued-gesture
181     [ target>> ] [ command>> ] bi invoke-command ;
182
183 : touchbar-invoke-command ( n -- )
184     [ touchbar-commands ] dip over [
185         rot nth second
186         send-touchbar-command queue-gesture notify-ui-thread
187         yield
188     ] [ 3drop ] if ;
189
190 IMPORT: NSAttributedString
191
192 <PRIVATE
193
194 :: >codepoint-index ( str utf16-index -- codepoint-index )
195     0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
196
197 :: >utf16-index ( str codepoint-index -- utf16-index )
198     0 codepoint-index str subseq utf16n encode length 2 /i ;
199
200 :: earlier-caret/mark ( editor -- loc )
201     editor editor-caret :> caret
202     editor editor-mark :> mark
203     caret first mark first = [
204         caret second mark second < [ caret ] [ mark ] if
205     ] [
206         caret first mark first < [ caret ] [ mark ] if
207     ] if ;
208
209 :: make-preedit-underlines ( gadget text range -- underlines )
210     f gadget preedit-selection-mode?<<
211     { } clone :> underlines!
212     text -> length :> text-length
213     0 0 <NSRange> :> effective-range
214     text -> string CF>string :> str
215     str utf16n encode :> byte-16n
216     0 :> cp-loc!
217     "NSMarkedClauseSegment" <NSString> :> segment-attr
218     [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
219         text
220         segment-attr
221         effective-range [ location>> ] [ length>> ] bi +
222         effective-range >c-ptr
223         -> attribute:atIndex:effectiveRange: drop
224         1 :> thickness!
225         range location>> effective-range location>> = [
226             2 thickness!
227             t gadget preedit-selection-mode?<<
228         ] when
229         underlines
230         effective-range [ location>> ] [ length>> ] bi over +
231         [ str swap >codepoint-index ] bi@ swap - :> len
232         cp-loc cp-loc len + dup cp-loc!
233         2array thickness 2array
234         suffix underlines!
235     ] while 
236     underlines length 1 = [
237         underlines first first 2 2array 1array  ! thickness: 2
238     ] [ underlines ] if ;
239
240 :: update-marked-text ( gadget str selectedRange replacementRange -- )
241     replacementRange location>> NSNotFound = [
242         gadget editor-caret first
243         dup gadget editor-line
244         [ 
245             replacementRange location>>
246             >codepoint-index
247             2array gadget set-caret
248         ] [
249             replacementRange [ location>> ] [ length>> ] bi +
250             >codepoint-index
251             2array gadget set-mark
252         ] 2bi
253         gadget earlier-caret/mark dup
254         gadget preedit-start<<
255         0 1 2array v+ gadget preedit-end<<
256     ] unless
257
258     gadget preedit? [
259         gadget remove-preedit-text
260     ] when
261
262     gadget earlier-caret/mark dup
263     gadget preedit-start<<
264     0 str length 2array v+ gadget preedit-end<<
265     str gadget temp-im-input drop
266     gadget preedit-start>>
267     0 str selectedRange location>> >codepoint-index 2array v+
268     dup gadget preedit-selected-start<<
269     0
270     selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
271     [ str swap >codepoint-index ] bi@ -
272     2array v+
273     dup gadget preedit-selected-end<<
274     dup gadget set-caret gadget set-mark
275     gadget preedit-start>> gadget preedit-end>> = [
276         gadget remove-preedit-info 
277     ] when ;
278
279 PRIVATE>
280
281 <CLASS: FactorView < NSOpenGLView
282     COCOA-PROTOCOL: NSTextInputClient
283
284     METHOD: void prepareOpenGL [
285
286         self SEL: setWantsBestResolutionOpenGLSurface:
287         -> respondsToSelector: c-bool> [
288
289             self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
290
291             self { double { id SEL } } ?-> backingScaleFactor
292
293             dup 1.0 > [
294                 gl-scale-factor set-global t retina? set-global
295                 cached-lines get-global clear-assoc
296             ] [ drop ] if
297
298             self -> update
299         ] when
300     ] ;
301
302     ! TouchBar
303     METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
304     METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
305     METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
306     METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
307     METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
308     METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
309     METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
310     METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
311
312     METHOD: id makeTouchBar [
313         touchbar-commands drop [
314             length 8 min <iota> [ number>string ] map
315         ] [ { } ] if* self make-touchbar
316     ] ;
317
318     METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
319         touchbar-commands drop [
320             [ self string CF>string dup string>number ] dip nth
321             second name>> "com-" ?head drop over
322             "touchBarCommand" prepend make-NSTouchBar-button
323         ] [ f ] if*
324     ] ;
325
326     ! Rendering
327     METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
328
329     ! Events
330     METHOD: char acceptsFirstMouse: id event [ 0 ] ;
331
332     METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
333
334     METHOD: void mouseExited: id event [ forget-rollover ] ;
335
336     METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
337
338     METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
339
340     METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
341
342     METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
343
344     METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
345
346     METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
347
348     METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
349
350     METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
351
352     METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
353
354     METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
355
356     METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
357
358     METHOD: void keyDown: id event [ self event send-key-down-event ] ;
359
360     METHOD: void keyUp: id event [ self event send-key-up-event ] ;
361
362     METHOD: char validateUserInterfaceItem: id event
363     [
364         self window :> window
365         window [
366             window world-focus :> gadget
367             gadget [
368                 gadget preedit? not [
369                     window event -> action utf8 alien>string validate-action
370                     [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
371                 ] [ 0 ] if
372             ] [ 0 ] if
373         ] [ 0 ] if
374     ] ;
375
376     METHOD: void undo: id event [ self event undo-action send-action$ ] ;
377
378     METHOD: void redo: id event [ self event redo-action send-action$ ] ;
379
380     METHOD: void cut: id event [ self event cut-action send-action$ ] ;
381
382     METHOD: void copy: id event [ self event copy-action send-action$ ] ;
383
384     METHOD: void paste: id event [ self event paste-action send-action$ ] ;
385
386     METHOD: void delete: id event [ self event delete-action send-action$ ] ;
387
388     METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
389
390     METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
391
392     METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
393
394     METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
395
396     METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
397
398     METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
399
400     ! Multi-touch gestures
401     METHOD: void magnifyWithEvent: id event
402     [
403         self event
404         dup -> deltaZ sgn {
405             {  1 [ zoom-in-action send-action$ ] }
406             { -1 [ zoom-out-action send-action$ ] }
407             {  0 [ 2drop ] }
408         } case
409     ] ;
410
411     METHOD: void swipeWithEvent: id event
412     [
413         self event
414         dup -> deltaX sgn {
415             {  1 [ left-action send-action$ ] }
416             { -1 [ right-action send-action$ ] }
417             {  0
418                 [
419                     dup -> deltaY sgn {
420                         {  1 [ up-action send-action$ ] }
421                         { -1 [ down-action send-action$ ] }
422                         {  0 [ 2drop ] }
423                     } case
424                 ]
425             }
426         } case
427     ] ;
428
429     METHOD: char acceptsFirstResponder [ 1 ] ;
430
431     ! Services
432     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
433     [
434         ! We return either self or nil
435         self window [
436             world-focus sendType returnType
437             valid-service? [ self ] [ f ] if
438         ] [ f ] if*
439     ] ;
440
441     METHOD: char writeSelectionToPasteboard: id pboard types: id types
442     [
443         NSStringPboardType types CF>string-array member? [
444             self window [
445                 world-focus gadget-selection
446                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
447             ] [ 0 ] if*
448         ] [ 0 ] if
449     ] ;
450
451     METHOD: char readSelectionFromPasteboard: id pboard
452     [
453         self window :> window
454         window [
455             pboard pasteboard-string
456             [ window user-input 1 ] [ 0 ] if*
457         ] [ 0 ] if
458     ] ;
459
460     ! Text input
461     METHOD: void insertText: id text replacementRange: NSRange replacementRange [
462         self window :> window
463         window [
464             "" clone :> str!
465             text NSString -> class -> isKindOfClass: 0 = not [
466                 text CF>string str!
467             ] [
468                 text -> string CF>string str!
469             ] if
470             window world-focus :> gadget
471             gadget [
472                 gadget support-input-methods? [
473                     replacementRange location>> NSNotFound = [
474                         gadget editor-caret first
475                         dup gadget editor-line
476                         [ 
477                             replacementRange location>> >codepoint-index
478                             2array gadget set-caret
479                         ] [
480                             replacementRange [ location>> ] [ length>> ] bi +
481                             >codepoint-index
482                             2array gadget set-mark
483                         ] 2bi
484                     ] unless
485                     gadget preedit? [
486                         gadget remove-preedit-text
487                         gadget remove-preedit-info
488                         str gadget user-input* drop
489                         f gadget preedit-selection-mode?<<
490                     ] [
491                         str window user-input
492                     ] if
493                 ] [ 
494                     str window user-input
495                 ] if
496             ] when
497         ] when
498     ] ;
499
500     METHOD: char hasMarkedText [
501         self window :> window
502         window [
503             window world-focus :> gadget
504             gadget [
505                 gadget preedit? 1 0 ?
506             ] [ 0 ] if
507         ] [ 0 ] if
508     ] ;
509
510     METHOD: NSRange markedRange [
511         self window :> window
512         window [
513             window world-focus :> gadget
514             gadget [
515                 gadget preedit? [
516                     gadget preedit-start>> second
517                     gadget preedit-end>> second < [
518                         gadget preedit-start>> first gadget editor-line :> str
519                         gadget preedit-start>> second           ! location
520                         gadget preedit-end>> second
521                         [ str swap >utf16-index ] bi@ over -    ! length
522                     ] [ NSNotFound 0 ] if
523                 ] [ NSNotFound 0 ] if
524             ] [ NSNotFound 0 ] if
525         ] [ NSNotFound 0 ] if
526         <NSRange>
527     ] ;
528
529     METHOD: NSRange selectedRange [
530         self window :> window
531         window [
532             window world-focus :> gadget
533             gadget [
534                 gadget support-input-methods? [
535                     gadget editor-caret first gadget editor-line :> str
536                     gadget preedit? [
537                         str
538                         gadget preedit-selected-start>> second
539                         gadget preedit-start>> second
540                         - >utf16-index                        ! location
541                         gadget preedit-selected-end>> second
542                         gadget preedit-selected-start>> second
543                         [ str swap >utf16-index ] bi@ -       ! length
544                     ] [
545                         str gadget editor-caret second >utf16-index 0
546                     ] if
547                 ] [ 0 0 ] if
548             ] [ 0 0 ] if
549         ] [ 0 0 ] if 
550         <NSRange>
551     ] ;
552
553     METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
554                                      replacementRange: NSRange replacementRange [
555         self window :> window
556         window [
557             window world-focus :> gadget
558             gadget [
559                 { } clone :> underlines!
560                 "" clone :> str!
561                 text NSString -> class -> isKindOfClass: 0 = not [
562                     text CF>string str!
563                 ] [
564                     text -> string CF>string str!
565                     gadget support-input-methods? [
566                         gadget text selectedRange make-preedit-underlines underlines!
567                     ] when
568                 ] if
569                 gadget support-input-methods? [
570                     gadget str selectedRange replacementRange update-marked-text
571                     underlines gadget preedit-underlines<<
572                 ] when
573             ] when
574         ] when
575     ] ;
576
577     METHOD: void unmarkText [
578         self window :> window
579         window [
580             window world-focus :> gadget
581             gadget [
582                 gadget support-input-methods? [
583                     gadget preedit? [
584                         gadget {
585                             [ preedit-start>> second ]
586                             [ preedit-end>> second ]
587                             [ preedit-start>> first ]
588                             [ editor-line ]
589                         } cleave subseq
590                         gadget remove-preedit-text
591                         gadget remove-preedit-info
592                         gadget user-input* drop
593                     ] when
594                     f gadget preedit-selection-mode?<<
595                 ] when
596             ] when
597         ] when
598     ] ;
599
600     METHOD: id validAttributesForMarkedText [
601         NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
602     ] ;
603
604     METHOD: id attributedSubstringForProposedRange: NSRange aRange
605                                        actualRange: id actualRange [ f ] ;
606
607     METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
608
609     METHOD: NSRect firstRectForCharacterRange: NSRange aRange
610                                   actualRange: NSRange actualRange [
611         self window :> window
612         window [
613             window world-focus :> gadget
614             gadget [
615                 gadget support-input-methods? [
616                     gadget editor-caret first gadget editor-line :> str
617                     str aRange location>> >codepoint-index :> start-pos
618                     gadget editor-caret first start-pos 2array gadget loc>x
619                     gadget caret-loc second gadget caret-dim second + 
620                     2array                     ! character pos
621                     gadget screen-loc v+       ! + gadget pos
622                     { 1 -1 } v*
623                     window handle>> window>> dup -> frame -> contentRectForFrameRect:
624                     CGRect-top-left 2array v+  ! + window pos
625                     first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
626                 ] [ 0 0 0 0 ] if
627             ] [ 0 0 0 0 ] if
628         ] [ 0 0 0 0 ] if
629         <CGRect>
630     ] ;
631
632     METHOD: void doCommandBySelector: SEL selector [ ] ;
633
634     ! Initialization
635     METHOD: void updateFactorGadgetSize: id notification
636     [
637         self window :> window
638         window [
639             self view-dim window dim<< yield
640         ] when
641     ] ;
642
643     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
644     [
645         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
646         dup dup add-resize-observer
647     ] ;
648
649     METHOD: char isOpaque [ 0 ] ;
650
651     METHOD: void dealloc
652     [
653         self remove-observer
654         self SUPER-> dealloc
655     ] ;
656 ;CLASS>
657
658 : sync-refresh-to-screen ( GLView -- )
659     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
660     CGLSetParameter drop ;
661
662 : <FactorView> ( dim pixel-format -- view )
663     [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
664
665 : save-position ( world window -- )
666     -> frame CGRect-top-left 2array >>window-loc drop ;
667
668 <CLASS: FactorWindowDelegate < NSObject
669
670     METHOD: void windowDidMove: id notification
671     [
672         notification -> object -> contentView window
673         [ notification -> object save-position ] when*
674     ] ;
675
676     METHOD: void windowDidBecomeKey: id notification
677     [
678         notification -> object -> contentView window
679         [ focus-world ] when*
680     ] ;
681
682     METHOD: void windowDidResignKey: id notification
683     [
684         forget-rollover
685         notification -> object -> contentView :> view
686         view window :> window
687         window [
688             view -> isInFullScreenMode 0 =
689             [ window unfocus-world ] when
690         ] when
691     ] ;
692
693     METHOD: char windowShouldClose: id notification [ 1 ] ;
694
695     METHOD: void windowWillClose: id notification
696     [
697         notification -> object -> contentView
698         [ window ungraft ] [ unregister-window ] bi
699     ] ;
700
701     METHOD: void windowDidChangeBackingProperties: id notification
702     [
703
704         notification -> object dup SEL: backingScaleFactor
705         -> respondsToSelector: c-bool> [
706             { double { id SEL } } ?-> backingScaleFactor
707
708             [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
709             [ 1.0 > retina? set-global ] bi
710         ] [ drop ] if
711     ] ;
712 ;CLASS>
713
714 : install-window-delegate ( window -- )
715     FactorWindowDelegate install-delegate ;