2019-03-29 19:36:34 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class TitleBar: NSVisualEffectView {
|
2020-06-27 09:12:46 +00:00
|
|
|
unowned var common: Common
|
2024-04-27 20:11:33 +00:00
|
|
|
var option: OptionHelper { return common.option }
|
2019-03-29 19:36:34 +00:00
|
|
|
|
2024-04-27 20:11:33 +00:00
|
|
|
var systemBar: NSView? { return common.window?.standardWindowButton(.closeButton)?.superview }
|
2019-03-29 19:36:34 +00:00
|
|
|
static var height: CGFloat {
|
2024-04-27 20:11:33 +00:00
|
|
|
return NSWindow.frameRect(forContentRect: CGRect.zero, styleMask: .titled).size.height
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
|
|
|
var buttons: [NSButton] {
|
2024-04-27 20:11:33 +00:00
|
|
|
return ([.closeButton, .miniaturizeButton, .zoomButton] as [NSWindow.ButtonType]).compactMap { common.window?.standardWindowButton($0) }
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:38:02 +00:00
|
|
|
override var material: NSVisualEffectView.Material {
|
|
|
|
get { return super.material }
|
|
|
|
set {
|
|
|
|
super.material = newValue
|
|
|
|
// fix for broken deprecated materials
|
2024-04-27 20:11:33 +00:00
|
|
|
if material == .light || material == .dark || material == .mediumLight || material == .ultraDark {
|
2019-03-29 19:38:02 +00:00
|
|
|
state = .active
|
|
|
|
} else {
|
|
|
|
state = .followsWindowActiveState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 09:12:46 +00:00
|
|
|
init(frame: NSRect, window: NSWindow, common com: Common) {
|
2024-04-27 20:11:33 +00:00
|
|
|
let f = NSRect(x: 0, y: frame.size.height - TitleBar.height, width: frame.size.width, height: TitleBar.height + 1)
|
2020-06-27 09:12:46 +00:00
|
|
|
common = com
|
2019-03-30 20:26:31 +00:00
|
|
|
super.init(frame: f)
|
2019-04-21 10:29:42 +00:00
|
|
|
buttons.forEach { $0.isHidden = true }
|
|
|
|
isHidden = true
|
2019-03-29 19:36:34 +00:00
|
|
|
alphaValue = 0
|
|
|
|
blendingMode = .withinWindow
|
2019-07-20 10:16:37 +00:00
|
|
|
autoresizingMask = [.width, .minYMargin]
|
2019-03-30 20:26:31 +00:00
|
|
|
systemBar?.alphaValue = 0
|
2019-03-29 19:38:02 +00:00
|
|
|
state = .followsWindowActiveState
|
2019-03-29 19:39:42 +00:00
|
|
|
wantsLayer = true
|
2019-03-29 19:36:34 +00:00
|
|
|
|
2019-03-30 20:26:31 +00:00
|
|
|
window.contentView?.addSubview(self, positioned: .above, relativeTo: nil)
|
2019-03-29 19:36:34 +00:00
|
|
|
window.titlebarAppearsTransparent = true
|
|
|
|
window.styleMask.insert(.fullSizeContentView)
|
2024-03-19 22:15:42 +00:00
|
|
|
set(appearance: Int(option.mac.macos_title_bar_appearance))
|
|
|
|
set(material: Int(option.mac.macos_title_bar_material))
|
|
|
|
set(color: option.mac.macos_title_bar_color)
|
2019-03-30 20:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// catch these events so they are not propagated to the underlying view
|
|
|
|
override func mouseDown(with event: NSEvent) { }
|
|
|
|
|
|
|
|
override func mouseUp(with event: NSEvent) {
|
|
|
|
if event.clickCount > 1 {
|
|
|
|
let def = UserDefaults.standard
|
|
|
|
var action = def.string(forKey: "AppleActionOnDoubleClick")
|
|
|
|
|
|
|
|
// macOS 10.10 and earlier
|
|
|
|
if action == nil {
|
|
|
|
action = def.bool(forKey: "AppleMiniaturizeOnDoubleClick") == true ?
|
|
|
|
"Minimize" : "Maximize"
|
|
|
|
}
|
|
|
|
|
|
|
|
if action == "Minimize" {
|
2019-03-30 20:26:31 +00:00
|
|
|
window?.miniaturize(self)
|
2019-03-29 19:36:34 +00:00
|
|
|
} else if action == "Maximize" {
|
2019-03-30 20:26:31 +00:00
|
|
|
window?.zoom(self)
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 12:33:22 +00:00
|
|
|
|
2020-06-27 09:12:46 +00:00
|
|
|
common.window?.isMoving = false
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 19:38:02 +00:00
|
|
|
func set(appearance: Any) {
|
|
|
|
if appearance is Int {
|
2019-03-30 20:26:31 +00:00
|
|
|
window?.appearance = appearanceFrom(string: String(appearance as? Int ?? 0))
|
2019-03-29 19:36:34 +00:00
|
|
|
} else {
|
2019-03-30 20:26:31 +00:00
|
|
|
window?.appearance = appearanceFrom(string: appearance as? String ?? "auto")
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
2019-03-29 19:38:02 +00:00
|
|
|
}
|
2019-03-29 19:36:34 +00:00
|
|
|
|
2019-03-29 19:38:02 +00:00
|
|
|
func set(material: Any) {
|
|
|
|
if material is Int {
|
2019-03-30 20:26:31 +00:00
|
|
|
self.material = materialFrom(string: String(material as? Int ?? 0))
|
2019-03-29 19:38:02 +00:00
|
|
|
} else {
|
2019-03-30 20:26:31 +00:00
|
|
|
self.material = materialFrom(string: material as? String ?? "titlebar")
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 19:39:42 +00:00
|
|
|
func set(color: Any) {
|
|
|
|
if color is String {
|
2019-03-30 20:26:31 +00:00
|
|
|
layer?.backgroundColor = NSColor(hex: color as? String ?? "#00000000").cgColor
|
2019-03-29 19:39:42 +00:00
|
|
|
} else {
|
2019-03-30 20:26:31 +00:00
|
|
|
let col = color as? m_color ?? m_color(r: 0, g: 0, b: 0, a: 0)
|
2019-03-29 19:39:42 +00:00
|
|
|
let red = CGFloat(col.r)/255
|
|
|
|
let green = CGFloat(col.g)/255
|
|
|
|
let blue = CGFloat(col.b)/255
|
|
|
|
let alpha = CGFloat(col.a)/255
|
|
|
|
layer?.backgroundColor = NSColor(calibratedRed: red, green: green,
|
|
|
|
blue: blue, alpha: alpha).cgColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 19:36:34 +00:00
|
|
|
func show() {
|
2020-06-27 09:12:46 +00:00
|
|
|
guard let window = common.window else { return }
|
2019-03-30 20:26:31 +00:00
|
|
|
if !window.border && !window.isInFullscreen { return }
|
2020-06-27 09:12:46 +00:00
|
|
|
let loc = common.view?.convert(window.mouseLocationOutsideOfEventStream, from: nil)
|
2019-03-29 19:36:34 +00:00
|
|
|
|
|
|
|
buttons.forEach { $0.isHidden = false }
|
2024-04-27 20:11:33 +00:00
|
|
|
NSAnimationContext.runAnimationGroup({ (context) in
|
2019-03-29 19:36:34 +00:00
|
|
|
context.duration = 0.20
|
2019-03-30 20:26:31 +00:00
|
|
|
systemBar?.animator().alphaValue = 1
|
|
|
|
if !window.isInFullscreen && !window.isAnimating {
|
2019-03-29 19:36:34 +00:00
|
|
|
animator().alphaValue = 1
|
|
|
|
isHidden = false
|
|
|
|
}
|
|
|
|
}, completionHandler: nil )
|
|
|
|
|
2019-03-30 20:26:31 +00:00
|
|
|
if loc?.y ?? 0 > TitleBar.height {
|
2019-03-29 19:36:34 +00:00
|
|
|
hideDelayed()
|
|
|
|
} else {
|
|
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hide), object: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 14:18:08 +00:00
|
|
|
@objc func hide(_ duration: TimeInterval = 0.20) {
|
2020-06-27 09:12:46 +00:00
|
|
|
guard let window = common.window else { return }
|
2019-03-30 20:26:31 +00:00
|
|
|
if window.isInFullscreen && !window.isAnimating {
|
2019-03-29 19:36:34 +00:00
|
|
|
alphaValue = 0
|
|
|
|
isHidden = true
|
|
|
|
return
|
|
|
|
}
|
2024-04-27 20:11:33 +00:00
|
|
|
NSAnimationContext.runAnimationGroup({ (context) in
|
2020-12-12 14:18:08 +00:00
|
|
|
context.duration = duration
|
2019-03-30 20:26:31 +00:00
|
|
|
systemBar?.animator().alphaValue = 0
|
2019-03-29 19:36:34 +00:00
|
|
|
animator().alphaValue = 0
|
|
|
|
}, completionHandler: {
|
|
|
|
self.buttons.forEach { $0.isHidden = true }
|
|
|
|
self.isHidden = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func hideDelayed() {
|
|
|
|
NSObject.cancelPreviousPerformRequests(withTarget: self,
|
|
|
|
selector: #selector(hide),
|
|
|
|
object: nil)
|
|
|
|
perform(#selector(hide), with: nil, afterDelay: 0.5)
|
|
|
|
}
|
2019-03-29 19:38:02 +00:00
|
|
|
|
|
|
|
func appearanceFrom(string: String) -> NSAppearance? {
|
|
|
|
switch string {
|
|
|
|
case "1", "aqua":
|
2019-07-20 10:16:37 +00:00
|
|
|
return NSAppearance(named: .aqua)
|
2023-11-09 17:57:41 +00:00
|
|
|
case "2", "darkAqua":
|
|
|
|
return NSAppearance(named: .darkAqua)
|
2019-03-29 19:38:02 +00:00
|
|
|
case "3", "vibrantLight":
|
2019-07-20 10:16:37 +00:00
|
|
|
return NSAppearance(named: .vibrantLight)
|
2019-03-29 19:38:02 +00:00
|
|
|
case "4", "vibrantDark":
|
2019-07-20 10:16:37 +00:00
|
|
|
return NSAppearance(named: .vibrantDark)
|
2023-11-09 17:57:41 +00:00
|
|
|
case "5", "aquaHighContrast":
|
|
|
|
return NSAppearance(named: .accessibilityHighContrastAqua)
|
|
|
|
case "6", "darkAquaHighContrast":
|
|
|
|
return NSAppearance(named: .accessibilityHighContrastDarkAqua)
|
|
|
|
case "7", "vibrantLightHighContrast":
|
|
|
|
return NSAppearance(named: .accessibilityHighContrastVibrantLight)
|
|
|
|
case "8", "vibrantDarkHighContrast":
|
|
|
|
return NSAppearance(named: .accessibilityHighContrastVibrantDark)
|
2024-04-27 20:11:33 +00:00
|
|
|
case "0", "auto":
|
|
|
|
return nil
|
2023-11-09 17:57:41 +00:00
|
|
|
default:
|
|
|
|
return nil
|
2019-03-29 19:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func materialFrom(string: String) -> NSVisualEffectView.Material {
|
|
|
|
switch string {
|
2024-04-27 20:11:33 +00:00
|
|
|
case "0", "titlebar": return .titlebar
|
|
|
|
case "1", "selection": return .selection
|
|
|
|
case "2", "menu": return .menu
|
|
|
|
case "3", "popover": return .popover
|
|
|
|
case "4", "sidebar": return .sidebar
|
|
|
|
case "5", "headerView": return .headerView
|
|
|
|
case "6", "sheet": return .sheet
|
|
|
|
case "7", "windowBackground": return .windowBackground
|
|
|
|
case "8", "hudWindow": return .hudWindow
|
|
|
|
case "9", "fullScreen": return .fullScreenUI
|
2023-11-09 17:57:41 +00:00
|
|
|
case "10", "toolTip": return .toolTip
|
|
|
|
case "11", "contentBackground": return .contentBackground
|
|
|
|
case "12", "underWindowBackground": return .underWindowBackground
|
|
|
|
case "13", "underPageBackground": return .underPageBackground
|
|
|
|
case "14", "dark": return .dark
|
|
|
|
case "15", "light": return .light
|
|
|
|
case "16", "mediumLight": return .mediumLight
|
|
|
|
case "17", "ultraDark": return .ultraDark
|
2023-11-23 16:34:55 +00:00
|
|
|
default: return .titlebar
|
2019-03-29 19:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-29 19:36:34 +00:00
|
|
|
}
|