Skip to content

Commit

Permalink
add: settings: force notch + size mode
Browse files Browse the repository at this point in the history
- Settings:
    - Window modifiers to be in front and toolbar style
    - Force notch on non-notched devices
    - Notch height mode: Auto / Menu Bar
  • Loading branch information
monuk7735 committed Mar 3, 2025
1 parent 19f1b2c commit 06b14ff
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 60 deletions.
4 changes: 2 additions & 2 deletions MewNotch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 0.1;
MARKETING_VERSION = 0.2;
PRODUCT_BUNDLE_IDENTIFIER = com.monuk7735.mew.notch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down Expand Up @@ -306,7 +306,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 0.1;
MARKETING_VERSION = 0.2;
PRODUCT_BUNDLE_IDENTIFIER = com.monuk7735.mew.notch;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
75 changes: 48 additions & 27 deletions MewNotch/DB/MewDefaultsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,20 @@ import SwiftUI
class MewDefaultsManager: ObservableObject {

@propertyWrapper
public struct RawEnumUserDefault<T> where T: RawRepresentable {
public struct CodableUserDefault<T> where T: Codable {

let key: String
let defaultValue: T
let suiteName: String?

let initializer: (T.RawValue) -> T?

public init(
_ key: String,
defaultValue: T,
suiteName: String? = nil,
initializer: @escaping (T.RawValue) -> T?
suiteName: String? = nil
) {
self.key = key
self.defaultValue = defaultValue
self.suiteName = suiteName

self.initializer = initializer
}

public var wrappedValue: T {
Expand All @@ -37,21 +32,22 @@ class MewDefaultsManager: ObservableObject {
suiteName: suiteName!
): UserDefaults.standard

return self.initializer(
defaults?.object(
forKey: key
) as? T.RawValue ?? defaultValue.rawValue
guard let data = defaults?.data(forKey: key) else {
return defaultValue
}

return (
try? JSONDecoder().decode(T.self, from: data)
) ?? defaultValue
}
set {
let defaults = suiteName != nil ? UserDefaults(
suiteName: suiteName!
): UserDefaults.standard

defaults?.set(
newValue.rawValue,
forKey: key
)
let encoded = try? JSONEncoder().encode(newValue)

defaults?.set(encoded, forKey: key)
}
}
}
Expand Down Expand Up @@ -93,14 +89,44 @@ class MewDefaultsManager: ObservableObject {

private let userDefaults: UserDefaults = .standard

// MARK: Variables
// MARK: Enums
public enum NotchHeightMode: String, CaseIterable, Identifiable, Codable {
var id: String { rawValue }

case Match_Notch
case Match_Menu_Bar
case Manual
}

enum HUDStyle: String, CaseIterable, Identifiable, Codable {
public enum HUDStyle: String, CaseIterable, Identifiable, Codable {
var id: String { rawValue }

case minimal
case progress
case notched
case Minimal
case Progress
case Notched
}


// MARK: Variables

@UserDefault(
"NotchForceEnabled",
defaultValue: false
)
var notchForceEnabled: Bool {
didSet {
self.objectWillChange.send()
}
}

@CodableUserDefault(
"NotchHeightMode",
defaultValue: NotchHeightMode.Match_Notch
)
var notchHeightMode: NotchHeightMode {
didSet {
self.objectWillChange.send()
}
}

@UserDefault(
Expand All @@ -113,14 +139,9 @@ class MewDefaultsManager: ObservableObject {
}
}

@RawEnumUserDefault(
@CodableUserDefault(
"HUDStyle",
defaultValue: HUDStyle.minimal,
initializer: {
return HUDStyle.init(
rawValue: $0
)
}
defaultValue: HUDStyle.Minimal
)
var hudStyle: HUDStyle {
didSet {
Expand Down
50 changes: 28 additions & 22 deletions MewNotch/MewAppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,35 @@ class MewAppDelegate: NSObject, NSApplicationDelegate {

guard let screen = NSScreen.main else { return }

let view: NSView = NSHostingView(
rootView: NotchView()
)

let panel = MewPanel(
contentRect: .zero,
styleMask: [
.borderless,
.nonactivatingPanel
],
backing: .buffered,
defer: true
)

panel.contentView = view
panel.orderFrontRegardless()
var panel: NSWindow! = windows[screen]

panel.setFrame(
screen.frame,
display: true
)

NotchSpaceManager.shared.notchSpace.windows.insert(panel)
if panel == nil {
let view: NSView = NSHostingView(
rootView: NotchView()
)

panel = MewPanel(
contentRect: .zero,
styleMask: [
.borderless,
.nonactivatingPanel
],
backing: .buffered,
defer: true
)

panel.contentView = view

panel.orderFrontRegardless()

panel.setFrame(
screen.frame,
display: true
)

self.windows[screen] = panel
NotchSpaceManager.shared.notchSpace.windows.insert(panel)
}

// self.createStatusItem()
}
Expand Down
4 changes: 4 additions & 0 deletions MewNotch/Utils/NotchUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class NotchUtils {
}

notchHeight = screen.safeAreaInsets.top

if MewDefaultsManager.shared.notchHeightMode == .Match_Menu_Bar {
notchHeight = screen.frame.maxY - screen.visibleFrame.maxY
}

return .init(
width: notchWidth,
Expand Down
10 changes: 6 additions & 4 deletions MewNotch/View/Notch/Collapsed/CollapsedNotchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct CollapsedNotchView: View {
HStack(
spacing: 0
) {
if defaultsManager.hudStyle == .minimal {
if defaultsManager.hudStyle == .Minimal {
if let hudIcon = notchViewModel.hudIcon {
Text(
"000 %"
Expand Down Expand Up @@ -59,7 +59,7 @@ struct CollapsedNotchView: View {
notchSize: notchViewModel.notchSize
)

if defaultsManager.hudStyle == .minimal {
if defaultsManager.hudStyle == .Minimal {
if let hudValue = notchViewModel.hudValue {
Text(
"000%"
Expand Down Expand Up @@ -99,11 +99,11 @@ struct CollapsedNotchView: View {
}
}

if defaultsManager.hudStyle == .progress {
if defaultsManager.hudStyle == .Progress {
ProgressHUDView(
notchViewModel: notchViewModel
)
} else if defaultsManager.hudStyle == .notched {
} else if defaultsManager.hudStyle == .Notched {
NotchedHUDView(
notchViewModel: notchViewModel
)
Expand Down Expand Up @@ -136,6 +136,8 @@ struct CollapsedNotchView: View {
} else {
OSDUIManager.shared.start()
}

notchViewModel.refreshNotchSize()
}
.onAppear {
if defaultsManager.hudEnabled {
Expand Down
13 changes: 13 additions & 0 deletions MewNotch/View/Settings/MewSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ struct MewSettingsView: View {
GeneraSettingsView()
}
)
.task {
guard let window = NSApp.windows.first(
where: {
$0.identifier?.rawValue == "com_apple_SwiftUI_Settings_window"
}
) else {
return
}

window.toolbarStyle = .unified
window.level = .mainMenu
window.styleMask.insert(.resizable)
}
}
}

Expand Down
78 changes: 74 additions & 4 deletions MewNotch/View/Settings/Pages/GeneraSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,73 @@ struct GeneraSettingsView: View {
Section(
content: {
Toggle(
"Enabled",
isOn: $defaultsManager.notchForceEnabled
) {
VStack(
alignment: .leading
) {
Text("Force Enable")

Text("Enable Notch on Non-notched Displays")
.font(.footnote)
}
}

Picker(
selection: $defaultsManager.notchHeightMode,
content: {
Text(
MewDefaultsManager.NotchHeightMode.Match_Notch.rawValue.replacingOccurrences(
of: "_",
with: " "
)
)
.tag(
MewDefaultsManager.NotchHeightMode.Match_Notch
)

Text(
MewDefaultsManager.NotchHeightMode.Match_Menu_Bar.rawValue.replacingOccurrences(
of: "_",
with: " "
)
)
.tag(
MewDefaultsManager.NotchHeightMode.Match_Menu_Bar
)
}
) {
VStack(
alignment: .leading
) {
Text("Height")

// Text("Design to be used for HUD")
// .font(.footnote)
}
}
},
header: {
Text("Notch")
}
)

Section(
content: {
Toggle(
isOn: $defaultsManager.hudEnabled
)
) {
VStack(
alignment: .leading
) {
Text("Enabled")

Text("Show Volume and Brightness changes on Notch and turn off system HUD")
.font(.footnote)
}
}

Picker(
"Style",
selection: $defaultsManager.hudStyle,
content: {
ForEach(
Expand All @@ -31,7 +92,16 @@ struct GeneraSettingsView: View {
.tag(style)
}
}
)
) {
VStack(
alignment: .leading
) {
Text("Style")

Text("Design to be used for HUD")
.font(.footnote)
}
}
.disabled(!defaultsManager.hudEnabled)
},
header: {
Expand Down
14 changes: 13 additions & 1 deletion MewNotch/ViewModel/CollapsedNotchViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CollapsedNotchViewModel: ObservableObject {
init() {
self.notchSize = NotchUtils.shared.notchSize(
screen: NSScreen.main,
force: true
force: MewDefaultsManager.shared.notchForceEnabled
)

withAnimation {
Expand All @@ -34,6 +34,18 @@ class CollapsedNotchViewModel: ObservableObject {
self.startListeners()
}

func refreshNotchSize() {
self.notchSize = NotchUtils.shared.notchSize(
screen: NSScreen.main,
force: MewDefaultsManager.shared.notchForceEnabled
)

withAnimation {
notchSize.width += extraNotchPadSize.width
notchSize.height += extraNotchPadSize.height
}
}

func startListeners() {
self.startListeningForVolumeChanges()
self.startListeningForBrightnessChanges()
Expand Down

0 comments on commit 06b14ff

Please sign in to comment.