From 39334f5a4836cb588517e96120834fa84b8ed0da Mon Sep 17 00:00:00 2001 From: Aditya Vaidyam Date: Thu, 15 Feb 2018 14:46:55 -0500 Subject: [PATCH] Add CGSSpace to pin SystemBezel to the screen. --- Hangouts/Event.swift | 14 ++++----- MochaUI/NSWindow+Transforms.swift | 50 +++++++++++++++++++++++++++++++ MochaUI/SystemBezel.swift | 4 +++ Parrot/Event.swift | 2 +- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Hangouts/Event.swift b/Hangouts/Event.swift index 3f157b5..94271cf 100755 --- a/Hangouts/Event.swift +++ b/Hangouts/Event.swift @@ -6,7 +6,6 @@ private let log = Logger(subsystem: "Hangouts.Event") /* TODO: Refactor ChatMessageSegment to match hangups and Protobuf docs. */ /* TODO: Include Markdown, HTML, and URL formatting parsers. */ - /* ConversationNotification EventNotification @@ -24,9 +23,6 @@ private let log = Logger(subsystem: "Hangouts.Event") RichPresenceEnabledStateNotification */ - - - // An event which becomes part of the permanent record of a conversation. // Acts as a base class for the events defined below. public class IEvent: ParrotServiceExtension.Event, Hashable, Equatable { @@ -59,17 +55,17 @@ public class IEvent: ParrotServiceExtension.Event, Hashable, Equatable { } // A timestamp of when the event occurred. - public lazy var timestamp: Date = { + public var timestamp: Date { return Date(UTC: self.event.timestamp ?? 0) - }() + } // A User.ID indicating who created the event. - public lazy var userID: User.ID = { + public var userID: User.ID { return User.ID( chatID: self.event.sender_id!.chat_id!, gaiaID: self.event.sender_id!.gaia_id! ) - }() + } // The ID of the conversation the event belongs to. public lazy var conversation_id: String = { @@ -227,7 +223,7 @@ public class IMembershipChangeEvent : IEvent, ParrotServiceExtension.MembershipC } // An event in a Hangouts voice/video call. -public class IHangoutEvent: IEvent, VideoCall { +public class IHangoutEvent: IEvent, ParrotServiceExtension.VideoCall { /* public var transferred_conversation_id: ConversationId? = nil public var refresh_timeout_secs: UInt64? = nil diff --git a/MochaUI/NSWindow+Transforms.swift b/MochaUI/NSWindow+Transforms.swift index e004650..8c23b2a 100644 --- a/MochaUI/NSWindow+Transforms.swift +++ b/MochaUI/NSWindow+Transforms.swift @@ -62,3 +62,53 @@ private func CAMesh(_ m: CGSMeshPoint, _ f: CGRect, _ o: CGPoint, _ t: CATransfo let n = CAPointApplyCATransform3D(t, f, CGPoint(x: Double(m.x), y: Double(m.y))) return CGSMeshPoint(x: Float(n.x + o.x), y: Float(n.y + o.y)) } + +/// Small Spaces API wrapper. +public final class CGSSpace { + private let identifier: CGSSpaceID + + public var windows: Set = [] { + didSet { + let remove = oldValue.subtracting(self.windows) + let add = self.windows.subtracting(oldValue) + + CGSRemoveWindowsFromSpaces(_CGSDefaultConnection(), + remove.map { $0.windowNumber } as NSArray, + [self.identifier]) + CGSAddWindowsToSpaces(_CGSDefaultConnection(), + add.map { $0.windowNumber } as NSArray, + [self.identifier]) + } + } + + public init(level: Int = 0) { + self.identifier = CGSSpaceCreate(_CGSDefaultConnection(), 0x0 /*0x1=facetime?*/, nil) + CGSSpaceSetAbsoluteLevel(_CGSDefaultConnection(), self.identifier, level/*400=facetime?*/) + CGSShowSpaces(_CGSDefaultConnection(), [self.identifier]) + } + + deinit { + CGSHideSpaces(_CGSDefaultConnection(), [self.identifier]) + CGSSpaceDestroy(_CGSDefaultConnection(), self.identifier) + } +} + +// CGSSpace stuff: +fileprivate typealias CGSConnectionID = UInt +fileprivate typealias CGSSpaceID = UInt64 +@_silgen_name("_CGSDefaultConnection") +fileprivate func _CGSDefaultConnection() -> CGSConnectionID +@_silgen_name("CGSSpaceCreate") +fileprivate func CGSSpaceCreate(_ cid: CGSConnectionID, _ unknown: Int, _ options: NSDictionary?) -> CGSSpaceID +@_silgen_name("CGSSpaceDestroy") +fileprivate func CGSSpaceDestroy(_ cid: CGSConnectionID, _ space: CGSSpaceID) +@_silgen_name("CGSSpaceSetAbsoluteLevel") +fileprivate func CGSSpaceSetAbsoluteLevel(_ cid: CGSConnectionID, _ space: CGSSpaceID, _ level: Int) +@_silgen_name("CGSAddWindowsToSpaces") +fileprivate func CGSAddWindowsToSpaces(_ cid: CGSConnectionID, _ windows: NSArray, _ spaces: NSArray) +@_silgen_name("CGSRemoveWindowsFromSpaces") +fileprivate func CGSRemoveWindowsFromSpaces(_ cid: CGSConnectionID, _ windows: NSArray, _ spaces: NSArray) +@_silgen_name("CGSHideSpaces") +fileprivate func CGSHideSpaces(_ cid: CGSConnectionID, _ spaces: NSArray) +@_silgen_name("CGSShowSpaces") +fileprivate func CGSShowSpaces(_ cid: CGSConnectionID, _ spaces: NSArray) diff --git a/MochaUI/SystemBezel.swift b/MochaUI/SystemBezel.swift index b841a73..13211fe 100755 --- a/MochaUI/SystemBezel.swift +++ b/MochaUI/SystemBezel.swift @@ -30,6 +30,8 @@ public class SystemBezel: Hashable, Equatable { return NSAppearance.systemAppearance.name == .vibrantDark ? .dark : .light } + public static var _space = CGSSpace(level: 1) + // // // @@ -202,6 +204,7 @@ public class SystemBezel: Hashable, Equatable { self.window.alphaValue = 0.0 self.window.orderFront(nil) + SystemBezel._space.windows.insert(self.window) NSAnimationContext.runAnimationGroup({ $0.duration = 0.33 self.window.animator().alphaValue = 1.0 @@ -216,6 +219,7 @@ public class SystemBezel: Hashable, Equatable { self.window.animator().alphaValue = 0.0 }, completionHandler: { self.window.close() + SystemBezel._space.windows.remove(self.window) handler() }) } diff --git a/Parrot/Event.swift b/Parrot/Event.swift index bb487b3..b6f651d 100644 --- a/Parrot/Event.swift +++ b/Parrot/Event.swift @@ -85,7 +85,7 @@ public struct BezelAction: EventAction { public static func perform(with event: EventDescriptor) { SystemBezel(image: event.image, text: event.contents) - .show().hide(after: 2.seconds) + .show().hide(after: 5.seconds) } }