All Projects → dscyrescotti → SwiftTheming

dscyrescotti / SwiftTheming

Licence: MIT license
A powerful lightweight theme 🎨 manager for SwiftUI

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to SwiftTheming

OMJoystick
This is the JoyStick UI library for SwiftUI.
Stars: ✭ 15 (-60.53%)
Mutual labels:  swiftui, swiftpackagemanager
SwiftUI-bez
Utilities for working with bezier curves in SwiftUI
Stars: ✭ 80 (+110.53%)
Mutual labels:  swiftui
AVPlayer-SwiftUI
Using AVPlayer in SwiftUI
Stars: ✭ 204 (+436.84%)
Mutual labels:  swiftui
typora-hivacruz-theme
A Typora theme forked from Cobalt to match my other color schemes.
Stars: ✭ 38 (+0%)
Mutual labels:  color-scheme
TokamakPublish
Use Tokamak in your Publish themes.
Stars: ✭ 19 (-50%)
Mutual labels:  swiftui
QtCreator-Color-Schemes
Color schemes for the QtCreator text editor
Stars: ✭ 64 (+68.42%)
Mutual labels:  color-scheme
icls-vs-code-dark-plus
Visual Studio Code Dark Plus-like color scheme for PhpStorm and other JetBrains IDEs
Stars: ✭ 24 (-36.84%)
Mutual labels:  color-scheme
themeX
The ultimate UNIVERSAL syntax color theme generator that let's you build your color scheme in just one file and compile for a wide range of different editors.
Stars: ✭ 26 (-31.58%)
Mutual labels:  color-scheme
vim-lighthaus
A Lighthaus theme for (n)vim, vim-airline and lightline
Stars: ✭ 33 (-13.16%)
Mutual labels:  color-scheme
cake-atom-syntax
Yummy syntax theme for Atom
Stars: ✭ 17 (-55.26%)
Mutual labels:  color-scheme
awesome-nord
This repository contains community-created ports and themes of this color palette for all our beloved applications.
Stars: ✭ 34 (-10.53%)
Mutual labels:  color-scheme
NXNavigationExtension
🔥 Lightweight, simple, and easy-to-use NavigationBar library.
Stars: ✭ 121 (+218.42%)
Mutual labels:  swiftui
Note.it
A Cut Down, Simple, Text Editor For Mac And iOS. Built With SwiftUI (Mostly).
Stars: ✭ 20 (-47.37%)
Mutual labels:  swiftui
kavsoft-swiftui-animations
SwiftUI animation tutorials, all of demos are consisted of youtube videos at website of kavsoft. 🔗 https://kavsoft.dev
Stars: ✭ 205 (+439.47%)
Mutual labels:  swiftui
FootballDataSwiftUI
Display Football Data such as scores, upcoming match, team standing, top scorers with football Data API and SwiftUI
Stars: ✭ 76 (+100%)
Mutual labels:  swiftui
WWDCNotes
WWDCNotes.com content
Stars: ✭ 343 (+802.63%)
Mutual labels:  swiftui
kcm-colorful
Make your KDE Plasma colorful.
Stars: ✭ 126 (+231.58%)
Mutual labels:  color-scheme
SupportDocs
Generate help centers for your iOS apps. Hosted by GitHub and always up-to-date.
Stars: ✭ 135 (+255.26%)
Mutual labels:  swiftui
material-colors
Material colorscheme
Stars: ✭ 25 (-34.21%)
Mutual labels:  color-scheme
SwiftCurrent
A library for managing complex workflows in Swift
Stars: ✭ 286 (+652.63%)
Mutual labels:  swiftui

Action Status MIT License

SwiftTheming 🎨 is a handy light-weight handy theme manager which handles multiple themes based on system-wide appearances - light and dark appearances and overrides the system appearance for the application.

📱 Demo

this slowpoke moves

You can see the demo project in Example folder.

🎉 Motivation

Imagine that you want to achieve injecting multiple themes and manage them depending on the current system appearance or your preferred appearance. As SwiftUI does not come with the mechanism to manage different themes, you have to come up with it on your own. To me, I want to focus on other time-consuming stuff and don't want to spend on it. So, the idea to implement the handy mechanism for developers came to me and I eventually started crafting it. That was the becoming of SwiftTheming. 🎉🎉🎉 Using SwiftTheming, we can manage theme and system appearance as you desire without too much sweating. All you have to do is declare your themes with different colors, images, fonts and gradients. Pretty easy!

⚠️ Requirements

  • iOS 14+, macOS 11+, watchOS 7+, tvOS 14+

SwiftTheming is developed using Xcode 13.0. Make sure you are using Xcode 13 and above.

🛠 Installation

📦 Using Swift Package Manager

Add it as a dependency within your Package.swift.

dependencies: [
    .package(url: "https://github.com/dscyrescotti/SwiftTheming.git", from: "2.0.0")
]

📦 Using Cocoapods

Add it inside your Podfile.

pod 'SwiftTheming', '~> 2.0.0'

Currently, SwiftTheming can be installed only via Swift Package Manager and Cocoapods.

👀 Migration guide for Version 2

SwiftTheming 🎨 has released Version 2 which inlcudes the major enhancement for code architecture and developer experience. Please check out the migration guide to migrate from Version 1.

🎯 Usage

Declaring multiple themes

To get started, you need to define four different types of assets for color, font, gradient and image. Later, they will be used when creating different themes by injecting them as type alias.

enum ColorAsset: ColorAssetable {
    case backgroundColor
    // more...
}
enum FontAsset: FontAssetable { /* more... */ }
enum GradientAsset: GradientAssetable { /* more... */ }
enum ImageAsset: ImageAssetable { /* more...}

You can omit some assets unless those are intended to use in themes.

Now, we can start designating different themes using the assets declared.

class SampleTheme: Themed, Assetable {
    typealias _ColorAsset = ColorAsset
    typealias _FontAsset = FontAsset
    typealias _GradientAsset = GradientAsset
    typealias _ImageAsset = ImageAsset

    func colorSet(for asset: ColorAsset) -> ColorSet {
        switch asset {
        case .backgroundColor:
            return ColorSet(light: Color(hex: 0xDEF8EA), dark: Color(hex: 0x22442E))
        }
    }
    func imageSet(for asset: ImageAsset) -> ImageSet { /* some stuff*/ }
    func fontSet(for asset: FontAsset) -> FontSet { /* some stuff */ }
    func gradientSet(for asset: GradientAsset) -> GradientSet { /* some stuff */ }
}

For empty asset, you can directly use EmptyAsset instead of declaring an asset.

class SampleTheme: Themed, Assetable {
  typealias _GradientAsset = EmptyAsset
}

After you create multiple themes, it is time to list down all themes in Theme extension that you are going to use in the app and provide the required specification for Themeable protocol.

extension Theme: Themeable {
    static let sampleTheme = Theme(key: "sampleTheme")
    // more themes
    
    public func themed() -> Themed {
        switch self{
        case .sampleTheme: return SampleTheme()
        // some stuff
        default: fatalError("You are accessing undefined theme.")
        }
    }
}

Declaring default theming

Before moving to the part that explains how to use theme providers and access interface elements in view layers, you need to set up the default theme and appearance for the first time running. You can achieve it by letting DefaultTheming conforms to Defaultable and providing desired theme and appearance which will be used as defaults.

extension DefaultTheming: Defaultable {
    public func defaultTheme() -> Theme {
        .sampleTheme
    }
    
    public func defaultAppearance() -> PreferredAppearance {
        .system
    }
}

Accessing theme provider across views

Yay! you are ready to use themes in your views. Let's get started passing ThemeProvider instance living as an environment object across view hierarchy so that it can be accessible across views.

WindowGroup {
    ContentView()
        .themeProviding()
}

Now, you can access ThemeProvider via @ThemeProviding property wrapper inside any view so that you can fully manage themes and override the system appearance as you want through themeProvider.

struct ContentView: View {
    @ThemeProviding var themeProvider
    
    var body: some View { /* some stuff */ }
}

You can switch theme and appearance by calling setTheme(with:) and setPreferredAppearance(with:)respectively.

🔎 Exploration

To explore more about SwiftTheming 🎨, you can check out the documentation or dig around the source code.

✍️ Author

Scotti (@dscyrescotti)

👨‍💻 Contributions

SwiftTheming 🎨 welcomes all developers to contribute if you have any idea to enhance and open an issue if you encounter any bug.

© License

SwiftTheming 🎨 is available under the MIT license. See the LICENSE file for more info.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].