在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):luckymarmot/ThemeKit开源软件地址(OpenSource Url):https://github.com/luckymarmot/ThemeKit开源编程语言(OpenSource Language):Swift 99.2%开源软件介绍(OpenSource Introduction):SummaryThemeKit is a lightweight theming library completely written in Swift that provides theming capabilities to both Swift and Objective-C macOS applications. ThemeKit is brought to you with QuickStart
Table of ContentsFeatures
Installation
There are multiple options to include ThemeKit on your project:
UsageSimple UsageAt its simpler usage, applications can be themed with a single line command: In Swift:func applicationWillFinishLaunching(_ notification: Notification) {
/// Apply the dark theme
ThemeManager.darkTheme.apply()
/// or, the light theme
//ThemeManager.lightTheme.apply()
/// or, the 'system' theme, which dynamically changes to light or dark,
/// respecting *System Preferences > General > Appearance* setting.
//ThemeManager.systemTheme.apply()
} In Objective-C:- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {
// Apply the dark theme
TKDarkTheme *darkTheme = TKThemeManager.darkTheme;
[[TKThemeManager sharedManager] setTheme:darkTheme];
} Advanced UsageThe following code will define which windows should be automatically themed ( In Swift:func applicationWillFinishLaunching(_ notification: Notification) {
/// Define default theme.
/// Used on first run. Default: `SystemTheme`.
/// Note: `SystemTheme` is a special theme that resolves to `ThemeManager.lightTheme` or `ThemeManager.darkTheme`,
/// depending on the macOS preference at 'System Preferences > General > Appearance'.
ThemeManager.defaultTheme = ThemeManager.lightTheme
/// Define window theme policy.
ThemeManager.shared.windowThemePolicy = .themeAllWindows
//ThemeManager.shared.windowThemePolicy = .themeSomeWindows(windowClasses: [MyWindow.self])
//ThemeManager.shared.windowThemePolicy = .doNotThemeSomeWindows(windowClasses: [NSPanel.self])
//ThemeManager.shared.windowThemePolicy = .doNotThemeWindows
/// Enable & configure user themes.
/// Will use folder `(...)/Application Support/{your_app_bundle_id}/Themes`.
let applicationSupportURLs = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)
let thisAppSupportURL = URL.init(fileURLWithPath: applicationSupportURLs.first!).appendingPathComponent(Bundle.main.bundleIdentifier!)
let userThemesFolderURL = thisAppSupportURL.appendingPathComponent("Themes")
ThemeManager.shared.userThemesFolderURL = userThemesFolderURL
/// Change the default light and dark theme, used when `SystemTheme` is selected.
//ThemeManager.lightTheme = ThemeManager.shared.theme(withIdentifier: PaperTheme.identifier)!
//ThemeManager.darkTheme = ThemeManager.shared.theme(withIdentifier: "com.luckymarmot.ThemeKit.PurpleGreen")!
/// Apply last applied theme (or the default theme, if no previous one)
ThemeManager.shared.applyLastOrDefaultTheme()
} In Objective-C:- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {
/// Define default theme.
/// Used on first run. Default: `SystemTheme`.
/// Note: `SystemTheme` is a special theme that resolves to `ThemeManager.lightTheme` or `ThemeManager.darkTheme`,
/// depending on the macOS preference at 'System Preferences > General > Appearance'.
[TKThemeManager setDefaultTheme:TKThemeManager.lightTheme];
/// Define window theme policy.
[TKThemeManager sharedManager].windowThemePolicy = TKThemeManagerWindowThemePolicyThemeAllWindows;
//[TKThemeManager sharedManager].windowThemePolicy = TKThemeManagerWindowThemePolicyThemeSomeWindows;
//[TKThemeManager sharedManager].themableWindowClasses = @[[MyWindow class]];
//[TKThemeManager sharedManager].windowThemePolicy = TKThemeManagerWindowThemePolicyDoNotThemeSomeWindows;
//[TKThemeManager sharedManager].notThemableWindowClasses = @[[NSPanel class]];
//[TKThemeManager sharedManager].windowThemePolicy = TKThemeManagerWindowThemePolicyDoNotThemeWindows;
/// Enable & configure user themes.
/// Will use folder `(...)/Application Support/{your_app_bundle_id}/Themes`.
NSArray<NSString*>* applicationSupportURLs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSURL* thisAppSupportURL = [[NSURL fileURLWithPath:applicationSupportURLs.firstObject] URLByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier];
NSURL* userThemesFolderURL = [thisAppSupportURL URLByAppendingPathComponent:@"Themes"];
[TKThemeManager sharedManager].userThemesFolderURL = userThemesFolderURL;
/// Change the default light and dark theme, used when `SystemTheme` is selected.
//TKThemeManager.lightTheme = [[TKThemeManager sharedManager] themeWithIdentifier:PaperTheme.identifier];
//TKThemeManager.darkTheme = [[TKThemeManager sharedManager] themeWithIdentifier:@"com.luckymarmot.ThemeKit.PurpleGreen"];
/// Apply last applied theme (or the default theme, if no previous one)
[[TKThemeManager sharedManager] applyLastOrDefaultTheme];
} Please check the Demo application source code for a more complete usage example of ThemeKit. Observing theme changesThemeKit provides the following notifications:
Example: // Register to be notified of theme changes
NotificationCenter.default.addObserver(self, selector: #selector(changedTheme(_:)), name: .didChangeTheme, object: nil)
@objc private func changedTheme(_ notification: Notification) {
// ...
} Additionally, the following properties are KVO compliant:
Example: // Register for KVO changes on ThemeManager.shared.effectiveTheme
ThemeManager.shared.addObserver(self, forKeyPath: "effectiveTheme", options: NSKeyValueObservingOptions.init(rawValue: 0), context: nil)
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "effectiveTheme" {
// ...
}
} Manually theming windowsIn case ( NSWindow Extension
Theme-aware Assets
Additionally, named colors from the For example, a project defines a
Similarly, defining a The NSColor Extension may be useful when overriding colors in ThemeColor extensions. Fallback AssetsThemeKit provides a simple fallback mechanism when looking up assets in the current theme. It will search for assets, in order:
However, for overridden system named colors, the fallback mechanism is different and simpler:
Please refer to Creating ThemesNative ThemesFor creating additional themes, you only need to create a class that conforms to the Sample theme: import Cocoa
import ThemeKit
class MyOwnTheme: NSObject, Theme {
/// Light theme identifier (static).
public static var identifier: String = "com.luckymarmot.ThemeKit.MyOwnTheme"
/// Unique theme identifier.
public var identifier: String = MyOwnTheme.identifier
/// Theme display name.
public var displayName: String = "My Own Theme"
/// Theme short display name.
public var shortDisplayName: String = "My Own"
/// Is this a dark theme?
public var isDarkTheme: Bool = false
/// Description (optional).
public override var description : String {
return "<\(MyOwnTheme.self): \(themeDescription(self))>"
}
// MARK: -
// MARK: Theme Assets
// Here you can define the instance methods for the class methods defined
// on `ThemeColor`, `ThemeGradient` and `ThemeImage`, if any. Check
// documentation of these classes for more details.
} User ThemesThemeKit also supports definition of additional themes with simple text files ( // ************************* Theme Info ************************* //
displayName = My Theme 1
identifier = com.luckymarmot.ThemeKit.MyTheme1
darkTheme = true
// ********************* Colors & Gradients ********************* //
# define color for `ThemeColor.brandColor`
brandColor = $blue
# define a new color for `NSColor.labelColor` (overriding)
labelColor = rgb(11, 220, 111)
# define gradient for `ThemeGradient.brandGradient`
brandGradient = linear-gradient($orange.sky, rgba(200, 140, 60, 1.0))
// ********************* Images & Patterns ********************** //
# define pattern image from named image "paper" for color `ThemeColor.contentBackgroundColor`
contentBackgroundColor = pattern(named:paper)
# define pattern image from filesystem (relative to user themes folder) for color `ThemeColor.bottomBackgroundColor`
bottomBackgroundColor = pattern(file:../some/path/some-file.png)
# define image using named image "apple"
namedImage = image(named:apple)
# define image using from filesystem (relative to user themes folder)
fileImage = image(file:../some/path/some-file.jpg)
// *********************** Common Colors ************************ //
blue = rgb(0, 170, 255)
orange.sky = rgb(160, 90, 45, .5)
// ********************** Fallback Assets *********************** //
fallbackForegroundColor = rgb(255, 10, 90, 1.0)
fallbackBackgroundColor = rgb(255, 200, 190)
fallbackGradient = linear-gradient($blue, rgba(200, 140, 60, 1.0)) To enable support for user themes, just need to set the location for them: // Setup ThemeKit user themes folder
ThemeManager.shared.userThemesFolderURL = //... Please refer to FAQWhere can I find the API documentation?Documentation can be found here. You can also install it on Dash. |