From 030c1db45c257fbf6de1d02743fe80e2afe1b876 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Fri, 31 Jul 2020 16:36:10 -0700 Subject: [PATCH] Long press on the window button to open a new tab --- SBrowser.xcodeproj/project.pbxproj | 4 ++++ .../Browser View/BrowserViewController.swift | 16 ++++++++++++++++ .../ToolbarViewController.swift | 5 +++++ .../Utilities/UIGestureRecognizer+Actions.swift | 16 ++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 SBrowser/Utilities/UIGestureRecognizer+Actions.swift diff --git a/SBrowser.xcodeproj/project.pbxproj b/SBrowser.xcodeproj/project.pbxproj index 392f9d4..59e79f7 100644 --- a/SBrowser.xcodeproj/project.pbxproj +++ b/SBrowser.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ 1A14FC2824D26749009B3F83 /* Tab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A14FC2724D26749009B3F83 /* Tab.swift */; }; 1AB88EFD24D3BA560006F850 /* TabController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB88EFC24D3BA560006F850 /* TabController.swift */; }; 1AB88EFF24D3BBA50006F850 /* TabPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB88EFE24D3BBA50006F850 /* TabPickerViewController.swift */; }; + 1AB88F0624D4D3A90006F850 /* UIGestureRecognizer+Actions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1AB88F0524D4D3A90006F850 /* UIGestureRecognizer+Actions.swift */; }; 1ADFF46024C7DE53006DC7AE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADFF45F24C7DE53006DC7AE /* AppDelegate.swift */; }; 1ADFF46224C7DE53006DC7AE /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1ADFF46124C7DE53006DC7AE /* SceneDelegate.swift */; }; 1ADFF46924C7DE54006DC7AE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1ADFF46824C7DE54006DC7AE /* Assets.xcassets */; }; @@ -62,6 +63,7 @@ 1A14FC2724D26749009B3F83 /* Tab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tab.swift; sourceTree = ""; }; 1AB88EFC24D3BA560006F850 /* TabController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabController.swift; sourceTree = ""; }; 1AB88EFE24D3BBA50006F850 /* TabPickerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabPickerViewController.swift; sourceTree = ""; }; + 1AB88F0524D4D3A90006F850 /* UIGestureRecognizer+Actions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIGestureRecognizer+Actions.swift"; sourceTree = ""; }; 1ADFF45C24C7DE53006DC7AE /* SBrowser.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SBrowser.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 = ""; }; @@ -238,6 +240,7 @@ children = ( 1ADFF4C224CA6AF6006DC7AE /* CGPoint+Utils.swift */, 1ADFF4C624CA6DEB006DC7AE /* UIEdgeInsets+Layout.swift */, + 1AB88F0524D4D3A90006F850 /* UIGestureRecognizer+Actions.swift */, ); path = Utilities; sourceTree = ""; @@ -360,6 +363,7 @@ 1ADFF47424C7DE9C006DC7AE /* BrowserViewController.swift in Sources */, 1ADFF4D024CBBCD1006DC7AE /* ScriptPolicyControl.swift in Sources */, 1ADFF48D24C8C176006DC7AE /* SBRProcessBundleBridge.m in Sources */, + 1AB88F0624D4D3A90006F850 /* UIGestureRecognizer+Actions.swift in Sources */, 1ADFF46224C7DE53006DC7AE /* SceneDelegate.swift in Sources */, 1ADFF4CB24CB8278006DC7AE /* ScriptControllerIconView.swift in Sources */, 1AB88EFD24D3BA560006F850 /* TabController.swift in Sources */, diff --git a/SBrowser/Browser View/BrowserViewController.swift b/SBrowser/Browser View/BrowserViewController.swift index 6351415..da825fc 100644 --- a/SBrowser/Browser View/BrowserViewController.swift +++ b/SBrowser/Browser View/BrowserViewController.swift @@ -123,6 +123,22 @@ class BrowserViewController: UIViewController, WKNavigationDelegate, self.present(navController, animated: true, completion: nil) }), for: .touchUpInside) + let newTabAction = UIAction { [unowned self] action in + if let gestureRecognizer = action.sender as? UILongPressGestureRecognizer { + if gestureRecognizer.state != .began { return } + } + + // Create new tab + let newTab = tabController.createNewTab(url: nil) + self.tab = newTab + } + + let gestureRecognizer = UILongPressGestureRecognizer(action: newTabAction) + toolbarController.windowButton.addGestureRecognizer(gestureRecognizer) + + // New tab button + toolbarController.newTabButton.addAction(newTabAction, for: .touchUpInside) + // TextField delegate toolbarController.urlBar.textField.delegate = self diff --git a/SBrowser/Titlebar and URL Bar/ToolbarViewController.swift b/SBrowser/Titlebar and URL Bar/ToolbarViewController.swift index d11e207..8dcd93e 100644 --- a/SBrowser/Titlebar and URL Bar/ToolbarViewController.swift +++ b/SBrowser/Titlebar and URL Bar/ToolbarViewController.swift @@ -155,6 +155,7 @@ class ToolbarViewController: UIViewController let windowButton = UIButton(frame: .zero) let backButton = UIButton(frame: .zero) let forwardButton = UIButton(frame: .zero) + let newTabButton = UIButton(frame: .zero) var darkModeEnabled: Bool = false { didSet { @@ -189,6 +190,9 @@ class ToolbarViewController: UIViewController // Forward button forwardButton.setImage(UIImage(systemName: "chevron.right"), for: .normal) + // New tab button + newTabButton.setImage(UIImage(systemName: "plus"), for: .normal) + let toolbarAnimationDuration: TimeInterval = 0.3 urlBar.textField.addAction(.init(handler: { [traitCollection, toolbarView, urlBar] _ in if traitCollection.horizontalSizeClass == .compact { @@ -223,6 +227,7 @@ class ToolbarViewController: UIViewController toolbarView.trailingButtonsView.addButtonView(darkModeButton) toolbarView.trailingButtonsView.addButtonView(shareButton) toolbarView.trailingButtonsView.addButtonView(scriptControllerIconView) + toolbarView.trailingButtonsView.addButtonView(newTabButton) toolbarView.trailingButtonsView.addButtonView(windowButton) } } diff --git a/SBrowser/Utilities/UIGestureRecognizer+Actions.swift b/SBrowser/Utilities/UIGestureRecognizer+Actions.swift new file mode 100644 index 0000000..96bd7f3 --- /dev/null +++ b/SBrowser/Utilities/UIGestureRecognizer+Actions.swift @@ -0,0 +1,16 @@ +// +// UIGestureRecognizer+Actions.swift +// SBrowser +// +// Created by James Magahern on 7/31/20. +// + +import UIKit + +extension UIGestureRecognizer +{ + convenience init(action: UIAction) { + self.init(target: action, action: NSSelectorFromString("_performActionWithSender:")) + objc_setAssociatedObject(self, "associatedUIAction", action, .OBJC_ASSOCIATION_RETAIN) + } +}