Skip to content

Commit 9a27568

Browse files
committed
Dark mode support
- use textColor / textBackgroundColor, where sensible - make play button a template image - intercept theme text color and use NSColor.textColor instead
1 parent a8aa7e0 commit 9a27568

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

RustPlayground/RustPlayground/Assets/Assets.xcassets/PlayIcon.imageset/Contents.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"info" : {
2020
"version" : 1,
2121
"author" : "xcode"
22+
},
23+
"properties" : {
24+
"template-rendering-intent" : "template"
2225
}
2326
}

RustPlayground/RustPlayground/EditView.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class EditView: NSView {
5252

5353
override func draw(_ dirtyRect: NSRect) {
5454
guard let lines = lineSource, lines.totalLines > 0 else { return }
55-
NSColor.white.setFill()
56-
dirtyRect.fill()
5755

5856
let font = EditorPreferences.shared.editorFont
5957
let linespace = font.linespace
@@ -65,7 +63,7 @@ class EditView: NSView {
6563

6664
for lineNumber in first..<last {
6765
let line = lines.getLine(line: UInt32(lineNumber)) ?? RawLine.placeholder()
68-
let attrString = NSMutableAttributedString(string: line.text, attributes: [.font: font, .foregroundColor: NSColor.black])
66+
let attrString = NSMutableAttributedString(string: line.text, attributes: [.font: font, .foregroundColor: NSColor.textColor])
6967

7068
for styleSpan in line.styles {
7169
let range = NSRange(location: styleSpan.start, length: styleSpan.len)
@@ -93,7 +91,7 @@ class EditView: NSView {
9391
let path = NSBezierPath()
9492
path.move(to: NSPoint(x: X_OFFSET + cursorPos, y: selY))
9593
path.line(to: NSPoint(x: X_OFFSET + cursorPos, y: selY + linespace))
96-
NSColor.black.setStroke()
94+
NSColor.textColor.setStroke()
9795
path.stroke()
9896
}
9997

@@ -229,7 +227,8 @@ extension NSMutableAttributedString {
229227

230228
var attrs = [NSAttributedString.Key : Any]()
231229
if style.foreground.alphaComponent != 0 {
232-
attrs[.foregroundColor] = style.foreground
230+
let color = style.foreground.isTextColor ? NSColor.textColor : style.foreground
231+
attrs[.foregroundColor] = color
233232
}
234233

235234
//FIXME: background color is always set, plus is paints over cursors.
@@ -252,3 +251,10 @@ extension NSMutableAttributedString {
252251
self.addAttributes(attrs, range: utf16Range)
253252
}
254253
}
254+
255+
extension NSColor {
256+
var isTextColor: Bool {
257+
let themeTextGray: CGFloat = 0.19607843137254902
258+
return self.blueComponent == themeTextGray && self.greenComponent == themeTextGray && self.redComponent == themeTextGray
259+
}
260+
}

RustPlayground/RustPlayground/EditViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class EditViewController: NSViewController {
7676
} else {
7777
mode = nil
7878
}
79+
scrollView.drawsBackground = true
80+
scrollView.backgroundColor = NSColor.textBackgroundColor
7981
}
8082

8183
override func viewWillAppear() {

RustPlayground/RustPlayground/OutputViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class OutputViewController: NSViewController {
4040
self.outputTextView.font = self.outputFont
4141
}
4242

43-
let attributes = attributes ?? [.foregroundColor: NSColor.black]
43+
let attributes = attributes ?? [.foregroundColor: NSColor.textColor]
4444
let insertedRange = NSRange(location: EOFRange.location, length: string.count)
4545
self.outputTextView.textStorage?.addAttributes(attributes, range: insertedRange)
4646
}

0 commit comments

Comments
 (0)