! 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
CONSTANT: modifiers {
{ S+ $ NSShiftKeyMask }
{ C+ $ NSControlKeyMask }
- { A+ $ NSCommandKeyMask }
- { M+ $ NSAlternateKeyMask }
+ { M+ $ NSCommandKeyMask }
+ { A+ $ NSAlternateKeyMask }
}
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* ;
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
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
] ;
] ;
! 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 ] ;
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 ] ;
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 ;
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>