From 43675d6f8703b364205cb8a3e6889a2301d501c7 Mon Sep 17 00:00:00 2001 From: Ivan Grachev Date: Wed, 23 Jun 2021 22:15:56 +0300 Subject: [PATCH] Fix status bar glitches when showing alerts --- Encrypted Ink.xcodeproj/project.pbxproj | 4 ++++ Encrypted Ink/Agent.swift | 5 ++++- Encrypted Ink/AppDelegate.swift | 2 +- .../Screens/AccountsListViewController.swift | 4 ++-- Encrypted Ink/Views/Alert.swift | 15 +++++++++++++++ Encrypted Ink/Window.swift | 6 ++++-- 6 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 Encrypted Ink/Views/Alert.swift diff --git a/Encrypted Ink.xcodeproj/project.pbxproj b/Encrypted Ink.xcodeproj/project.pbxproj index 968b4a50..d15fc789 100644 --- a/Encrypted Ink.xcodeproj/project.pbxproj +++ b/Encrypted Ink.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 2C208A9F26813408005BA500 /* Secrets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C208A9E26813408005BA500 /* Secrets.swift */; }; 2C528A16267FA8EB00CA3ADD /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C528A15267FA8EB00CA3ADD /* Defaults.swift */; }; 2C6706A5267A6BFE006AAEF2 /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C6706A4267A6BFE006AAEF2 /* Bundle.swift */; }; + 2C78F8282683BDCC00C10670 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C78F8272683BDCC00C10670 /* Alert.swift */; }; 2C797E7E267BB88800F2CE2D /* WelcomeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C797E7D267BB88800F2CE2D /* WelcomeViewController.swift */; }; 2C8A09B52675101300993638 /* AccountsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09B42675101300993638 /* AccountsService.swift */; }; 2C8A09C6267513FC00993638 /* Agent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C8A09C5267513FC00993638 /* Agent.swift */; }; @@ -73,6 +74,7 @@ 2C208A9E26813408005BA500 /* Secrets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Secrets.swift; sourceTree = ""; }; 2C528A15267FA8EB00CA3ADD /* Defaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Defaults.swift; sourceTree = ""; }; 2C6706A4267A6BFE006AAEF2 /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = ""; }; + 2C78F8272683BDCC00C10670 /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = ""; }; 2C797E7D267BB88800F2CE2D /* WelcomeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeViewController.swift; sourceTree = ""; }; 2C8A09B42675101300993638 /* AccountsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsService.swift; sourceTree = ""; }; 2C8A09C5267513FC00993638 /* Agent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Agent.swift; sourceTree = ""; }; @@ -217,6 +219,7 @@ isa = PBXGroup; children = ( 2C8A09E226757FC000993638 /* AccountCellView.swift */, + 2C78F8272683BDCC00C10670 /* Alert.swift */, ); path = Views; sourceTree = ""; @@ -391,6 +394,7 @@ 2C8A09E326757FC000993638 /* AccountCellView.swift in Sources */, 0DB729202674E2DB0011F7A1 /* EIP712Domain.swift in Sources */, 0DB729182674E2DB0011F7A1 /* EIP712Signer.swift in Sources */, + 2C78F8282683BDCC00C10670 /* Alert.swift in Sources */, 2C8A09EE2675965F00993638 /* WaitingViewController.swift in Sources */, 2C797E7E267BB88800F2CE2D /* WelcomeViewController.swift in Sources */, 0DB729192674E2DB0011F7A1 /* EIP712TypedData.swift in Sources */, diff --git a/Encrypted Ink/Agent.swift b/Encrypted Ink/Agent.swift index 3a27f22f..1efc318e 100644 --- a/Encrypted Ink/Agent.swift +++ b/Encrypted Ink/Agent.swift @@ -13,6 +13,7 @@ class Agent: NSObject { private var statusBarItem: NSStatusItem! private var hasPassword = Keychain.password != nil private var didEnterPasswordOnStart = false + var statusBarButtonIsBlocked = false func start() { checkPasteboardAndOpen(onAppStart: true) @@ -107,7 +108,7 @@ class Agent: NSObject { func warnBeforeQuitting() { Window.activateWindow(nil) - let alert = NSAlert() + let alert = Alert() alert.messageText = "Quit Encrypted Ink?" alert.informativeText = "You won't be able to sign requests." alert.alertStyle = .warning @@ -116,6 +117,7 @@ class Agent: NSObject { if alert.runModal() == .alertFirstButtonReturn { NSApp.terminate(nil) } + setupStatusBarItem() } @objc private func didSelectQuitMenuItem() { @@ -132,6 +134,7 @@ class Agent: NSObject { } @objc private func statusBarButtonClicked(sender: NSStatusBarButton) { + guard !statusBarButtonIsBlocked else { return } if let event = NSApp.currentEvent, event.type == .rightMouseUp { statusBarItem.menu = statusBarMenu statusBarItem.button?.performClick(nil) diff --git a/Encrypted Ink/AppDelegate.swift b/Encrypted Ink/AppDelegate.swift index ad2dc63b..7eb3b4ce 100644 --- a/Encrypted Ink/AppDelegate.swift +++ b/Encrypted Ink/AppDelegate.swift @@ -32,7 +32,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { } @IBAction func didCmdQ(_ sender: Any) { - Window.closeAll() + Window.closeAll(updateStatusBarItem: false) agent.warnBeforeQuitting() } diff --git a/Encrypted Ink/Screens/AccountsListViewController.swift b/Encrypted Ink/Screens/AccountsListViewController.swift index cfeedae9..33b42060 100644 --- a/Encrypted Ink/Screens/AccountsListViewController.swift +++ b/Encrypted Ink/Screens/AccountsListViewController.swift @@ -72,7 +72,7 @@ class AccountsListViewController: NSViewController { @objc private func didClickRemoveAccount(_ sender: AnyObject) { let row = tableView.clickedRow - let alert = NSAlert() + let alert = Alert() alert.messageText = "Removed accounts can't be recovered." alert.alertStyle = .critical alert.addButton(withTitle: "Cancel") @@ -89,7 +89,7 @@ class AccountsListViewController: NSViewController { } private func showInstructionsAlert() { - let alert = NSAlert() + let alert = Alert() alert.messageText = "How to start?" alert.informativeText = "1. Open your favourite dapp.\n\n2. Press “Copy to clipboard”\nunder WalletConnect QR code.\n\n3. Launch Encrypted Ink." alert.alertStyle = .informational diff --git a/Encrypted Ink/Views/Alert.swift b/Encrypted Ink/Views/Alert.swift new file mode 100644 index 00000000..7f60fb5c --- /dev/null +++ b/Encrypted Ink/Views/Alert.swift @@ -0,0 +1,15 @@ +// Copyright © 2021 Encrypted Ink. All rights reserved. + +import Cocoa + +class Alert: NSAlert { + + override func runModal() -> NSApplication.ModalResponse { + defer { + Agent.shared.statusBarButtonIsBlocked = false + } + Agent.shared.statusBarButtonIsBlocked = true + return super.runModal() + } + +} diff --git a/Encrypted Ink/Window.swift b/Encrypted Ink/Window.swift index 87214fe2..683bbda5 100644 --- a/Encrypted Ink/Window.swift +++ b/Encrypted Ink/Window.swift @@ -26,9 +26,11 @@ struct Window { activateBrowser() } - static func closeAll() { + static func closeAll(updateStatusBarItem: Bool = true) { NSApplication.shared.windows.forEach { $0.close() } - Agent.shared.setupStatusBarItem() + if updateStatusBarItem { + Agent.shared.setupStatusBarItem() + } } static func activateBrowser() {