From f4706205b0a54199bff17c4ae1dfa0181c792ac4 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sun, 3 Jul 2022 22:38:12 -0700 Subject: [PATCH] observe font change and contrast change in main app for macos --- .../Sources/App/MacMenuCommands.swift | 21 +++++++++---------- apple/Sources/MainApp.swift | 16 ++++++++++++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/MacMenuCommands.swift b/apple/OmnivoreKit/Sources/App/MacMenuCommands.swift index b6201c4dc..e84877e05 100644 --- a/apple/OmnivoreKit/Sources/App/MacMenuCommands.swift +++ b/apple/OmnivoreKit/Sources/App/MacMenuCommands.swift @@ -9,8 +9,9 @@ import Views ) @AppStorage(UserDefaultKey.preferredWebLineSpacing.rawValue) var storedLineSpacing = 150 @AppStorage(UserDefaultKey.preferredWebMaxWidthPercentage.rawValue) var storedMaxWidthPercentage = 100 - @AppStorage(UserDefaultKey.preferredWebFont.rawValue) var preferredFont = WebFont.inter.rawValue - @AppStorage(UserDefaultKey.prefersHighContrastWebFont.rawValue) var prefersHighContrastText = true + + @Binding var preferredFont: String + @Binding var prefersHighContrastText: Bool public var fontSizeButtons: some View { Group { @@ -76,7 +77,13 @@ import Views } } - public init() {} + public init( + preferredFont: Binding, + prefersHighContrastText: Binding + ) { + self._preferredFont = preferredFont + self._prefersHighContrastText = prefersHighContrastText + } public var body: some Commands { CommandMenu("Reader Settings") { @@ -96,20 +103,12 @@ import Views ForEach(WebFont.allCases, id: \.self) { font in Text(font.displayValue).tag(font.rawValue) } - // TODO: fix this since it doesn't work - .onChange(of: preferredFont) { _ in - NSNotification.readerSettingsChanged() - } } Toggle( isOn: $prefersHighContrastText, label: { Text("High Contrast Text") } ) - // TODO: fix this since it doesn't work - .onChange(of: prefersHighContrastText) { _ in - NSNotification.readerSettingsChanged() - } } } } diff --git a/apple/Sources/MainApp.swift b/apple/Sources/MainApp.swift index 5635335b8..43fbf54eb 100644 --- a/apple/Sources/MainApp.swift +++ b/apple/Sources/MainApp.swift @@ -1,19 +1,22 @@ // swiftlint:disable weak_delegate import App import SwiftUI +import Utils #if os(macOS) import AppKit + import Views #elseif os(iOS) import Intercom import UIKit - import Utils #endif @main struct MainApp: App { #if os(macOS) @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate + @AppStorage(UserDefaultKey.preferredWebFont.rawValue) var preferredFont = WebFont.inter.rawValue + @AppStorage(UserDefaultKey.prefersHighContrastWebFont.rawValue) var prefersHighContrastText = true #elseif os(iOS) @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate #endif @@ -34,7 +37,16 @@ struct MainApp: App { RootView(intercomProvider: nil) } .commands { - MacMenuCommands() + MacMenuCommands( + preferredFont: $preferredFont, + prefersHighContrastText: $prefersHighContrastText + ) + } + .onChange(of: preferredFont) { _ in + NSNotification.readerSettingsChanged() + } + .onChange(of: prefersHighContrastText) { _ in + NSNotification.readerSettingsChanged() } #endif }