From 27b9326d07024f2e34d745985a5970b22c905f52 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Mon, 8 Mar 2021 10:06:30 +0800 Subject: [PATCH 1/4] fixes #2823 --- .../Detail/DetailWebViewController.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index 5c6f9a5ed..674067a4f 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -116,7 +116,7 @@ final class DetailWebViewController: NSViewController, WKUIDelegate { NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(bigSurOffsetFix(_:)), name: NSWindow.didExitFullScreenNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(bigSurOffsetFix(_:)), name: NSWindow.didChangeScreenNotification, object: nil) webView.loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL) } @@ -142,15 +142,19 @@ final class DetailWebViewController: NSViewController, WKUIDelegate { } } - /// On macOS 11, when a user exits full screen, the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by 1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. + /// On macOS 11, when a user exits full screen or zoomed mode (full screen with menu bar showing), the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by 1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. @objc func bigSurOffsetFix(_ note: Notification) { if #available(macOS 11, *) { - guard var frame = self.view.window?.frame else { + print(note.name) + guard var frame = view.window?.frame else { return } - frame.size = NSSize(width: self.view.window!.frame.width, height: self.view.window!.frame.height - 1) - self.view.window!.setFrame(frame, display: true) + frame.size = NSSize(width: view.window!.frame.width, height: view.window!.frame.height - 1) + view.window!.setFrame(frame, display: false) + frame.size = NSSize(width: view.window!.frame.width, height: view.window!.frame.height + 1) + view.window!.setFrame(frame, display: false) } + } // MARK: Media Functions From a59df3e6fc09496543ea568b20edaba2736fcdf2 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Mon, 8 Mar 2021 10:28:51 +0800 Subject: [PATCH 2/4] Moves frame tweaking to end of liveResize --- Mac/MainWindow/Detail/DetailWebView.swift | 13 +++++++++++++ .../Detail/DetailWebViewController.swift | 16 ---------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Mac/MainWindow/Detail/DetailWebView.swift b/Mac/MainWindow/Detail/DetailWebView.swift index e235b7d7b..0b4c8e873 100644 --- a/Mac/MainWindow/Detail/DetailWebView.swift +++ b/Mac/MainWindow/Detail/DetailWebView.swift @@ -53,6 +53,19 @@ final class DetailWebView: WKWebView { override func viewDidEndLiveResize() { super.viewDidEndLiveResize() evaluateJavaScript("document.body.style.overflow = 'visible';", completionHandler: nil) + + + /// On macOS 11, when a user exits full screen or zoomed mode (full screen with menu bar showing), the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by 1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. + if #available(macOS 11, *) { + guard var frame = window?.frame else { + return + } + frame.size = NSSize(width: window!.frame.width, height: window!.frame.height - 1) + window!.setFrame(frame, display: false) + frame.size = NSSize(width: window!.frame.width, height: window!.frame.height + 1) + window!.setFrame(frame, display: false) + } + } } diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index 674067a4f..b661a3e86 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -116,7 +116,6 @@ final class DetailWebViewController: NSViewController, WKUIDelegate { NotificationCenter.default.addObserver(self, selector: #selector(avatarDidBecomeAvailable(_:)), name: .AvatarDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(faviconDidBecomeAvailable(_:)), name: .FaviconDidBecomeAvailable, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(bigSurOffsetFix(_:)), name: NSWindow.didChangeScreenNotification, object: nil) webView.loadFileURL(ArticleRenderer.blank.url, allowingReadAccessTo: ArticleRenderer.blank.baseURL) } @@ -142,21 +141,6 @@ final class DetailWebViewController: NSViewController, WKUIDelegate { } } - /// On macOS 11, when a user exits full screen or zoomed mode (full screen with menu bar showing), the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by 1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. - @objc func bigSurOffsetFix(_ note: Notification) { - if #available(macOS 11, *) { - print(note.name) - guard var frame = view.window?.frame else { - return - } - frame.size = NSSize(width: view.window!.frame.width, height: view.window!.frame.height - 1) - view.window!.setFrame(frame, display: false) - frame.size = NSSize(width: view.window!.frame.width, height: view.window!.frame.height + 1) - view.window!.setFrame(frame, display: false) - } - - } - // MARK: Media Functions func stopMediaPlayback() { From f630cb16dbcfd066f63db939c9366a925e2e7746 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Mon, 8 Mar 2021 10:30:58 +0800 Subject: [PATCH 3/4] comments --- Mac/MainWindow/Detail/DetailWebView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mac/MainWindow/Detail/DetailWebView.swift b/Mac/MainWindow/Detail/DetailWebView.swift index 0b4c8e873..0dde85087 100644 --- a/Mac/MainWindow/Detail/DetailWebView.swift +++ b/Mac/MainWindow/Detail/DetailWebView.swift @@ -55,7 +55,7 @@ final class DetailWebView: WKWebView { evaluateJavaScript("document.body.style.overflow = 'visible';", completionHandler: nil) - /// On macOS 11, when a user exits full screen or zoomed mode (full screen with menu bar showing), the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by 1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. + // On macOS 11, when a user exits full screen or exits zoomed mode (full screen with menu bar showing) by disconnecting an external display, the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by -1pt/+1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. if #available(macOS 11, *) { guard var frame = window?.frame else { return From e2f21c15c268fde877cc9c38b41cc20471ed8004 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Mon, 8 Mar 2021 14:12:32 +0800 Subject: [PATCH 4/4] formatting --- Mac/MainWindow/Detail/DetailWebView.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Mac/MainWindow/Detail/DetailWebView.swift b/Mac/MainWindow/Detail/DetailWebView.swift index 0dde85087..7252adde2 100644 --- a/Mac/MainWindow/Detail/DetailWebView.swift +++ b/Mac/MainWindow/Detail/DetailWebView.swift @@ -54,8 +54,14 @@ final class DetailWebView: WKWebView { super.viewDidEndLiveResize() evaluateJavaScript("document.body.style.overflow = 'visible';", completionHandler: nil) + /* + On macOS 11, when a user exits full screen + or exits zoomed mode by disconnecting an external display + the webview's `origin.y` is offset by a sizeable amount. - // On macOS 11, when a user exits full screen or exits zoomed mode (full screen with menu bar showing) by disconnecting an external display, the webview's origin.y is offset by a sizeable amount. This function adjusts the height of the window height by -1pt/+1pt which puts the webview back in the correct place. This is an issue with SwiftUI and AppKit. + This code adjusts the height of the window by -1pt/+1pt, + which puts the webview back in the correct place. + */ if #available(macOS 11, *) { guard var frame = window?.frame else { return @@ -65,7 +71,6 @@ final class DetailWebView: WKWebView { frame.size = NSSize(width: window!.frame.width, height: window!.frame.height + 1) window!.setFrame(frame, display: false) } - } }