-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ImageButton with some bells and whistles ✨
- Loading branch information
1 parent
4858036
commit 2da90dd
Showing
5 changed files
with
72 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// ImageButton.swift | ||
// Calendr | ||
// | ||
// Created by Paker on 24/11/2022. | ||
// | ||
|
||
import Cocoa | ||
|
||
class ImageButton: NSButton { | ||
|
||
private var trackingArea: NSTrackingArea? | ||
|
||
init() { | ||
super.init(frame: .zero) | ||
|
||
isBordered = false | ||
bezelStyle = .roundRect | ||
refusesFirstResponder = true | ||
showsBorderOnlyWhileMouseInside = true // this actually controls highlight ¯\_(ツ)_/¯ | ||
setContentCompressionResistancePriority(.required, for: .horizontal) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
// 🔨 For some reason addCursorRect is not reliable, | ||
// maybe because the container may clip the touch area and mess up | ||
// the mouse enter detection, but to be honest I have no idea why | ||
override func updateTrackingAreas() { | ||
|
||
if let trackingArea = trackingArea { | ||
removeTrackingArea(trackingArea) | ||
} | ||
|
||
trackingArea = NSTrackingArea( | ||
rect: bounds, | ||
options: [.cursorUpdate, .mouseMoved, .activeAlways], | ||
owner: self | ||
) | ||
|
||
addTrackingArea(trackingArea!) | ||
|
||
super.updateTrackingAreas() | ||
} | ||
|
||
override func cursorUpdate(with event: NSEvent) { | ||
super.cursorUpdate(with: event) | ||
NSCursor.pointingHand.set() | ||
} | ||
|
||
override func mouseMoved(with event: NSEvent) { | ||
super.mouseMoved(with: event) | ||
cursorUpdate(with: event) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters