]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/ui/backend/cocoa/views/views.factor
io.encodings.utf16n: merge with io.encodings.utf16
[factor.git] / basis / ui / backend / cocoa / views / views.factor
index 62e04d8f9eeeaa4d25e819a665bb875e33392241..be51f635b72c4b39b07c3376a57e491899b14bf6 100644 (file)
@@ -1,18 +1,19 @@
 ! 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 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.utf16n
-io.encodings.string classes.struct ;
+cocoa.types cocoa.views combinators continuations
+core-foundation.strings core-graphics core-graphics.types
+core-text debugger io.encodings.string io.encodings.utf16
+io.encodings.utf8 kernel literals math math.order math.parser
+math.rectangles math.vectors namespaces opengl sequences
+splitting threads ui.backend.cocoa.input-methods ui.commands
+ui.gadgets ui.gadgets.editors ui.gadgets.line-support
+ui.gadgets.private ui.gadgets.worlds ui.gestures ui.private
+words ;
+
 IN: ui.backend.cocoa.views
 
 SLOT: window
@@ -43,8 +44,8 @@ CONSTANT: NSFunctionKeyMask   0x800000
 CONSTANT: modifiers {
         { S+ $ NSShiftKeyMask }
         { C+ $ NSControlKeyMask }
-        { A+ $ NSCommandKeyMask }
-        { M+ $ NSAlternateKeyMask }
+        { M+ $ NSCommandKeyMask }
+        { A+ $ NSAlternateKeyMask }
     }
 
 CONSTANT: key-codes
@@ -125,12 +126,6 @@ CONSTANT: key-codes
 : send-action$ ( view event gesture -- )
     [ drop window ] dip over [ send-action ] [ 2drop ] if ;
 
-: add-resize-observer ( observer object -- )
-    [
-        "updateFactorGadgetSize:"
-        "NSViewFrameDidChangeNotification" <NSString>
-    ] dip add-observer ;
-
 : string-or-nil? ( NSString -- ? )
     [ CF>string NSStringPboardType = ] [ t ] if* ;
 
@@ -195,7 +190,7 @@ IMPORT: NSAttributedString
     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 ;
+    0 codepoint-index str subseq utf16n encode length 2 /i ;
 
 :: earlier-caret/mark ( editor -- loc )
     editor editor-caret :> caret
@@ -276,26 +271,24 @@ IMPORT: NSAttributedString
         gadget remove-preedit-info 
     ] when ;
 
+: set-scale-factor ( n -- )
+    [ 1.0 > ] keep f ? gl-scale-factor set-global
+    cached-lines get-global clear-assoc ;
+
 PRIVATE>
 
 <CLASS: FactorView < NSOpenGLView
     COCOA-PROTOCOL: NSTextInputClient
 
     METHOD: void prepareOpenGL [
+        self -> backingScaleFactor set-scale-factor
+        self -> update
+    ] ;
 
-        self SEL: setWantsBestResolutionOpenGLSurface:
-        -> respondsToSelector: c-bool> [
-
-            self 1 { void { id SEL char } } ?-> setWantsBestResolutionOpenGLSurface:
-
-            self { double { id SEL } } ?-> backingScaleFactor
-
-            dup 1.0 > [
-                gl-scale-factor set-global t retina? set-global
-                cached-lines get-global clear-assoc
-            ] [ drop ] if
-
-            self -> update
+    METHOD: void reshape [
+        self window :> window
+        window [
+            self view-dim window dim<<
         ] when
     ] ;
 
@@ -324,7 +317,20 @@ PRIVATE>
     ] ;
 
     ! Rendering
-    METHOD: void drawRect: NSRect rect [ self window [ draw-world ] when* ] ;
+    METHOD: void drawRect: NSRect rect [
+        self window [
+            [ draw-world yield ] [ print-error drop ] recover
+        ] when*
+    ] ;
+
+    ! Light/Dark Mode
+
+!     METHOD: void viewDidChangeEffectiveAppearance [
+!         self -> effectiveAppearance -> name [
+!             CF>string "NSAppearanceNameDarkAqua" =
+!             dark-theme light-theme ? switch-theme-if-default
+!         ] when*
+!     ] ;
 
     ! Events
     METHOD: char acceptsFirstMouse: id event [ 0 ] ;
@@ -632,18 +638,9 @@ PRIVATE>
     METHOD: void doCommandBySelector: SEL selector [ ] ;
 
     ! Initialization
-    METHOD: void updateFactorGadgetSize: id notification
-    [
-        self window :> window
-        window [
-            self view-dim window dim<< yield
-        ] when
-    ] ;
-
     METHOD: id initWithFrame: NSRect frame pixelFormat: id pixelFormat
     [
         self frame pixelFormat SUPER-> initWithFrame:pixelFormat:
-        dup dup add-resize-observer
     ] ;
 
     METHOD: char isOpaque [ 0 ] ;
@@ -660,7 +657,9 @@ PRIVATE>
     CGLSetParameter drop ;
 
 : <FactorView> ( dim pixel-format -- view )
-    [ FactorView ] 2dip <GLView> [ sync-refresh-to-screen ] keep ;
+    [ FactorView ] 2dip <GLView>
+    [ -> backingScaleFactor set-scale-factor ] keep
+    [ sync-refresh-to-screen ] keep ;
 
 : save-position ( world window -- )
     -> frame CGRect-top-left 2array >>window-loc drop ;
@@ -700,14 +699,7 @@ PRIVATE>
 
     METHOD: void windowDidChangeBackingProperties: id notification
     [
-
-        notification -> object dup SEL: backingScaleFactor
-        -> respondsToSelector: c-bool> [
-            { double { id SEL } } ?-> backingScaleFactor
-
-            [ [ 1.0 > ] keep f ? gl-scale-factor set-global ]
-            [ 1.0 > retina? set-global ] bi
-        ] [ drop ] if
+        notification -> object -> backingScaleFactor set-scale-factor
     ] ;
 ;CLASS>