From 3e46c06ba8ca3a55aea945c6294d19668337c754 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 1 Apr 2020 19:37:00 -0400 Subject: [PATCH] Start adding toolbar --- MongoView.xcodeproj/project.pbxproj | 20 +++-- MongoView/MongoController.swift | 21 ++++- .../Windows/DatabaseWindowController.swift | 78 +++++++++++++++++++ .../Windows/DatabaseWindowController.xib | 14 +++- MongoView/Windows/WindowStatusView.swift | 32 ++++++++ MongoView/Windows/WindowStatusView.xib | 42 ++++++++++ 6 files changed, 197 insertions(+), 10 deletions(-) create mode 100644 MongoView/Windows/WindowStatusView.swift create mode 100644 MongoView/Windows/WindowStatusView.xib diff --git a/MongoView.xcodeproj/project.pbxproj b/MongoView.xcodeproj/project.pbxproj index 5e293bb..a9b22bd 100644 --- a/MongoView.xcodeproj/project.pbxproj +++ b/MongoView.xcodeproj/project.pbxproj @@ -40,6 +40,8 @@ D63CDF4123C839010012D658 /* QueryViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDF3F23C839010012D658 /* QueryViewController.xib */; }; D63CDF4423C970C50012D658 /* DatabaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDF4223C970C50012D658 /* DatabaseViewController.swift */; }; D63CDF4523C970C50012D658 /* DatabaseViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDF4323C970C50012D658 /* DatabaseViewController.xib */; }; + D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6A7D095243541A400B46857 /* WindowStatusView.swift */; }; + D6A7D09A243546B500B46857 /* WindowStatusView.xib in Resources */ = {isa = PBXBuildFile; fileRef = D6A7D099243546B500B46857 /* WindowStatusView.xib */; }; D6D4665323CB730C00F13B1B /* MongoEvaluator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D4665223CB730C00F13B1B /* MongoEvaluator.swift */; }; /* End PBXBuildFile section */ @@ -93,6 +95,8 @@ D63CDF3F23C839010012D658 /* QueryViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = QueryViewController.xib; sourceTree = ""; }; D63CDF4223C970C50012D658 /* DatabaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseViewController.swift; sourceTree = ""; }; D63CDF4323C970C50012D658 /* DatabaseViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DatabaseViewController.xib; sourceTree = ""; }; + D6A7D095243541A400B46857 /* WindowStatusView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowStatusView.swift; sourceTree = ""; }; + D6A7D099243546B500B46857 /* WindowStatusView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = WindowStatusView.xib; sourceTree = ""; }; D6D4665223CB730C00F13B1B /* MongoEvaluator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MongoEvaluator.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -123,6 +127,8 @@ D60C863823CA2DD100C9DB8E /* ServerConnectWindowController.xib */, D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */, D63CDF3B23C838470012D658 /* DatabaseWindowController.xib */, + D6A7D095243541A400B46857 /* WindowStatusView.swift */, + D6A7D099243546B500B46857 /* WindowStatusView.xib */, ); path = Windows; sourceTree = ""; @@ -253,6 +259,7 @@ D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */, D60C863A23CA2DD100C9DB8E /* ServerConnectWindowController.xib in Resources */, D63CDF4523C970C50012D658 /* DatabaseViewController.xib in Resources */, + D6A7D09A243546B500B46857 /* WindowStatusView.xib in Resources */, D63CDF3D23C838470012D658 /* DatabaseWindowController.xib in Resources */, D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */, D63CDF4123C839010012D658 /* QueryViewController.xib in Resources */, @@ -274,6 +281,7 @@ D60C863F23CA2E2100C9DB8E /* ServerConnectViewController.swift in Sources */, D63CDF4423C970C50012D658 /* DatabaseViewController.swift in Sources */, D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */, + D6A7D096243541A400B46857 /* WindowStatusView.swift in Sources */, D63CDF4023C839010012D658 /* QueryViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -412,15 +420,15 @@ CODE_SIGN_ENTITLEMENTS = MongoView/MongoView.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 2; - DEVELOPMENT_TEAM = HGYVAQA9FW; + CURRENT_PROJECT_VERSION = 3; + DEVELOPMENT_TEAM = V4WK9KR9U2; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = MongoView/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.MongoView; + PRODUCT_BUNDLE_IDENTIFIER = space.vaccor.MongoView; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; }; @@ -433,15 +441,15 @@ CODE_SIGN_ENTITLEMENTS = MongoView/MongoView.entitlements; CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; - CURRENT_PROJECT_VERSION = 2; - DEVELOPMENT_TEAM = HGYVAQA9FW; + CURRENT_PROJECT_VERSION = 3; + DEVELOPMENT_TEAM = V4WK9KR9U2; ENABLE_HARDENED_RUNTIME = YES; INFOPLIST_FILE = MongoView/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/../Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = net.shadowfacts.MongoView; + PRODUCT_BUNDLE_IDENTIFIER = space.vaccor.MongoView; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; }; diff --git a/MongoView/MongoController.swift b/MongoView/MongoController.swift index 8c0d084..75164b3 100644 --- a/MongoView/MongoController.swift +++ b/MongoView/MongoController.swift @@ -16,6 +16,13 @@ class MongoController { var group: EventLoopGroup! var client: MongoClient! + + var status: Status = .connecting { + didSet { + statusDidChange.forEach { $0(status) } + } + } + var statusDidChange = [(Status) -> Void]() init(connectionString: String) { self.connectionString = connectionString @@ -31,7 +38,13 @@ class MongoController { group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - client = try! MongoClient(connectionString, using: group) + do { + client = try MongoClient(connectionString, using: group) + status = .success + } catch { + status = .failed + print("Failed to connect to Mongo: \(error)") + } } func db(for collection: DatabaseCollection) -> MongoDatabase { @@ -43,3 +56,9 @@ class MongoController { } } + +extension MongoController { + enum Status { + case connecting, success, failed + } +} diff --git a/MongoView/Windows/DatabaseWindowController.swift b/MongoView/Windows/DatabaseWindowController.swift index 8a7a5bb..7625dd9 100644 --- a/MongoView/Windows/DatabaseWindowController.swift +++ b/MongoView/Windows/DatabaseWindowController.swift @@ -10,6 +10,10 @@ import Cocoa class DatabaseWindowController: NSWindowController { + @IBOutlet weak var toolbar: NSToolbar! + + weak var statusView: WindowStatusView? + private var titleObservation: NSKeyValueObservation? var mongoController: MongoController! @@ -28,6 +32,11 @@ class DatabaseWindowController: NSWindowController { mongoController = MongoController(connectionString: "mongodb://localhost:27017") mongoController.setup() } + mongoController.statusDidChange.append({ [weak self] (status) in + guard let self = self else { return } + self.updateStatusText(status) + }) + self.updateStatusText(mongoController.status) databaseViewController = DatabaseViewController(mongoController: mongoController) contentViewController = databaseViewController @@ -43,6 +52,7 @@ class DatabaseWindowController: NSWindowController { window!.tabbingMode = .preferred window!.tabbingIdentifier = mongoController.connectionString + window!.titleVisibility = .hidden } override func newWindowForTab(_ sender: Any?) { @@ -52,5 +62,73 @@ class DatabaseWindowController: NSWindowController { private func updateWindowTitle() { window?.title = databaseViewController.title ?? "MongoView" } + + private func updateStatusText(_ status: MongoController.Status) { + guard let statusView = self.statusView else { return } + switch status { + case .connecting: + statusView.setText("Connecting...") + case .failed: + statusView.setText("Failed to connect") + case .success: + statusView.setText("Connected to \(self.mongoController.connectionString)") + } + } } + +extension NSToolbarItem.Identifier { + static let mongoStatus = NSToolbarItem.Identifier("MongoView.mongoStatus") + static let refresh = NSToolbarItem.Identifier("MongoView.refresh") +} + +extension DatabaseWindowController: NSToolbarDelegate { + func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { + return [ + .space, + .flexibleSpace, + .mongoStatus, + .refresh, + ] + } + + func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] { + return [ + .refresh, + .flexibleSpace, + .mongoStatus, + .flexibleSpace, + ] + } + + func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? { + if itemIdentifier == .mongoStatus { + return createMongoStatusToolbarItem() + } else if itemIdentifier == .refresh { + return createRefreshToolbarItem() + } + + return NSToolbarItem(itemIdentifier: itemIdentifier) + } + + func createMongoStatusToolbarItem() -> NSToolbarItem { + let item = NSToolbarItem(itemIdentifier: .mongoStatus) + item.label = "Status" + item.paletteLabel = "Status" + let statusView = WindowStatusView.create() + self.statusView = statusView + item.view = statusView + return item + } + + func createRefreshToolbarItem() -> NSToolbarItem { + let item = NSToolbarItem(itemIdentifier: .refresh) + item.label = "Refresh" + item.paletteLabel = "Refresh" + item.target = self + let button = NSButton(image: NSImage(named: NSImage.refreshTemplateName)!, target: nil, action: #selector(DatabaseViewController.refresh(_:))) + button.bezelStyle = .texturedRounded + item.view = button + return item + } +} diff --git a/MongoView/Windows/DatabaseWindowController.xib b/MongoView/Windows/DatabaseWindowController.xib index 3a204bf..16c0f49 100644 --- a/MongoView/Windows/DatabaseWindowController.xib +++ b/MongoView/Windows/DatabaseWindowController.xib @@ -1,12 +1,13 @@ - + - + + @@ -19,8 +20,15 @@ - + + + + + + + + diff --git a/MongoView/Windows/WindowStatusView.swift b/MongoView/Windows/WindowStatusView.swift new file mode 100644 index 0000000..38f9f9e --- /dev/null +++ b/MongoView/Windows/WindowStatusView.swift @@ -0,0 +1,32 @@ +// +// WindowStatusView.swift +// MongoView +// +// Created by Shadowfacts on 4/1/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import Cocoa + +class WindowStatusView: NSView { + + @IBOutlet weak var button: NSButton! + + static func create() -> WindowStatusView { + let nib = NSNib(nibNamed: "WindowStatusView", bundle: .main) + var objects: NSArray? = NSArray() + nib?.instantiate(withOwner: nil, topLevelObjects: &objects) + return objects!.first(where: { $0 is WindowStatusView }) as! WindowStatusView + } + + override func awakeFromNib() { + super.awakeFromNib() + + (button.cell as! NSButtonCell).imageDimsWhenDisabled = false + } + + func setText(_ text: String) { + button.title = text + } + +} diff --git a/MongoView/Windows/WindowStatusView.xib b/MongoView/Windows/WindowStatusView.xib new file mode 100644 index 0000000..3966cae --- /dev/null +++ b/MongoView/Windows/WindowStatusView.xib @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +