From f04621a8fa9353aa91f76d559f1eadca12c2705a Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Wed, 1 Jun 2022 11:46:12 -0700 Subject: [PATCH] create a web prefs form sheet --- .../Views/WebReader/WebReaderContainer.swift | 17 ++---- .../Views/FontSizeAdjustmentPopoverView.swift | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index d9fa6d44c..482e55677 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -26,13 +26,6 @@ import WebKit @Environment(\.presentationMode) var presentationMode: Binding @StateObject var viewModel = WebReaderViewModel() - var fontAdjustmentPopoverView: some View { - FontSizeAdjustmentPopoverView( - increaseFontAction: { increaseFontActionID = UUID() }, - decreaseFontAction: { decreaseFontActionID = UUID() } - ) - } - func webViewActionHandler(message: WKScriptMessage, replyHandler: WKScriptMessageReplyHandler?) { if let replyHandler = replyHandler { viewModel.webViewActionWithReplyHandler( @@ -150,9 +143,6 @@ import WebKit }, webViewActionHandler: webViewActionHandler, navBarVisibilityRatioUpdater: { -// if $0 < 1 { -// showPreferencesPopover = false -// } navBarVisibilityRatio = $0 }, increaseFontActionID: $increaseFontActionID, @@ -202,8 +192,11 @@ import WebKit Spacer() } } - .popover(isPresented: $showPreferencesPopover) { - Text("Popover") + .formSheet(isPresented: $showPreferencesPopover) { + WebPreferencesPopoverView( + increaseFontAction: { increaseFontActionID = UUID() }, + decreaseFontAction: { decreaseFontActionID = UUID() } + ) } .onDisappear { // Clear the shared webview content when exiting diff --git a/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift b/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift index 11f5131d5..916febbd4 100644 --- a/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift +++ b/apple/OmnivoreKit/Sources/Views/FontSizeAdjustmentPopoverView.swift @@ -1,6 +1,62 @@ import SwiftUI import Utils +public struct WebPreferencesPopoverView: View { + let increaseFontAction: () -> Void + let decreaseFontAction: () -> Void + + static let preferredWebFontSizeKey = UserDefaultKey.preferredWebFontSize.rawValue + #if os(macOS) + @AppStorage(preferredWebFontSizeKey) var storedFontSize = Int(NSFont.userFont(ofSize: 16)?.pointSize ?? 16) + #else + @AppStorage(preferredWebFontSizeKey) var storedFontSize: Int = UITraitCollection.current.preferredWebFontSize + #endif + + public init( + increaseFontAction: @escaping () -> Void, + decreaseFontAction: @escaping () -> Void + ) { + self.increaseFontAction = increaseFontAction + self.decreaseFontAction = decreaseFontAction + } + + public var body: some View { + HStack(alignment: .center, spacing: 0) { + Button( + action: { + storedFontSize = max(storedFontSize - 2, 10) + decreaseFontAction() + }, + label: { + Image(systemName: "minus") + #if os(iOS) + .foregroundColor(.systemLabel) + .padding() + #endif + } + ) + .frame(width: 55, height: 40, alignment: .center) + Divider() + .frame(height: 30) + .background(Color.systemLabel) + Button( + action: { + storedFontSize = min(storedFontSize + 2, 28) + increaseFontAction() + }, + label: { + Image(systemName: "plus") + #if os(iOS) + .foregroundColor(.systemLabel) + .padding() + #endif + } + ) + .frame(width: 55, height: 40, alignment: .center) + } + } +} + public struct FontSizeAdjustmentPopoverView: View { let increaseFontAction: () -> Void let decreaseFontAction: () -> Void