Working with resizing

This commit is contained in:
Hariharan Mudaliar 2025-07-30 18:16:07 +05:30
commit 0cc6f369a7
3 changed files with 118 additions and 17 deletions

View file

@ -85,6 +85,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var closeNotchWorkItem: DispatchWorkItem?
private var previousScreens: [NSScreen]?
private var onboardingWindowController: NSWindowController?
private var cancellables = Set<AnyCancellable>()
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return false
@ -162,10 +163,102 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
private func updateWindowSizeIfNeeded() {
// Calculate required size based on current state
let requiredSize = calculateRequiredNotchSize()
// Update all windows if size has changed
for (screen, window) in windows {
if window.frame.size != requiredSize {
let screenFrame = screen.frame
let newFrame = NSRect(
x: screenFrame.origin.x + (screenFrame.width / 2) - requiredSize.width / 2,
y: screenFrame.origin.y + screenFrame.height - requiredSize.height,
width: requiredSize.width,
height: requiredSize.height
)
// Animate the frame change for smooth transition
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.3
context.allowsImplicitAnimation = true
window.setFrame(newFrame, display: true, animate: true)
}
}
}
}
private func calculateRequiredNotchSize() -> CGSize {
// Only apply dynamic sizing when on stats tab and stats are enabled
guard coordinator.currentView == .stats && Defaults[.enableStatsFeature] else {
return openNotchSize
}
let enabledGraphsCount = [
Defaults[.showCpuGraph],
Defaults[.showMemoryGraph],
Defaults[.showGpuGraph],
Defaults[.showNetworkGraph],
Defaults[.showDiskGraph]
].filter { $0 }.count
// If 4+ graphs are enabled, increase width
if enabledGraphsCount >= 4 {
let extraWidth: CGFloat = CGFloat(enabledGraphsCount - 3) * 120
return CGSize(width: openNotchSize.width + extraWidth, height: openNotchSize.height)
}
return openNotchSize
}
func applicationDidFinishLaunching(_ notification: Notification) {
coordinator.setupWorkersNotificationObservers()
// Observe tab changes to update window size dynamically
coordinator.objectWillChange.sink { [weak self] in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
// Observe stats settings changes
Defaults.publisher(.enableStatsFeature, options: []).sink { [weak self] _ in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
Defaults.publisher(.showCpuGraph, options: []).sink { [weak self] _ in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
Defaults.publisher(.showMemoryGraph, options: []).sink { [weak self] _ in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
Defaults.publisher(.showGpuGraph, options: []).sink { [weak self] _ in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
Defaults.publisher(.showNetworkGraph, options: []).sink { [weak self] _ in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
Defaults.publisher(.showDiskGraph, options: []).sink { [weak self] _ in
DispatchQueue.main.async {
self?.updateWindowSizeIfNeeded()
}
}.store(in: &cancellables)
NotificationCenter.default.addObserver(
self,
selector: #selector(screenConfigurationDidChange),

View file

@ -118,21 +118,6 @@ struct NotchStatsView: View {
return graphs
}
var dynamicNotchSize: CGSize {
let graphCount = availableGraphs.count
// Base size for 1-3 graphs
let baseWidth: CGFloat = 610
// If 4+ graphs, increase width to accommodate
if graphCount >= 4 {
let extraWidth: CGFloat = CGFloat(graphCount - 3) * 120
return CGSize(width: baseWidth + extraWidth, height: 200)
}
return CGSize(width: baseWidth, height: 200)
}
var gridColumns: [GridItem] {
let graphCount = availableGraphs.count
@ -264,7 +249,7 @@ struct NotchStatsView: View {
}
}
}
.frame(maxWidth: dynamicNotchSize.width, maxHeight: dynamicNotchSize.height)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(NSColor.windowBackgroundColor).opacity(0.1))
.clipShape(RoundedRectangle(cornerRadius: 12))
.onAppear {

View file

@ -116,7 +116,7 @@ class DynamicIslandViewModel: NSObject, ObservableObject {
func open() {
withAnimation(.bouncy) {
self.notchSize = openNotchSize
self.notchSize = calculateDynamicNotchSize()
self.notchState = .open
}
@ -124,6 +124,29 @@ class DynamicIslandViewModel: NSObject, ObservableObject {
MusicManager.shared.forceUpdate()
}
private func calculateDynamicNotchSize() -> CGSize {
// Only apply dynamic sizing when on stats tab and stats are enabled
guard DynamicIslandViewCoordinator.shared.currentView == .stats && Defaults[.enableStatsFeature] else {
return openNotchSize
}
let enabledGraphsCount = [
Defaults[.showCpuGraph],
Defaults[.showMemoryGraph],
Defaults[.showGpuGraph],
Defaults[.showNetworkGraph],
Defaults[.showDiskGraph]
].filter { $0 }.count
// If 4+ graphs are enabled, increase width
if enabledGraphsCount >= 4 {
let extraWidth: CGFloat = CGFloat(enabledGraphsCount - 3) * 120
return CGSize(width: openNotchSize.width + extraWidth, height: openNotchSize.height)
}
return openNotchSize
}
func close() {
withAnimation(.smooth) { [weak self] in
guard let self = self else { return }