From 184396b1d877f1fffb198fdcee5453996a94d013 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Mon, 22 Aug 2022 17:10:47 -0700 Subject: [PATCH] Feature: make Reader use center window --- App/AppDelegate.swift | 4 ++ App/Browser View/BrowserViewController.swift | 25 ++++++++--- App/Reader View/ReaderViewController.swift | 8 +++- .../BrowserSceneDelegate.swift} | 27 +++++++++--- App/Scene Delegates/ReaderSceneDelegate.swift | 41 +++++++++++++++++++ .../SettingsSceneDelegate.swift | 0 App/Supporting Files/Info.plist | 8 +++- SBrowser.xcodeproj/project.pbxproj | 22 +++++++--- 8 files changed, 116 insertions(+), 19 deletions(-) rename App/{SceneDelegate.swift => Scene Delegates/BrowserSceneDelegate.swift} (61%) create mode 100644 App/Scene Delegates/ReaderSceneDelegate.swift rename App/{ => Scene Delegates}/SettingsSceneDelegate.swift (100%) diff --git a/App/AppDelegate.swift b/App/AppDelegate.swift index 8d73772..3d76da3 100644 --- a/App/AppDelegate.swift +++ b/App/AppDelegate.swift @@ -23,6 +23,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate { if userActivity.activityType == SessionActivityType.SettingsWindow.rawValue { return UISceneConfiguration(name: "Settings", sessionRole: connectingSceneSession.role) } + + if userActivity.activityType == SessionActivityType.ReaderWindow.rawValue { + return UISceneConfiguration(name: "Reader", sessionRole: connectingSceneSession.role) + } } return UISceneConfiguration(name: "Browser", sessionRole: connectingSceneSession.role) diff --git a/App/Browser View/BrowserViewController.swift b/App/Browser View/BrowserViewController.swift index 80160f0..b79d845 100644 --- a/App/Browser View/BrowserViewController.swift +++ b/App/Browser View/BrowserViewController.swift @@ -299,13 +299,26 @@ class BrowserViewController: UIViewController DispatchQueue.main.async { documentControls.dismiss(animated: true, completion: nil) - let readableViewController = ReaderViewController(readableHTMLString: string, baseURL: self.tab.bridge.webView.url) - readableViewController.title = self.tab.bridge.webView.title - readableViewController.darkModeEnabled = self.tab.bridge.darkModeEnabled - readableViewController.delegate = self + if self.traitCollection.userInterfaceIdiom == .phone { + let readableViewController = ReaderViewController(readableHTMLString: string, baseURL: self.tab.bridge.webView.url) + readableViewController.title = self.tab.bridge.webView.title + readableViewController.darkModeEnabled = self.tab.bridge.darkModeEnabled + readableViewController.delegate = self + + let navigationController = UINavigationController(rootViewController: readableViewController) + self.present(navigationController, animated: true, completion: nil) + } else { + let userActivity = NSUserActivity(activityType: SessionActivityType.ReaderWindow.rawValue) + userActivity.title = self.tab.title + userActivity.userInfo = [ + ReaderUserActivityKeys.baseURL.rawValue : self.tab.bridge.webView.url ?? NSNull(), + ReaderUserActivityKeys.htmlString.rawValue : string, + ] + let requestOptions = UIWindowScene.ActivationRequestOptions() + requestOptions.preferredPresentationStyle = .prominent + UIApplication.shared.requestSceneSessionActivation(nil, userActivity: userActivity, options: requestOptions) + } - let navigationController = UINavigationController(rootViewController: readableViewController) - self.present(navigationController, animated: true, completion: nil) } } }, for: .touchUpInside) diff --git a/App/Reader View/ReaderViewController.swift b/App/Reader View/ReaderViewController.swift index 6dc1b39..50152f1 100644 --- a/App/Reader View/ReaderViewController.swift +++ b/App/Reader View/ReaderViewController.swift @@ -55,7 +55,13 @@ class ReaderViewController: UIViewController @objc private func didTapDoneButton(_ sender: Any?) { - dismiss(animated: true, completion: nil) + if traitCollection.userInterfaceIdiom == .phone { + dismiss(animated: true, completion: nil) + } else { + if let sceneSession = view.window?.windowScene?.session { + UIApplication.shared.requestSceneSessionDestruction(sceneSession, options: nil, errorHandler: nil) + } + } } @objc diff --git a/App/SceneDelegate.swift b/App/Scene Delegates/BrowserSceneDelegate.swift similarity index 61% rename from App/SceneDelegate.swift rename to App/Scene Delegates/BrowserSceneDelegate.swift index 5b2a845..f7c10a2 100644 --- a/App/SceneDelegate.swift +++ b/App/Scene Delegates/BrowserSceneDelegate.swift @@ -1,5 +1,5 @@ // -// SceneDelegate.swift +// BrowserSceneDelegate.swift // SBrowser // // Created by James Magahern on 7/21/20. @@ -10,9 +10,10 @@ import UIKit public enum SessionActivityType: String { case BrowserWindow = "net.buzzert.attractor.browser" case SettingsWindow = "net.buzzert.attractor.settings" + case ReaderWindow = "net.buzzert.attractor.reader" } -class SceneDelegate: UIResponder, UIWindowSceneDelegate { +class BrowserSceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? var browserViewController: BrowserViewController? @@ -32,10 +33,24 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { browserViewController.tab.beginLoadingURL(url) } - #if targetEnvironment(macCatalyst) - windowScene.titlebar?.titleVisibility = .hidden - windowScene.titlebar?.separatorStyle = .none - #endif +#if targetEnvironment(macCatalyst) + windowScene.titlebar?.titleVisibility = .hidden + windowScene.titlebar?.separatorStyle = .none + + if #available(macCatalyst 16.0, *) { + let screenSize = windowScene.screen.bounds.size + let preferredSize = CGSize(width: 1000, height: 1100) + let geometryPreferences = UIWindowScene.GeometryPreferences.Mac(systemFrame: CGRect( + origin: CGPoint( + x: (screenSize.width - preferredSize.width) / 2, + y: (screenSize.height - preferredSize.height) / 2 + ), + + size: preferredSize + )) + windowScene.requestGeometryUpdate(geometryPreferences) + } +#endif window.makeKeyAndVisible() self.window = window diff --git a/App/Scene Delegates/ReaderSceneDelegate.swift b/App/Scene Delegates/ReaderSceneDelegate.swift new file mode 100644 index 0000000..893c290 --- /dev/null +++ b/App/Scene Delegates/ReaderSceneDelegate.swift @@ -0,0 +1,41 @@ +// +// ReaderSceneDelegate.swift +// App +// +// Created by James Magahern on 8/22/22. +// + +import UIKit + +internal enum ReaderUserActivityKeys: String { + case htmlString + case baseURL +} + +class ReaderSceneDelegate: UIResponder, UIWindowSceneDelegate { + var window: UIWindow? + var readerViewController: ReaderViewController? + + func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) + { + guard let windowScene = (scene as? UIWindowScene) else { return } + guard let activity = connectionOptions.userActivities.first else { return } + + guard let userInfo = activity.userInfo, let htmlString: String = userInfo[ReaderUserActivityKeys.htmlString.rawValue] as? String else { return } + let baseURL = userInfo[ReaderUserActivityKeys.baseURL.rawValue] as? URL + + let window = UIWindow(windowScene: windowScene) + + let readerViewController = ReaderViewController(readableHTMLString: htmlString, baseURL: baseURL) + readerViewController.title = activity.title + + self.readerViewController = readerViewController + window.rootViewController = UINavigationController(rootViewController: readerViewController) + + windowScene.title = activity.title ?? "Reader" + windowScene.sizeRestrictions?.maximumSize = CGSize(width: 700.0, height: 1000.0) + + window.makeKeyAndVisible() + self.window = window + } +} diff --git a/App/SettingsSceneDelegate.swift b/App/Scene Delegates/SettingsSceneDelegate.swift similarity index 100% rename from App/SettingsSceneDelegate.swift rename to App/Scene Delegates/SettingsSceneDelegate.swift diff --git a/App/Supporting Files/Info.plist b/App/Supporting Files/Info.plist index baa99bb..2311df4 100644 --- a/App/Supporting Files/Info.plist +++ b/App/Supporting Files/Info.plist @@ -53,7 +53,7 @@ UISceneConfigurationName Browser UISceneDelegateClassName - $(PRODUCT_MODULE_NAME).SceneDelegate + $(PRODUCT_MODULE_NAME).BrowserSceneDelegate UISceneConfigurationName @@ -61,6 +61,12 @@ UISceneDelegateClassName $(PRODUCT_MODULE_NAME).SettingsSceneDelegate + + UISceneConfigurationName + Reader + UISceneDelegateClassName + $(PRODUCT_MODULE_NAME).ReaderSceneDelegate + diff --git a/SBrowser.xcodeproj/project.pbxproj b/SBrowser.xcodeproj/project.pbxproj index 57c305f..d9e80a8 100644 --- a/SBrowser.xcodeproj/project.pbxproj +++ b/SBrowser.xcodeproj/project.pbxproj @@ -22,7 +22,7 @@ 1AD3104325254FB900A4A952 /* FindOnPageViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD3104225254FB900A4A952 /* FindOnPageViewController.swift */; }; 1AD310452525586B00A4A952 /* DocumentControlItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AD310442525586B00A4A952 /* DocumentControlItemView.swift */; }; 1ADFF46024C7DE53006DC7AE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADFF45F24C7DE53006DC7AE /* AppDelegate.swift */; }; - 1ADFF46224C7DE53006DC7AE /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADFF46124C7DE53006DC7AE /* SceneDelegate.swift */; }; + 1ADFF46224C7DE53006DC7AE /* BrowserSceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADFF46124C7DE53006DC7AE /* BrowserSceneDelegate.swift */; }; 1ADFF46924C7DE54006DC7AE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1ADFF46824C7DE54006DC7AE /* Assets.xcassets */; }; 1ADFF46C24C7DE54006DC7AE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1ADFF46A24C7DE54006DC7AE /* LaunchScreen.storyboard */; }; 1ADFF47424C7DE9C006DC7AE /* BrowserViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADFF47324C7DE9C006DC7AE /* BrowserViewController.swift */; }; @@ -63,6 +63,7 @@ CD9B88C2272201E900DAAB7E /* SBRScriptPolicy.m in Sources */ = {isa = PBXBuildFile; fileRef = CD361CF5271A3718006E9CA5 /* SBRScriptPolicy.m */; }; CDAD9CE8263A2DF200FF7199 /* DocumentControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDAD9CE7263A2DF200FF7199 /* DocumentControlsView.swift */; }; CDAD9CEA263A318F00FF7199 /* ShareableURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDAD9CE9263A318F00FF7199 /* ShareableURL.swift */; }; + CDB6807D28B446FC007D787E /* ReaderSceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDB6807C28B446FC007D787E /* ReaderSceneDelegate.swift */; }; CDC4A1CF25E9D8F7007D33C6 /* Tagger.js in Resources */ = {isa = PBXBuildFile; fileRef = CDC4A1CE25E9D8F7007D33C6 /* Tagger.js */; }; CDC5DA3A25DB774D00BA8D99 /* Readability.js in Resources */ = {isa = PBXBuildFile; fileRef = CDC5DA3925DB774D00BA8D99 /* Readability.js */; }; CDC5DA3E25DB7C2C00BA8D99 /* ReaderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDC5DA3D25DB7C2C00BA8D99 /* ReaderViewController.swift */; }; @@ -121,7 +122,7 @@ 1AD310442525586B00A4A952 /* DocumentControlItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentControlItemView.swift; sourceTree = ""; }; 1ADFF45C24C7DE53006DC7AE /* Attractor.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Attractor.app; sourceTree = BUILT_PRODUCTS_DIR; }; 1ADFF45F24C7DE53006DC7AE /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; - 1ADFF46124C7DE53006DC7AE /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; + 1ADFF46124C7DE53006DC7AE /* BrowserSceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserSceneDelegate.swift; sourceTree = ""; }; 1ADFF46824C7DE54006DC7AE /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 1ADFF46B24C7DE54006DC7AE /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 1ADFF46D24C7DE54006DC7AE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -170,6 +171,7 @@ CD97CF9125D5BE6F00288FEE /* NavigationControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NavigationControlsView.swift; sourceTree = ""; }; CDAD9CE7263A2DF200FF7199 /* DocumentControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentControlsView.swift; sourceTree = ""; }; CDAD9CE9263A318F00FF7199 /* ShareableURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareableURL.swift; sourceTree = ""; }; + CDB6807C28B446FC007D787E /* ReaderSceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderSceneDelegate.swift; sourceTree = ""; }; CDC4A1CE25E9D8F7007D33C6 /* Tagger.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = Tagger.js; sourceTree = ""; }; CDC5DA3925DB774D00BA8D99 /* Readability.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = Readability.js; sourceTree = ""; }; CDC5DA3D25DB7C2C00BA8D99 /* ReaderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReaderViewController.swift; sourceTree = ""; }; @@ -286,8 +288,6 @@ isa = PBXGroup; children = ( 1ADFF45F24C7DE53006DC7AE /* AppDelegate.swift */, - 1ADFF46124C7DE53006DC7AE /* SceneDelegate.swift */, - CDF3468D276C105900FB3141 /* SettingsSceneDelegate.swift */, CD7A89162519872D0075991E /* KeyboardShortcuts.swift */, CD7A89132519759D0075991E /* Autocomplete */, 1ADFF47A24C7E176006DC7AE /* Backend */, @@ -297,6 +297,7 @@ 1AD3104125254FA300A4A952 /* Find on Page */, CD7F2133265DACEE0001D042 /* Hacks */, CDC5DA3C25DB7A5500BA8D99 /* Reader View */, + CDB6807B28B4456B007D787E /* Scene Delegates */, 1ADFF4CE24CBBCBD006DC7AE /* Script Policy UI */, CDE6A30225F023A000E912A4 /* Settings */, CDF255FB289DD7BD0059F021 /* Sync */, @@ -435,6 +436,16 @@ path = History; sourceTree = ""; }; + CDB6807B28B4456B007D787E /* Scene Delegates */ = { + isa = PBXGroup; + children = ( + 1ADFF46124C7DE53006DC7AE /* BrowserSceneDelegate.swift */, + CDF3468D276C105900FB3141 /* SettingsSceneDelegate.swift */, + CDB6807C28B446FC007D787E /* ReaderSceneDelegate.swift */, + ); + path = "Scene Delegates"; + sourceTree = ""; + }; CDC5DA3C25DB7A5500BA8D99 /* Reader View */ = { isa = PBXGroup; children = ( @@ -612,12 +623,13 @@ 1A03810D24E71CA700826501 /* ToolbarView.swift in Sources */, CD936A3D289DB88B0093A1AC /* UIView+Utils.swift in Sources */, CD470C4425DE070400AFBE0E /* BrowserViewController+Keyboard.swift in Sources */, + CDB6807D28B446FC007D787E /* ReaderSceneDelegate.swift in Sources */, CDD0522425F8055700DD1771 /* SearchProvider.swift in Sources */, CD853BD424E77BF900D2BDCC /* HistoryItem.swift in Sources */, 1ADFF48D24C8C176006DC7AE /* SBRProcessBundleBridge.m in Sources */, 1AB88F0624D4D3A90006F850 /* UIGestureRecognizer+Actions.swift in Sources */, CD7313E4270534B800053347 /* ScriptPolicyViewControllerDelegate.swift in Sources */, - 1ADFF46224C7DE53006DC7AE /* SceneDelegate.swift in Sources */, + 1ADFF46224C7DE53006DC7AE /* BrowserSceneDelegate.swift in Sources */, CD16844D269E709400B8F8A5 /* Box.swift in Sources */, CD853BCE24E7763900D2BDCC /* BrowserHistory.swift in Sources */, 1A03810B24E71C5600826501 /* ToolbarButtonContainerView.swift in Sources */,