Skip to content

Commit 6cf7ba4

Browse files
author
Aditya Vaidyam
committed
Partial flush of Swift 4.2 + Mojave support. Some things still don't fully work (toolbar!)
1 parent eed6232 commit 6cf7ba4

36 files changed

+138
-164
lines changed

Hangouts/Conversation.swift

+10-7
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,18 @@ public class ConversationList: ParrotServiceExtension.ConversationList {
445445
fileprivate unowned let client: Client
446446
internal var convDict = [String: IConversation]() /* TODO: Should be fileprivate! */
447447
public var syncTimestamp: Date? = nil
448+
private var tokens: [Any] = []
448449

449450
public init(client: Client) {
450451
self.client = client
451452
self._sync()
452-
hangoutsCenter.addObserver(self, selector: #selector(ConversationList.clientDidUpdateState(_:)),
453-
name: Client.didUpdateStateNotification, object: client)
454-
455-
NotificationCenter.default.addObserver(self, selector: #selector(self.personDidUpdate(_:)),
456-
name: Notification.Person.DidUpdate, object: nil)
453+
let a = hangoutsCenter.addObserver(forName: Client.didUpdateStateNotification, object: client, queue: nil) { [weak self] in
454+
self?.clientDidUpdateState($0)
455+
}
456+
let b = NotificationCenter.default.addObserver(forName: Notification.Person.DidUpdate, object: nil, queue: nil) { [weak self] in
457+
self?.personDidUpdate($0)
458+
}
459+
self.tokens = [a, b]
457460
}
458461

459462
deinit {
@@ -569,15 +572,15 @@ public class ConversationList: ParrotServiceExtension.ConversationList {
569572
extension ConversationList {
570573

571574
// If a person changed, any conversations with that person also have changed.
572-
@objc func personDidUpdate(_ note: Notification) {
575+
public func personDidUpdate(_ note: Notification) {
573576
guard let user = note.object as? User else { return }
574577
for conv in (self.convDict.values.filter { $0.users.contains(user) }) {
575578
NotificationCenter.default.post(name: Notification.Conversation.DidUpdate, object: conv)
576579
}
577580
}
578581

579582
// Receive a ClientStateUpdate and fan out to Conversations
580-
@objc public func clientDidUpdateState(_ note: Notification) {
583+
public func clientDidUpdateState(_ note: Notification) {
581584
guard let update = (note.userInfo)?[Client.didUpdateStateKey] as? ClientStateUpdate else {
582585
log.error("Encountered an error! \(note)"); return
583586
}

Hangouts/User.swift

+6-7
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public class UserList: Directory {
159159
public var blocked: [String: Person] {
160160
return [:]
161161
}
162+
private var tokens: [Any] = []
162163

163164
// Initialize the list of Users.
164165
public init(client: Client, users: [User] = []) {
@@ -172,12 +173,10 @@ public class UserList: Directory {
172173
self.users[me.id] = me
173174
self.me = me
174175

175-
hangoutsCenter.addObserver(self, selector: #selector(UserList._updatedState(_:)),
176-
name: Client.didUpdateStateNotification, object: client)
177-
}
178-
179-
deinit {
180-
hangoutsCenter.removeObserver(self)
176+
let a = hangoutsCenter.addObserver(forName: Client.didUpdateStateNotification, object: client, queue: nil) { [weak self] in
177+
self?._updatedState($0)
178+
}
179+
self.tokens = [a]
181180
}
182181

183182
public subscript(_ identifier: String) -> Person {
@@ -277,7 +276,7 @@ public class UserList: Directory {
277276
}
278277

279278
/// Note: all notification objects are expected to be deltas.
280-
@objc internal func _updatedState(_ notification: Notification) {
279+
@objc dynamic internal func _updatedState(_ notification: Notification) {
281280
guard let userInfo = notification.userInfo,
282281
let update = userInfo[Client.didUpdateStateKey] as? ClientStateUpdate
283282
else { return }

HangoutsCore/ProtobufRuntime.swift

-12
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,6 @@ public func random64(_ upper_bound: UInt64) -> UInt64 {
8181
return rnd % upper_bound
8282
}
8383

84-
extension Optional: Hashable where Wrapped: Hashable {
85-
public var hashValue: Int {
86-
return self?.hashValue ?? 0
87-
}
88-
}
89-
90-
extension Array: Hashable where Element: Hashable {
91-
public var hashValue: Int {
92-
return self.reduce(5381) { ($0 << 5) &+ $0 &+ $1.hashValue }
93-
}
94-
}
95-
9684
extension Dictionary where Key: Hashable, Value: Hashable {
9785
public var hashValue: Int {
9886
return self.reduce(5381) { ($0 << 5) &+ $0 &+ $1.key.hashValue &+ $1.value.hashValue }

Mocha/Analytics.swift

+2
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ public struct AppProperties {
6868
public struct GoogleAnalytics {
6969
private init() {}
7070

71+
///
7172
public struct Screen: RawRepresentable, Equatable, Hashable {
7273
public let rawValue: String
7374
public init(rawValue: String) { self.rawValue = rawValue }
7475
public init(_ rawValue: String) { self.rawValue = rawValue }
7576
}
7677

78+
///
7779
public struct Category: RawRepresentable, Equatable, Hashable {
7880
public let rawValue: String
7981
public init(rawValue: String) { self.rawValue = rawValue }

Mocha/Subscription.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Subscription {
4040
}
4141

4242
/// The worker function bridged into Objective-C.
43-
@objc private func _runHandler(_ note: Notification) {
43+
@objc dynamic private func _runHandler(_ note: Notification) {
4444
(self.queue ?? .main).async {
4545
self.handler(note)
4646
}

Mocha/SwiftSupport.swift

+6-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public extension Collection {
2020
}
2121
}
2222

23+
extension RawRepresentable where RawValue: Hashable {
24+
public var hashValue: Int {
25+
return rawValue.hashValue
26+
}
27+
}
28+
2329
// All default Swift types:
2430
// - Boolean
2531
// - BinaryInteger
@@ -59,16 +65,3 @@ public extension Strideable where Self.Stride: SignedInteger {
5965
return max(self, range.lowerBound)
6066
}
6167
}
62-
63-
public struct RawWrapper<Type, Original: Hashable>: RawRepresentable, Hashable, Equatable {
64-
public let rawValue: Original
65-
public init(rawValue: Original) {
66-
self.rawValue = rawValue
67-
}
68-
}
69-
70-
extension RawRepresentable where RawValue: Hashable {
71-
public var hashValue: Int {
72-
return rawValue.hashValue
73-
}
74-
}

MochaUI/Animation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct Animation {
3030
fileprivate init(_ handler: @escaping () -> ()) {
3131
self.handler = handler
3232
}
33-
@objc public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
33+
@objc dynamic public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
3434
self.handler()
3535
}
3636
}

MochaUI/AppKit+Extensions.swift

+28-18
Original file line numberDiff line numberDiff line change
@@ -262,28 +262,38 @@ public extension NSSound {
262262
}
263263
}
264264

265-
/// Register for AppleEvents that follow our URL scheme as a compatibility
266-
/// layer for macOS 10.13 methods.
267-
///
268-
/// Note: this only applies to CFBundleURLTypes, and not CFBundleDocumentTypes
269-
/// on compatibility platforms. -application:openFiles: and -application:openFile:
270-
/// will still be invoked for documents.
271-
///
272-
/// A "typealias" for the traditional NSApplication delegation.
273-
open class NSApplicationController: NSObject, NSApplicationDelegate {
265+
/// A concrete controller type for the management of `NSApplication`.
266+
open class NSApplicationController: NSResponder, NSApplicationDelegate {
274267
public override init() {
275268
super.init()
276-
if floor(NSAppKitVersion.current.rawValue) <= NSAppKitVersion.macOS10_12.rawValue {
277-
let ae = NSAppleEventManager.shared()
278-
ae.setEventHandler(self, andSelector: #selector(self.handleURL(event:withReply:)),
279-
forEventClass: UInt32(kInternetEventClass),
280-
andEventID: UInt32(kAEGetURL)
281-
)
282-
}
269+
self._init()
270+
}
271+
public required init?(coder: NSCoder) {
272+
super.init(coder: coder)
273+
self._init()
283274
}
284-
@objc private func handleURL(event: NSAppleEventDescriptor, withReply reply: NSAppleEventDescriptor) {
275+
276+
/// Perform one-time URL event registration if not on macOS 13+
277+
///
278+
/// Note: this only applies to CFBundleURLTypes, and not CFBundleDocumentTypes
279+
/// on compatibility platforms. -application:openFiles: and -application:openFile:
280+
/// will still be invoked for documents.
281+
private func _init() {
282+
guard floor(NSAppKitVersion.current.rawValue) > NSAppKitVersion.macOS10_12.rawValue else { return }
283+
let ae = NSAppleEventManager.shared()
284+
ae.setEventHandler(self,
285+
andSelector: #selector(self.handleURL(event:withReply:)),
286+
forEventClass: UInt32(kInternetEventClass),
287+
andEventID: UInt32(kAEGetURL)
288+
)
289+
}
290+
291+
/// Wrap the pre-macOS 13 handler and trampoline into the post-macOS 13 one.
292+
@objc dynamic private func handleURL(event: NSAppleEventDescriptor, withReply reply: NSAppleEventDescriptor) {
285293
guard let urlString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue,
286-
let url = URL(string: urlString) else { return }
294+
let url = URL(string: urlString)
295+
else { return }
296+
287297
let sel = Selector(("application:" + "openURLs:")) // since DNE on < macOS 13
288298
if self.responds(to: sel) {
289299
self.perform(sel, with: NSApp, with: [url])

MochaUI/BlockTrampoline.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public extension NSMenuItem {
7979
self.action = action
8080
}
8181

82-
@objc fileprivate func performAction(_ sender: Any!) {
82+
@objc dynamic fileprivate func performAction(_ sender: Any!) {
8383
self.action()
8484
}
8585

MochaUI/ExtendedTextView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class ExtendedTextView: NSTextView {
1414

1515
// Apparently this property has been private on macOS since 10.7.
1616
@IBInspectable
17-
@objc public var placeholderString: String? = nil
18-
@objc public var placeholderAttributedString: NSAttributedString? = nil
17+
@objc dynamic public var placeholderString: String? = nil
18+
@objc dynamic public var placeholderAttributedString: NSAttributedString? = nil
1919

2020
public var providesContentSize: Bool = true {
2121
didSet {

MochaUI/Interpolate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public class Interpolate<T: Interpolatable>: AnyInterpolate {
193193
/**
194194
Next function used by animation(). Increments fractionComplete based on the duration.
195195
*/
196-
@objc fileprivate func next(_ fps: Double = 60.0) {
196+
@objc dynamic fileprivate func next(_ fps: Double = 60.0) {
197197

198198
let direction: CGFloat = (targetProgress > fractionComplete) ? 1.0 : -1.0
199199
let oldProgress = fractionComplete

MochaUI/KeyboardShortcutView.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
118118

119119

120120
/// Eh, why not. It's good practice.
121-
open static var supportsSecureCoding: Bool {
121+
public static var supportsSecureCoding: Bool {
122122
return true
123123
}
124124

@@ -146,13 +146,13 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
146146
/// text color of the currently-recording shortcut.
147147
///
148148
/// Note: by using a dynamic color, it will always match the current NSAppearance.
149-
@objc open var tintColor: NSColor = .keyboardFocusIndicatorColor {
149+
@objc dynamic open var tintColor: NSColor = .keyboardFocusIndicatorColor {
150150
didSet { self.needsDisplay = true }
151151
}
152152

153153
/// The string to display to the user when no shortcut is displayed by, or
154154
/// being recorded by the control.
155-
@objc open var placeholderString: String? {
155+
@objc dynamic open var placeholderString: String? {
156156
get { return self.textLabel.placeholderAttributedString?.string }
157157
set {
158158
let style = NSMutableParagraphStyle()
@@ -204,7 +204,7 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
204204
}
205205

206206
/// Returns `true` if the control is currently recording a new shortcut.
207-
@objc open private(set) var isRecording = false {
207+
@objc dynamic open private(set) var isRecording = false {
208208
willSet { self.willChangeValue(forKey: #function) }
209209
didSet { self.didChangeValue(forKey: #function) }
210210
}
@@ -245,7 +245,7 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
245245

246246
/// Determines whether the control is capable of modifying its shortcut or
247247
/// recording a new shortcut.
248-
@objc open override var isEnabled: Bool {
248+
@objc dynamic open override var isEnabled: Bool {
249249
didSet {
250250
if !self.isEnabled { self.endRecording() }
251251
self.needsDisplay = true
@@ -720,7 +720,7 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
720720

721721
/// The trailing record/stop button was pressed: start recording if we're
722722
/// not already doing so, and if we are, clear the shortcut and end recording.
723-
@objc private func buttonAction(_ button: NSButton) {
723+
@objc dynamic private func buttonAction(_ button: NSButton) {
724724
if !self.isRecording && self.shortcut == nil { // cleared state
725725
self.window?.makeFirstResponder(self)
726726
_ = self.beginRecording()
@@ -740,7 +740,7 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
740740

741741
/// A `suggestions` shortcut was selected: circumvent recording and directly set.
742742
/// Note that a suggestion may be selected without the control being recording.
743-
@objc private func selectAction(_ item: NSMenuItem) {
743+
@objc dynamic private func selectAction(_ item: NSMenuItem) {
744744
guard self.isEnabled else { return }
745745
self.endRecording()
746746
self.shortcut = self.suggestions[item.tag]
@@ -784,7 +784,7 @@ open class KeyboardShortcutView: NSControl, NSSecureCoding, NSAccessibilityButto
784784
/// responder and discard any recording shortcut.
785785
///
786786
/// Note: Unbalanced global mode changes can dramatically impact system usability!
787-
@objc private func windowKeyednessChanged(_ note: Notification) {
787+
@objc dynamic private func windowKeyednessChanged(_ note: Notification) {
788788
guard let window = self.window, (note.object as? NSWindow) == window else { return }
789789
if window.isKeyWindow && window.firstResponder == self { // becomeKey
790790
guard self.savedOperatingMode == nil else { return }

MochaUI/NSDrawer+Extensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public extension NSDrawer {
6060
fileprivate extension NSDrawer {
6161

6262
/// This swizzle hook allows __setupModernDrawer to be invoked automatically.
63-
@objc func swizzle__doSetParentWindow(_ arg1: AnyObject!) {
63+
@objc dynamic func swizzle__doSetParentWindow(_ arg1: AnyObject!) {
6464
__setupModernDrawer()
6565
swizzle__doSetParentWindow(arg1)
6666
}

MochaUI/NSToolTipManager+Appearance.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import Mocha
3333

3434
/// Swizzle: make sure we return a dynamic system color to match the visual
3535
/// effect view.
36-
@objc func swizzle_toolTipTextColor() -> NSColor! {
36+
@objc dynamic func swizzle_toolTipTextColor() -> NSColor! {
3737
return NSColor.labelColor
3838
}
3939

MochaUI/PreviewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public class PreviewController: NSObject, NSPopoverDelegate, NSGestureRecognizer
132132
self.content = content
133133
}
134134

135-
@objc private func gesture(_ sender: PressureGestureRecognizer) {
135+
@objc dynamic private func gesture(_ sender: PressureGestureRecognizer) {
136136
switch sender.state {
137137
case .possible: break // ignore
138138
case .began:

MochaUI/ViewAttachmentCell.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class ViewAttachmentCell: NSTextAttachmentCell {
5151
self.view.frame = cellFrame
5252
}
5353

54-
@objc private func verifyAttached(_ note: Notification) {
54+
@objc dynamic private func verifyAttached(_ note: Notification) {
5555
let str = (note.object as? NSTextView)?.attributedString()
5656
var attached = false
5757

0 commit comments

Comments
 (0)