Skip to content

Commit

Permalink
Dark mode support
Browse files Browse the repository at this point in the history
- use textColor / textBackgroundColor, where sensible
- make play button a template image
- intercept theme text color and use NSColor.textColor instead
  • Loading branch information
cmyr committed Apr 27, 2019
1 parent a8aa7e0 commit 9a27568
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"info" : {
"version" : 1,
"author" : "xcode"
},
"properties" : {
"template-rendering-intent" : "template"
}
}
16 changes: 11 additions & 5 deletions RustPlayground/RustPlayground/EditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ class EditView: NSView {

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

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

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

for styleSpan in line.styles {
let range = NSRange(location: styleSpan.start, length: styleSpan.len)
Expand Down Expand Up @@ -93,7 +91,7 @@ class EditView: NSView {
let path = NSBezierPath()
path.move(to: NSPoint(x: X_OFFSET + cursorPos, y: selY))
path.line(to: NSPoint(x: X_OFFSET + cursorPos, y: selY + linespace))
NSColor.black.setStroke()
NSColor.textColor.setStroke()
path.stroke()
}

Expand Down Expand Up @@ -229,7 +227,8 @@ extension NSMutableAttributedString {

var attrs = [NSAttributedString.Key : Any]()
if style.foreground.alphaComponent != 0 {
attrs[.foregroundColor] = style.foreground
let color = style.foreground.isTextColor ? NSColor.textColor : style.foreground
attrs[.foregroundColor] = color
}

//FIXME: background color is always set, plus is paints over cursors.
Expand All @@ -252,3 +251,10 @@ extension NSMutableAttributedString {
self.addAttributes(attrs, range: utf16Range)
}
}

extension NSColor {
var isTextColor: Bool {
let themeTextGray: CGFloat = 0.19607843137254902
return self.blueComponent == themeTextGray && self.greenComponent == themeTextGray && self.redComponent == themeTextGray
}
}
2 changes: 2 additions & 0 deletions RustPlayground/RustPlayground/EditViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class EditViewController: NSViewController {
} else {
mode = nil
}
scrollView.drawsBackground = true
scrollView.backgroundColor = NSColor.textBackgroundColor
}

override func viewWillAppear() {
Expand Down
2 changes: 1 addition & 1 deletion RustPlayground/RustPlayground/OutputViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OutputViewController: NSViewController {
self.outputTextView.font = self.outputFont
}

let attributes = attributes ?? [.foregroundColor: NSColor.black]
let attributes = attributes ?? [.foregroundColor: NSColor.textColor]
let insertedRange = NSRange(location: EOFRange.location, length: string.count)
self.outputTextView.textStorage?.addAttributes(attributes, range: insertedRange)
}
Expand Down

0 comments on commit 9a27568

Please sign in to comment.