Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Auto-adjust docked position to match available screen space (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatsJustCheesy authored and sindresorhus committed Jul 4, 2019
1 parent 71785b3 commit c184ba5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 0 additions & 1 deletion Touch Bar Simulator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = E3FE2CB61E726CE800C6713A;
Expand Down
19 changes: 17 additions & 2 deletions Touch Bar Simulator/TouchBarWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ final class TouchBarWindow: NSPanel {
switch self {
case .floating:
window.addTitlebar()
case .dockedToTop:
window.removeTitlebar()
case .dockedToBottom:
window.removeTitlebar()
}
reposition(window: window)
}
func reposition(window: NSWindow) {
switch self {
case .floating:
if let prevPosition = defaults[.lastFloatingPosition] {
window.setFrameOrigin(prevPosition)
}
case .dockedToTop:
window.removeTitlebar()
window.moveTo(x: .center, y: .top)
case .dockedToBottom:
window.removeTitlebar()
window.moveTo(x: .center, y: .bottom)
}
}
Expand Down Expand Up @@ -50,6 +58,11 @@ final class TouchBarWindow: NSPanel {
}
}

@objc
func didChangeScreenParameters(_ notification: Notification) {
docking.reposition(window: self)
}

var showOnAllDesktops: Bool = false {
didSet {
if showOnAllDesktops {
Expand Down Expand Up @@ -350,6 +363,8 @@ final class TouchBarWindow: NSPanel {
if !dockBehavior {
orderFront(nil)
}

NotificationCenter.default.addObserver(self, selector: #selector(didChangeScreenParameters(_:)), name: NSApplication.didChangeScreenParametersNotification, object: nil)
}

deinit {
Expand Down
9 changes: 7 additions & 2 deletions Touch Bar Simulator/util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ extension NSWindow {
}

func moveTo(x xPositioning: MoveXPositioning, y yPositioning: MoveYPositioning) {
guard let visibleFrame = NSScreen.main?.visibleFrame else {
guard let screen = NSScreen.main else {
return
}
let visibleFrame = screen.visibleFrame

let x: CGFloat, y: CGFloat
switch xPositioning {
Expand All @@ -72,7 +73,11 @@ extension NSWindow {
}
switch yPositioning {
case .top:
y = visibleFrame.maxY - frame.height
// Defect fix: keep docked windows below menubar area
// Previously, the window would obstruct menubar clicks when the menubar was set to auto-hide.
// Now, the window stays below that area.
let menubarThickness = NSStatusBar.system.thickness
y = min(visibleFrame.maxY - frame.height, screen.frame.maxY - menubarThickness - frame.height)
case .center:
y = visibleFrame.midY - frame.height / 2
case .bottom:
Expand Down

0 comments on commit c184ba5

Please sign in to comment.