Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Multi-screen support.
Browse files Browse the repository at this point in the history
  • Loading branch information
megabitsenmzq committed Oct 19, 2021
1 parent ddb6d9f commit d6b47b1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
12 changes: 8 additions & 4 deletions Notch Simulator/Notch Simulator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
C05C0B8A271E5D5C007BDF89 /* NotchViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C05C0B88271E5D5C007BDF89 /* NotchViewController.xib */; };
C05C0B90271E861F007BDF89 /* MyAppsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05C0B8E271E861F007BDF89 /* MyAppsViewController.swift */; };
C05C0B91271E861F007BDF89 /* MyAppsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C05C0B8F271E861F007BDF89 /* MyAppsViewController.xib */; };
C05C0B93271F03DA007BDF89 /* NotchWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = C05C0B92271F03DA007BDF89 /* NotchWindow.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -27,6 +28,7 @@
C05C0B8D271E6783007BDF89 /* Notch-Simulator-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = "Notch-Simulator-Info.plist"; sourceTree = SOURCE_ROOT; };
C05C0B8E271E861F007BDF89 /* MyAppsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyAppsViewController.swift; sourceTree = "<group>"; };
C05C0B8F271E861F007BDF89 /* MyAppsViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MyAppsViewController.xib; sourceTree = "<group>"; };
C05C0B92271F03DA007BDF89 /* NotchWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotchWindow.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -63,6 +65,7 @@
C05C0B7A271E59DA007BDF89 /* Assets.xcassets */,
C05C0B88271E5D5C007BDF89 /* NotchViewController.xib */,
C05C0B87271E5D5C007BDF89 /* NotchViewController.swift */,
C05C0B92271F03DA007BDF89 /* NotchWindow.swift */,
C05C0B8F271E861F007BDF89 /* MyAppsViewController.xib */,
C05C0B8E271E861F007BDF89 /* MyAppsViewController.swift */,
C05C0B7C271E59DA007BDF89 /* MainMenu.xib */,
Expand Down Expand Up @@ -144,6 +147,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C05C0B93271F03DA007BDF89 /* NotchWindow.swift in Sources */,
C05C0B90271E861F007BDF89 /* MyAppsViewController.swift in Sources */,
C05C0B79271E59D6007BDF89 /* AppDelegate.swift in Sources */,
C05C0B89271E5D5C007BDF89 /* NotchViewController.swift in Sources */,
Expand Down Expand Up @@ -287,7 +291,7 @@
CODE_SIGN_ENTITLEMENTS = "Notch Simulator/Notch_Simulator.entitlements";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = QJN4A73F8D;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -301,7 +305,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.2;
MARKETING_VERSION = 1.3;
PRODUCT_BUNDLE_IDENTIFIER = "com.JinyuMeng.Notch-Simulator";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -317,7 +321,7 @@
CODE_SIGN_ENTITLEMENTS = "Notch Simulator/Notch_Simulator.entitlements";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = QJN4A73F8D;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -331,7 +335,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 1.2;
MARKETING_VERSION = 1.3;
PRODUCT_BUNDLE_IDENTIFIER = "com.JinyuMeng.Notch-Simulator";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
56 changes: 38 additions & 18 deletions Notch Simulator/Notch Simulator/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import Cocoa
@main
class AppDelegate: NSObject, NSApplicationDelegate {


let notchWindow = NSWindow()
let notchWindowController = NSWindowController()
let screens = NSScreen.screens
var windows = [NotchWindow]()
var windowControllers = [NSWindowController]()
let notchViewController = NotchViewController(nibName: "NotchViewController", bundle: nil)

let myAppsWindow = NSWindow()
Expand All @@ -23,6 +23,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// Insert code here to initialize your application
setupWindow()

NotificationCenter.default.addObserver(self, selector: #selector(resetWindow), name: NSApplication.didChangeScreenParametersNotification, object: nil)

if !UserDefaults.standard.bool(forKey: "viewMyApps") {
setupMyAppWindow()
UserDefaults.standard.set(true, forKey: "viewMyApps")
Expand All @@ -40,26 +42,44 @@ class AppDelegate: NSObject, NSApplicationDelegate {


func setupWindow() {
notchWindow.styleMask = .borderless
notchWindow.backingType = .buffered
notchWindow.backgroundColor = .clear
notchWindow.hasShadow = false
notchWindow.level = .screenSaver
notchWindow.contentViewController = notchViewController
if screens.count == 0 {
NSApplication.shared.terminate(self)
}

NotificationCenter.default.addObserver(self, selector: #selector(resizeWindow), name: NSApplication.didChangeScreenParametersNotification, object: nil)
resizeWindow()
let menubarHeight = NSApplication.shared.mainMenu!.menuBarHeight

notchWindowController.contentViewController = notchWindow.contentViewController
notchWindowController.window = notchWindow
notchWindowController.showWindow(self)
for i in 0..<screens.count {
let notchWindow = NotchWindow()
notchWindow.targetScreen = screens[i]
notchWindow.styleMask = .borderless
notchWindow.backingType = .buffered
notchWindow.backgroundColor = .clear
notchWindow.hasShadow = false
notchWindow.level = .screenSaver
notchWindow.contentViewController = NotchViewController(nibName: "NotchViewController", bundle: nil)

let screenFrame = screens[i].frame
notchWindow.setFrame(NSRect(x: screenFrame.origin.x, y: screenFrame.origin.y + screenFrame.size.height - menubarHeight, width: screenFrame.size.width, height: menubarHeight), display: true)

let notchWindowController = NSWindowController()
notchWindowController.contentViewController = notchWindow.contentViewController
notchWindowController.window = notchWindow
notchWindowController.showWindow(self)

windows.append(notchWindow)
windowControllers.append(notchWindowController)
}
}

@objc func resizeWindow() {
let screenSize = NSScreen.main!.frame.size
let menubarHeight = NSApplication.shared.mainMenu!.menuBarHeight
@objc func resetWindow() {
for item in windows {
item.close()
}

notchWindow.setFrame(NSRect(x: 0, y: screenSize.height - menubarHeight, width: screenSize.width, height: menubarHeight), display: true)
windows.removeAll()
windowControllers.removeAll()

setupWindow()
}


Expand Down
6 changes: 0 additions & 6 deletions Notch Simulator/Notch Simulator/NotchViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ class NotchViewController: NSViewController {

override func viewDidLoad() {
super.viewDidLoad()

// Old Codes
// view.wantsLayer = true
// view.layer?.backgroundColor = .black
// view.layer?.cornerRadius = 10
// view.layer?.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
}

override func viewDidAppear() {
Expand Down
16 changes: 16 additions & 0 deletions Notch Simulator/Notch Simulator/NotchWindow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NotchWindow.swift
// Notch Simulator
//
// Created by 孟金羽 on 2021/10/19.
//

import Cocoa

class NotchWindow: NSWindow {
var targetScreen: NSScreen!

override func constrainFrameRect(_ frameRect: NSRect, to screen: NSScreen?) -> NSRect {
return super.constrainFrameRect(frameRect, to: targetScreen)
}
}

0 comments on commit d6b47b1

Please sign in to comment.