Allow creating multiple window tabs

This commit is contained in:
Shadowfacts 2020-07-15 23:09:42 -04:00
parent f59c43afa7
commit 6f2b98ac31
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 30 additions and 78 deletions

View File

@ -13,45 +13,11 @@ import Combine
@NSApplicationMain @NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
// var window: NSWindow!
var windowControllers = [BrowserWindowController]() var windowControllers = [BrowserWindowController]()
let homePage = URL(string: "gemini://gemini.circumlunar.space/")! static let homePage = URL(string: "gemini://gemini.circumlunar.space/")!
// private(set) lazy var navigationManager = NavigationManager(url: homePage)
// private var toolbarTextField: NSTextField!
// private var toolbarUpdater: Cancellable?
func applicationDidFinishLaunching(_ aNotification: Notification) { 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() let wc = BrowserWindowController()
windowControllers.append(wc) windowControllers.append(wc)
wc.showWindow(nil) wc.showWindow(nil)
@ -61,42 +27,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Insert code here to tear down your application // Insert code here to tear down your application
} }
} func createNewWindowTab(attachedTo existingWindow: NSWindow) {
let wc = BrowserWindowController()
windowControllers.append(wc)
//extension AppDelegate: NSToolbarDelegate { if let tabGroup = existingWindow.tabGroup {
// func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { wc.window!.setFrame(existingWindow.frame, display: false)
// return [ tabGroup.addWindow(wc.window!)
// .flexibleSpace, tabGroup.selectedWindow = wc.window!
// .urlField, }
// ] }
// }
// @IBAction func newTab(_ sender: Any?) {
// func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { if let keyWindow = windowControllers.first(where: { $0.window != nil && $0.window!.isKeyWindow })?.window {
// return [ createNewWindowTab(attachedTo: keyWindow)
// .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")
//}

View File

@ -14,9 +14,10 @@ class BrowserWindowController: NSWindowController {
@IBOutlet weak var toolbar: NSToolbar! @IBOutlet weak var toolbar: NSToolbar!
private var locationTextField: NSTextField? 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() { convenience init() {
self.init(windowNibName: "BrowserWindowController") self.init(windowNibName: "BrowserWindowController")
@ -27,11 +28,17 @@ class BrowserWindowController: NSWindowController {
contentViewController = NSHostingController(rootView: ContentView(navigator: navigator)) 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 locationTextField?.stringValue = newValue.absoluteString
window?.title = newValue.absoluteString
}
override func newWindowForTab(_ sender: Any?) {
(NSApp.delegate as! AppDelegate).createNewWindowTab(attachedTo: window!)
} }
} }

View File

@ -13,7 +13,7 @@
</customObject> </customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/> <customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/> <customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" frameAutosaveName="BrowserWindow" animationBehavior="default" tabbingMode="preferred" toolbarStyle="unified" titleVisibility="hidden" id="F0z-JX-Cv5"> <window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" frameAutosaveName="BrowserWindow" animationBehavior="default" tabbingIdentifier="BrowserWindow" tabbingMode="preferred" toolbarStyle="unified" titleVisibility="hidden" id="F0z-JX-Cv5">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES" fullSizeContentView="YES"/> <windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES" fullSizeContentView="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/> <windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="270"/> <rect key="contentRect" x="196" y="240" width="480" height="270"/>