From ff9e0501bde9b72748b91e5575d8a2fc726cdc73 Mon Sep 17 00:00:00 2001 From: Ivan Grachyov Date: Sun, 13 Jun 2021 05:45:54 +0300 Subject: [PATCH] Better windows management --- Encrypted Ink/Agent.swift | 35 +++++++++++++------ Encrypted Ink/AppDelegate.swift | 3 +- .../Screens/AccountsListViewController.swift | 17 ++++++--- .../Screens/Base.lproj/Main.storyboard | 24 ++++++------- .../Screens/ImportViewController.swift | 6 ++-- Encrypted Ink/Window.swift | 29 +++++++++++++++ 6 files changed, 81 insertions(+), 33 deletions(-) diff --git a/Encrypted Ink/Agent.swift b/Encrypted Ink/Agent.swift index 453106d2..845ab683 100644 --- a/Encrypted Ink/Agent.swift +++ b/Encrypted Ink/Agent.swift @@ -3,29 +3,42 @@ import Cocoa class Agent { - + private var connectivity: NearbyConnectivity! func start() { connectivity = NearbyConnectivity(delegate: self) + showInitialScreen(onAppStart: true) + } + + func reopen() { + showInitialScreen(onAppStart: false) + } + + func showInitialScreen(onAppStart: Bool) { + let windowController: NSWindowController + if onAppStart, let currentWindowController = Window.current { + windowController = currentWindowController + Window.activate(windowController) + } else { + windowController = Window.showNew() + } + + let accounts = AccountsService.getAccounts() + if !accounts.isEmpty { + windowController.contentViewController = AccountsListViewController.with(preloadedAccounts: accounts) + } else { + windowController.contentViewController = instantiate(ImportViewController.self) + } } -} - -func showScreen() { // TODO: make private - NSApplication.shared.windows.forEach { $0.close() } - let storyboard = NSStoryboard(name: "Main", bundle: nil) - let windowController = storyboard.instantiateInitialController() as? NSWindowController - windowController?.showWindow(nil) - NSApp.activate(ignoringOtherApps: true) - windowController?.window?.makeKeyAndOrderFront(nil) } extension Agent: NearbyConnectivityDelegate { func didFind(link: String) { globalLink = link - showScreen() + // showScreen() // TODO: should show account selection } } diff --git a/Encrypted Ink/AppDelegate.swift b/Encrypted Ink/AppDelegate.swift index 26144e76..c34c5cef 100644 --- a/Encrypted Ink/AppDelegate.swift +++ b/Encrypted Ink/AppDelegate.swift @@ -13,11 +13,10 @@ class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { agent.start() - showScreen() } func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { - showScreen() + agent.reopen() return true } diff --git a/Encrypted Ink/Screens/AccountsListViewController.swift b/Encrypted Ink/Screens/AccountsListViewController.swift index 4945f9c4..758a1806 100644 --- a/Encrypted Ink/Screens/AccountsListViewController.swift +++ b/Encrypted Ink/Screens/AccountsListViewController.swift @@ -4,7 +4,13 @@ import Cocoa class AccountsListViewController: NSViewController { - private var accounts = AccountsService.getAccounts() + private var accounts = [Account]() + + static func with(preloadedAccounts: [Account]) -> AccountsListViewController { + let new = instantiate(AccountsListViewController.self) + new.accounts = preloadedAccounts + return new + } @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var tableView: NSTableView! { @@ -21,12 +27,15 @@ class AccountsListViewController: NSViewController { menu.addItem(NSMenuItem(title: "Copy address", action: #selector(didClickCopyAddress(_:)), keyEquivalent: "")) menu.addItem(NSMenuItem(title: "Remove account", action: #selector(didClickRemoveAccount(_:)), keyEquivalent: "")) tableView.menu = menu + + if accounts.isEmpty { + accounts = AccountsService.getAccounts() + } } @IBAction func addButtonTapped(_ sender: NSButton) { - if let importViewController = storyboard?.instantiateController(withIdentifier: "ImportViewController") as? ImportViewController { - view.window?.contentViewController = importViewController - } + let importViewController = instantiate(ImportViewController.self) + view.window?.contentViewController = importViewController } @objc private func didClickCopyAddress(_ sender: AnyObject) { diff --git a/Encrypted Ink/Screens/Base.lproj/Main.storyboard b/Encrypted Ink/Screens/Base.lproj/Main.storyboard index 83554f63..bfd8db78 100644 --- a/Encrypted Ink/Screens/Base.lproj/Main.storyboard +++ b/Encrypted Ink/Screens/Base.lproj/Main.storyboard @@ -693,13 +693,10 @@ - - - - + @@ -793,11 +790,11 @@ DQ - + - + @@ -805,12 +802,12 @@ DQ - - - + + + - + @@ -872,6 +869,9 @@ DQ + + +