├── README.md ├── SpaceSwitcher.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── SpaceSwitcher ├── Appkit-categories.swift ├── Info.plist ├── SpaceChangeObserver.swift ├── SpaceSwitcher.h └── SpaceSwitcher.swift └── SpaceSwitcherDemo ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj └── Main.storyboard ├── Info.plist ├── SpaceSwitcherDemo.entitlements └── ViewController.swift /README.md: -------------------------------------------------------------------------------- 1 | # SpaceSwitcher 2 | 3 | A macOS API that allows the unique identification and programmatical switching of spaces. 4 | 5 | 6 | ## 'no cheating' layer 7 | 8 | an API for limited spaces functionality, without dependency on private API's: 9 | - detect spaces as the user changes the current space; 10 | - programmatically change the current space. 11 | 12 | 13 | ## 'full cheats' layer (coming soon) 14 | 15 | an API that wraps the CoreGraphics private API, so you can e.g. 16 | - send windows to a specific space; 17 | - detect the full set of spaces without having to 'discover' them by making them current; 18 | - create new spaces; 19 | - if someone can really really convince me this is not insane, even *delete* spaces. 20 | 21 | Note the usage of private APIs will render apps that utilise the 'full cheats' layer ineligible for submission to 22 | the Mac App Store. 23 | 24 | 25 | ## Demo 26 | 27 | - Build and run the SpaceSwitcherDemo app to test out Spaces functionality. 28 | - Walk through the app code to understand how to use the framework. 29 | 30 | 31 | ## Notes 32 | 33 | The API designers of macOS have steadfastly denied application developers of an officially designed and documented API 34 | for Spaces / Mission Control. 35 | 36 | Given no help from the system's public frameworks, SpaceSwitcher makes use of how the system switches 37 | to the Space of a window which has been brought to focus. 38 | Every time the user makes a new Space current, SpaceSwitcher will create a transparent window in that space. 39 | This window is then used as a 'handle' to the space it belongs to. 40 | 41 | A bunch of 'tying up loose ends' goes around this core approach to make it behave seamlessly, 42 | making it sensible to reuse the effort. 43 | -------------------------------------------------------------------------------- /SpaceSwitcher.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 5110DB7620DBB5A400D3A409 /* Appkit-categories.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5110DB7520DBB5A400D3A409 /* Appkit-categories.swift */; }; 11 | 512B685B20DB0970006D54D1 /* SpaceChangeObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51AAC72A20DAE7760000CBE9 /* SpaceChangeObserver.swift */; }; 12 | 51AAC70620DA79400000CBE9 /* SpaceSwitcher.h in Headers */ = {isa = PBXBuildFile; fileRef = 51AAC70420DA79400000CBE9 /* SpaceSwitcher.h */; settings = {ATTRIBUTES = (Public, ); }; }; 13 | 51AAC71620DA79B80000CBE9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51AAC71520DA79B80000CBE9 /* AppDelegate.swift */; }; 14 | 51AAC71820DA79B80000CBE9 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51AAC71720DA79B80000CBE9 /* ViewController.swift */; }; 15 | 51AAC71A20DA79B90000CBE9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 51AAC71920DA79B90000CBE9 /* Assets.xcassets */; }; 16 | 51AAC71D20DA79B90000CBE9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 51AAC71B20DA79B90000CBE9 /* Main.storyboard */; }; 17 | 51AAC72320DA79CD0000CBE9 /* SpaceSwitcher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 51AAC70220DA79400000CBE9 /* SpaceSwitcher.framework */; }; 18 | 51AAC72420DA79CD0000CBE9 /* SpaceSwitcher.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 51AAC70220DA79400000CBE9 /* SpaceSwitcher.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 19 | 51AAC72920DA7A3A0000CBE9 /* SpaceSwitcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51AAC72820DA7A3A0000CBE9 /* SpaceSwitcher.swift */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXContainerItemProxy section */ 23 | 51AAC72520DA79CD0000CBE9 /* PBXContainerItemProxy */ = { 24 | isa = PBXContainerItemProxy; 25 | containerPortal = 51AAC6C320DA78D50000CBE9 /* Project object */; 26 | proxyType = 1; 27 | remoteGlobalIDString = 51AAC70120DA79400000CBE9; 28 | remoteInfo = SpaceSwitcher; 29 | }; 30 | /* End PBXContainerItemProxy section */ 31 | 32 | /* Begin PBXCopyFilesBuildPhase section */ 33 | 51AAC72720DA79CE0000CBE9 /* Embed Frameworks */ = { 34 | isa = PBXCopyFilesBuildPhase; 35 | buildActionMask = 2147483647; 36 | dstPath = ""; 37 | dstSubfolderSpec = 10; 38 | files = ( 39 | 51AAC72420DA79CD0000CBE9 /* SpaceSwitcher.framework in Embed Frameworks */, 40 | ); 41 | name = "Embed Frameworks"; 42 | runOnlyForDeploymentPostprocessing = 0; 43 | }; 44 | /* End PBXCopyFilesBuildPhase section */ 45 | 46 | /* Begin PBXFileReference section */ 47 | 5110DB7520DBB5A400D3A409 /* Appkit-categories.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Appkit-categories.swift"; sourceTree = ""; }; 48 | 512B685C20DB11E1006D54D1 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 49 | 51AAC70220DA79400000CBE9 /* SpaceSwitcher.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SpaceSwitcher.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 50 | 51AAC70420DA79400000CBE9 /* SpaceSwitcher.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SpaceSwitcher.h; sourceTree = ""; }; 51 | 51AAC70520DA79400000CBE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 52 | 51AAC71320DA79B80000CBE9 /* SpaceSwitcherDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SpaceSwitcherDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 53 | 51AAC71520DA79B80000CBE9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 54 | 51AAC71720DA79B80000CBE9 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 55 | 51AAC71920DA79B90000CBE9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 56 | 51AAC71C20DA79B90000CBE9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 57 | 51AAC71E20DA79B90000CBE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 58 | 51AAC71F20DA79B90000CBE9 /* SpaceSwitcherDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SpaceSwitcherDemo.entitlements; sourceTree = ""; }; 59 | 51AAC72820DA7A3A0000CBE9 /* SpaceSwitcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpaceSwitcher.swift; sourceTree = ""; }; 60 | 51AAC72A20DAE7760000CBE9 /* SpaceChangeObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpaceChangeObserver.swift; sourceTree = ""; }; 61 | /* End PBXFileReference section */ 62 | 63 | /* Begin PBXFrameworksBuildPhase section */ 64 | 51AAC6FE20DA79400000CBE9 /* Frameworks */ = { 65 | isa = PBXFrameworksBuildPhase; 66 | buildActionMask = 2147483647; 67 | files = ( 68 | ); 69 | runOnlyForDeploymentPostprocessing = 0; 70 | }; 71 | 51AAC71020DA79B80000CBE9 /* Frameworks */ = { 72 | isa = PBXFrameworksBuildPhase; 73 | buildActionMask = 2147483647; 74 | files = ( 75 | 51AAC72320DA79CD0000CBE9 /* SpaceSwitcher.framework in Frameworks */, 76 | ); 77 | runOnlyForDeploymentPostprocessing = 0; 78 | }; 79 | /* End PBXFrameworksBuildPhase section */ 80 | 81 | /* Begin PBXGroup section */ 82 | 51AAC6C220DA78D50000CBE9 = { 83 | isa = PBXGroup; 84 | children = ( 85 | 512B685C20DB11E1006D54D1 /* README.md */, 86 | 51AAC70320DA79400000CBE9 /* SpaceSwitcher */, 87 | 51AAC71420DA79B80000CBE9 /* SpaceSwitcherDemo */, 88 | 51AAC6CD20DA78D50000CBE9 /* Products */, 89 | ); 90 | sourceTree = ""; 91 | }; 92 | 51AAC6CD20DA78D50000CBE9 /* Products */ = { 93 | isa = PBXGroup; 94 | children = ( 95 | 51AAC70220DA79400000CBE9 /* SpaceSwitcher.framework */, 96 | 51AAC71320DA79B80000CBE9 /* SpaceSwitcherDemo.app */, 97 | ); 98 | name = Products; 99 | sourceTree = ""; 100 | }; 101 | 51AAC70320DA79400000CBE9 /* SpaceSwitcher */ = { 102 | isa = PBXGroup; 103 | children = ( 104 | 51AAC72820DA7A3A0000CBE9 /* SpaceSwitcher.swift */, 105 | 51AAC72A20DAE7760000CBE9 /* SpaceChangeObserver.swift */, 106 | 51AAC70420DA79400000CBE9 /* SpaceSwitcher.h */, 107 | 51AAC70520DA79400000CBE9 /* Info.plist */, 108 | 5110DB7520DBB5A400D3A409 /* Appkit-categories.swift */, 109 | ); 110 | path = SpaceSwitcher; 111 | sourceTree = ""; 112 | }; 113 | 51AAC71420DA79B80000CBE9 /* SpaceSwitcherDemo */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | 51AAC71520DA79B80000CBE9 /* AppDelegate.swift */, 117 | 51AAC71720DA79B80000CBE9 /* ViewController.swift */, 118 | 51AAC71920DA79B90000CBE9 /* Assets.xcassets */, 119 | 51AAC71B20DA79B90000CBE9 /* Main.storyboard */, 120 | 51AAC71E20DA79B90000CBE9 /* Info.plist */, 121 | 51AAC71F20DA79B90000CBE9 /* SpaceSwitcherDemo.entitlements */, 122 | ); 123 | path = SpaceSwitcherDemo; 124 | sourceTree = ""; 125 | }; 126 | /* End PBXGroup section */ 127 | 128 | /* Begin PBXHeadersBuildPhase section */ 129 | 51AAC6FF20DA79400000CBE9 /* Headers */ = { 130 | isa = PBXHeadersBuildPhase; 131 | buildActionMask = 2147483647; 132 | files = ( 133 | 51AAC70620DA79400000CBE9 /* SpaceSwitcher.h in Headers */, 134 | ); 135 | runOnlyForDeploymentPostprocessing = 0; 136 | }; 137 | /* End PBXHeadersBuildPhase section */ 138 | 139 | /* Begin PBXNativeTarget section */ 140 | 51AAC70120DA79400000CBE9 /* SpaceSwitcher */ = { 141 | isa = PBXNativeTarget; 142 | buildConfigurationList = 51AAC70B20DA79400000CBE9 /* Build configuration list for PBXNativeTarget "SpaceSwitcher" */; 143 | buildPhases = ( 144 | 51AAC6FD20DA79400000CBE9 /* Sources */, 145 | 51AAC6FE20DA79400000CBE9 /* Frameworks */, 146 | 51AAC6FF20DA79400000CBE9 /* Headers */, 147 | 51AAC70020DA79400000CBE9 /* Resources */, 148 | ); 149 | buildRules = ( 150 | ); 151 | dependencies = ( 152 | ); 153 | name = SpaceSwitcher; 154 | productName = SpaceSwitcher; 155 | productReference = 51AAC70220DA79400000CBE9 /* SpaceSwitcher.framework */; 156 | productType = "com.apple.product-type.framework"; 157 | }; 158 | 51AAC71220DA79B80000CBE9 /* SpaceSwitcherDemo */ = { 159 | isa = PBXNativeTarget; 160 | buildConfigurationList = 51AAC72020DA79B90000CBE9 /* Build configuration list for PBXNativeTarget "SpaceSwitcherDemo" */; 161 | buildPhases = ( 162 | 51AAC70F20DA79B80000CBE9 /* Sources */, 163 | 51AAC71020DA79B80000CBE9 /* Frameworks */, 164 | 51AAC71120DA79B80000CBE9 /* Resources */, 165 | 51AAC72720DA79CE0000CBE9 /* Embed Frameworks */, 166 | ); 167 | buildRules = ( 168 | ); 169 | dependencies = ( 170 | 51AAC72620DA79CD0000CBE9 /* PBXTargetDependency */, 171 | ); 172 | name = SpaceSwitcherDemo; 173 | productName = SpaceSwitcherDemo; 174 | productReference = 51AAC71320DA79B80000CBE9 /* SpaceSwitcherDemo.app */; 175 | productType = "com.apple.product-type.application"; 176 | }; 177 | /* End PBXNativeTarget section */ 178 | 179 | /* Begin PBXProject section */ 180 | 51AAC6C320DA78D50000CBE9 /* Project object */ = { 181 | isa = PBXProject; 182 | attributes = { 183 | LastSwiftUpdateCheck = 0940; 184 | LastUpgradeCheck = 0940; 185 | ORGANIZATIONNAME = "Big Bear Labs"; 186 | TargetAttributes = { 187 | 51AAC70120DA79400000CBE9 = { 188 | CreatedOnToolsVersion = 9.4; 189 | LastSwiftMigration = 1010; 190 | }; 191 | 51AAC71220DA79B80000CBE9 = { 192 | CreatedOnToolsVersion = 9.4; 193 | LastSwiftMigration = 1010; 194 | }; 195 | }; 196 | }; 197 | buildConfigurationList = 51AAC6C620DA78D50000CBE9 /* Build configuration list for PBXProject "SpaceSwitcher" */; 198 | compatibilityVersion = "Xcode 9.3"; 199 | developmentRegion = en; 200 | hasScannedForEncodings = 0; 201 | knownRegions = ( 202 | en, 203 | Base, 204 | ); 205 | mainGroup = 51AAC6C220DA78D50000CBE9; 206 | productRefGroup = 51AAC6CD20DA78D50000CBE9 /* Products */; 207 | projectDirPath = ""; 208 | projectRoot = ""; 209 | targets = ( 210 | 51AAC70120DA79400000CBE9 /* SpaceSwitcher */, 211 | 51AAC71220DA79B80000CBE9 /* SpaceSwitcherDemo */, 212 | ); 213 | }; 214 | /* End PBXProject section */ 215 | 216 | /* Begin PBXResourcesBuildPhase section */ 217 | 51AAC70020DA79400000CBE9 /* Resources */ = { 218 | isa = PBXResourcesBuildPhase; 219 | buildActionMask = 2147483647; 220 | files = ( 221 | ); 222 | runOnlyForDeploymentPostprocessing = 0; 223 | }; 224 | 51AAC71120DA79B80000CBE9 /* Resources */ = { 225 | isa = PBXResourcesBuildPhase; 226 | buildActionMask = 2147483647; 227 | files = ( 228 | 51AAC71A20DA79B90000CBE9 /* Assets.xcassets in Resources */, 229 | 51AAC71D20DA79B90000CBE9 /* Main.storyboard in Resources */, 230 | ); 231 | runOnlyForDeploymentPostprocessing = 0; 232 | }; 233 | /* End PBXResourcesBuildPhase section */ 234 | 235 | /* Begin PBXSourcesBuildPhase section */ 236 | 51AAC6FD20DA79400000CBE9 /* Sources */ = { 237 | isa = PBXSourcesBuildPhase; 238 | buildActionMask = 2147483647; 239 | files = ( 240 | 512B685B20DB0970006D54D1 /* SpaceChangeObserver.swift in Sources */, 241 | 5110DB7620DBB5A400D3A409 /* Appkit-categories.swift in Sources */, 242 | 51AAC72920DA7A3A0000CBE9 /* SpaceSwitcher.swift in Sources */, 243 | ); 244 | runOnlyForDeploymentPostprocessing = 0; 245 | }; 246 | 51AAC70F20DA79B80000CBE9 /* Sources */ = { 247 | isa = PBXSourcesBuildPhase; 248 | buildActionMask = 2147483647; 249 | files = ( 250 | 51AAC71820DA79B80000CBE9 /* ViewController.swift in Sources */, 251 | 51AAC71620DA79B80000CBE9 /* AppDelegate.swift in Sources */, 252 | ); 253 | runOnlyForDeploymentPostprocessing = 0; 254 | }; 255 | /* End PBXSourcesBuildPhase section */ 256 | 257 | /* Begin PBXTargetDependency section */ 258 | 51AAC72620DA79CD0000CBE9 /* PBXTargetDependency */ = { 259 | isa = PBXTargetDependency; 260 | target = 51AAC70120DA79400000CBE9 /* SpaceSwitcher */; 261 | targetProxy = 51AAC72520DA79CD0000CBE9 /* PBXContainerItemProxy */; 262 | }; 263 | /* End PBXTargetDependency section */ 264 | 265 | /* Begin PBXVariantGroup section */ 266 | 51AAC71B20DA79B90000CBE9 /* Main.storyboard */ = { 267 | isa = PBXVariantGroup; 268 | children = ( 269 | 51AAC71C20DA79B90000CBE9 /* Base */, 270 | ); 271 | name = Main.storyboard; 272 | sourceTree = ""; 273 | }; 274 | /* End PBXVariantGroup section */ 275 | 276 | /* Begin XCBuildConfiguration section */ 277 | 51AAC6D220DA78D50000CBE9 /* Debug */ = { 278 | isa = XCBuildConfiguration; 279 | buildSettings = { 280 | ALWAYS_SEARCH_USER_PATHS = NO; 281 | CLANG_ANALYZER_NONNULL = YES; 282 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 283 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 284 | CLANG_CXX_LIBRARY = "libc++"; 285 | CLANG_ENABLE_MODULES = YES; 286 | CLANG_ENABLE_OBJC_ARC = YES; 287 | CLANG_ENABLE_OBJC_WEAK = YES; 288 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 289 | CLANG_WARN_BOOL_CONVERSION = YES; 290 | CLANG_WARN_COMMA = YES; 291 | CLANG_WARN_CONSTANT_CONVERSION = YES; 292 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 293 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 294 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 295 | CLANG_WARN_EMPTY_BODY = YES; 296 | CLANG_WARN_ENUM_CONVERSION = YES; 297 | CLANG_WARN_INFINITE_RECURSION = YES; 298 | CLANG_WARN_INT_CONVERSION = YES; 299 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 300 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 301 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 302 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 303 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 304 | CLANG_WARN_STRICT_PROTOTYPES = YES; 305 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 306 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 307 | CLANG_WARN_UNREACHABLE_CODE = YES; 308 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 309 | CODE_SIGN_IDENTITY = "Mac Developer"; 310 | COPY_PHASE_STRIP = NO; 311 | CURRENT_PROJECT_VERSION = 1; 312 | DEBUG_INFORMATION_FORMAT = dwarf; 313 | ENABLE_STRICT_OBJC_MSGSEND = YES; 314 | ENABLE_TESTABILITY = YES; 315 | GCC_C_LANGUAGE_STANDARD = gnu11; 316 | GCC_DYNAMIC_NO_PIC = NO; 317 | GCC_NO_COMMON_BLOCKS = YES; 318 | GCC_OPTIMIZATION_LEVEL = 0; 319 | GCC_PREPROCESSOR_DEFINITIONS = ( 320 | "DEBUG=1", 321 | "$(inherited)", 322 | ); 323 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 324 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 325 | GCC_WARN_UNDECLARED_SELECTOR = YES; 326 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 327 | GCC_WARN_UNUSED_FUNCTION = YES; 328 | GCC_WARN_UNUSED_VARIABLE = YES; 329 | MACOSX_DEPLOYMENT_TARGET = 10.13; 330 | MTL_ENABLE_DEBUG_INFO = YES; 331 | ONLY_ACTIVE_ARCH = YES; 332 | SDKROOT = macosx; 333 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 334 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 335 | VERSIONING_SYSTEM = "apple-generic"; 336 | VERSION_INFO_PREFIX = ""; 337 | }; 338 | name = Debug; 339 | }; 340 | 51AAC6D320DA78D50000CBE9 /* Release */ = { 341 | isa = XCBuildConfiguration; 342 | buildSettings = { 343 | ALWAYS_SEARCH_USER_PATHS = NO; 344 | CLANG_ANALYZER_NONNULL = YES; 345 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 346 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 347 | CLANG_CXX_LIBRARY = "libc++"; 348 | CLANG_ENABLE_MODULES = YES; 349 | CLANG_ENABLE_OBJC_ARC = YES; 350 | CLANG_ENABLE_OBJC_WEAK = YES; 351 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 352 | CLANG_WARN_BOOL_CONVERSION = YES; 353 | CLANG_WARN_COMMA = YES; 354 | CLANG_WARN_CONSTANT_CONVERSION = YES; 355 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 356 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 357 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 358 | CLANG_WARN_EMPTY_BODY = YES; 359 | CLANG_WARN_ENUM_CONVERSION = YES; 360 | CLANG_WARN_INFINITE_RECURSION = YES; 361 | CLANG_WARN_INT_CONVERSION = YES; 362 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 363 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 364 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 365 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 366 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 367 | CLANG_WARN_STRICT_PROTOTYPES = YES; 368 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 369 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 370 | CLANG_WARN_UNREACHABLE_CODE = YES; 371 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 372 | CODE_SIGN_IDENTITY = "Mac Developer"; 373 | COPY_PHASE_STRIP = NO; 374 | CURRENT_PROJECT_VERSION = 1; 375 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 376 | ENABLE_NS_ASSERTIONS = NO; 377 | ENABLE_STRICT_OBJC_MSGSEND = YES; 378 | GCC_C_LANGUAGE_STANDARD = gnu11; 379 | GCC_NO_COMMON_BLOCKS = YES; 380 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 381 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 382 | GCC_WARN_UNDECLARED_SELECTOR = YES; 383 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 384 | GCC_WARN_UNUSED_FUNCTION = YES; 385 | GCC_WARN_UNUSED_VARIABLE = YES; 386 | MACOSX_DEPLOYMENT_TARGET = 10.13; 387 | MTL_ENABLE_DEBUG_INFO = NO; 388 | SDKROOT = macosx; 389 | SWIFT_COMPILATION_MODE = wholemodule; 390 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 391 | VERSIONING_SYSTEM = "apple-generic"; 392 | VERSION_INFO_PREFIX = ""; 393 | }; 394 | name = Release; 395 | }; 396 | 51AAC70C20DA79400000CBE9 /* Debug */ = { 397 | isa = XCBuildConfiguration; 398 | buildSettings = { 399 | CLANG_ENABLE_MODULES = YES; 400 | CODE_SIGN_IDENTITY = ""; 401 | CODE_SIGN_STYLE = Automatic; 402 | COMBINE_HIDPI_IMAGES = YES; 403 | DEFINES_MODULE = YES; 404 | DEVELOPMENT_TEAM = XK44TD8U4S; 405 | DYLIB_COMPATIBILITY_VERSION = 1; 406 | DYLIB_CURRENT_VERSION = 1; 407 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 408 | FRAMEWORK_VERSION = A; 409 | INFOPLIST_FILE = SpaceSwitcher/Info.plist; 410 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 411 | LD_RUNPATH_SEARCH_PATHS = ( 412 | "$(inherited)", 413 | "@executable_path/../Frameworks", 414 | "@loader_path/Frameworks", 415 | ); 416 | PRODUCT_BUNDLE_IDENTIFIER = com.bigbearlabs.SpaceSwitcher; 417 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 418 | SKIP_INSTALL = YES; 419 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 420 | SWIFT_VERSION = 4.2; 421 | }; 422 | name = Debug; 423 | }; 424 | 51AAC70D20DA79400000CBE9 /* Release */ = { 425 | isa = XCBuildConfiguration; 426 | buildSettings = { 427 | CLANG_ENABLE_MODULES = YES; 428 | CODE_SIGN_IDENTITY = ""; 429 | CODE_SIGN_STYLE = Automatic; 430 | COMBINE_HIDPI_IMAGES = YES; 431 | DEFINES_MODULE = YES; 432 | DEVELOPMENT_TEAM = XK44TD8U4S; 433 | DYLIB_COMPATIBILITY_VERSION = 1; 434 | DYLIB_CURRENT_VERSION = 1; 435 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 436 | FRAMEWORK_VERSION = A; 437 | INFOPLIST_FILE = SpaceSwitcher/Info.plist; 438 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 439 | LD_RUNPATH_SEARCH_PATHS = ( 440 | "$(inherited)", 441 | "@executable_path/../Frameworks", 442 | "@loader_path/Frameworks", 443 | ); 444 | PRODUCT_BUNDLE_IDENTIFIER = com.bigbearlabs.SpaceSwitcher; 445 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 446 | SKIP_INSTALL = YES; 447 | SWIFT_VERSION = 4.2; 448 | }; 449 | name = Release; 450 | }; 451 | 51AAC72120DA79B90000CBE9 /* Debug */ = { 452 | isa = XCBuildConfiguration; 453 | buildSettings = { 454 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 455 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 456 | CODE_SIGN_ENTITLEMENTS = SpaceSwitcherDemo/SpaceSwitcherDemo.entitlements; 457 | CODE_SIGN_STYLE = Automatic; 458 | COMBINE_HIDPI_IMAGES = YES; 459 | DEVELOPMENT_TEAM = XK44TD8U4S; 460 | INFOPLIST_FILE = SpaceSwitcherDemo/Info.plist; 461 | LD_RUNPATH_SEARCH_PATHS = ( 462 | "$(inherited)", 463 | "@executable_path/../Frameworks", 464 | ); 465 | PRODUCT_BUNDLE_IDENTIFIER = com.bigbearlabs.SpaceSwitcherDemo; 466 | PRODUCT_NAME = "$(TARGET_NAME)"; 467 | SWIFT_VERSION = 4.2; 468 | }; 469 | name = Debug; 470 | }; 471 | 51AAC72220DA79B90000CBE9 /* Release */ = { 472 | isa = XCBuildConfiguration; 473 | buildSettings = { 474 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 475 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 476 | CODE_SIGN_ENTITLEMENTS = SpaceSwitcherDemo/SpaceSwitcherDemo.entitlements; 477 | CODE_SIGN_STYLE = Automatic; 478 | COMBINE_HIDPI_IMAGES = YES; 479 | DEVELOPMENT_TEAM = XK44TD8U4S; 480 | INFOPLIST_FILE = SpaceSwitcherDemo/Info.plist; 481 | LD_RUNPATH_SEARCH_PATHS = ( 482 | "$(inherited)", 483 | "@executable_path/../Frameworks", 484 | ); 485 | PRODUCT_BUNDLE_IDENTIFIER = com.bigbearlabs.SpaceSwitcherDemo; 486 | PRODUCT_NAME = "$(TARGET_NAME)"; 487 | SWIFT_VERSION = 4.2; 488 | }; 489 | name = Release; 490 | }; 491 | /* End XCBuildConfiguration section */ 492 | 493 | /* Begin XCConfigurationList section */ 494 | 51AAC6C620DA78D50000CBE9 /* Build configuration list for PBXProject "SpaceSwitcher" */ = { 495 | isa = XCConfigurationList; 496 | buildConfigurations = ( 497 | 51AAC6D220DA78D50000CBE9 /* Debug */, 498 | 51AAC6D320DA78D50000CBE9 /* Release */, 499 | ); 500 | defaultConfigurationIsVisible = 0; 501 | defaultConfigurationName = Release; 502 | }; 503 | 51AAC70B20DA79400000CBE9 /* Build configuration list for PBXNativeTarget "SpaceSwitcher" */ = { 504 | isa = XCConfigurationList; 505 | buildConfigurations = ( 506 | 51AAC70C20DA79400000CBE9 /* Debug */, 507 | 51AAC70D20DA79400000CBE9 /* Release */, 508 | ); 509 | defaultConfigurationIsVisible = 0; 510 | defaultConfigurationName = Release; 511 | }; 512 | 51AAC72020DA79B90000CBE9 /* Build configuration list for PBXNativeTarget "SpaceSwitcherDemo" */ = { 513 | isa = XCConfigurationList; 514 | buildConfigurations = ( 515 | 51AAC72120DA79B90000CBE9 /* Debug */, 516 | 51AAC72220DA79B90000CBE9 /* Release */, 517 | ); 518 | defaultConfigurationIsVisible = 0; 519 | defaultConfigurationName = Release; 520 | }; 521 | /* End XCConfigurationList section */ 522 | }; 523 | rootObject = 51AAC6C320DA78D50000CBE9 /* Project object */; 524 | } 525 | -------------------------------------------------------------------------------- /SpaceSwitcher.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /SpaceSwitcher.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SpaceSwitcher/Appkit-categories.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | 3 | 4 | 5 | extension NSWindow { 6 | 7 | @IBInspectable 8 | public var transparent: Bool { 9 | get { 10 | return 11 | !self.isOpaque 12 | && self.backgroundColor == NSColor.clear 13 | } 14 | set { 15 | self.isOpaque = !newValue 16 | self.backgroundColor = newValue ? NSColor.clear : self.backgroundColor 17 | } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /SpaceSwitcher/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSHumanReadableCopyright 22 | Copyright © 2018 Big Bear Labs. All rights reserved. 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /SpaceSwitcher/SpaceChangeObserver.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | 3 | 4 | public protocol SpaceChangeObserver { 5 | 6 | func observeSpaceChangedNotifications() 7 | 8 | func onSpaceChanged() 9 | } 10 | 11 | 12 | extension SpaceChangeObserver where Self: AnyObject { 13 | 14 | public func observeSpaceChangedNotifications() { 15 | 16 | NSWorkspace.shared.notificationCenter.addObserver( 17 | forName: NSWorkspace.activeSpaceDidChangeNotification, 18 | object: nil, 19 | queue: nil) 20 | { [unowned self] notification in 21 | 22 | self.onSpaceChanged() 23 | } 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /SpaceSwitcher/SpaceSwitcher.h: -------------------------------------------------------------------------------- 1 | // 2 | // SpaceSwitcher.h 3 | // SpaceSwitcher 4 | // 5 | // Created by ilo on 20/06/2018. 6 | // Copyright © 2018 Big Bear Labs. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for SpaceSwitcher. 12 | FOUNDATION_EXPORT double SpaceSwitcherVersionNumber; 13 | 14 | //! Project version string for SpaceSwitcher. 15 | FOUNDATION_EXPORT const unsigned char SpaceSwitcherVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /SpaceSwitcher/SpaceSwitcher.swift: -------------------------------------------------------------------------------- 1 | import AppKit 2 | 3 | 4 | 5 | extension SpaceSwitcher: SpaceChangeObserver { 6 | 7 | /// place an anchor whenever it is found that no anchor window exists for the current space. 8 | public func onSpaceChanged() { 9 | 10 | ensureNoMultipleAnchorWindowsInSpace() 11 | 12 | if self.anchorWindowForCurrentSpace == nil { 13 | 14 | // drop an anchor. 15 | placeAnchorWindow() 16 | } 17 | 18 | ensureNoMultipleAnchorWindowsInSpace() 19 | 20 | if let currentSpaceToken = self.spaceTokenForCurrentSpace { 21 | self.changeHandler(currentSpaceToken) 22 | } 23 | 24 | 25 | } 26 | 27 | } 28 | 29 | 30 | 31 | public class SpaceSwitcher: NSObject { 32 | 33 | 34 | public var spaceTokens: [Int] { 35 | return anchorWindows.map { $0.windowNumber } 36 | } 37 | 38 | public var spaceTokenForCurrentSpace: Int? { 39 | return anchorWindowForCurrentSpace?.windowNumber 40 | } 41 | 42 | 43 | var anchorWindows: [SpaceAnchorWindow] = [] 44 | 45 | 46 | let changeHandler: (Int?) -> () 47 | 48 | 49 | public init(changeHandler: @escaping (Int?) -> () = {_ in }) { 50 | self.changeHandler = changeHandler 51 | super.init() 52 | 53 | observeSpaceChangedNotifications() 54 | 55 | onSpaceChanged() 56 | } 57 | 58 | 59 | @discardableResult 60 | func placeAnchorWindow() -> SpaceAnchorWindow? { 61 | 62 | defer { 63 | ensureNoMultipleAnchorWindowsInSpace() 64 | } 65 | 66 | let anchorWindow = SpaceAnchorWindow() 67 | 68 | guard anchorWindow.isOnActiveSpace else { 69 | 70 | // something will prevent this window from activating in the current space. 71 | // this is probably the dashboard space, or a full-screen space where 72 | // other app windows cannot legally show up. 73 | 74 | // just give up placing an anchor window. 75 | 76 | return nil 77 | } 78 | 79 | // finalise the anchor state. 80 | anchorWindow.setIsVisible(true) 81 | 82 | self.anchorWindows.append(anchorWindow) 83 | 84 | return anchorWindow 85 | } 86 | 87 | 88 | public func activateAnchorWindow(forSpaceToken: Int) { 89 | 90 | guard let anchorWindow = 91 | self.anchorWindows 92 | .filter({ $0.windowNumber == forSpaceToken }) 93 | .first 94 | else { 95 | fatalError("could not find anchor window \(forSpaceToken) to activate; please file a bug.") 96 | } 97 | 98 | guard anchorWindow != anchorWindowForCurrentSpace else { 99 | // anchor window is already in current space. 100 | return 101 | } 102 | 103 | anchorWindow.activateToSwitchSpace() 104 | 105 | } 106 | 107 | 108 | func ensureNoMultipleAnchorWindowsInSpace() { 109 | 110 | // when > 1 anchor window found in space, 111 | let anchorWindowsInSpace = self.anchorWindows.filter { 112 | $0.isOnActiveSpace 113 | } 114 | 115 | guard anchorWindowsInSpace.count <= 1 else { 116 | 117 | // remove all but the first one. 118 | let obsoleteAnchorWindows = anchorWindowsInSpace.dropFirst() 119 | for window in obsoleteAnchorWindows { 120 | 121 | if let i = anchorWindows.index(of: window) { 122 | anchorWindows.remove(at: i) 123 | } else { 124 | // obsolete anchor window not found in our list of anchor windows? 125 | // invalid situation. 126 | fatalError() 127 | } 128 | } 129 | 130 | return 131 | } 132 | 133 | } 134 | 135 | 136 | var anchorWindowForCurrentSpace: SpaceAnchorWindow? { 137 | 138 | ensureNoMultipleAnchorWindowsInSpace() 139 | 140 | let anchorWindowsOnSpace = anchorWindows.filter { 141 | $0.isOnActiveSpace 142 | } 143 | 144 | assert(anchorWindowsOnSpace.count <= 1, 145 | "multiple anchor windows found in space; please file a bug.") 146 | 147 | return anchorWindowsOnSpace.first 148 | } 149 | 150 | 151 | } 152 | 153 | 154 | class SpaceAnchorWindow: NSWindow { 155 | 156 | convenience init() { 157 | self.init( 158 | contentRect: CGRect(x: 0, y: 0, width: 5, height: 5), 159 | styleMask: [.borderless], 160 | backing: .buffered, 161 | defer: false) 162 | 163 | // ensure windows are released when no longer referenced. 164 | self.isReleasedWhenClosed = true 165 | 166 | // make anchor window transparent. 167 | self.transparent = true 168 | 169 | // exclude anchor window from window behaviour that might get in the way. 170 | self.collectionBehavior = [.ignoresCycle, .stationary] 171 | 172 | } 173 | 174 | func activateToSwitchSpace() { 175 | 176 | // the app be active in order for window to fling the space. 177 | NSApp.activate(ignoringOtherApps: true) 178 | 179 | // grab some state prior to the voodoo magic, in order to restore them afterwards. 180 | let ( 181 | windowsVisiblePriorToSwitch, 182 | collectionBehaviour 183 | ) = ( 184 | NSApp.windows.filter { 185 | $0 is SpaceAnchorWindow == false 186 | && $0.isVisible 187 | }, 188 | self.collectionBehavior 189 | ) 190 | 191 | // the window must be set to the expose-recognised collection behaviour in order to fling the space. 192 | self.collectionBehavior = [.ignoresCycle] 193 | 194 | DispatchQueue.main.async { 195 | 196 | self.makeKeyAndOrderFront(self) 197 | 198 | // need to hide this app in order not to disturb the system-default 199 | // app activation behaviour when switching spaces. 200 | NSApp.hide(self) 201 | 202 | // restore window states prior to the app activation and hiding. 203 | // a delay is needed before which spaces transition should complete in order for this approach to work. 204 | // this will probably break (in a minor way) on slower systems or when the window server is sluggish. 205 | let smallDelay = 0.5 206 | DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + smallDelay) { 207 | 208 | for window in windowsVisiblePriorToSwitch { 209 | window.setIsVisible(true) 210 | } 211 | 212 | self.collectionBehavior = collectionBehaviour 213 | } 214 | 215 | } 216 | } 217 | 218 | 219 | // override framework methods to work around cocoa assumptions of a transparent window's 220 | // behaviour. 221 | 222 | override var canBecomeKey: Bool { 223 | return true 224 | } 225 | 226 | override var canBecomeMain: Bool { 227 | return true 228 | } 229 | 230 | } 231 | -------------------------------------------------------------------------------- /SpaceSwitcherDemo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import SpaceSwitcher 3 | 4 | 5 | 6 | @NSApplicationMain 7 | class AppDelegate: NSObject, NSApplicationDelegate { 8 | 9 | var spaceSwitcher: SpaceSwitcher? 10 | 11 | 12 | func applicationDidFinishLaunching(_ aNotification: Notification) { 13 | 14 | // SpaceSwitcher will obtain a space token every time the app discovers a new space. 15 | // the info is used by ViewController to add a button that will switch to each discovered 16 | // space. 17 | spaceSwitcher = SpaceSwitcher() 18 | } 19 | 20 | } 21 | 22 | 23 | 24 | class DemoPanel: NSPanel { 25 | 26 | // forbid the panel to take focus, to rule out accidentally lucky spaces switches when it comes in focus. 27 | 28 | override var canBecomeMain: Bool { 29 | return false 30 | } 31 | override var canBecomeKey: Bool { 32 | return false 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /SpaceSwitcherDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "size" : "16x16", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "size" : "16x16", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "size" : "32x32", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "size" : "32x32", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "size" : "128x128", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "size" : "128x128", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "size" : "256x256", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "size" : "256x256", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "size" : "512x512", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "size" : "512x512", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /SpaceSwitcherDemo/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /SpaceSwitcherDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | Default 530 | 531 | 532 | 533 | 534 | 535 | 536 | Left to Right 537 | 538 | 539 | 540 | 541 | 542 | 543 | Right to Left 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | Default 555 | 556 | 557 | 558 | 559 | 560 | 561 | Left to Right 562 | 563 | 564 | 565 | 566 | 567 | 568 | Right to Left 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | -------------------------------------------------------------------------------- /SpaceSwitcherDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | Copyright © 2018 Big Bear Labs. All rights reserved. 27 | NSMainStoryboardFile 28 | Main 29 | NSPrincipalClass 30 | NSApplication 31 | 32 | 33 | -------------------------------------------------------------------------------- /SpaceSwitcherDemo/SpaceSwitcherDemo.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.files.user-selected.read-only 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /SpaceSwitcherDemo/ViewController.swift: -------------------------------------------------------------------------------- 1 | import Cocoa 2 | import SpaceSwitcher 3 | 4 | 5 | class ViewController: NSViewController { 6 | 7 | 8 | @IBOutlet weak var buttonsStackView: NSStackView! 9 | 10 | 11 | override func viewDidLoad() { 12 | super.viewDidLoad() 13 | 14 | observeSpaceChangedNotifications() 15 | 16 | onSpaceChanged() 17 | } 18 | 19 | 20 | // MARK: - key operations 21 | 22 | @IBAction 23 | func switchToSpace(_ spaceButton: NSButton) { 24 | let spaceToken = spaceButton.cell!.representedObject as! Int 25 | 26 | self.spaceSwitcher.activateAnchorWindow(forSpaceToken: spaceToken) 27 | } 28 | 29 | 30 | func refreshSwitchToSpaceButtons() { 31 | self.removeAllSwitchToSpaceButtons() 32 | 33 | let currentSpaceToken = self.spaceSwitcher.spaceTokenForCurrentSpace 34 | for token in self.spaceSwitcher.spaceTokens { 35 | let isCurrent = (token == currentSpaceToken) 36 | self.addButton(forSpaceToken: token, markAsCurrent: isCurrent) 37 | } 38 | } 39 | 40 | 41 | // MARK: - internals 42 | 43 | func addButton(forSpaceToken spaceToken: Int, markAsCurrent: Bool = false) { 44 | var title = String(spaceToken) 45 | if markAsCurrent { 46 | title = "\(title) *" 47 | } 48 | let button = NSButton( 49 | title: title, 50 | target: self, 51 | action: #selector(switchToSpace(_:))) 52 | button.cell!.representedObject = spaceToken 53 | 54 | buttonsStackView.addView(button, in: .top) 55 | } 56 | 57 | func removeAllSwitchToSpaceButtons() { 58 | for button in buttonsStackView.views(in: .top) where button is NSButton { 59 | buttonsStackView.removeView(button) 60 | } 61 | } 62 | 63 | 64 | var spaceSwitcher: SpaceSwitcher { 65 | return (NSApp.delegate as! AppDelegate).spaceSwitcher! 66 | } 67 | 68 | } 69 | 70 | 71 | // MARK: - responding to events 72 | 73 | extension ViewController: SpaceChangeObserver { 74 | 75 | func onSpaceChanged() { 76 | 77 | // ensure we handle the event after SpaceSwitcher. 78 | DispatchQueue.main.async { 79 | 80 | self.refreshSwitchToSpaceButtons() 81 | } 82 | 83 | } 84 | 85 | } 86 | --------------------------------------------------------------------------------