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

Proposal: Improve Theme System usability #61

Open
NoahKamara opened this issue May 30, 2024 · 0 comments
Open

Proposal: Improve Theme System usability #61

NoahKamara opened this issue May 30, 2024 · 0 comments

Comments

@NoahKamara
Copy link

Current problem:

the use of subscript(dynamicMember member: String) obfuscates which properties are available.
this makes it hard to get started with theming.

SwiftUI's Environment provides a nice solution to that exact problem. The result is a type safe key that is easy to implement in custom nodes and easy to use when theming.

I think it would be relatively easy adapting it to this library (I was able to do naive refactor in just a few minutes)

protocol ThemeKey {
  static var defaultValue: AttributedDict { get }
}

struct Theme {
  private var attributes: [ObjectIdentifier: AttributeDict] = [:]

    public subscript<Key: ThemeKey>(_ key: Key.Type) -> AttributeDict {
        get {
            return attributes[ObjectIdentifier(key)] ?? Key.defaultValue
        }
        set {
            attributes[ObjectIdentifier(key)] = newValue
        }
    }
}

An implementation for the link node could look like this:

fileprivate struct LinkThemeKey {
  static let defaultValue: AttributedDict = [.foregroundColor: UIColor.blue]
}

extension Theme {
  var link: AttributedDict {
    get { 
      self[LinkThemeKey.self] 
    }
    set { 
      self[LinkThemeKey.self] = newValue
    }
  }
}

Is there an interest in moving the library towards a more typed approach like this?
I would be willing to work on this myself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant