Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions SlimHUD/Services/Displayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ class Displayer: HudsControllerInterface {
}

func updateShadows() {
volumeHud.setShadow(shadowType: settingsManager.shadowType,
shadowRadius: settingsManager.shadowRadius, color: settingsManager.shadowColor,
volumeHud.setShadow(type: settingsManager.shadowType,
radius: settingsManager.shadowRadius, color: settingsManager.shadowColor,
inset: settingsManager.shadowInset)
brightnessHud.setShadow(shadowType: settingsManager.shadowType,
shadowRadius: settingsManager.shadowRadius, color: settingsManager.shadowColor,
brightnessHud.setShadow(type: settingsManager.shadowType,
radius: settingsManager.shadowRadius, color: settingsManager.shadowColor,
inset: settingsManager.shadowInset)
keyboardHud.setShadow(shadowType: settingsManager.shadowType,
shadowRadius: settingsManager.shadowRadius, color: settingsManager.shadowColor,
keyboardHud.setShadow(type: settingsManager.shadowType,
radius: settingsManager.shadowRadius, color: settingsManager.shadowColor,
inset: settingsManager.shadowInset)
}

Expand Down
4 changes: 4 additions & 0 deletions SlimHUD/Services/HudAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class HudAnimator {
private static let GrowFactorComplementary: CGFloat = (1-GrowShrinkFactor) / 2
private static let ShrinkFactorComplementary: CGFloat = (1-1/GrowShrinkFactor) / 2

public static func cancel(barView: BarView) {
barView.layer?.removeAllAnimations()
}

public static func popIn(barView: BarView) {
barView.alphaValue = 1
}
Expand Down
26 changes: 7 additions & 19 deletions SlimHUD/Views/BarView/BarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class BarView: NSView {
@IBOutlet private var icon: NSImageView!

private var shadowView: NSView!
private var shadowColor: NSColor = .black
private var shadowRadius: Int = 0
private var shadowInset: Int = 5

override func awakeFromNib() {
if let icon = icon { // not set in
Expand All @@ -28,7 +25,6 @@ class BarView: NSView {

public func hideIcon(isHidden: Bool) {
icon.isHidden = isHidden
updateShadowView()
}

@available(OSX 10.14, *)
Expand Down Expand Up @@ -78,22 +74,15 @@ class BarView: NSView {
}

public func setupShadowAsView(radius: Int, color: NSColor = .black, inset: Int = 5) {
shadowColor = color
shadowInset = inset
shadowRadius = radius
updateShadowView()
}

public func updateShadowView() {
disableShadowView()

let shadowFrame = calculateShadowFrame()
let shadowFrame = calculateShadowFrame(shadowInset: inset)
shadowView = NSView(frame: shadowFrame)
shadowView.wantsLayer = true
shadowView.layer?.cornerRadius = (min(self.frame.height, self.frame.width) - CGFloat(shadowInset * 2)) / 2.2 // rounded rectangle
shadowView.layer?.backgroundColor = shadowColor.cgColor
if shadowRadius > 0 {
shadowView.layer?.mask = createMaskLayer(shadowFrame: shadowFrame)
shadowView.layer?.cornerRadius = (min(self.frame.height, self.frame.width) - CGFloat(inset * 2)) / 2.2 // rounded rectangle
shadowView.layer?.backgroundColor = color.cgColor
if radius > 0 {
shadowView.layer?.mask = createMaskLayer(shadowFrame: shadowFrame, shadowRadius: radius)
}
self.addSubview(shadowView, positioned: .below, relativeTo: icon.isHidden ? bar : self)
}
Expand All @@ -103,8 +92,7 @@ class BarView: NSView {
shadowView = nil
}
}

private func createMaskLayer(shadowFrame: NSRect) -> CALayer {
private func createMaskLayer(shadowFrame: NSRect, shadowRadius: Int) -> CALayer {
let verticalGradientLength = CGFloat(shadowRadius) / shadowFrame.height * 1.5 // only because it looks better
let verticalGradient = CAGradientLayer()
verticalGradient.startPoint = CGPoint(x: 0.0, y: 0.0)
Expand All @@ -129,7 +117,7 @@ class BarView: NSView {
return verticalGradient.flatten()
}

private func calculateShadowFrame() -> NSRect {
private func calculateShadowFrame(shadowInset: Int) -> NSRect {
if icon.isHidden {
return bar.frame.insetBy(dx: CGFloat(-20+(shadowInset)), dy: CGFloat(-20+shadowInset))
} else {
Expand Down
44 changes: 37 additions & 7 deletions SlimHUD/Views/Hud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ class Hud: NSView {
return windowController?.window?.contentView
}

private var shadowType: ShadowType = .nsshadow
private var shadowColor: NSColor = .black
private var shadowRadius: Int = 0
private var shadowInset: Int = 5

private var windowController: NSWindowController?

// flags to handle animation cancel (hud will show while hiding)
private var animatingOut = false
private var canceledAnimationOut = false

private override init(frame frameRect: NSRect) {
originPosition = .zero
super.init(frame: frameRect)
Expand All @@ -48,8 +57,12 @@ class Hud: NSView {
}

func show() {
if isHidden {
if isHidden || animatingOut {
self.isHidden = false
if animatingOut {
HudAnimator.cancel(barView: barView)
canceledAnimationOut = true
}
guard let hudView = hudView else { return }
if hudView.subviews.isEmpty {
hudView.addSubview(barView)
Expand All @@ -76,6 +89,7 @@ class Hud: NSView {

func hide(animated: Bool) {
if !isHidden {
animatingOut = true
if animated {
switch animationStyle {
case .none: HudAnimator.popOut(barView: barView, completion: commonAnimationOutCompletion)
Expand All @@ -95,9 +109,17 @@ class Hud: NSView {
}
}
}
/// HudAnimator.cancel() completes the running animations immediately, so this completion is always being called
/// We need to check if it has been called because the hud really closed, or because it was canceled
/// Only if really ended (wasn't canceled), we close the windowController (otherwise it would make the hud disappear for some frames)
private func commonAnimationOutCompletion() {
self.isHidden = true
self.windowController?.close()
if !canceledAnimationOut {
self.windowController?.close()
}
// reset all flags
animatingOut = false
canceledAnimationOut = false
}

@objc private func hideDelayed(_ animated: AnyObject?) {
Expand All @@ -113,6 +135,7 @@ class Hud: NSView {

public func hideIcon(isHidden: Bool) {
barView.hideIcon(isHidden: isHidden)
updateShadow()
}

@available(macOS 10.14, *)
Expand All @@ -124,12 +147,19 @@ class Hud: NSView {
barView.setIconImage(icon: icon, force: force)
}

public func setShadow(shadowType: ShadowType, shadowRadius: Int, color: NSColor, inset: Int = 5) {
if shadowType == .none { // FIXME: solve this in a better way
public func setShadow(type: ShadowType, radius: Int, color: NSColor, inset: Int = 5) {
shadowType = type
shadowColor = color
shadowInset = inset
shadowRadius = radius
updateShadow()
}
private func updateShadow() {
if shadowType == .none {
barView.setupShadow(enabled: false, shadowRadius: Constants.ShadowRadius)
barView.disableShadowView()
} else if shadowType == .view {
barView.setupShadowAsView(radius: shadowRadius, color: color, inset: inset)
barView.setupShadowAsView(radius: shadowRadius, color: shadowColor, inset: shadowInset)
barView.setupShadow(enabled: false, shadowRadius: Constants.ShadowRadius)
} else {
barView.setupShadow(enabled: true, shadowRadius: Constants.ShadowRadius)
Expand All @@ -139,7 +169,7 @@ class Hud: NSView {

public func setHeight(height: CGFloat) {
barView.setFrameSize(NSSize(width: barView.frame.width, height: height + Constants.ShadowRadius * 3))
barView.updateShadowView()
updateShadow()
}

public func setThickness(thickness: CGFloat, flatBar: Bool) {
Expand All @@ -152,7 +182,7 @@ class Hud: NSView {
}
barView.bar.layer?.cornerRadius = thickness/2 // setting up outer layer
barView.bar.frame.size.width = thickness
barView.updateShadowView()
updateShadow()
}

public func getFrame() -> NSRect {
Expand Down