Better windows management

This commit is contained in:
Ivan Grachyov 2021-06-13 05:45:54 +03:00
parent 5ad2750b9b
commit ff9e0501bd
6 changed files with 81 additions and 33 deletions

View File

@ -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
}
}

View File

@ -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
}

View File

@ -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) {

View File

@ -693,13 +693,10 @@
<outlet property="delegate" destination="B8D-0N-5wS" id="98r-iN-zZc"/>
</connections>
</window>
<connections>
<segue destination="XfG-lQ-9wD" kind="relationship" relationship="window.shadowedContentViewController" id="cq2-FE-JQM"/>
</connections>
</windowController>
<customObject id="Oky-zY-oP4" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="253.5"/>
<point key="canvasLocation" x="-220" y="740"/>
</scene>
<!--Import View Controller-->
<scene sceneID="hIz-AP-VOD">
@ -793,11 +790,11 @@ DQ
<objects>
<viewController storyboardIdentifier="AccountsListViewController" id="29s-Rd-OUf" customClass="AccountsListViewController" customModule="Encrypted_Ink" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="Yjc-Zm-uZY">
<rect key="frame" x="0.0" y="0.0" width="251" height="350"/>
<rect key="frame" x="0.0" y="0.0" width="250" height="350"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="dkh-kG-EFj">
<rect key="frame" x="54" y="292" width="144" height="34"/>
<rect key="frame" x="53" y="292" width="144" height="34"/>
<textFieldCell key="cell" controlSize="large" selectable="YES" alignment="center" title="Accounts" id="9No-vQ-vBK">
<font key="font" metaFont="systemHeavy" size="29"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -805,12 +802,12 @@ DQ
</textFieldCell>
</textField>
<scrollView autohidesScrollers="YES" horizontalLineScroll="40" horizontalPageScroll="10" verticalLineScroll="40" verticalPageScroll="10" hasHorizontalScroller="NO" usesPredominantAxisScrolling="NO" horizontalScrollElasticity="none" translatesAutoresizingMaskIntoConstraints="NO" id="7bs-Kr-ija">
<rect key="frame" x="0.0" y="0.0" width="251" height="272"/>
<clipView key="contentView" id="RjU-hi-SHx">
<rect key="frame" x="1" y="1" width="249" height="270"/>
<rect key="frame" x="0.0" y="0.0" width="250" height="272"/>
<clipView key="contentView" ambiguous="YES" id="RjU-hi-SHx">
<rect key="frame" x="1" y="1" width="248" height="270"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnSelection="YES" multipleSelection="NO" autosaveColumns="NO" rowHeight="40" rowSizeStyle="automatic" viewBased="YES" id="glA-FK-Kdd">
<tableView verticalHuggingPriority="750" ambiguous="YES" allowsExpansionToolTips="YES" columnAutoresizingStyle="lastColumnOnly" columnSelection="YES" multipleSelection="NO" autosaveColumns="NO" rowHeight="40" rowSizeStyle="automatic" viewBased="YES" id="glA-FK-Kdd">
<rect key="frame" x="0.0" y="0.0" width="249" height="270"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<size key="intercellSpacing" width="17" height="0.0"/>
@ -872,6 +869,9 @@ DQ
</tableView>
</subviews>
</clipView>
<constraints>
<constraint firstAttribute="width" constant="250" id="zxW-2l-wsU"/>
</constraints>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="IT1-B8-OHF">
<rect key="frame" x="-100" y="-100" width="238" height="16"/>
<autoresizingMask key="autoresizingMask"/>
@ -882,7 +882,7 @@ DQ
</scroller>
</scrollView>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="ngQ-Bn-Kwd">
<rect key="frame" x="206" y="299" width="33" height="21"/>
<rect key="frame" x="205" y="299" width="33" height="21"/>
<buttonCell key="cell" type="inline" bezelStyle="inline" image="plus" catalog="system" imagePosition="only" alignment="center" lineBreakMode="truncatingTail" state="on" imageScaling="proportionallyDown" inset="2" id="JVh-da-a0h">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="systemBold" size="21"/>
@ -911,7 +911,7 @@ DQ
</viewController>
<customObject id="JTb-7y-Jwq" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="400" y="740"/>
<point key="canvasLocation" x="380" y="740"/>
</scene>
</scenes>
<resources>

View File

@ -33,14 +33,12 @@ class ImportViewController: NSViewController {
}
private func showAccountsList() {
if let accounts = storyboard?.instantiateController(withIdentifier: "AccountsListViewController") as? AccountsListViewController {
view.window?.contentViewController = accounts
}
let accountsListViewController = instantiate(AccountsListViewController.self)
view.window?.contentViewController = accountsListViewController
}
@IBAction func cancelButtonTapped(_ sender: NSButton) {
showAccountsList()
// TODO: in some cases should close the window
}
}

View File

@ -4,6 +4,19 @@ import Cocoa
struct Window {
static func showNew() -> NSWindowController {
closeAll()
let windowController = new
activate(windowController)
return windowController
}
static func activate(_ windowController: NSWindowController) {
windowController.showWindow(nil)
NSApp.activate(ignoringOtherApps: true)
windowController.window?.makeKeyAndOrderFront(nil)
}
static func closeAll() {
NSApplication.shared.windows.forEach { $0.close() }
}
@ -14,4 +27,20 @@ struct Window {
}
}
static var current: NSWindowController? {
return NSApplication.shared.windows.first?.windowController
}
static var new: NSWindowController {
return NSStoryboard.main.instantiateInitialController() as! NSWindowController
}
}
extension NSStoryboard {
static let main = NSStoryboard(name: "Main", bundle: nil)
}
func instantiate<ViewController: NSViewController>(_ type: ViewController.Type) -> ViewController {
return NSStoryboard.main.instantiateController(withIdentifier: String(describing: type)) as! ViewController
}