Skip to content

Commit

Permalink
allow cmd+Q for quit, resize on resolution change
Browse files Browse the repository at this point in the history
  • Loading branch information
vgmoose committed Oct 25, 2021
1 parent 6c13dd6 commit 5110803
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
8 changes: 4 additions & 4 deletions TheNotch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@
CODE_SIGN_ENTITLEMENTS = TheNotch/TheNotch.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"TheNotch/Preview Content\"";
DEVELOPMENT_TEAM = 9UDT7LSCEF;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -295,7 +295,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.2;
PRODUCT_BUNDLE_IDENTIFIER = llc.newt.apps.notch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -311,7 +311,7 @@
CODE_SIGN_ENTITLEMENTS = TheNotch/TheNotch.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_ASSET_PATHS = "\"TheNotch/Preview Content\"";
DEVELOPMENT_TEAM = 9UDT7LSCEF;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -326,7 +326,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.2;
PRODUCT_BUNDLE_IDENTIFIER = llc.newt.apps.notch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
3 changes: 1 addition & 2 deletions TheNotch/ConfigView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ struct ConfigView: View {
in: 25...75,
step: 5,
onEditingChanged: { editing in
NotchWindow.notchHeight = Int(Double(NotchWindow.defaultNotchHeight) * (scale / 50.0))
NotchWindow.notchWidth = Int(Double(NotchWindow.defaultNotchWidth) * (scale / 50.0))
NotchWindow.scale = (scale / 50.0)
NotchWindow.singleton?.refreshNotch()
}
)
Expand Down
35 changes: 21 additions & 14 deletions TheNotch/NotchWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class NotchWindow : NSWindow
static var showOnAllSpaces = true
static var showInDock = true

static var scale = 1.0

override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool)
{
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
Expand All @@ -41,17 +43,22 @@ class NotchWindow : NSWindow

func refreshNotch() {

// update how it can be interacted with
self.ignoresMouseEvents = !NotchWindow.allowMoving
self.isMovableByWindowBackground = NotchWindow.allowMoving

self.collectionBehavior = NotchWindow.allowMoving ? .managed : .stationary

if NotchWindow.showOnAllSpaces {
self.collectionBehavior = [ self.collectionBehavior, .canJoinAllSpaces ]
}

var screenWidth = 1680
var screenHeight = 1050
// default 13-inch resolution (is used if some display change events being nil)
var screenWidth = 1440
var screenHeight = 900

// update the size to the latest scaling info from the config
NotchWindow.notchHeight = Int(Double(NotchWindow.defaultNotchHeight) * (NotchWindow.scale))
NotchWindow.notchWidth = Int(Double(NotchWindow.defaultNotchWidth) * (NotchWindow.scale))

let notchWidth = NotchWindow.notchWidth
let notchOffset = 10
Expand All @@ -66,18 +73,18 @@ class NotchWindow : NSWindow
NSApp.activate(ignoringOtherApps: true)
}

if let screen = self.screen {
let frame = screen.frame

// get the current window's screen and its width and heights
screenWidth = Int(frame.width)
screenHeight = Int(frame.height)

// update default notch sizes based on display, so other screen sizes don't get anything unexpected
NotchWindow.defaultNotchWidth = Int(Double(screenWidth) / 10.5)
NotchWindow.defaultNotchHeight = Int(Double(screenHeight) / 26.25)
}
// get window's screen dimensions, or default if they aren't available for some reason
// TODO: default to main display?
let frame = self.screen?.frame ?? NSRect(x: 0, y: 0, width: screenWidth, height: screenHeight)

// get the current window's screen and its width and heights
screenWidth = Int(frame.width)
screenHeight = Int(frame.height)

// update default notch sizes based on display, so other screen sizes don't get anything unexpected
NotchWindow.defaultNotchWidth = Int(Double(screenWidth) / 10.5)
NotchWindow.defaultNotchHeight = Int(Double(screenHeight) / 26.25)

self.contentView = NSHostingView(rootView: NotchView())

if !NotchWindow.allowMoving {
Expand Down
27 changes: 27 additions & 0 deletions TheNotch/TheNotchApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,32 @@ class TheNotchApp: NSApplication, NSApplicationDelegate {

self.window = window
TheNotchApp.singleton = self

// set up menu bar and quit button
// https://stackoverflow.com/a/46348417
NSApp.setActivationPolicy(.regular)
let menubar = NSMenu()
let appMenuItem = NSMenuItem()
menubar.addItem(appMenuItem)
NSApp.mainMenu = menubar
let appMenu = NSMenu()
let appName = ProcessInfo.processInfo.processName
let quitTitle = "Quit " + appName
let quitMenuItem = NSMenuItem.init(title:quitTitle,
action:#selector(NSApplication.terminate),keyEquivalent:"q")
appMenu.addItem(quitMenuItem);
appMenuItem.submenu = appMenu;

// react to resolution changes too
// https://stackoverflow.com/a/52071507
NotificationCenter.default.addObserver(
forName: NSApplication.didChangeScreenParametersNotification,
object: NSApplication.shared,
queue: OperationQueue.main
) {
notification -> Void in
NotchWindow.singleton?.refreshNotch()
print("screen parameters changed", NotchWindow.singleton)
}
}
}

0 comments on commit 5110803

Please sign in to comment.