Skip to content

Proposal: Improve Theme System usability #61

@NoahKamara

Description

@NoahKamara

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions