From 0cc6f369a757a47c52b89b82e6caf7d42b6275d3 Mon Sep 17 00:00:00 2001 From: Hariharan Mudaliar Date: Wed, 30 Jul 2025 18:16:07 +0530 Subject: [PATCH] Working with resizing --- DynamicIsland/DynamicIslandApp.swift | 93 +++++++++++++++++++ .../components/Notch/NotchStatsView.swift | 17 +--- .../models/DynamicIslandViewModel.swift | 25 ++++- 3 files changed, 118 insertions(+), 17 deletions(-) diff --git a/DynamicIsland/DynamicIslandApp.swift b/DynamicIsland/DynamicIslandApp.swift index f4c7ebd..b4970d4 100644 --- a/DynamicIsland/DynamicIslandApp.swift +++ b/DynamicIsland/DynamicIslandApp.swift @@ -85,6 +85,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { var closeNotchWorkItem: DispatchWorkItem? private var previousScreens: [NSScreen]? private var onboardingWindowController: NSWindowController? + private var cancellables = Set() func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { return false @@ -162,9 +163,101 @@ 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, diff --git a/DynamicIsland/components/Notch/NotchStatsView.swift b/DynamicIsland/components/Notch/NotchStatsView.swift index 754eed4..556a96f 100644 --- a/DynamicIsland/components/Notch/NotchStatsView.swift +++ b/DynamicIsland/components/Notch/NotchStatsView.swift @@ -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 { diff --git a/DynamicIsland/models/DynamicIslandViewModel.swift b/DynamicIsland/models/DynamicIslandViewModel.swift index d73371b..a55cf2c 100644 --- a/DynamicIsland/models/DynamicIslandViewModel.swift +++ b/DynamicIsland/models/DynamicIslandViewModel.swift @@ -116,13 +116,36 @@ class DynamicIslandViewModel: NSObject, ObservableObject { func open() { withAnimation(.bouncy) { - self.notchSize = openNotchSize + self.notchSize = calculateDynamicNotchSize() self.notchState = .open } // Force music information update when notch is opened 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