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