Files
2021-05-06 12:11:46 +03:00

76 lines
1.7 KiB
Swift

//
// View.swift
// Cyberlock
//
// Created by Juraldinio on 9/1/19.
// Copyright © 2019 Omicronmedia. All rights reserved.
//
import Foundation
import AppKit
class View: NSView {
var useHandCursor = false {
didSet {
self.window?.invalidateCursorRects(for: self)
}
}
var isHoverEnabled = false
weak var target: AnyObject?
var action: Selector?
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init() {
super.init(frame: .zero)
setup()
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
setup()
}
func setup() {
addSubviews()
addConstraints()
}
func addSubviews() { }
func addConstraints() { }
func onMouseHover(_ hover: Bool) { }
override func resetCursorRects() {
super.resetCursorRects()
guard self.useHandCursor else { return }
self.addCursorRect(self.bounds, cursor: NSCursor.pointingHand)
}
override func mouseDown(with event: NSEvent) {
guard self.useHandCursor
, let action = self.action else {
super.mouseDown(with: event)
return
}
NSApp.sendAction(action, to: self.target, from: self)
}
override func mouseEntered(with theEvent: NSEvent) {
super.mouseEntered(with: theEvent)
guard self.isHoverEnabled else { return }
self.onMouseHover(true)
}
override func mouseExited(with theEvent: NSEvent) {
super.mouseExited(with: theEvent)
guard self.isHoverEnabled else { return }
self.onMouseHover(false)
}
}