Signal-iOS/SignalUI/ViewControllers/OWSWindow.swift
Adam Sharp 5f1820748e Don't ignore theme changes when switching to system interface style
When switching to the "system" theme mode, if the effective appearance matched
the previous overridden theme mode, the theme change would be ignored. By
comparing the mode instead of just the effective appearance, we properly react
to system appearance changes.

Additionally, rework the transition animation for changing themes to match the
animation of system appearance changes (i.e., a 0.5-second cross-dissolve).

Fixes: https://signalmessenger.atlassian.net/browse/IOS-4431
2024-05-13 16:16:53 -05:00

56 lines
1.5 KiB
Swift

//
// Copyright 2019 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import UIKit
public class OWSWindow: UIWindow {
public override init(frame: CGRect) {
super.init(frame: frame)
NotificationCenter.default.addObserver(
self,
selector: #selector(themeDidChange),
name: .themeDidChange,
object: nil
)
applyTheme()
}
// This useless override is defined so that you can call `-init` from Swift.
public override init(windowScene: UIWindowScene) {
fatalError("init(windowScene:) has not been implemented")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc
private func themeDidChange() {
applyTheme()
}
private func applyTheme() {
// Ensure system UI elements use the appropriate styling for the selected theme.
switch Theme.getOrFetchCurrentMode() {
case .light:
overrideUserInterfaceStyle = .light
case .dark:
overrideUserInterfaceStyle = .dark
case .system:
overrideUserInterfaceStyle = .unspecified
}
}
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
Theme.systemThemeChanged()
}
}
}