]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Cut/copy/paste now work
authorEric Kuck <eric@bluelinelabs.com>
Thu, 10 Jan 2019 09:21:20 +0000 (11:21 +0200)
committerRoopesh Chander <roop@roopc.net>
Mon, 14 Jan 2019 09:22:36 +0000 (14:52 +0530)
Signed-off-by: Eric Kuck <eric@bluelinelabs.com>
WireGuard/WireGuard/UI/macOS/Application.swift
WireGuard/WireGuard/UI/macOS/StatusMenu.swift
WireGuard/WireGuard/UI/macOS/View/ConfTextStorage.swift

index 9f7b81055c69ad1b489ac750c1bf56226c1539cf..fba0ac6e70402970fbbab71e3b1f9156635c8ad4 100644 (file)
@@ -3,15 +3,45 @@
 
 import Cocoa
 
-var appDelegate: AppDelegate?
-
 class Application: NSApplication {
+
+    private let editorCommands = [
+        "x": #selector(NSText.cut(_:)),
+        "c": #selector(NSText.copy(_:)),
+        "v": #selector(NSText.paste(_:)),
+        "z": #selector(UndoActionRespondable.undo(_:)),
+        "a": #selector(NSResponder.selectAll(_:)),
+        "Z": #selector(UndoActionRespondable.redo(_:))
+    ]
+
+    private var appDelegate: AppDelegate? //swiftlint:disable:this weak_delegate
+
     // We use a custom Application class to be able to set the app delegate
     // before app.run() gets called in NSApplicationMain().
-    override class var shared: NSApplication {
-        let app = NSApplication.shared
+    override init() {
+        super.init()
         appDelegate = AppDelegate() // Keep a strong reference to the app delegate
-        app.delegate = appDelegate
-        return app
+        delegate = appDelegate
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
     }
+
+    override func sendEvent(_ event: NSEvent) {
+        let modifierFlags = event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue
+
+        if event.type == .keyDown,
+            (modifierFlags == NSEvent.ModifierFlags.command.rawValue || modifierFlags == NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue),
+            let selector = editorCommands[event.charactersIgnoringModifiers ?? ""] {
+            sendAction(selector, to: nil, from: self)
+        } else {
+            super.sendEvent(event)
+        }
+    }
+}
+
+@objc protocol UndoActionRespondable {
+    func undo(_ sender: AnyObject)
+    func redo(_ sender: AnyObject)
 }
index a8a913f216a72165cd067a2bbddeeb27e1f04471..0f5d6655fa41e189fe8665cfb2baf00d5072d4e6 100644 (file)
@@ -10,8 +10,8 @@ class StatusMenu: NSMenu {
 
     var statusMenuItem: NSMenuItem?
     var networksMenuItem: NSMenuItem?
-    var firstTunnelMenuItemIndex: Int = 0
-    var numberOfTunnelMenuItems: Int = 0
+    var firstTunnelMenuItemIndex = 0
+    var numberOfTunnelMenuItems = 0
 
     var manageTunnelsRootVC: ManageTunnelsRootViewController?
     lazy var manageTunnelsWindow: NSWindow = {
@@ -62,6 +62,7 @@ class StatusMenu: NSMenu {
     }
 
     @discardableResult
+    //swiftlint:disable:next cyclomatic_complexity
     func updateStatusMenuItems(with tunnel: TunnelContainer, ignoreInactive: Bool) -> Bool {
         guard let statusMenuItem = statusMenuItem, let networksMenuItem = networksMenuItem else { return false }
         var statusText: String
index 5ed3e6809c3860cd88bf9b333b8e7574fdb83393..4678cc37b9a9160a5cc5ed0b6835d6a125d08c74 100644 (file)
@@ -43,6 +43,7 @@ class ConfTextStorage: NSTextStorage {
         fatalError("init(pasteboardPropertyList:ofType:) has not been implemented")
     }
 
+    //swiftlint:disable:next function_body_length
     func updateAttributes(for theme: TextColorTheme) {
         self.defaultAttributes = [
             .foregroundColor: theme.plainText,