]> gitweb.factorcode.org Git - factor.git/blobdiff - 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
index 949b1354297e5b366df49b04f6de5fa918429cc7..c8838748dcafecf06a298ed50cd2149510738b07 100644 (file)
@@ -1,29 +1,50 @@
 ! Copyright (C) 2006, 2010 Slava Pestov
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien alien.c-types alien.data alien.strings
-arrays assocs cocoa cocoa.application cocoa.classes
-cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
-cocoa.views combinators core-foundation.strings core-graphics
-core-graphics.types core-text io.encodings.utf8 kernel locals
-math math.rectangles namespaces opengl sequences threads
-ui.gadgets ui.gadgets.private ui.gadgets.worlds ui.gestures
-ui.private ;
+arrays assocs classes cocoa cocoa.application cocoa.classes
+cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.touchbar
+cocoa.types cocoa.views combinators core-foundation.strings
+core-graphics core-graphics.types core-text io.encodings.utf8
+kernel literals locals math math.order math.parser
+math.rectangles namespaces opengl sequences splitting threads
+ui.commands ui.gadgets ui.gadgets.private ui.gadgets.worlds
+ui.gestures ui.private words sorting math.vectors
+ui.baseline-alignment ui.gadgets.line-support
+ui.gadgets.editors ui.backend.cocoa.input-methods
+ui.backend.cocoa.input-methods.editors io.encodings.utf16
+io.encodings.string classes.struct ;
 IN: ui.backend.cocoa.views
 
+SLOT: window
+
 : send-mouse-moved ( view event -- )
     [ mouse-location ] [ drop window ] 2bi
-    dup [ move-hand fire-motion yield ] [ 2drop ] if ;
+    [ move-hand fire-motion yield ] [ drop ] if* ;
 
+! Issue #1453
 : button ( event -- n )
-    #! Cocoa -> Factor UI button mapping
-    -> buttonNumber H{ { 0 1 } { 2 2 } { 1 3 } } at ;
-
-CONSTANT: modifiers
-    {
-        { S+ 0x20000 }
-        { C+ 0x40000 }
-        { A+ 0x100000 }
-        { M+ 0x80000 }
+    ! Cocoa -> Factor UI button mapping
+    -> buttonNumber {
+        { 0 [ 1 ] }
+        { 1 [ 3 ] }
+        { 2 [ 2 ] }
+        [ ]
+    } case ;
+
+CONSTANT: NSAlphaShiftKeyMask 0x10000
+CONSTANT: NSShiftKeyMask      0x20000
+CONSTANT: NSControlKeyMask    0x40000
+CONSTANT: NSAlternateKeyMask  0x80000
+CONSTANT: NSCommandKeyMask    0x100000
+CONSTANT: NSNumericPadKeyMask 0x200000
+CONSTANT: NSHelpKeyMask       0x400000
+CONSTANT: NSFunctionKeyMask   0x800000
+
+CONSTANT: modifiers {
+        { S+ $ NSShiftKeyMask }
+        { C+ $ NSControlKeyMask }
+        { A+ $ NSCommandKeyMask }
+        { M+ $ NSAlternateKeyMask }
     }
 
 CONSTANT: key-codes
@@ -64,7 +85,7 @@ CONSTANT: key-codes
     [ event-modifiers ] [ key-code ] bi ;
 
 : send-key-event ( view gesture -- )
-    swap window dup [ propagate-key-gesture ] [ 2drop ] if ;
+    swap window [ propagate-key-gesture ] [ drop ] if* ;
 
 : interpret-key-event ( view event -- )
     NSArray swap -> arrayWithObject: -> interpretKeyEvents: ;
@@ -85,21 +106,21 @@ CONSTANT: key-codes
     [ mouse-location ]
     [ drop window ]
     2tri
-    dup [ send-button-down ] [ 3drop ] if ;
+    [ send-button-down ] [ 2drop ] if* ;
 
 : send-button-up$ ( view event -- )
     [ nip mouse-event>gesture <button-up> ]
     [ mouse-location ]
     [ drop window ]
     2tri
-    dup [ send-button-up ] [ 3drop ] if ;
+    [ send-button-up ] [ 2drop ] if* ;
 
 : send-scroll$ ( view event -- )
     [ nip [ -> deltaX ] [ -> deltaY ] bi [ neg ] bi@ 2array ]
     [ mouse-location ]
     [ drop window ]
     2tri
-    dup [ send-scroll ] [ 3drop ] if ;
+    [ send-scroll ] [ 2drop ] if* ;
 
 : send-action$ ( view event gesture -- )
     [ drop window ] dip over [ send-action ] [ 2drop ] if ;
@@ -146,61 +167,200 @@ CONSTANT: selector>action H{
     selector>action at
     [ swap world-focus parents-handle-gesture? t ] [ drop f f ] if* ;
 
-CLASS: FactorView < NSOpenGLView NSTextInput
-[
+: touchbar-commands ( -- commands/f gadget )
+    world get-global [
+        children>> [
+            class-of "commands" word-prop
+            "touchbar" of dup [ commands>> ] when
+        ] map-find
+    ] [ f f ] if* ;
+
+TUPLE: send-touchbar-command target command ;
+
+M: send-touchbar-command send-queued-gesture
+    [ target>> ] [ command>> ] bi invoke-command ;
+
+: touchbar-invoke-command ( n -- )
+    [ touchbar-commands ] dip over [
+        rot nth second
+        send-touchbar-command queue-gesture notify-ui-thread
+        yield
+    ] [ 3drop ] if ;
+
+CONSTANT: NSNotFound 9223372036854775807 inline
+
+IMPORT: NSAttributedString
+
+<PRIVATE
+
+:: >codepoint-index ( str utf16-index -- codepoint-index )
+    0 utf16-index 2 * str utf16n encode subseq utf16n decode length ;
+
+:: >utf16-index ( str codepoint-index -- utf16-index )
+    0 codepoint-index str subseq utf16n encode length 2 / >integer ;
+
+:: earlier-caret/mark ( editor -- loc )
+    editor editor-caret :> caret
+    editor editor-mark :> mark
+    caret first mark first = [
+        caret second mark second < [ caret ] [ mark ] if
+    ] [
+        caret first mark first < [ caret ] [ mark ] if
+    ] if ;
+
+:: make-preedit-underlines ( gadget text range -- underlines )
+    f gadget preedit-selection-mode?<<
+    { } clone :> underlines!
+    text -> length :> text-length
+    0 0 <NSRange> :> effective-range
+    text -> string CF>string :> str
+    str utf16n encode :> byte-16n
+    0 :> cp-loc!    
+    "NSMarkedClauseSegment" <NSString> :> segment-attr
+    [ effective-range [ location>> ] [ length>> ] bi + text-length < ] [
+        text
+        segment-attr
+        effective-range [ location>> ] [ length>> ] bi +
+        effective-range >c-ptr
+        -> attribute:atIndex:effectiveRange: drop
+        1 :> thickness!
+        range location>> effective-range location>> = [
+            2 thickness!
+            t gadget preedit-selection-mode?<<
+        ] when
+        underlines
+        effective-range [ location>> ] [ length>> ] bi over +
+        [ str swap >codepoint-index ] bi@ swap - :> len
+        cp-loc cp-loc len + dup cp-loc!
+        2array thickness 2array
+        suffix underlines! 
+    ] while 
+    underlines length 1 = [
+        underlines first first 2 2array 1array  ! thickness: 2
+    ] [ underlines ] if ;
+
+:: update-marked-text ( gadget str selectedRange replacementRange -- )
+    replacementRange location>>  NSNotFound = not     ! [ 
+    replacementRange length>> NSNotFound = not and [  ! erase this line
+        gadget editor-caret first
+        dup gadget editor-line
+        [ 
+            replacementRange length>> ! location>>
+            >codepoint-index
+            2array gadget set-caret
+        ] [
+            replacementRange length>> 1 + ! [ location>> ] [ length>> ] bi +
+            >codepoint-index
+            2array gadget set-mark
+        ] 2bi
+        gadget earlier-caret/mark dup
+        gadget preedit-start<<
+        0 1 2array v+ gadget preedit-end<<
+    ] when
+
+    gadget preedit? [
+        gadget remove-preedit-text
+    ] when
+    
+    gadget earlier-caret/mark dup
+    gadget preedit-start<<
+    0 str length 2array v+ gadget preedit-end<<
+    str gadget temp-im-input drop
+    gadget preedit-start>>
+    0 str selectedRange location>> >codepoint-index 2array v+
+    dup gadget preedit-selected-start<<
+    0
+    selectedRange [ location>> ] [ length>> ] bi + selectedRange location>>
+    [ str swap >codepoint-index ] bi@ -
+    2array v+
+    dup gadget preedit-selected-end<<
+    dup gadget set-caret gadget set-mark          
+    gadget preedit-start>> gadget preedit-end>> = [
+        gadget remove-preedit-info 
+    ] when ;
+
+PRIVATE>
+
+<CLASS: FactorView < NSOpenGLView
+    COCOA-PROTOCOL: NSTextInputClient
 
     METHOD: void prepareOpenGL [
 
         self SEL: setWantsBestResolutionOpenGLSurface:
         -> respondsToSelector: c-bool> [
 
-            self SEND: setWantsBestResolutionOpenGLSurface:
-            1 swap execute( x x x -- )
+            self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
+
+            self { double { id SEL } } ?-> backingScaleFactor
 
-            self SEND: backingScaleFactor execute( x x -- x )
             dup 1.0 > [
                 gl-scale-factor set-global t retina? set-global
                 cached-lines get-global clear-assoc
             ] [ drop ] if
 
+            self -> update
         ] when
-    ]
+    ] ;
+
+    ! TouchBar
+    METHOD: void touchBarCommand0 [ 0 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand1 [ 1 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand2 [ 2 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand3 [ 3 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand4 [ 4 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand5 [ 5 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand6 [ 6 touchbar-invoke-command ] ;
+    METHOD: void touchBarCommand7 [ 7 touchbar-invoke-command ] ;
+
+    METHOD: id makeTouchBar [
+        touchbar-commands drop [
+            length 8 min <iota> [ number>string ] map
+        ] [ { } ] if* self make-touchbar
+    ] ;
+
+    METHOD: id touchBar: id touchbar makeItemForIdentifier: id string [
+        touchbar-commands drop [
+            [ self string CF>string dup string>number ] dip nth
+            second name>> "com-" ?head drop over
+            "touchBarCommand" prepend make-NSTouchBar-button
+        ] [ f ] if*
+    ] ;
 
     ! Rendering
-    METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ]
+    METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
 
     ! Events
-    METHOD: char acceptsFirstMouse: id event [ 0 ]
+    METHOD: char acceptsFirstMouse: id event [ 0 ] ;
 
-    METHOD: void mouseEntered: id event [ self event send-mouse-moved ]
+    METHOD: void mouseEntered: id event [ self event send-mouse-moved ] ;
 
-    METHOD: void mouseExited: id event [ forget-rollover ]
+    METHOD: void mouseExited: id event [ forget-rollover ] ;
 
-    METHOD: void mouseMoved: id event [ self event send-mouse-moved ]
+    METHOD: void mouseMoved: id event [ self event send-mouse-moved ] ;
 
-    METHOD: void mouseDragged: id event [ self event send-mouse-moved ]
+    METHOD: void mouseDragged: id event [ self event send-mouse-moved ] ;
 
-    METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ]
+    METHOD: void rightMouseDragged: id event [ self event send-mouse-moved ] ;
 
-    METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ]
+    METHOD: void otherMouseDragged: id event [ self event send-mouse-moved ] ;
 
-    METHOD: void mouseDown: id event [ self event send-button-down$ ]
+    METHOD: void mouseDown: id event [ self event send-button-down$ ] ;
 
-    METHOD: void mouseUp: id event [ self event send-button-up$ ]
+    METHOD: void mouseUp: id event [ self event send-button-up$ ] ;
 
-    METHOD: void rightMouseDown: id event [ self event send-button-down$ ]
+    METHOD: void rightMouseDown: id event [ self event send-button-down$ ] ;
 
-    METHOD: void rightMouseUp: id event [ self event send-button-up$ ]
+    METHOD: void rightMouseUp: id event [ self event send-button-up$ ] ;
 
-    METHOD: void otherMouseDown: id event [ self event send-button-down$ ]
+    METHOD: void otherMouseDown: id event [ self event send-button-down$ ] ;
 
-    METHOD: void otherMouseUp: id event [ self event send-button-up$ ]
+    METHOD: void otherMouseUp: id event [ self event send-button-up$ ] ;
 
-    METHOD: void scrollWheel: id event [ self event send-scroll$ ]
+    METHOD: void scrollWheel: id event [ self event send-scroll$ ] ;
 
-    METHOD: void keyDown: id event [ self event send-key-down-event ]
+    METHOD: void keyDown: id event [ self event send-key-down-event ] ;
 
-    METHOD: void keyUp: id event [ self event send-key-up-event ]
+    METHOD: void keyUp: id event [ self event send-key-up-event ] ;
 
     METHOD: char validateUserInterfaceItem: id event
     [
@@ -208,31 +368,31 @@ CLASS: FactorView < NSOpenGLView NSTextInput
             event -> action utf8 alien>string validate-action
             [ >c-bool ] [ drop self event SUPER-> validateUserInterfaceItem: ] if
         ] [ 0 ] if*
-    ]
+    ] ;
 
-    METHOD: id undo: id event [ self event undo-action send-action$ f ]
+    METHOD: void undo: id event [ self event undo-action send-action$ ] ;
 
-    METHOD: id redo: id event [ self event redo-action send-action$ f ]
+    METHOD: void redo: id event [ self event redo-action send-action$ ] ;
 
-    METHOD: id cut: id event [ self event cut-action send-action$ f ]
+    METHOD: void cut: id event [ self event cut-action send-action$ ] ;
 
-    METHOD: id copy: id event [ self event copy-action send-action$ f ]
+    METHOD: void copy: id event [ self event copy-action send-action$ ] ;
 
-    METHOD: id paste: id event [ self event paste-action send-action$ f ]
+    METHOD: void paste: id event [ self event paste-action send-action$ ] ;
 
-    METHOD: id delete: id event [ self event delete-action send-action$ f ]
+    METHOD: void delete: id event [ self event delete-action send-action$ ] ;
 
-    METHOD: id selectAll: id event [ self event select-all-action send-action$ f ]
+    METHOD: void selectAll: id event [ self event select-all-action send-action$ ] ;
 
-    METHOD: id newDocument: id event [ self event new-action send-action$ f ]
+    METHOD: void newDocument: id event [ self event new-action send-action$ ] ;
 
-    METHOD: id openDocument: id event [ self event open-action send-action$ f ]
+    METHOD: void openDocument: id event [ self event open-action send-action$ ] ;
 
-    METHOD: id saveDocument: id event [ self event save-action send-action$ f ]
+    METHOD: void saveDocument: id event [ self event save-action send-action$ ] ;
 
-    METHOD: id saveDocumentAs: id event [ self event save-as-action send-action$ f ]
+    METHOD: void saveDocumentAs: id event [ self event save-as-action send-action$ ] ;
 
-    METHOD: id revertDocumentToSaved: id event [ self event revert-action send-action$ f ]
+    METHOD: void revertDocumentToSaved: id event [ self event revert-action send-action$ ] ;
 
     ! Multi-touch gestures
     METHOD: void magnifyWithEvent: id event
@@ -243,7 +403,7 @@ CLASS: FactorView < NSOpenGLView NSTextInput
             { -1 [ zoom-out-action send-action$ ] }
             {  0 [ 2drop ] }
         } case
-    ]
+    ] ;
 
     METHOD: void swipeWithEvent: id event
     [
@@ -261,9 +421,9 @@ CLASS: FactorView < NSOpenGLView NSTextInput
                 ]
             }
         } case
-    ]
+    ] ;
 
-    METHOD: char acceptsFirstResponder [ 1 ]
+    METHOD: char acceptsFirstResponder [ 1 ] ;
 
     ! Services
     METHOD: id validRequestorForSendType: id sendType returnType: id returnType
@@ -273,7 +433,7 @@ CLASS: FactorView < NSOpenGLView NSTextInput
             world-focus sendType returnType
             valid-service? [ self ] [ f ] if
         ] [ f ] if*
-    ]
+    ] ;
 
     METHOD: char writeSelectionToPasteboard: id pboard types: id types
     [
@@ -283,7 +443,7 @@ CLASS: FactorView < NSOpenGLView NSTextInput
                 [ pboard set-pasteboard-string 1 ] [ 0 ] if*
             ] [ 0 ] if*
         ] [ 0 ] if
-    ]
+    ] ;
 
     METHOD: char readSelectionFromPasteboard: id pboard
     [
@@ -292,36 +452,178 @@ CLASS: FactorView < NSOpenGLView NSTextInput
             pboard pasteboard-string
             [ window user-input 1 ] [ 0 ] if*
         ] [ 0 ] if
-    ]
+    ] ;
 
     ! Text input
-    METHOD: void insertText: id text
-    [
-        self window :> window
-        window [
-            text CF>string window user-input
-        ] when
-    ]
-
-    METHOD: char hasMarkedText [ 0 ]
-
-    METHOD: NSRange markedRange [ 0 0 <NSRange> ]
-
-    METHOD: NSRange selectedRange [ 0 0 <NSRange> ]
-
-    METHOD: void setMarkedText: id text selectedRange: NSRange range [ ]
-
-    METHOD: void unmarkText [ ]
-
-    METHOD: id validAttributesForMarkedText [ NSArray -> array ]
-
-    METHOD: id attributedSubstringFromRange: NSRange range [ f ]
-
-    METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ]
-
-    METHOD: NSRect firstRectForCharacterRange: NSRange range [ 0 0 0 0 <CGRect> ]
-
-    METHOD: NSInteger conversationIdentifier [ self alien-address ]
+    METHOD: void insertText: id text replacementRange: NSRange replacementRange [
+            self window :> window
+            window [
+                "" clone :> str!
+                text NSString -> class -> isKindOfClass: 0 = not [
+                    text CF>string str!               
+                ] [
+                    text -> string CF>string str!               
+                ] if
+                window world-focus :> gadget
+                gadget [
+                    gadget support-input-methods? [                        
+                        replacementRange location>> NSNotFound = [                        
+                            gadget editor-caret first
+                            dup gadget editor-line
+                            [ 
+                                replacementRange location>> >codepoint-index
+                                2array gadget set-caret
+                            ] [
+                                replacementRange [ location>> ] [ length>> ] bi + 
+                                >codepoint-index
+                                2array gadget set-mark
+                            ] 2bi                        
+                        ] unless
+                        gadget preedit? [
+                            gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
+                        ] when
+                        str gadget user-input* drop                    
+                        f gadget preedit-selection-mode?<<
+                    ] [ 
+                        str window user-input
+                    ] if
+                ] when
+            ] when
+        ] ;
+
+    METHOD: char hasMarkedText [
+            self window :> window
+            window [
+                window world-focus :> gadget
+                gadget [
+                    gadget preedit? [ 1 ] [ 0 ] if
+                ] [ 0 ] if
+            ] [ 0 ] if
+        ] ;
+
+    METHOD: NSRange markedRange [ 
+            self window :> window
+            window [
+                window world-focus :> gadget
+                gadget [
+                    gadget preedit? [
+                        gadget [ preedit-start>> second ] [ preedit-end>> second ] bi >= [ 
+                            NSNotFound 0 
+                        ] [
+                            gadget preedit-start>> first gadget editor-line :> str
+                            gadget
+                            [ preedit-start>> second ]                 ! location
+                            [ preedit-end>> second ]
+                            bi [ str swap >utf16-index ] bi@ over -    ! length
+                        ] if
+                    ] [  NSNotFound 0 ] if
+                ] [  NSNotFound 0 ] if
+            ] [ NSNotFound 0 ] if
+            <NSRange>
+        ] ;
+
+    METHOD: NSRange selectedRange [
+            self window :> window
+            window [
+                window world-focus :> gadget
+                gadget [
+                    gadget support-input-methods? [
+                        gadget editor-caret first gadget editor-line :> str
+                        gadget preedit? [
+                            str
+                            gadget
+                            [ preedit-selected-start>> second ]
+                            [ preedit-start>> second ] 
+                            bi - >utf16-index                        ! location
+                            gadget
+                            [ preedit-selected-end>> second ]
+                            [ preedit-selected-start>> second ]
+                            bi [ str swap >utf16-index ] bi@ -       ! length
+                        ] [                            
+                            str gadget editor-caret second >utf16-index 0
+                        ] if
+                    ] [ 0 0 ] if
+                ] [ 0 0 ] if
+            ] [ 0 0 ] if 
+            <NSRange>
+        ] ;
+   
+    METHOD: void setMarkedText: id text selectedRange: NSRange selectedRange
+                                     replacementRange: NSRange replacementRange [           
+            self window :> window
+            window [
+                window world-focus :> gadget
+                gadget [
+                    { } clone :> underlines!
+                    "" clone :> str!
+                    text NSString -> class -> isKindOfClass: 0 = not [
+                        text CF>string str!               
+                    ] [
+                        text -> string CF>string str!               
+                        gadget support-input-methods? [
+                            gadget text selectedRange make-preedit-underlines underlines!
+                        ] when
+                    ] if
+                    gadget support-input-methods? [
+                        gadget str selectedRange replacementRange update-marked-text
+                        underlines gadget preedit-underlines<<
+                    ] when
+                ] when
+            ] when            
+        ] ;
+             
+    METHOD: void unmarkText [ 
+            self window :> window
+            window [
+                window world-focus :> gadget
+                gadget [
+                    gadget support-input-methods? [
+                        gadget preedit? [
+                            gadget {
+                                [ preedit-start>> second ]
+                                [ preedit-end>> second ]
+                                [ preedit-start>> first ] [ editor-line ]
+                            } cleave subseq
+                            gadget [ remove-preedit-text ] [ remove-preedit-info ] bi
+                            gadget user-input* drop
+                        ] when
+                        f gadget preedit-selection-mode?<<
+                    ] when
+                ] when
+            ] when
+        ] ;
+    
+    METHOD: id validAttributesForMarkedText [             
+            NSArray "NSMarkedClauseSegment" <NSString> -> arrayWithObject:
+        ] ;
+
+    METHOD: id attributedSubstringForProposedRange: NSRange aRange
+                                       actualRange: id actualRange [ f ] ;
+    
+    METHOD: NSUInteger characterIndexForPoint: NSPoint point [ 0 ] ;
+
+    METHOD: NSRect firstRectForCharacterRange: NSRange aRange
+                                  actualRange: NSRange actualRange [
+            self window :> window
+            window [
+                window world-focus :> gadget
+                gadget [
+                    gadget support-input-methods? [
+                        gadget editor-caret first gadget editor-line :> str                
+                        str aRange location>> >codepoint-index :> start-pos
+                        gadget editor-caret first start-pos 2array gadget loc>x
+                        gadget caret-loc second gadget caret-dim second + 
+                        2array                     ! character pos
+                        gadget screen-loc v+       ! + gadget pos 
+                        { 1 -1 } v*
+                        window handle>> window>> dup -> frame -> contentRectForFrameRect:
+                        CGRect-top-left 2array v+  ! + window pos
+                        first2 [ >fixnum ] bi@ 0 gadget line-height >fixnum
+                    ] [ 0 0 0 0 ] if
+                ] [ 0 0 0 0 ] if
+            ] [ 0 0 0 0 ] if
+            <CGRect>
+        ] ;
 
     ! Initialization
     METHOD: void updateFactorGadgetSize: id notification
@@ -330,24 +632,24 @@ CLASS: FactorView < NSOpenGLView NSTextInput
         window [
             self view-dim window dim<< yield
         ] when
-    ]
+    ] ;
 
-    METHOD: void doCommandBySelector: SEL selector [ ]
+    METHOD: void doCommandBySelector: SEL selector [ ] ;
 
     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
     [
         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
         dup dup add-resize-observer
-    ]
+    ] ;
 
-    METHOD: char isOpaque [ 0 ]
+    METHOD: char isOpaque [ 0 ] ;
 
     METHOD: void dealloc
     [
         self remove-observer
         self SUPER-> dealloc
-    ]
-]
+    ] ;
+;CLASS>
 
 : sync-refresh-to-screen ( GLView -- )
     -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 int <ref>
@@ -359,19 +661,19 @@ CLASS: FactorView < NSOpenGLView NSTextInput
 : save-position ( world window -- )
     -> frame CGRect-top-left 2array >>window-loc drop ;
 
-CLASS: FactorWindowDelegate < NSObject
-[
+<CLASS: FactorWindowDelegate < NSObject
+
     METHOD: void windowDidMove: id notification
     [
         notification -> object -> contentView window
         [ notification -> object save-position ] when*
-    ]
+    ] ;
 
     METHOD: void windowDidBecomeKey: id notification
     [
         notification -> object -> contentView window
         [ focus-world ] when*
-    ]
+    ] ;
 
     METHOD: void windowDidResignKey: id notification
     [
@@ -382,27 +684,28 @@ CLASS: FactorWindowDelegate < NSObject
             view -> isInFullScreenMode 0 =
             [ window unfocus-world ] when
         ] when
-    ]
+    ] ;
 
-    METHOD: char windowShouldClose: id notification [ 1 ]
+    METHOD: char windowShouldClose: id notification [ 1 ] ;
 
     METHOD: void windowWillClose: id notification
     [
         notification -> object -> contentView
         [ window ungraft ] [ unregister-window ] bi
-    ]
+    ] ;
 
     METHOD: void windowDidChangeBackingProperties: id notification
     [
 
         notification -> object dup SEL: backingScaleFactor
         -> respondsToSelector: c-bool> [
-            SEND: backingScaleFactor execute( x x -- x )
+            { double { id SEL } } ?-> backingScaleFactor
+
             [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
             [ 1.0 > retina? set-global ] bi
         ] [ drop ] if
-    ]
-]
+    ] ;
+;CLASS>
 
 : install-window-delegate ( window -- )
     FactorWindowDelegate install-delegate ;