Browse Source

fix: switch between themes

Tienson Qin 1 week ago
parent
commit
590ee304fe

+ 34 - 0
ios/App/App/AppViewController.swift

@@ -7,6 +7,7 @@
 
 import Foundation
 import Capacitor
+import UIKit
 
 @objc public class AppViewController: CAPBridgeViewController {
   override public func capacitorDidLoad() {
@@ -15,4 +16,37 @@ import Capacitor
     bridge?.registerPluginInstance(LiquidTabsPlugin())
     bridge?.registerPluginInstance(NativeBottomSheetPlugin())
   }
+
+    public override func viewDidLoad() {
+        super.viewDidLoad()
+
+        // initial setup
+        applyLogseqBackground()
+    }
+
+    private func applyLogseqBackground() {
+        let bg = UIColor.logseqBackground
+        view.backgroundColor = bg
+
+        if let webView = self.webView {
+            webView.isOpaque = true
+            webView.backgroundColor = bg
+            webView.scrollView.backgroundColor = bg
+
+            // Sometimes WKWebView uses an internal subview for its background
+            webView.scrollView.subviews.first?.backgroundColor = bg
+        }
+    }
+
+    public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
+        super.traitCollectionDidChange(previousTraitCollection)
+
+        guard let previousTraitCollection,
+              traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) else {
+            return
+        }
+
+        // Re-apply dynamic colors when light/dark changes
+        applyLogseqBackground()
+    }
 }

+ 0 - 2
ios/App/App/SceneDelegate.swift

@@ -16,7 +16,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
         // 1) Create your nav controller
         let nav = UINavigationController()
         nav.navigationBar.prefersLargeTitles = false
-        nav.view.backgroundColor = .logseqBackground
 
         // hook the delegate on AppDelegate so all your existing
         // UINavigationControllerDelegate logic keeps working
@@ -45,7 +44,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
         // 3) Standard UIWindowScene setup
         let window = UIWindow(windowScene: windowScene)
         window.rootViewController = hosting
-        window.backgroundColor = .logseqBackground
         self.window = window
         window.makeKeyAndVisible()
 

+ 0 - 29
ios/App/App/SharedWebViewController.swift

@@ -21,17 +21,6 @@ import Capacitor
         // Ensure the view is loaded so that vc.webView is created
         vc.loadViewIfNeeded()
 
-        // Host view uses opaque logseq background to avoid black flashes.
-        vc.view.backgroundColor = .logseqBackground
-        vc.view.isOpaque = true
-
-        // Ensure the internal WKWebView is also opaque with the same background.
-        if let webView = vc.webView {
-            webView.isOpaque = true
-            webView.backgroundColor = .logseqBackground
-            webView.scrollView.backgroundColor = .logseqBackground
-        }
-
         return vc
     }()
 
@@ -53,9 +42,6 @@ import Capacitor
            placeholderView == nil,
            let snapshot = previous.view.snapshotView(afterScreenUpdates: true) {
 
-            previous.view.backgroundColor = .logseqBackground
-
-            snapshot.backgroundColor = .logseqBackground
             snapshot.frame = previous.view.bounds
             snapshot.autoresizingMask = [.flexibleWidth, .flexibleHeight]
             previous.view.addSubview(snapshot)
@@ -72,25 +58,11 @@ import Capacitor
         // 3) Attach to new parent
         currentParent = parent
 
-        // Parent view is also opaque with the same background.
-        parent.view.backgroundColor = .logseqBackground
-        parent.view.isOpaque = true
-
         parent.addChild(vc)
 
         vc.view.frame = parent.view.bounds
         vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
 
-        // Keep host view opaque; background is logseqBackground.
-        vc.view.backgroundColor = .logseqBackground
-        vc.view.isOpaque = true
-
-        if let webView = vc.webView {
-            webView.isOpaque = true
-            webView.backgroundColor = .logseqBackground
-            webView.scrollView.backgroundColor = .logseqBackground
-        }
-
         parent.view.addSubview(vc.view)
         vc.didMove(toParent: parent)
     }
@@ -113,7 +85,6 @@ import Capacitor
         if let snapshotView = vc.view.snapshotView(afterScreenUpdates: true) {
             snapshotView.frame = bounds
             snapshotView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
-            snapshotView.backgroundColor = .logseqBackground
             return snapshotView
         }