From 6f2b98ac31efbe8fda07aa8c9bdce318f85ba274 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 15 Jul 2020 23:09:42 -0400 Subject: [PATCH] Allow creating multiple window tabs --- Gemini/AppDelegate.swift | 91 ++++++---------------------- Gemini/BrowserWindowController.swift | 15 +++-- Gemini/BrowserWindowController.xib | 2 +- 3 files changed, 30 insertions(+), 78 deletions(-) diff --git a/Gemini/AppDelegate.swift b/Gemini/AppDelegate.swift index d3d8b85..c1f8944 100644 --- a/Gemini/AppDelegate.swift +++ b/Gemini/AppDelegate.swift @@ -12,46 +12,12 @@ import Combine @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { - -// var window: NSWindow! var windowControllers = [BrowserWindowController]() - let homePage = URL(string: "gemini://gemini.circumlunar.space/")! -// private(set) lazy var navigationManager = NavigationManager(url: homePage) - -// private var toolbarTextField: NSTextField! -// private var toolbarUpdater: Cancellable? + static let homePage = URL(string: "gemini://gemini.circumlunar.space/")! func applicationDidFinishLaunching(_ aNotification: Notification) { -// // Create the SwiftUI view that provides the window contents. -// let contentView = ContentView(navigator: navigationManager) -// -// // Create the window and set the content view. -// window = NSWindow( -// contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), -// styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], -// backing: .buffered, defer: false) -// window.isReleasedWhenClosed = false -// window.center() -// window.setFrameAutosaveName("Main Window") -// window.contentView = NSHostingView(rootView: contentView) -//// window.title = "Gemini" -//// let toolbar = NSToolbar() -//// toolbar.delegate = self -//// toolbar.displayMode = .iconAndLabel -//// window.toolbar = toolbar -// window.makeKeyAndOrderFront(nil) -// -// toolbarTextField = NSTextField(string: navigationManager.currentURL.absoluteString) -// toolbarTextField.delegate = self -// -// toolbarTextField.widthAnchor.constraint(equalToConstant: 250).isActive = true -// -// toolbarUpdater = navigationManager.$currentURL.sink(receiveValue: { (newValue) in -// self.toolbarTextField.stringValue = newValue.absoluteString -// }) - let wc = BrowserWindowController() windowControllers.append(wc) wc.showWindow(nil) @@ -60,43 +26,22 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } + + func createNewWindowTab(attachedTo existingWindow: NSWindow) { + let wc = BrowserWindowController() + windowControllers.append(wc) + + if let tabGroup = existingWindow.tabGroup { + wc.window!.setFrame(existingWindow.frame, display: false) + tabGroup.addWindow(wc.window!) + tabGroup.selectedWindow = wc.window! + } + } + + @IBAction func newTab(_ sender: Any?) { + if let keyWindow = windowControllers.first(where: { $0.window != nil && $0.window!.isKeyWindow })?.window { + createNewWindowTab(attachedTo: keyWindow) + } + } } - -//extension AppDelegate: NSToolbarDelegate { -// func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { -// return [ -// .flexibleSpace, -// .urlField, -// ] -// } -// -// func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { -// return [ -// .flexibleSpace, -// .urlField, -// .flexibleSpace, -// ] -// } -// -// func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? { -// if itemIdentifier == .urlField { -// let item = NSToolbarItem(itemIdentifier: .urlField) -// item.label = "URL" -// item.view = toolbarTextField -// return item -// } else { -// return NSToolbarItem(itemIdentifier: .urlField) -// } -// } -//} -// -//extension AppDelegate: NSTextFieldDelegate { -// func controlTextDidEndEditing(_ obj: Notification) { -// print("did end edting, new text: '\(toolbarTextField.stringValue)'") -// } -//} -// -//extension NSToolbarItem.Identifier { -// static let urlField = NSToolbarItem.Identifier("GeminiUrlField") -//} diff --git a/Gemini/BrowserWindowController.swift b/Gemini/BrowserWindowController.swift index 219351c..3905a89 100644 --- a/Gemini/BrowserWindowController.swift +++ b/Gemini/BrowserWindowController.swift @@ -14,9 +14,10 @@ class BrowserWindowController: NSWindowController { @IBOutlet weak var toolbar: NSToolbar! private var locationTextField: NSTextField? - private var locationUpdater: Cancellable? - let navigator = NavigationManager(url: URL(string: "gemini://gemini.circumlunar.space/")!) + private var urlUpdater: Cancellable? + + let navigator = NavigationManager(url: AppDelegate.homePage) convenience init() { self.init(windowNibName: "BrowserWindowController") @@ -27,11 +28,17 @@ class BrowserWindowController: NSWindowController { contentViewController = NSHostingController(rootView: ContentView(navigator: navigator)) - locationUpdater = navigator.$currentURL.sink(receiveValue: self.updateLocationField(_:)) + urlUpdater = navigator.$currentURL.sink(receiveValue: self.currentURLChanged(_:)) } - private func updateLocationField(_ newValue: URL) { + private func currentURLChanged(_ newValue: URL) { locationTextField?.stringValue = newValue.absoluteString + + window?.title = newValue.absoluteString + } + + override func newWindowForTab(_ sender: Any?) { + (NSApp.delegate as! AppDelegate).createNewWindowTab(attachedTo: window!) } } diff --git a/Gemini/BrowserWindowController.xib b/Gemini/BrowserWindowController.xib index 4e89cda..c2aaa94 100644 --- a/Gemini/BrowserWindowController.xib +++ b/Gemini/BrowserWindowController.xib @@ -13,7 +13,7 @@ - +