Skip to content

Commit

Permalink
Add FXIOS-7714 [v121] new theming system to Tab.swift (#17217)
Browse files Browse the repository at this point in the history
* Add new theming system to Tab.swift

* Refactor as per code review

---------

Co-authored-by: Filippo Zazzeroni <[email protected]>
  • Loading branch information
FilippoZazzeroni and Filippo Zazzeroni authored Nov 9, 2023
1 parent 983ec11 commit 7f43112
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
12 changes: 12 additions & 0 deletions BrowserKit/Sources/Common/Theming/ThemeType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ public enum ThemeType: String {
return "\(name)_dark"
}
}

public func keyboardAppearence(isPrivate: Bool) -> UIKeyboardAppearance {
if isPrivate {
return .dark
}
return switch self {
case .dark:
.dark
case .light:
.light
}
}
}
9 changes: 7 additions & 2 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1762,11 +1762,11 @@ class BrowserViewController: UIViewController,

// Update the `background-color` of any blank webviews.
let webViews = tabManager.tabs.compactMap({ $0.webView })
webViews.forEach({ $0.applyTheme() })
webViews.forEach({ $0.applyTheme(theme: currentTheme) })

let tabs = tabManager.tabs
tabs.forEach {
$0.applyTheme()
$0.applyTheme(theme: currentTheme)
}

guard let contentScript = tabManager.selectedTab?.getContentScript(name: ReaderMode.name()) else { return }
Expand Down Expand Up @@ -2114,6 +2114,11 @@ extension BrowserViewController: TabManagerDelegate {

let ui: [PrivateModeUI?] = [toolbar, topTabsViewController, urlBar]
ui.forEach { $0?.applyUIMode(isPrivate: tab.isPrivate, theme: themeManager.currentTheme) }
} else {
// Theme is applied to the tab and webView in the else case
// because in the if block is applied already to all the tabs and web views
tab.applyTheme(theme: themeManager.currentTheme)
webView.applyTheme(theme: themeManager.currentTheme)
}

readerModeCache = tab.isPrivate ? MemoryReaderModeCache.sharedInstance : DiskReaderModeCache.sharedInstance
Expand Down
6 changes: 0 additions & 6 deletions Client/TabManagement/Legacy/LegacyTabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ class LegacyTabManager: NSObject, FeatureFlaggable, TabManager, TabEventHandler
}
if let tab = selectedTab {
TabEvent.post(.didGainFocus, for: tab)
tab.applyTheme()
}
TelemetryWrapper.recordEvent(category: .action, method: .tap, object: .tab)

Expand Down Expand Up @@ -552,11 +551,6 @@ class LegacyTabManager: NSObject, FeatureFlaggable, TabManager, TabEventHandler
let url = NewTabHomePageAccessors.getHomePage(profile.prefs)!
tab.loadRequest(URLRequest(url: url))
case .blankPage:
// If we're showing "about:blank" in a webview, set
// the <html> `background-color` to match the theme.
if let webView = tab.webView {
webView.applyTheme()
}
break
default:
// The common case, where the NewTabPage enum defines
Expand Down
34 changes: 19 additions & 15 deletions Client/TabManagement/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ enum TabUrlType: String {
case googleTopSiteFollowOn
}

class Tab: NSObject {
class Tab: NSObject, ThemeApplicable {
static let privateModeKey = "PrivateModeKey"
private var _isPrivate = false
private(set) var isPrivate: Bool {
Expand Down Expand Up @@ -830,10 +830,6 @@ class Tab: NSObject {
}
}

func applyTheme() {
UITextField.appearance().keyboardAppearance = isPrivate ? .dark : (LegacyThemeManager.instance.currentName == .dark ? .dark : .light)
}

func getProviderForUrl() -> SearchEngine {
guard let url = self.webView?.url else {
return .none
Expand All @@ -845,6 +841,12 @@ class Tab: NSObject {

return .none
}

// MARK: - ThemeApplicable

func applyTheme(theme: Theme) {
UITextField.appearance().keyboardAppearance = theme.type.keyboardAppearence(isPrivate: isPrivate)
}
}

extension Tab: UIGestureRecognizerDelegate {
Expand Down Expand Up @@ -954,7 +956,7 @@ protocol TabWebViewDelegate: AnyObject {
func tabWebViewSearchWithFirefox(_ tabWebViewSearchWithFirefox: TabWebView, didSelectSearchWithFirefoxForSelection selection: String)
}

class TabWebView: WKWebView, MenuHelperInterface {
class TabWebView: WKWebView, MenuHelperInterface, ThemeApplicable {
var accessoryView = AccessoryViewProvider()
private var logger: Logger = DefaultLogger.shared
private weak var delegate: TabWebViewDelegate?
Expand Down Expand Up @@ -1002,15 +1004,6 @@ class TabWebView: WKWebView, MenuHelperInterface {
fatalError("init(coder:) has not been implemented")
}

// Updates the `background-color` of the webview to match
// the theme if the webview is showing "about:blank" (nil).
func applyTheme() {
if url == nil {
let backgroundColor = LegacyThemeManager.instance.current.browser.background.hexString
evaluateJavascriptInDefaultContentWorld("document.documentElement.style.backgroundColor = '\(backgroundColor)';")
}
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return super.canPerformAction(action, withSender: sender) || action == MenuHelper.SelectorFindInPage
}
Expand Down Expand Up @@ -1046,4 +1039,15 @@ class TabWebView: WKWebView, MenuHelperInterface {
override func evaluateJavaScript(_ javaScriptString: String, completionHandler: ((Any?, Error?) -> Void)? = nil) {
super.evaluateJavaScript(javaScriptString, completionHandler: completionHandler)
}

// MARK: - ThemeApplicable

/// Updates the `background-color` of the webview to match
/// the theme if the webview is showing "about:blank" (nil).
func applyTheme(theme: Theme) {
if url == nil {
let backgroundColor = theme.colors.layer1.hexString
evaluateJavascriptInDefaultContentWorld("document.documentElement.style.backgroundColor = '\(backgroundColor)';")
}
}
}

0 comments on commit 7f43112

Please sign in to comment.