69 lines
2.1 KiB
Swift
69 lines
2.1 KiB
Swift
//
|
|
// EditTextfield.swift
|
|
// PrivadoVPN
|
|
//
|
|
// Created by Murad Shabanov on 28.10.2021.
|
|
// Copyright © 2021 Privado LLC. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class EditTextField: NSTextField {
|
|
|
|
private func performEditingKeyEquivalent(with event: NSEvent) -> Bool {
|
|
|
|
guard event.type == NSEvent.EventType.keyDown else { return false }
|
|
|
|
let commandKey = NSEvent.ModifierFlags.command.rawValue
|
|
let commandShiftKey = NSEvent.ModifierFlags.command.rawValue | NSEvent.ModifierFlags.shift.rawValue
|
|
|
|
let key = event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue
|
|
switch key {
|
|
case commandKey: return self.handleCommandKey(keyCode: event.keyCode)
|
|
case commandShiftKey: return self.handleShiftKey(keyCode: event.keyCode)
|
|
default:
|
|
break
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
private func handleCommandKey(keyCode: UInt16) -> Bool {
|
|
|
|
switch keyCode {
|
|
case KeyCode.x:
|
|
if NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: self) { return true }
|
|
case KeyCode.c:
|
|
if NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: self) { return true }
|
|
case KeyCode.v:
|
|
if NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: self) { return true }
|
|
case KeyCode.z:
|
|
if NSApp.sendAction(Selector(("undo:")), to: nil, from: self) { return true }
|
|
case KeyCode.a:
|
|
if NSApp.sendAction(#selector(NSResponder.selectAll(_:)), to: nil, from: self) { return true }
|
|
default:
|
|
break
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
private func handleShiftKey(keyCode: UInt16) -> Bool {
|
|
if keyCode == KeyCode.z {
|
|
if NSApp.sendAction(Selector(("redo:")), to: nil, from: self) { return true }
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// Allow hotkeys in field
|
|
override func performKeyEquivalent(with event: NSEvent) -> Bool {
|
|
if self.performEditingKeyEquivalent(with: event) {
|
|
return true
|
|
}
|
|
|
|
return super.performKeyEquivalent(with: event)
|
|
}
|
|
|
|
}
|