Skip to content

Commit

Permalink
ApplicationWindowState class
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Nov 8, 2019
1 parent 28654b3 commit 54a4690
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Convenience wrappers around observation of NSWindow states like `key` and `main`

Super-simple class that uses `WindowStateObserver` and provides a nice base for NSView subclasses that need to change their appearance depending on host window state.

**ApplicationWindowState**

A class that tracks changes in the application's windows, simplifying logic that depends on the state of all windows.

## NSWindow Extensions

Sizing conveniences:
Expand Down
12 changes: 8 additions & 4 deletions WindowTreatment.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
C9342E442376132C00A92D0C /* ApplicationWindowState.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9342E432376132C00A92D0C /* ApplicationWindowState.swift */; };
C965DBF8229F26DB00184E34 /* WindowStateObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = C965DBF7229F26DB00184E34 /* WindowStateObserver.swift */; };
C965DBFA229F270100184E34 /* WindowStateAwareView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C965DBF9229F270100184E34 /* WindowStateAwareView.swift */; };
C965DBFC229F28B000184E34 /* NSView+Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = C965DBFB229F28B000184E34 /* NSView+Window.swift */; };
Expand All @@ -32,6 +33,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C9342E432376132C00A92D0C /* ApplicationWindowState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ApplicationWindowState.swift; sourceTree = "<group>"; };
C965DBF7229F26DB00184E34 /* WindowStateObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowStateObserver.swift; sourceTree = "<group>"; };
C965DBF9229F270100184E34 /* WindowStateAwareView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowStateAwareView.swift; sourceTree = "<group>"; };
C965DBFB229F28B000184E34 /* NSView+Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSView+Window.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -100,6 +102,7 @@
C965DBF7229F26DB00184E34 /* WindowStateObserver.swift */,
C965DBF9229F270100184E34 /* WindowStateAwareView.swift */,
C965DBFB229F28B000184E34 /* NSView+Window.swift */,
C9342E432376132C00A92D0C /* ApplicationWindowState.swift */,
);
path = WindowTreatment;
sourceTree = "<group>";
Expand Down Expand Up @@ -224,6 +227,7 @@
files = (
C965DBF8229F26DB00184E34 /* WindowStateObserver.swift in Sources */,
C965DBFC229F28B000184E34 /* NSView+Window.swift in Sources */,
C9342E442376132C00A92D0C /* ApplicationWindowState.swift in Sources */,
C965DBFA229F270100184E34 /* WindowStateAwareView.swift in Sources */,
C9CB634322861571003470EC /* NSWindow+Tabbing.swift in Sources */,
C9CB63412285C308003470EC /* WindowTitlebarAwareViewController.swift in Sources */,
Expand Down Expand Up @@ -381,7 +385,7 @@
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -395,7 +399,7 @@
"@loader_path/Frameworks",
);
MACH_O_TYPE = staticlib;
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.chimehq.WindowTreatment;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand All @@ -411,7 +415,7 @@
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
Expand All @@ -425,7 +429,7 @@
"@loader_path/Frameworks",
);
MACH_O_TYPE = staticlib;
MARKETING_VERSION = 1.2.2;
MARKETING_VERSION = 1.2.3;
PRODUCT_BUNDLE_IDENTIFIER = com.chimehq.WindowTreatment;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
Expand Down
52 changes: 52 additions & 0 deletions WindowTreatment/ApplicationWindowState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// ApplicationWindowState.swift
// WindowTreatment
//
// Created by Matt Massicotte on 2019-11-08.
// Copyright © 2019 Chime Systems Inc. All rights reserved.
//

import Foundation

public class ApplicationWindowState {
private var windows: Set<NSWindow>
private var lastMainWindow: NSWindow?

public var changeHandler: (() -> Void)?

public init() {
self.windows = Set()

NotificationCenter.default.addObserver(self, selector: #selector(windowsChangedNotification(_:)), name: NSWindow.willCloseNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(windowsChangedNotification(_:)), name: NSWindow.didBecomeMainNotification, object: nil)
}

deinit {
NotificationCenter.default.removeObserver(self)
}

@objc private func windowsChangedNotification(_ notification: NSNotification) {
let current = Set(currentWindows)

if windows == current && mainWindow == lastMainWindow {
return
}

windows = Set(current)
lastMainWindow = mainWindow

changeHandler?()
}

public var windowsMenu: NSMenu? {
NSApp.windowsMenu
}

public var currentWindows: [NSWindow] {
return NSApp.windows
}

public var mainWindow: NSWindow? {
return NSApp.mainWindow
}
}

0 comments on commit 54a4690

Please sign in to comment.