Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix preferences window not appearing when the content views are not using auto-layout #28

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
468785d
Implements more robust view size calculation and a view size cache
SamusAranX Apr 16, 2019
7162d1d
change toViewController source
SamusAranX Apr 16, 2019
a8467e6
add logging
SamusAranX Apr 16, 2019
4f266b7
set window frame when window is opened up again
SamusAranX Apr 16, 2019
90219aa
add more logging
SamusAranX Apr 16, 2019
ce6cca1
add more logging
SamusAranX Apr 17, 2019
3260cf5
Merge branch 'master' into resize-fix-wip
SamusAranX May 8, 2019
a665db8
Made PreferencePaneIdentifier conform to the Hashable protocol to fix…
SamusAranX May 8, 2019
ba81225
add more logging
SamusAranX May 8, 2019
4d9607f
wip
SamusAranX May 8, 2019
75cda18
Implemented suggested changes
SamusAranX May 8, 2019
dca09a0
add more logging
SamusAranX May 8, 2019
d107d1b
print driven development ftw
SamusAranX May 8, 2019
8438c56
give preferredContentSize a try
SamusAranX May 8, 2019
17b9f38
Make the VC's view perform layout updates before fetching its size
SamusAranX May 8, 2019
1ecd91e
wip
SamusAranX May 8, 2019
1d6ad2c
revised contentSize zero check
SamusAranX May 8, 2019
00cf359
Made view.bounds.size the default choice
SamusAranX May 8, 2019
a554795
Removed all print()s
SamusAranX May 8, 2019
ba9c27f
Forgot to undo one edit before committing
SamusAranX May 8, 2019
90e400f
Removed isAnimated property and animation options
SamusAranX Sep 23, 2019
ee04b81
Merge branch 'master' into resize-fix
SamusAranX Sep 23, 2019
875306b
Update PreferencesTabViewController.swift
sindresorhus Sep 23, 2019
288e26e
Update PreferencesTabViewController.swift
sindresorhus Sep 23, 2019
a3ca0d1
Remove usages of PreferencePane.viewController
SamusAranX Sep 23, 2019
da9ebc1
Hopefully finally fixed view controller animation
SamusAranX Sep 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Preferences/PreferencePane.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Cocoa

public struct PreferencePaneIdentifier: Hashable, RawRepresentable, Codable {
public struct PreferencePaneIdentifier: Hashable, RawRepresentable {
SamusAranX marked this conversation as resolved.
Show resolved Hide resolved
public typealias Identifier = PreferencePaneIdentifier

public let rawValue: String
Expand Down
78 changes: 29 additions & 49 deletions Sources/Preferences/PreferencesTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont
preferencesStyleController?.toolbarItemIdentifiers() ?? []
}

var window: NSWindow! { view.window }
private var viewSizeCache = [PreferencePaneIdentifier: CGSize]()

var isAnimated: Bool = true
var window: NSWindow! {
return view.window
}

override func loadView() {
self.view = NSView()
Expand Down Expand Up @@ -63,6 +65,7 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont
activeTab = index
preferencesStyleController.selectTab(index: index)
updateWindowTitle(tabIndex: index)
setWindowFrame(for: preferencePanes[index].viewController, animated: animated)
}

if activeTab == nil {
Expand Down Expand Up @@ -111,24 +114,19 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont
return
}

let fromViewController = preferencePanes[activeTab]
let toViewController = preferencePanes[index]

// View controller animations only work on macOS 10.14 and newer.
let options: NSViewController.TransitionOptions
if #available(macOS 10.14, *) {
options = animated && isAnimated ? [.crossfade] : []
} else {
options = []
}
let fromViewController = children[activeTab]
let toViewController = children[index]

view.removeConstraints(activeChildViewConstraints)

fromViewController.view.layer?.opacity = 0
toViewController.view.layer?.opacity = 0
transition(
from: fromViewController,
to: toViewController,
options: options
options: []
) {
toViewController.view.layer?.opacity = 1
self.activeChildViewConstraints = toViewController.view.constrainToSuperviewBounds()
}
}
Expand All @@ -139,48 +137,30 @@ final class PreferencesTabViewController: NSViewController, PreferencesStyleCont
options: NSViewController.TransitionOptions = [],
completionHandler completion: (() -> Void)? = nil
) {
let isAnimated = options
.intersection([
.crossfade,
.slideUp,
.slideDown,
.slideForward,
.slideBackward,
.slideLeft,
.slideRight
])
.isEmpty == false

if isAnimated {
NSAnimationContext.runAnimationGroup({ context in
context.allowsImplicitAnimation = true
context.duration = 0.25
context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
setWindowFrame(for: toViewController, animated: true)

super.transition(
from: fromViewController,
to: toViewController,
options: options,
completionHandler: completion
)
}, completionHandler: nil)
} else {
super.transition(
from: fromViewController,
to: toViewController,
options: options,
completionHandler: completion
)
}
super.transition(
from: fromViewController,
to: toViewController,
options: [],
completionHandler: completion
)
}

private func setWindowFrame(for viewController: NSViewController, animated: Bool = false) {
guard let window = window else {
guard
let window = window,
let preferencePane = preferencePanes.first(where: { $0.viewController == viewController })
else {
preconditionFailure()
}

let contentSize = viewController.view.fittingSize
var contentSize = CGSize.zero
if let cachedSize = self.viewSizeCache[preferencePane.preferencePaneIdentifier] {
contentSize = cachedSize
} else {
contentSize = viewController.view.bounds.size

self.viewSizeCache[preferencePane.preferencePaneIdentifier] = contentSize
}

let newWindowSize = window.frameRect(forContentRect: CGRect(origin: .zero, size: contentSize)).size
var frame = window.frame
Expand Down
8 changes: 0 additions & 8 deletions Sources/Preferences/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ extension NSWindow.FrameAutosaveName {
public final class PreferencesWindowController: NSWindowController {
private let tabViewController = PreferencesTabViewController()

public var isAnimated: Bool {
get { tabViewController.isAnimated }
set {
tabViewController.isAnimated = newValue
}
}

public var hidesToolbarForSingleItem: Bool {
didSet {
updateToolbarVisibility()
Expand Down Expand Up @@ -54,7 +47,6 @@ public final class PreferencesWindowController: NSWindowController {
return (preferencePanes.count <= 1) ? .visible : .hidden
}
}()
tabViewController.isAnimated = animated
tabViewController.configure(preferencePanes: preferencePanes, style: style)
updateToolbarVisibility()
}
Expand Down