-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement dynamic menu bar appearance
Closes #393
- Loading branch information
1 parent
bda9d86
commit 326893d
Showing
10 changed files
with
435 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
Ice/MenuBar/Appearance/Configurations/MenuBarAppearanceConfigurationV2.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// | ||
// MenuBarAppearanceConfigurationV2.swift | ||
// Ice | ||
// | ||
|
||
import CoreGraphics | ||
import Foundation | ||
|
||
struct MenuBarAppearanceConfigurationV2: Hashable { | ||
var lightModeConfiguration: MenuBarAppearancePartialConfiguration | ||
var darkModeConfiguration: MenuBarAppearancePartialConfiguration | ||
var staticConfiguration: MenuBarAppearancePartialConfiguration | ||
var shapeKind: MenuBarShapeKind | ||
var fullShapeInfo: MenuBarFullShapeInfo | ||
var splitShapeInfo: MenuBarSplitShapeInfo | ||
var isInset: Bool | ||
var isDynamic: Bool | ||
|
||
var hasRoundedShape: Bool { | ||
switch shapeKind { | ||
case .none: false | ||
case .full: fullShapeInfo.hasRoundedShape | ||
case .split: splitShapeInfo.hasRoundedShape | ||
} | ||
} | ||
|
||
var current: MenuBarAppearancePartialConfiguration { | ||
if isDynamic { | ||
switch SystemAppearance.current { | ||
case .light: lightModeConfiguration | ||
case .dark: darkModeConfiguration | ||
} | ||
} else { | ||
staticConfiguration | ||
} | ||
} | ||
} | ||
|
||
// MARK: Default Configuration | ||
extension MenuBarAppearanceConfigurationV2 { | ||
static let defaultConfiguration = MenuBarAppearanceConfigurationV2( | ||
lightModeConfiguration: .defaultConfiguration, | ||
darkModeConfiguration: .defaultConfiguration, | ||
staticConfiguration: .defaultConfiguration, | ||
shapeKind: .none, | ||
fullShapeInfo: .default, | ||
splitShapeInfo: .default, | ||
isInset: true, | ||
isDynamic: false | ||
) | ||
} | ||
|
||
extension MenuBarAppearanceConfigurationV2: Codable { | ||
private enum CodingKeys: CodingKey { | ||
case lightModeConfiguration | ||
case darkModeConfiguration | ||
case staticConfiguration | ||
case shapeKind | ||
case fullShapeInfo | ||
case splitShapeInfo | ||
case isInset | ||
case isDynamic | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
try self.init( | ||
lightModeConfiguration: container.decodeIfPresent(MenuBarAppearancePartialConfiguration.self, forKey: .lightModeConfiguration) ?? Self.defaultConfiguration.lightModeConfiguration, | ||
darkModeConfiguration: container.decodeIfPresent(MenuBarAppearancePartialConfiguration.self, forKey: .darkModeConfiguration) ?? Self.defaultConfiguration.darkModeConfiguration, | ||
staticConfiguration: container.decodeIfPresent(MenuBarAppearancePartialConfiguration.self, forKey: .staticConfiguration) ?? Self.defaultConfiguration.staticConfiguration, | ||
shapeKind: container.decodeIfPresent(MenuBarShapeKind.self, forKey: .shapeKind) ?? Self.defaultConfiguration.shapeKind, | ||
fullShapeInfo: container.decodeIfPresent(MenuBarFullShapeInfo.self, forKey: .fullShapeInfo) ?? Self.defaultConfiguration.fullShapeInfo, | ||
splitShapeInfo: container.decodeIfPresent(MenuBarSplitShapeInfo.self, forKey: .splitShapeInfo) ?? Self.defaultConfiguration.splitShapeInfo, | ||
isInset: container.decodeIfPresent(Bool.self, forKey: .isInset) ?? Self.defaultConfiguration.isInset, | ||
isDynamic: container.decodeIfPresent(Bool.self, forKey: .isDynamic) ?? Self.defaultConfiguration.isDynamic | ||
) | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(lightModeConfiguration, forKey: .lightModeConfiguration) | ||
try container.encode(darkModeConfiguration, forKey: .darkModeConfiguration) | ||
try container.encode(staticConfiguration, forKey: .staticConfiguration) | ||
try container.encode(shapeKind, forKey: .shapeKind) | ||
try container.encode(fullShapeInfo, forKey: .fullShapeInfo) | ||
try container.encode(splitShapeInfo, forKey: .splitShapeInfo) | ||
try container.encode(isInset, forKey: .isInset) | ||
try container.encode(isDynamic, forKey: .isDynamic) | ||
} | ||
} | ||
|
||
// MARK: - MenuBarAppearancePartialConfiguration | ||
|
||
struct MenuBarAppearancePartialConfiguration: Hashable { | ||
var hasShadow: Bool | ||
var hasBorder: Bool | ||
var borderColor: CGColor | ||
var borderWidth: Double | ||
var tintKind: MenuBarTintKind | ||
var tintColor: CGColor | ||
var tintGradient: CustomGradient | ||
} | ||
|
||
// MARK: Default Partial Configuration | ||
extension MenuBarAppearancePartialConfiguration { | ||
static let defaultConfiguration = MenuBarAppearancePartialConfiguration( | ||
hasShadow: false, | ||
hasBorder: false, | ||
borderColor: .black, | ||
borderWidth: 1, | ||
tintKind: .none, | ||
tintColor: .black, | ||
tintGradient: .defaultMenuBarTint | ||
) | ||
} | ||
|
||
// MARK: MenuBarAppearancePartialConfiguration: Codable | ||
extension MenuBarAppearancePartialConfiguration: Codable { | ||
private enum CodingKeys: CodingKey { | ||
case hasShadow | ||
case hasBorder | ||
case borderColor | ||
case borderWidth | ||
case shapeKind | ||
case fullShapeInfo | ||
case splitShapeInfo | ||
case tintKind | ||
case tintColor | ||
case tintGradient | ||
} | ||
|
||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
try self.init( | ||
hasShadow: container.decodeIfPresent(Bool.self, forKey: .hasShadow) ?? Self.defaultConfiguration.hasShadow, | ||
hasBorder: container.decodeIfPresent(Bool.self, forKey: .hasBorder) ?? Self.defaultConfiguration.hasBorder, | ||
borderColor: container.decodeIfPresent(CodableColor.self, forKey: .borderColor)?.cgColor ?? Self.defaultConfiguration.borderColor, | ||
borderWidth: container.decodeIfPresent(Double.self, forKey: .borderWidth) ?? Self.defaultConfiguration.borderWidth, | ||
tintKind: container.decodeIfPresent(MenuBarTintKind.self, forKey: .tintKind) ?? Self.defaultConfiguration.tintKind, | ||
tintColor: container.decodeIfPresent(CodableColor.self, forKey: .tintColor)?.cgColor ?? Self.defaultConfiguration.tintColor, | ||
tintGradient: container.decodeIfPresent(CustomGradient.self, forKey: .tintGradient) ?? Self.defaultConfiguration.tintGradient | ||
) | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(hasShadow, forKey: .hasShadow) | ||
try container.encode(hasBorder, forKey: .hasBorder) | ||
try container.encode(CodableColor(cgColor: borderColor), forKey: .borderColor) | ||
try container.encode(borderWidth, forKey: .borderWidth) | ||
try container.encode(tintKind, forKey: .tintKind) | ||
try container.encode(CodableColor(cgColor: tintColor), forKey: .tintColor) | ||
try container.encode(tintGradient, forKey: .tintGradient) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.