]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/backend/cocoa/views/views.factor
io.encodings.utf16: add a utf16n word for native utf16 type.
[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.utf16
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 CONSTANT: NSNotFound 9223372036854775807 inline
191
192 IMPORT: NSAttributedString
193
194 <PRIVATE
195
196 :: >codepoint-index ( str utf16-index -- codepoint-index )
197     0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
198
199 :: >utf16-index ( str codepoint-index -- utf16-index )
200     0 codepoint-index str subseq utf16n encode length 2 / >integer ;
201
202 :: earlier-caret/mark ( editor -- loc )
203     editor editor-caret :> caret
204     editor editor-mark :> mark
205     caret first mark first = [
206         caret second mark second < [ caret ] [ mark ] if
207     ] [
208         caret first mark first < [ caret ] [ mark ] if
209     ] if ;
210
211 :: make-preedit-underlines ( gadget text range -- underlines )
212     f gadget preedit-selection-mode?<<
213     { } clone :> underlines!
214     text -> length :> text-length
215     0 0 <NSRange> :> effective-range
216     text -> string CF>string :> str
217     str utf16n encode :> byte-16n
218     0 :> cp-loc!    
219     "NSMarkedClauseSegment" <NSString> :> segment-attr
220     [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
221         text
222         segment-attr
223         effective-range [ location>> ] [ length>> ] bi +
224         effective-range >c-ptr
225         -> attribute:atIndex:effectiveRange: drop
226         1 :> thickness!
227         range location>> effective-range location>> = [
228             2 thickness!
229             t gadget preedit-selection-mode?<<
230         ] when
231         underlines
232         effective-range [ location>> ] [ length>> ] bi over +
233         [ str swap >codepoint-index ] bi@ swap - :> len
234         cp-loc cp-loc len + dup cp-loc!
235         2array thickness 2array
236         suffix underlines! 
237     ] while 
238     underlines length 1 = [
239         underlines first first 2 2array 1array  ! thickness: 2
240     ] [ underlines ] if ;
241
242 :: update-marked-text ( gadget str selectedRange replacementRange -- )
243     replacementRange location>>  NSNotFound = not     ! [ 
244     replacementRange length>> NSNotFound = not and [  ! erase this line
245         gadget editor-caret first
246         dup gadget editor-line
247         [ 
248             replacementRange length>> ! location>>
249             >codepoint-index
250             2array gadget set-caret
251         ] [
252             replacementRange length>> 1 + ! [ location>> ] [ length>> ] bi +
253             >codepoint-index
254             2array gadget set-mark
255         ] 2bi
256         gadget earlier-caret/mark dup
257         gadget preedit-start<<
258         0 1 2array v+ gadget preedit-end<<
259     ] when
260
261     gadget preedit? [
262         gadget remove-preedit-text
263     ] when
264     
265     gadget earlier-caret/mark dup
266     gadget preedit-start<<
267     0 str length 2array v+ gadget preedit-end<<
268     str gadget temp-im-input drop
269     gadget preedit-start>>
270     0 str selectedRange location>> >codepoint-index 2array v+
271     dup gadget preedit-selected-start<<
272     0
273     selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
274     [ str swap >codepoint-index ] bi@ -
275     2array v+
276     dup gadget preedit-selected-end<<
277     dup gadget set-caret gadget set-mark          
278     gadget preedit-start>> gadget preedit-end>> = [
279         gadget remove-preedit-info 
280     ] when ;
281
282 PRIVATE>
283
284 <CLASS: FactorView < NSOpenGLView
285     COCOA-PROTOCOL: NSTextInputClient
286
287     METHOD: void prepareOpenGL [
288
289         self SEL: setWantsBestResolutionOpenGLSurface:
290         -> respondsToSelector: c-bool> [
291
292             self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
293
294             self { double { id SEL } } ?-> backingScaleFactor
295
296             dup 1.0 > [
297                 gl-scale-factor set-global t retina? set-global
298                 cached-lines get-global clear-assoc
299             ] [ drop ] if
300
301             self -> update
302         ] when
303     ] ;
304
305     ! TouchBar
306     METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
307     METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
308     METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
309     METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
310     METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
311     METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
312     METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
313     METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
314
315     METHOD: id makeTouchBar [
316         touchbar-commands drop [
317             length 8 min <iota> [ number>string ] map
318         ] [ { } ] if* self make-touchbar
319     ] ;
320
321     METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
322         touchbar-commands drop [
323             [ self string CF>string dup string>number ] dip nth
324             second name>> "com-" ?head drop over
325             "touchBarCommand" prepend make-NSTouchBar-button
326         ] [ f ] if*
327     ] ;
328
329     ! Rendering
330     METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
331
332     ! Events
333     METHOD: char acceptsFirstMouse: id event [ 0 ] ;
334
335     METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
336
337     METHOD: void mouseExited: id event [ forget-rollover ] ;
338
339     METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
340
341     METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
342
343     METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
344
345     METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
346
347     METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
348
349     METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
350
351     METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
352
353     METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
354
355     METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
356
357     METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
358
359     METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
360
361     METHOD: void keyDown: id event [ self event send-key-down-event ] ;
362
363     METHOD: void keyUp: id event [ self event send-key-up-event ] ;
364
365     METHOD: char validateUserInterfaceItem: id event
366     [
367         self window [
368             event -> action utf8 alien>string validate-action
369             [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
370         ] [ 0 ] if*
371     ] ;
372
373     METHOD: void undo: id event [ self event undo-action send-action$ ] ;
374
375     METHOD: void redo: id event [ self event redo-action send-action$ ] ;
376
377     METHOD: void cut: id event [ self event cut-action send-action$ ] ;
378
379     METHOD: void copy: id event [ self event copy-action send-action$ ] ;
380
381     METHOD: void paste: id event [ self event paste-action send-action$ ] ;
382
383     METHOD: void delete: id event [ self event delete-action send-action$ ] ;
384
385     METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
386
387     METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
388
389     METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
390
391     METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
392
393     METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
394
395     METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
396
397     ! Multi-touch gestures
398     METHOD: void magnifyWithEvent: id event
399     [
400         self event
401         dup -> deltaZ sgn {
402             {  1 [ zoom-in-action send-action$ ] }
403             { -1 [ zoom-out-action send-action$ ] }
404             {  0 [ 2drop ] }
405         } case
406     ] ;
407
408     METHOD: void swipeWithEvent: id event
409     [
410         self event
411         dup -> deltaX sgn {
412             {  1 [ left-action send-action$ ] }
413             { -1 [ right-action send-action$ ] }
414             {  0
415                 [
416                     dup -> deltaY sgn {
417                         {  1 [ up-action send-action$ ] }
418                         { -1 [ down-action send-action$ ] }
419                         {  0 [ 2drop ] }
420                     } case
421                 ]
422             }
423         } case
424     ] ;
425
426     METHOD: char acceptsFirstResponder [ 1 ] ;
427
428     ! Services
429     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
430     [
431         ! We return either self or nil
432         self window [
433             world-focus sendType returnType
434             valid-service? [ self ] [ f ] if
435         ] [ f ] if*
436     ] ;
437
438     METHOD: char writeSelectionToPasteboard: id pboard types: id types
439     [
440         NSStringPboardType types CF>string-array member? [
441             self window [
442                 world-focus gadget-selection
443                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
444             ] [ 0 ] if*
445         ] [ 0 ] if
446     ] ;
447
448     METHOD: char readSelectionFromPasteboard: id pboard
449     [
450         self window :> window
451         window [
452             pboard pasteboard-string
453             [ window user-input 1 ] [ 0 ] if*
454         ] [ 0 ] if
455     ] ;
456
457     ! Text input
458     METHOD: void insertText: id text replacementRange: NSRange replacementRange [
459             self window :> window
460             window [
461                 "" clone :> str!
462                 text NSString -> class -> isKindOfClass: 0 = not [
463                     text CF>string str!               
464                 ] [
465                     text -> string CF>string str!               
466                 ] if
467                 window world-focus :> gadget
468                 gadget [
469                     gadget support-input-methods? [                        
470                         replacementRange location>> NSNotFound = [                        
471                             gadget editor-caret first
472                             dup gadget editor-line
473                             [ 
474                                 replacementRange location>> >codepoint-index
475                                 2array gadget set-caret
476                             ] [
477                                 replacementRange [ location>> ] [ length>> ] bi + 
478                                 >codepoint-index
479                                 2array gadget set-mark
480                             ] 2bi                        
481                         ] unless
482                         gadget preedit? [
483                             gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
484                         ] when
485                         str gadget user-input* drop                    
486                         f gadget preedit-selection-mode?<<
487                     ] [ 
488                         str window user-input
489                     ] if
490                 ] when
491             ] when
492         ] ;
493
494     METHOD: char hasMarkedText [
495             self window :> window
496             window [
497                 window world-focus :> gadget
498                 gadget [
499                     gadget preedit? [ 1 ] [ 0 ] if
500                 ] [ 0 ] if
501             ] [ 0 ] if
502         ] ;
503
504     METHOD: NSRange markedRange [ 
505             self window :> window
506             window [
507                 window world-focus :> gadget
508                 gadget [
509                     gadget preedit? [
510                         gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [ 
511                             NSNotFound 0 
512                         ] [
513                             gadget preedit-start>> first gadget editor-line :> str
514                             gadget
515                             [ preedit-start>> second ]                 ! location
516                             [ preedit-end>> second ]
517                             bi [ str swap >utf16-index ] bi@ over -    ! length
518                         ] if
519                     ] [  NSNotFound 0 ] if
520                 ] [  NSNotFound 0 ] if
521             ] [ NSNotFound 0 ] if
522             <NSRange>
523         ] ;
524
525     METHOD: NSRange selectedRange [
526             self window :> window
527             window [
528                 window world-focus :> gadget
529                 gadget [
530                     gadget support-input-methods? [
531                         gadget editor-caret first gadget editor-line :> str
532                         gadget preedit? [
533                             str
534                             gadget
535                             [ preedit-selected-start>> second ]
536                             [ preedit-start>> second ] 
537                             bi - >utf16-index                        ! location
538                             gadget
539                             [ preedit-selected-end>> second ]
540                             [ preedit-selected-start>> second ]
541                             bi [ str swap >utf16-index ] bi@ -       ! length
542                         ] [                            
543                             str gadget editor-caret second >utf16-index 0
544                         ] if
545                     ] [ 0 0 ] if
546                 ] [ 0 0 ] if
547             ] [ 0 0 ] if 
548             <NSRange>
549         ] ;
550    
551     METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
552                                      replacementRange: NSRange replacementRange [           
553             self window :> window
554             window [
555                 window world-focus :> gadget
556                 gadget [
557                     { } clone :> underlines!
558                     "" clone :> str!
559                     text NSString -> class -> isKindOfClass: 0 = not [
560                         text CF>string str!               
561                     ] [
562                         text -> string CF>string str!               
563                         gadget support-input-methods? [
564                             gadget text selectedRange make-preedit-underlines underlines!
565                         ] when
566                     ] if
567                     gadget support-input-methods? [
568                         gadget str selectedRange replacementRange update-marked-text
569                         underlines gadget preedit-underlines<<
570                     ] when
571                 ] when
572             ] when            
573         ] ;
574              
575     METHOD: void unmarkText [ 
576             self window :> window
577             window [
578                 window world-focus :> gadget
579                 gadget [
580                     gadget support-input-methods? [
581                         gadget preedit? [
582                             gadget {
583                                 [ preedit-start>> second ]
584                                 [ preedit-end>> second ]
585                                 [ preedit-start>> first ] [ editor-line ]
586                             } cleave subseq
587                             gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
588                             gadget user-input* drop
589                         ] when
590                         f gadget preedit-selection-mode?<<
591                     ] when
592                 ] when
593             ] when
594         ] ;
595     
596     METHOD: id validAttributesForMarkedText [             
597             NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
598         ] ;
599
600     METHOD: id attributedSubstringForProposedRange: NSRange aRange
601                                        actualRange: id actualRange [ f ] ;
602     
603     METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
604
605     METHOD: NSRect firstRectForCharacterRange: NSRange aRange
606                                   actualRange: NSRange actualRange [
607             self window :> window
608             window [
609                 window world-focus :> gadget
610                 gadget [
611                     gadget support-input-methods? [
612                         gadget editor-caret first gadget editor-line :> str                
613                         str aRange location>> >codepoint-index :> start-pos
614                         gadget editor-caret first start-pos 2array gadget loc>x
615                         gadget caret-loc second gadget caret-dim second + 
616                         2array                     ! character pos
617                         gadget screen-loc v+       ! + gadget pos 
618                         { 1 -1 } v*
619                         window handle>> window>> dup -> frame -> contentRectForFrameRect:
620                         CGRect-top-left 2array v+  ! + window pos
621                         first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
622                     ] [ 0 0 0 0 ] if
623                 ] [ 0 0 0 0 ] if
624             ] [ 0 0 0 0 ] if
625             <CGRect>
626         ] ;
627
628     ! Initialization
629     METHOD: void updateFactorGadgetSize: id notification
630     [
631         self window :> window
632         window [
633             self view-dim window dim<< yield
634         ] when
635     ] ;
636
637     METHOD: void doCommandBySelector: SEL selector [ ] ;
638
639     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
640     [
641         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
642         dup dup add-resize-observer
643     ] ;
644
645     METHOD: char isOpaque [ 0 ] ;
646
647     METHOD: void dealloc
648     [
649         self remove-observer
650         self SUPER-> dealloc
651     ] ;
652 ;CLASS>
653
654 : sync-refresh-to-screen ( GLView -- )
655     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
656     CGLSetParameter drop ;
657
658 : <FactorView> ( dim pixel-format -- view )
659     [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
660
661 : save-position ( world window -- )
662     -> frame CGRect-top-left 2array >>window-loc drop ;
663
664 <CLASS: FactorWindowDelegate < NSObject
665
666     METHOD: void windowDidMove: id notification
667     [
668         notification -> object -> contentView window
669         [ notification -> object save-position ] when*
670     ] ;
671
672     METHOD: void windowDidBecomeKey: id notification
673     [
674         notification -> object -> contentView window
675         [ focus-world ] when*
676     ] ;
677
678     METHOD: void windowDidResignKey: id notification
679     [
680         forget-rollover
681         notification -> object -> contentView :> view
682         view window :> window
683         window [
684             view -> isInFullScreenMode 0 =
685             [ window unfocus-world ] when
686         ] when
687     ] ;
688
689     METHOD: char windowShouldClose: id notification [ 1 ] ;
690
691     METHOD: void windowWillClose: id notification
692     [
693         notification -> object -> contentView
694         [ window ungraft ] [ unregister-window ] bi
695     ] ;
696
697     METHOD: void windowDidChangeBackingProperties: id notification
698     [
699
700         notification -> object dup SEL: backingScaleFactor
701         -> respondsToSelector: c-bool> [
702             { double { id SEL } } ?-> backingScaleFactor
703
704             [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
705             [ 1.0 > retina? set-global ] bi
706         ] [ drop ] if
707     ] ;
708 ;CLASS>
709
710 : install-window-delegate ( window -- )
711     FactorWindowDelegate install-delegate ;