Skip to content

Commit

Permalink
@UserDefaultLanguageColors -> @LanguageColorsAppStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 23, 2025
1 parent 2a0c802 commit 8559097
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 77 deletions.
49 changes: 49 additions & 0 deletions src/ShowyEdge/swift/Models/LanguageColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,52 @@ class LanguageColor: Identifiable {
self.colors = colors
}
}

@propertyWrapper
struct LanguageColorsAppStorage {
let key: String
let defaultValue: [[String: String]]

init(_ key: String) {
self.key = key
defaultValue = []
}

var wrappedValue: [LanguageColor] {
get {
var languageColors: [LanguageColor] = []
(UserDefaults.standard.object(forKey: key) as? [[String: String]] ?? []).forEach {
let inputSourceID = $0["inputsourceid"] ?? ""
if inputSourceID != "" {
languageColors.append(
LanguageColor(
inputSourceID,
(
Color(colorString: $0["color0"] ?? ""),
Color(colorString: $0["color1"] ?? ""),
Color(colorString: $0["color2"] ?? "")
)
))
}
}
return languageColors
}
nonmutating set {
var languageColors: [[String: String]] = []
newValue.forEach {
let hexStrings = (
$0.colors.0.hexString,
$0.colors.1.hexString,
$0.colors.2.hexString
)
languageColors.append([
"inputsourceid": $0.inputSourceID,
"color0": hexStrings.0,
"color1": hexStrings.1,
"color2": hexStrings.2,
])
}
UserDefaults.standard.set(languageColors, forKey: key)
}
}
}
51 changes: 0 additions & 51 deletions src/ShowyEdge/swift/UserDefaults-propertyWrapper.swift

This file was deleted.

46 changes: 20 additions & 26 deletions src/ShowyEdge/swift/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,34 @@ final class UserSettings: ObservableObject {

@AppStorage("initialOpenAtLoginRegistered") var initialOpenAtLoginRegistered = false

//
// Indicator settings
//

@AppStorage("kIndicatorHeightPx") var indicatorHeightPx = 5.0
@AppStorage("kIndicatorOpacity2") var indicatorOpacity = 100.0
@AppStorage("kHideInFullScreenSpace") var hideInFullScreenSpace = false
@AppStorage("kShowIndicatorBehindAppWindows") var showIndicatorBehindAppWindows = false
@AppStorage("kColorsLayoutOrientation") var colorsLayoutOrientation = "horizontal"
@AppStorage("kUseCustomFrame") var useCustomFrame = false
@AppStorage("kCustomFrameOrigin") var customFrameOrigin = 0
@AppStorage("kCustomFrameLeft") var customFrameLeft = 0.0
@AppStorage("kCustomFrameTop") var customFrameTop = 0.0
@AppStorage("kCustomFrameWidth") var customFrameWidth = 100.0
@AppStorage("kCustomFrameWidthUnit") var customFrameWidthUnit = 0
@AppStorage("kCustomFrameHeight") var customFrameHeight = 100.0
@AppStorage("kCustomFrameHeightUnit") var customFrameHeightUnit = 0
@AppStorage("kCustomFramePillShape") var customFramePillShape = false

//
// Color settings
//

@UserDefaultLanguageColors("CustomizedLanguageColor")
@LanguageColorsAppStorage("CustomizedLanguageColor")
var customizedLanguageColors {
willSet {
objectWillChange.send()
}
didSet {
NotificationCenter.default.post(
name: UserSettings.indicatorConfigurationChanged,
object: nil
)
}
}

func customizedLanguageColorIndex(inputSourceID: String) -> Int? {
Expand Down Expand Up @@ -91,23 +104,4 @@ final class UserSettings: ObservableObject {
func removeCustomizedLanguageColor(_ inputSourceID: String) {
customizedLanguageColors.removeAll(where: { $0.inputSourceID == inputSourceID })
}

//
// Indicator settings
//

@AppStorage("kIndicatorHeightPx") var indicatorHeightPx = 5.0
@AppStorage("kIndicatorOpacity2") var indicatorOpacity = 100.0
@AppStorage("kHideInFullScreenSpace") var hideInFullScreenSpace = false
@AppStorage("kShowIndicatorBehindAppWindows") var showIndicatorBehindAppWindows = false
@AppStorage("kColorsLayoutOrientation") var colorsLayoutOrientation = "horizontal"
@AppStorage("kUseCustomFrame") var useCustomFrame = false
@AppStorage("kCustomFrameOrigin") var customFrameOrigin = 0
@AppStorage("kCustomFrameLeft") var customFrameLeft = 0.0
@AppStorage("kCustomFrameTop") var customFrameTop = 0.0
@AppStorage("kCustomFrameWidth") var customFrameWidth = 100.0
@AppStorage("kCustomFrameWidthUnit") var customFrameWidthUnit = 0
@AppStorage("kCustomFrameHeight") var customFrameHeight = 100.0
@AppStorage("kCustomFrameHeightUnit") var customFrameHeightUnit = 0
@AppStorage("kCustomFramePillShape") var customFramePillShape = false
}

0 comments on commit 8559097

Please sign in to comment.