Skip to content

Commit

Permalink
Theme dark mode support + loading colors from xcassets file (#8)
Browse files Browse the repository at this point in the history
1. Added dark mode support.
2. Loading colors from xcassets file.
3. A way to add more Themes either from App's Bundle or from Package's
Bundle (module)."
  • Loading branch information
saharpepper authored May 25, 2024
1 parent ebf5427 commit cde16fc
Show file tree
Hide file tree
Showing 27 changed files with 737 additions and 123 deletions.
1 change: 0 additions & 1 deletion DemoApp/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class EditorViewController: NSViewController {
NeonPlugin(theme: .default, language: .swift)
)

textView.backgroundColor = .controlBackgroundColor
textView.font = .monospacedSystemFont(ofSize: 0, weight: .regular)

textView.string = """
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ let package = Package(
.product(name: "STTextView", package: "STTextView"),
"Neon",
.product(name: "TreeSitter", package: "tree-sitter-xcframework")
]
],
path: "Sources/NeonPlugin",
resources: [.process("Themes.xcassets")]
)
]
)
18 changes: 9 additions & 9 deletions Sources/NeonPlugin/Coordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ public class Coordinator {
}

// set textview default font to theme default font
textView.font = theme.tokens[.default]?.font?.value ?? textView.font
textView.font = theme.defaultFont ?? textView.font

highlighter = Neon.Highlighter(textInterface: STTextViewSystemInterface(textView: textView) { neonToken in
var attributes: [NSAttributedString.Key: Any] = [:]
if let tvFont = textView.font {
attributes[.font] = tvFont
}

if let themeValue = theme.tokens[TokenName(neonToken.name)] {
attributes[.foregroundColor] = themeValue.color.value
if let themeColor = theme.color(forToken: TokenName(neonToken.name)) {
attributes[.foregroundColor] = themeColor

if let font = themeValue.font?.value {
attributes[.font] = font
if let themeFont = theme.font(forToken: TokenName(neonToken.name)) {
attributes[.font] = themeFont
}
} else if let themeValue = theme.tokens[.default]{
attributes[.foregroundColor] = themeValue.color.value
} else if let themeDefaultColor = theme.defaultColor {
attributes[.foregroundColor] = themeDefaultColor

if let font = themeValue.font?.value {
attributes[.font] = font
if let themeFont = theme.font(forToken: TokenName(neonToken.name)) {
attributes[.font] = themeFont
}
}

Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions Sources/NeonPlugin/Theme+Default.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation
import AppKit

extension Theme {

public static let `default` = Theme(
colors: Colors(bundle: Bundle.module, name: "neon.plugin.default"),
tokensCustomFontDict: [
"default": NSFont.monospacedSystemFont(ofSize: 0, weight: .regular),
"keyword": NSFont.monospacedSystemFont(ofSize: 0, weight: .bold),
"constructor": NSFont.monospacedSystemFont(ofSize: 0, weight: .bold)

]
)
}
49 changes: 49 additions & 0 deletions Sources/NeonPlugin/Theme.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Cocoa

public struct Theme {

// MARK: - Props
let colors: Colors
let tokensCustomFontDict: [TokenName: NSFont]

// MARK: - Lifecycle
public init(colors: Colors, tokensCustomFontDict: [TokenName: NSFont] = [:]) {
self.colors = colors
self.tokensCustomFontDict = tokensCustomFontDict
}

public var defaultFont: NSFont? {
return tokensCustomFontDict["default"]
}

public var defaultColor: NSColor? {
return color(forToken: "default")
}

public func color(forToken tokenName: TokenName) -> NSColor? {
return colors.color(forToken: tokenName)
}

public func font(forToken tokenName: TokenName) -> NSFont? {
return tokensCustomFontDict[tokenName]
}
}

extension Theme {

public struct Colors {

let bundle: Bundle
let name: String

public init(bundle: Bundle, name: String) {
self.bundle = bundle
self.name = name
}

func color(forToken tokenName: TokenName) -> NSColor? {
let colorName = "\(name)/\(tokenName)"
return NSColor(named: colorName, bundle: bundle)
}
}
}
112 changes: 0 additions & 112 deletions Sources/NeonPlugin/Theme/Theme.swift

This file was deleted.

6 changes: 6 additions & 0 deletions Sources/NeonPlugin/Themes.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"provides-namespace" : true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "147",
"green" : "35",
"red" : "155"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "108",
"green" : "220",
"red" : "100"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "0.500",
"blue" : "0x00",
"green" : "0x00",
"red" : "0x00"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "0.550",
"blue" : "255",
"green" : "255",
"red" : "255"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"colors" : [
{
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "147",
"green" : "35",
"red" : "155"
}
},
"idiom" : "universal"
},
{
"appearances" : [
{
"appearance" : "luminosity",
"value" : "dark"
}
],
"color" : {
"color-space" : "srgb",
"components" : {
"alpha" : "1.000",
"blue" : "108",
"green" : "220",
"red" : "100"
}
},
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading

0 comments on commit cde16fc

Please sign in to comment.