ThemeGradient
open class ThemeGradient: NSGradient
ThemeGradient is a NSGradient subclass that dynamically changes its colors
whenever a new theme is make current.
Theme-aware means you don’t need to check any conditions when choosing which gradient to draw. E.g.:
ThemeGradient.rainbowGradient.draw(in: bounds, angle: 0)
The drawing code will draw with different gradient depending on the selected theme. Unless some drawing cache is being done, there’s no need to refresh the UI after changing the current theme.
Defining theme-aware gradients
The recommended way of adding your own dynamic gradients is as follows:
Add a
ThemeGradientclass extension (orTKThemeGradientcategory on Objective-C) to add class methods for your gradients. E.g.:In Swift:
extension ThemeGradient { static var brandGradient: ThemeGradient { return ThemeGradient.gradient(with: #function) } }In Objective-C:
@interface TKThemeGradient (Demo) + (TKThemeGradient*)brandGradient; @end @implementation TKThemeGradient (Demo) + (TKThemeGradient*)brandGradient { return [TKThemeGradient gradientWithSelector:_cmd]; } @endAdd Class Extensions on any
Themeyou want to support (e.g.,LightThemeandDarkTheme-TKLightThemeandTKDarkThemeon Objective-C) to provide instance methods for each theme gradient class method defined on (1). E.g.:In Swift:
extension LightTheme { var brandGradient: NSGradient { return NSGradient(starting: NSColor.white, ending: NSColor.black) } } extension DarkTheme { var brandGradient: NSGradient { return NSGradient(starting: NSColor.black, ending: NSColor.white) } }In Objective-C:
@interface TKLightTheme (Demo) @end @implementation TKLightTheme (Demo) - (NSGradient*)brandGradient { return [[NSGradient alloc] initWithStartingColor:[NSColor whiteColor] endingColor:[NSColor blackColor]]; } @end @interface TKDarkTheme (Demo) @end @implementation TKDarkTheme (Demo) - (NSGradient*)brandGradient { return [[NSGradient alloc] initWithStartingColor:[NSColor blackColor] endingColor:[NSColor whiteColor]]; } @endIf supporting
UserTheme‘s, define properties on user theme files (.theme) for each theme gradient class method defined on (1). E.g.:displayName = Sample User Theme identifier = com.luckymarmot.ThemeKit.SampleUserTheme darkTheme = false orangeSky = rgb(160, 90, 45, .5) brandGradient = linear-gradient($orangeSky, rgb(200, 140, 60))
Fallback colors
Unimplemented properties/methods on target theme class will default to
fallbackGradient. This too, can be customized per theme.
Please check ThemeColor for theme-aware colors and ThemeImage for theme-aware images.
-
ThemeGradientgradient selector used as theme instance method for same selector or, if inexistent, as argument in the theme instance methodthemeAsset(_:).Declaration
Swift
@objc public var themeGradientSelector: Selector? -
Resolved gradient from current theme (dynamically changes with the current theme).
Declaration
Swift
@objc public var resolvedThemeGradient: NSGradient?
-
Create a new ThemeGradient instance for the specified selector.
Declaration
Swift
public class func gradient(with selector: Selector) -> ThemeGradient?Parameters
selectorSelector for color method.
Return Value
A
ThemeGradientinstance for the specified selector. -
Current theme gradient, but respecting view appearance and any window specific theme (if set).
If a
NSWindow.windowThemewas set, it will be used instead. Some views may be using a different appearance than the theme appearance. In thoses cases, gradient won’t be resolved using current theme, but from eitherlightThemeordarkTheme, depending of whether view appearance is light or dark, respectively.Declaration
Swift
public class func gradient(for view: NSView, selector: Selector) -> NSGradient?Parameters
viewA
NSViewinstance.selectorA gradient selector.
Return Value
Resolved gradient for specified selector on given view.
-
Forces dynamic gradient resolution into
resolvedThemeGradientand cache it. You should not need to manually call this function.Declaration
Swift
@objc open func recacheGradient() -
Clear all caches. You should not need to manually call this function.
Declaration
Swift
@objc class open func emptyCache()
View on GitHub
Install in Dash
ThemeGradient Class Reference