Skip to content

Commit

Permalink
Build Theme module cross-platform
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 6, 2024
1 parent 58f16dd commit c5220eb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 42 deletions.
10 changes: 1 addition & 9 deletions Edit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@
C9BDB54B2B00FD89009225FB /* SwiftStandaloneExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9BDB54A2B00FD89009225FB /* SwiftStandaloneExtension.swift */; };
C9BDB5502B00FD89009225FB /* SwiftExtension.appex in Copy Extensions */ = {isa = PBXBuildFile; fileRef = C9BDB5482B00FD89009225FB /* SwiftExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
C9BDB5572B010094009225FB /* ChimeKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9FE52F42A7539B100CACA1A /* ChimeKit.framework */; };
C9BFA1A32BBF4FA200E86487 /* NSUI in Frameworks */ = {isa = PBXBuildFile; productRef = C9BFA1A22BBF4FA200E86487 /* NSUI */; };
C9BFA1B02BC0927A00E86487 /* Glyph in Frameworks */ = {isa = PBXBuildFile; productRef = C9BFA1AF2BC0927A00E86487 /* Glyph */; };
C9C568582B7418B60093C068 /* Lowlight in Frameworks */ = {isa = PBXBuildFile; productRef = C9C568572B7418B60093C068 /* Lowlight */; };
C9CDEB552B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9CDEB542B504485009E252A /* TextSystem+SwiftTreeSitterLayer.swift */; };
Expand Down Expand Up @@ -1142,7 +1141,6 @@
C9FE53A92A76713900CACA1A /* libStatus.a in Frameworks */,
C9FE53942A766AB300CACA1A /* libSearch.a in Frameworks */,
C9FE53932A766AAD00CACA1A /* libInspector.a in Frameworks */,
C9BFA1A32BBF4FA200E86487 /* NSUI in Frameworks */,
C9FE53B22A7677E100CACA1A /* ScrollViewPlus in Frameworks */,
C9409C662A98CC9800B5D898 /* Outline in Frameworks */,
C97918DA2A9E080C0046EAF1 /* ViewPlus in Frameworks */,
Expand Down Expand Up @@ -2291,7 +2289,6 @@
C9B8AA5C2B370A1800C79606 /* SwiftTreeSitter */,
C9CDEB8E2B56ACA3009E252A /* MainOffender */,
C9C568572B7418B60093C068 /* Lowlight */,
C9BFA1A22BBF4FA200E86487 /* NSUI */,
);
productName = Edit;
productReference = C9FE52992A7525D000CACA1A /* Chime.app */;
Expand Down Expand Up @@ -4485,7 +4482,7 @@
repositoryURL = "https://github.com/mattmassicotte/nsui.git";
requirement = {
kind = revision;
revision = d7c7aedb5b6b69cc5561f25c5bb02214cd6101b5;
revision = 928c88c141b8e8cf82b67a965812eca9d235139f;
};
};
C9BFA1AE2BC0927A00E86487 /* XCRemoteSwiftPackageReference "Glyph" */ = {
Expand Down Expand Up @@ -4783,11 +4780,6 @@
package = C9B8A9B52B235EF100C79606 /* XCRemoteSwiftPackageReference "Rearrange" */;
productName = Rearrange;
};
C9BFA1A22BBF4FA200E86487 /* NSUI */ = {
isa = XCSwiftPackageProductDependency;
package = C9BFA1A12BBF4FA200E86487 /* XCRemoteSwiftPackageReference "nsui" */;
productName = NSUI;
};
C9BFA1A42BBF4FD600E86487 /* NSUI */ = {
isa = XCSwiftPackageProductDependency;
package = C9BFA1A12BBF4FA200E86487 /* XCRemoteSwiftPackageReference "nsui" */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "a18832f6f4b2ecdb4982a6946458da0a688f5e9539c5bec95566af78f39c9d23",
"originHash" : "98bea74f59d07a987f233c8d9d6d16c353c1b055d77cce0094ea59b1498f10df",
"pins" : [
{
"identity" : "asyncxpcconnection",
Expand Down Expand Up @@ -135,7 +135,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattmassicotte/nsui.git",
"state" : {
"revision" : "d7c7aedb5b6b69cc5561f25c5bb02214cd6101b5"
"revision" : "928c88c141b8e8cf82b67a965812eca9d235139f"
}
},
{
Expand Down
5 changes: 4 additions & 1 deletion Edit/Modules/Theme/Extensions/NSAppearance+Style.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Cocoa
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit

// From: https://github.com/ChimeHQ/Dusk

Expand Down Expand Up @@ -31,3 +32,5 @@ extension NSAppearance {
}
}
}

#endif
61 changes: 32 additions & 29 deletions Edit/Modules/Theme/Theme.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import SwiftUI
import NSUI

import ColorToolbox
import Utility
Expand Down Expand Up @@ -32,6 +33,7 @@ public struct Theme: Hashable, Sendable {
case inactive
case hover

#if os(macOS)
init(controlActiveState: ControlActiveState) {
switch controlActiveState {
case .active, .key:
Expand All @@ -42,14 +44,15 @@ public struct Theme: Hashable, Sendable {
self = .active
}
}
#endif
}

public struct Context {
public var controlActiveState: ControlActiveState
public var controlActiveState: NSUIControlActiveState
public var hover: Bool
public var colorScheme: ColorScheme

public init(controlActiveState: ControlActiveState = .active, hover: Bool = false, colorScheme: ColorScheme) {
public init(controlActiveState: NSUIControlActiveState = .active, hover: Bool = false, colorScheme: ColorScheme) {
self.controlActiveState = controlActiveState
self.hover = hover
self.colorScheme = colorScheme
Expand All @@ -61,73 +64,71 @@ public struct Theme: Hashable, Sendable {
}

extension Theme {
public func color(for target: Target, context: Context) -> NSColor {
public func color(for target: Target, context: Context) -> NSUIColor {
switch target {
case .source:
NSColor.textColor
NSUIColor.label
case .insertionPoint:
NSColor.textColor
NSUIColor.label
case .background:
NSColor.windowBackgroundColor
NSUIColor.systemBackground
case .statusBackground:
NSColor.green
NSUIColor.green
case .statusLabel:
NSColor.white
NSUIColor.white
case let .syntaxSpecifier(name):
resolveSyntaxColor(for: name)
}
}

private func resolveSyntaxColor(for specifier: String) -> NSColor {
syntaxColor(for: specifier) ?? NSColor.textColor
private func resolveSyntaxColor(for specifier: String) -> NSUIColor {
syntaxColor(for: specifier) ?? NSUIColor.label
}

private func syntaxColor(for name: String) -> NSColor? {
private func syntaxColor(for name: String) -> NSUIColor? {
switch name {
case "type":
NSColor(hex: "#8FBCBB")
NSUIColor(hex: "#8FBCBB")
case "member.constructor", "invocation.function", "member.method":
NSColor(hex: "#88C0D0")
NSUIColor(hex: "#88C0D0")
case "parameter", "member.property":
NSColor(hex: "#D8DEE9")
NSUIColor(hex: "#D8DEE9")
case "invocation.macro":
NSColor(hex: "#526B9E")
NSUIColor(hex: "#526B9E")
case "keyword.return", "keyword.function", "keyword", "keyword.loop", "keyword.include", "keyword.conditional":
NSColor(hex: "#81A1C1")
NSUIColor(hex: "#81A1C1")
case "keyword.operator.text", "keyword.operator":
NSColor(hex: "#81A1C1")
NSUIColor(hex: "#81A1C1")
case "label":
NSColor(hex: "#526B9E")
NSUIColor(hex: "#526B9E")
case "comment":
NSColor(hex: "#4C566A")
NSUIColor(hex: "#4C566A")
case "literal.string", "literal.regex":
NSColor(hex: "#A3BE8C")
NSUIColor(hex: "#A3BE8C")
case "literal.boolean", "literal.float", "literal.number":
NSColor(hex: "#B48EAD")
NSUIColor(hex: "#B48EAD")
case "variable", "variable.builtin":
NSColor(hex: "#D8DEE9")
NSUIColor(hex: "#D8DEE9")
default:
nil
}
}
}

extension Theme {
private var defaultFont: NSFont {
NSFont(name: "SF Mono", size: 12.0) ?? .monospacedSystemFont(ofSize: 12.0, weight: .regular)
private var defaultFont: NSUIFont {
NSUIFont(name: "SF Mono", size: 12.0) ?? .monospacedSystemFont(ofSize: 12.0, weight: .regular)
}

public func font(for target: Target, context: Context) -> NSFont {
public func font(for target: Target, context: Context) -> NSUIFont {
defaultFont
}
}

extension Theme {
public var isDark: Bool {
// TODO: this is not correct...
guard let color = NSColor.windowBackgroundColor.usingColorSpace(.deviceRGB) else { return false }

return color.brightnessComponent < 0.5
// TODO: this is not a great way to calculate the theme being dark
NSUIColor.systemBackground.relativeLuminance > 0.5
}
}

Expand All @@ -152,6 +153,7 @@ extension Theme {
}

extension Theme.Context {
#if os(macOS)
@MainActor
public init(window: NSWindow?) {
self.init(appearance: window?.appearance)
Expand All @@ -163,4 +165,5 @@ extension Theme.Context {

self.init(controlActiveState: .inactive, hover: false, colorScheme: dark ? .dark : .light)
}
#endif
}
2 changes: 1 addition & 1 deletion Edit/Modules/Theme/ThemeMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct ThemeMonitor<Content: View>: View {
typealias ThemeUpdateHandler = (Theme, Theme.Context) -> Void

@Environment(\.theme) private var theme
@Environment(\.controlActiveState) private var controlActiveState
@Environment(\.nsuiControlActiveState) private var controlActiveState
@Environment(\.colorScheme) private var colorScheme
let content: Content
let themeUpdateAction: ThemeUpdateHandler
Expand Down

0 comments on commit c5220eb

Please sign in to comment.