Skip to content

Commit dacc2f8

Browse files
cntrumpinsanoid
andauthored
feat: update TextColor and BackgroundColor (#124)
* Update TextView color and font * Update TextColor and BackgroundColor * improve dark mode checking Co-authored-by: Karthikeya Udupa <karthikeyaudupa@gmail.com>
1 parent 256ac3e commit dacc2f8

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

SwiftyJSONAccelerator/UI/SJTextView.swift

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,37 @@
88

99
import Cocoa
1010

11+
extension NSColor {
12+
13+
@inline(__always) public convenience init(RGB hex: UInt32, alpha: CGFloat = 1) {
14+
let red = CGFloat((hex & 0xff0000) >> 16) / 255.0
15+
let green = CGFloat((hex & 0xff00) >> 8) / 255.0
16+
let blue = CGFloat(hex & 0xff) / 255.0
17+
18+
self.init(deviceRed: red, green: green, blue: blue, alpha: alpha)
19+
}
20+
}
21+
22+
extension NSView {
23+
24+
@inline(__always) public var isDarkMode: Bool {
25+
if #available(OSX 10.14, *) {
26+
return effectiveAppearance.name == .darkAqua
27+
}
28+
29+
return false
30+
}
31+
}
32+
1133
/// A textview customization to handle formatting and handling removal of quotes.
1234
class SJTextView: NSTextView {
35+
36+
let lightTextColor = NSColor(RGB: 0x24292d)
37+
let lightBackgroundColor = NSColor(RGB: 0xf6f8fa)
38+
39+
let darkTextColor = NSColor(RGB: 0xd1d5da)
40+
let darkBackgroundColor = NSColor(RGB: 0x24292d)
41+
1342
override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
1443
super.init(frame: frameRect, textContainer: container)
1544
disableAutoReplacement()
@@ -25,8 +54,19 @@ class SJTextView: NSTextView {
2554
}
2655

2756
internal func updateFormat() {
28-
textStorage?.font = NSFont(name: "Menlo", size: 12)
29-
textColor = NSColor.textColor
57+
textStorage?.font = NSFont(name: "Monaco", size: 12)
58+
59+
let color = NSColor(RGB: 0x07c160)
60+
insertionPointColor = color
61+
selectedTextAttributes = [.backgroundColor: color.withAlphaComponent(0.2)]
62+
63+
if isDarkMode {
64+
textColor = darkTextColor
65+
backgroundColor = darkBackgroundColor
66+
} else {
67+
textColor = lightTextColor
68+
backgroundColor = lightBackgroundColor
69+
}
3070
}
3171

3272
override func paste(_ sender: Any?) {
@@ -43,4 +83,10 @@ class SJTextView: NSTextView {
4383
isAutomaticDashSubstitutionEnabled = false
4484
isAutomaticTextReplacementEnabled = false
4585
}
86+
87+
override func layout() {
88+
super.layout()
89+
90+
updateFormat()
91+
}
4692
}

0 commit comments

Comments
 (0)