Skip to content

Commit

Permalink
Fixed window button crash
Browse files Browse the repository at this point in the history
  • Loading branch information
TimOliver committed Nov 24, 2020
1 parent 9f51b3c commit f6639a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
19 changes: 0 additions & 19 deletions YT Music/Controllers/ViewController.swift
Expand Up @@ -63,25 +63,6 @@ class ViewController: NSViewController {

var y = webView.isFlipped ? 22 : webView.frame.height - 39

if let btn = view.window?.standardWindowButton(.closeButton) {
btn.removeFromSuperview()
btn.setFrameOrigin(NSPoint(x: 17, y: y))
view.addSubview(btn)
}

if let btn = view.window?.standardWindowButton(.miniaturizeButton) {
btn.removeFromSuperview()
btn.setFrameOrigin(NSPoint(x: 37, y: y))
view.addSubview(btn)
}

if let btn = view.window?.standardWindowButton(.zoomButton) {
btn.removeFromSuperview()
btn.setFrameOrigin(NSPoint(x: 57, y: y))
view.addSubview(btn)
}


movableView.frame = CGRect(x: 0, y: webView.isFlipped ? 0 : webView.frame.height - 20, width: webView.frame.width, height: 20)

y = webView.isFlipped ? 14 : webView.frame.height - 46
Expand Down
49 changes: 45 additions & 4 deletions YT Music/Views/CustomWindow.swift
Expand Up @@ -9,7 +9,9 @@
import Cocoa

class CustomWindow: NSWindow {


var buttons: [NSView] = []

override func awakeFromNib() {
super.awakeFromNib()

Expand All @@ -19,12 +21,51 @@ class CustomWindow: NSWindow {
titleVisibility = .hidden
titlebarAppearsTransparent = true
// styleMask.insert(.fullSizeContentView)

identifier = NSUserInterfaceItemIdentifier(rawValue: "main")
backgroundColor = NSColor(hue:0.00, saturation:0.00, brightness:0.07, alpha:1.00)
contentMinSize = NSSize(width: 800, height: 500)
setFrameAutosaveName(NSWindow.FrameAutosaveName(rawValue: "uk.co.wearecocoon.ytmusic.main"))


isMovableByWindowBackground = true

configureButtons()
}

// https://zhenchao.li/2018-07-04-positioning-traffic-lights-of-your-cocoa-app/
private func configureButtons() {
if styleMask.contains(.fullScreen) { return }

let close = standardWindowButton(NSWindow.ButtonType.closeButton)
let minimize = standardWindowButton(NSWindow.ButtonType.miniaturizeButton)
let maximize = standardWindowButton(NSWindow.ButtonType.zoomButton)

buttons.append(close!)
buttons.append(minimize!)
buttons.append(maximize!)

buttons.forEach { (btn) in
btn.superview?.willRemoveSubview(btn)
btn.removeFromSuperview()

btn.viewWillMove(toSuperview: contentView)
contentView!.addSubview(btn)
btn.viewDidMoveToSuperview()
}

var offsetX: CGFloat = 20
buttons.forEach { (btn) in
btn.translatesAutoresizingMaskIntoConstraints = false
btn.removeConstraints(btn.constraints)

btn.topAnchor.constraint(equalTo: contentView!.topAnchor, constant: 23).isActive = true
btn.leadingAnchor.constraint(equalTo: contentView!.leadingAnchor, constant: offsetX).isActive = true
btn.widthAnchor.constraint(equalToConstant: 14).isActive = true
btn.heightAnchor.constraint(equalToConstant: 14).isActive = true
offsetX += 22
}
contentView!.layoutSubtreeIfNeeded()

contentView?.superview!.viewDidEndLiveResize()
}

}

0 comments on commit f6639a6

Please sign in to comment.