Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TextColor and BackgroundColor #124

Merged
merged 4 commits into from
Oct 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions SwiftyJSONAccelerator/UI/SJTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,37 @@

import Cocoa

extension NSColor {

@inline(__always) public convenience init(RGB hex: UInt32, alpha: CGFloat = 1) {
let red = CGFloat((hex & 0xff0000) >> 16) / 255.0
let green = CGFloat((hex & 0xff00) >> 8) / 255.0
let blue = CGFloat(hex & 0xff) / 255.0

self.init(deviceRed: red, green: green, blue: blue, alpha: alpha)
}
}

extension NSView {

@inline(__always) public var isDarkMode: Bool {
if #available(OSX 10.14, *) {
return effectiveAppearance.name == .darkAqua
}

return false
}
}

/// A textview customization to handle formatting and handling removal of quotes.
class SJTextView: NSTextView {

let lightTextColor = NSColor(RGB: 0x24292d)
let lightBackgroundColor = NSColor(RGB: 0xf6f8fa)

let darkTextColor = NSColor(RGB: 0xd1d5da)
let darkBackgroundColor = NSColor(RGB: 0x24292d)

override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
super.init(frame: frameRect, textContainer: container)
disableAutoReplacement()
Expand All @@ -25,8 +54,19 @@ class SJTextView: NSTextView {
}

internal func updateFormat() {
textStorage?.font = NSFont(name: "Menlo", size: 12)
textColor = NSColor.textColor
textStorage?.font = NSFont(name: "Monaco", size: 12)

let color = NSColor(RGB: 0x07c160)
insertionPointColor = color
selectedTextAttributes = [.backgroundColor: color.withAlphaComponent(0.2)]

if isDarkMode {
textColor = darkTextColor
backgroundColor = darkBackgroundColor
} else {
textColor = lightTextColor
backgroundColor = lightBackgroundColor
}
}

override func paste(_ sender: Any?) {
Expand All @@ -43,4 +83,10 @@ class SJTextView: NSTextView {
isAutomaticDashSubstitutionEnabled = false
isAutomaticTextReplacementEnabled = false
}

override func layout() {
super.layout()

updateFormat()
}
}