Keep the inspector’s top-left origin steady on changing views.

This commit is contained in:
Brent Simmons 2018-01-21 12:46:22 -08:00
parent 394274f1e5
commit ca5703e14f
4 changed files with 67 additions and 9 deletions

View File

@ -69,6 +69,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations,
dockBadge.appDelegate = self
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(sidebarSelectionDidChange(_:)), name: .SidebarSelectionDidChange, object: nil)
appDelegate = self
}
@ -195,6 +197,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations,
let _ = faviconDownloader.favicon(for: feed)
}
@objc func sidebarSelectionDidChange(_ note: Notification) {
guard let inspectorWindowController = inspectorWindowController, inspectorWindowController.isOpen else {
return
}
inspectorWindowController.objects = objectsForInspector()
}
// MARK: Main Window
func windowControllerWithName(_ storyboardName: String) -> NSWindowController {
@ -310,13 +320,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations,
inspectorWindowController!.window!.performClose(self)
}
else {
var selectedObjects: [Any]? = nil
if let window = NSApplication.shared.mainWindow {
if let windowController = window.windowController as? MainWindowController {
selectedObjects = windowController.selectedObjectsInSidebar()
}
}
inspectorWindowController!.objects = selectedObjects
inspectorWindowController!.objects = objectsForInspector()
inspectorWindowController!.showWindow(self)
}
}
@ -432,4 +436,12 @@ private extension AppDelegate {
return windowControllerWithName("MainWindow")
}
func objectsForInspector() -> [Any]? {
guard let window = NSApplication.shared.mainWindow, let windowController = window.windowController as? MainWindowController else {
return nil
}
return windowController.selectedObjectsInSidebar()
}
}

View File

@ -8,8 +8,8 @@
<!--Window Controller-->
<scene sceneID="nxk-j5-jp4">
<objects>
<windowController showSeguePresentationStyle="single" id="cfG-Pn-VJS" customClass="InspectorWindowController" customModule="Evergreen" customModuleProvider="target" sceneMemberID="viewController">
<window key="window" title="Inspector" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" tabbingMode="disallowed" id="bma-LM-jVu" customClass="NSPanel">
<windowController storyboardIdentifier="WindowController" showSeguePresentationStyle="single" id="cfG-Pn-VJS" customClass="InspectorWindowController" customModule="Evergreen" customModuleProvider="target" sceneMemberID="viewController">
<window key="window" identifier="InspectorPanel" title="Inspector" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" tabbingMode="disallowed" id="bma-LM-jVu" customClass="NSPanel">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" utility="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="314" y="928" width="256" height="256"/>

View File

@ -64,6 +64,8 @@ final class InspectorWindowController: NSWindowController {
inspectors = [feedInspector, folderInspector, builtinSmartFeedInspector, nothingInspector]
currentInspector = nothingInspector
window?.flippedOrigin = NSPoint(x: 128, y: 128)
}
func inspector(for objects: [Any]?) -> InspectorViewController {
@ -95,6 +97,12 @@ private extension InspectorWindowController {
guard let window = window, inspector !== window.contentViewController else {
return
}
let flippedOrigin = window.flippedOrigin
window.contentViewController = inspector
window.layoutIfNeeded()
if let origin = flippedOrigin {
window.setFlippedOriginAdjustingForScreen(origin)
}
}
}

View File

@ -45,4 +45,42 @@ public extension NSWindow {
setFrame(frame, display: true)
setFrameTopLeftPoint(frame.origin)
}
public var flippedOrigin: NSPoint? {
// Screen coordinates start at lower-left.
// With this we can use upper-left, like sane people.
get {
guard let screenFrame = screen?.frame else {
return nil
}
let flippedPoint = NSPoint(x: frame.origin.x, y: screenFrame.maxY - frame.maxY)
return flippedPoint
}
set {
guard let screenFrame = screen?.frame else {
return
}
var point = newValue!
point.y = screenFrame.maxY - point.y
setFrameTopLeftPoint(point)
}
}
public func setFlippedOriginAdjustingForScreen(_ point: NSPoint) {
guard let screenFrame = screen?.frame else {
return
}
let paddingFromBottomEdge: CGFloat = 8.0
var unflippedPoint = point
unflippedPoint.y = (screenFrame.maxY - point.y) - frame.height
if unflippedPoint.y < 0 {
unflippedPoint.y = paddingFromBottomEdge
}
setFrameOrigin(unflippedPoint)
}
}