This commit is contained in:
Alin
2024-09-30 11:20:11 -06:00
parent d8f650116c
commit dbcf0ba655
9 changed files with 149 additions and 145 deletions
+21 -9
View File
@@ -7,6 +7,7 @@
import Foundation
import SwiftUI
import AlinFoundation
//import FinderSync
let home = FileManager.default.homeDirectoryForCurrentUser.path
@@ -29,16 +30,27 @@ class AppState: ObservableObject {
@Published var sentinelMode: Bool = false
@Published var showConditionBuilder: Bool = false
var operationQueueLeftover = OperationQueue()
@Published var shouldCancelOperations = false
func getBundleSize(for appInfo: AppInfo, updateState: @escaping (Int64) -> Void) {
// Step 1: Check if the size is available and not 0 in the sortedApps cache
if let existingAppInfo = sortedApps.first(where: { $0.path == appInfo.path }),
existingAppInfo.bundleSize != 0 {
// Size is available in the cache, update the state
DispatchQueue.main.async {
updateState(existingAppInfo.bundleSize)
}
return
}
func cancelQueueOperations() {
operationQueueLeftover.cancelAllOperations()
shouldCancelOperations = true
DispatchQueue.main.async {
self.leftoverProgress = ("Search canceled", 0.0)
self.showProgress = false
self.currentView = .empty
// Step 2: If we reach here, we need to calculate the size
DispatchQueue.global(qos: .userInitiated).async {
let calculatedSize = totalSizeOnDisk(for: appInfo.path).logical
DispatchQueue.main.async {
// Update the state and the array
updateState(calculatedSize)
if let index = self.sortedApps.firstIndex(where: { $0.path == appInfo.path }) {
self.sortedApps[index].bundleSize = calculatedSize
}
}
}
}
+2 -1
View File
@@ -50,8 +50,9 @@ func findAndSetWindowFrame(named titles: [String], windowSettings: WindowSetting
for title in titles {
if let window = NSApp.windows.first(where: { $0.title == title }) {
window.isRestorable = false
window.isReleasedWhenClosed = false
let frame = windowSettings.loadWindowSettings()
window.setFrame(frame, display: true)
window.setFrame(frame, display: true, animate: true)
}
}
}
+76 -23
View File
@@ -9,13 +9,84 @@ import SwiftUI
import AlinFoundation
import Combine
class WindowSettings {
class WindowSettings: ObservableObject {
static let shared = WindowSettings()
private let windowWidthKey = "windowWidthKey"
private let windowHeightKey = "windowHeightKey"
private let windowXKey = "windowXKey"
private let windowYKey = "windowYKey"
var windows: [NSWindow] = []
var windowRef: NSWindow?
init() {
trackMainWindow()
registerDefaultWindowSettings()
}
func trackMainWindow() {
if let mainWindow = NSApplication.shared.windows.first(where: { $0.title == "Pearcleaner" }) {
windowRef = mainWindow
print("Main window detected and tracked: \(mainWindow.title)")
}
}
// Launch new app windows on demand
func newWindow<V: View>(mini: Bool, withView view: @escaping () -> V) {
let frame = self.resetWindowSettings(mini: mini)
if windowRef == nil {
windowRef = NSWindow(
contentRect: .zero,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
}
// Update the existing windowRef with all desired settings
windowRef?.contentView = NSHostingView(rootView: view())
windowRef?.setFrame(frame, display: true, animate: true)
windowRef?.isMovableByWindowBackground = true
windowRef?.title = "Pearcleaner"
windowRef?.titlebarAppearsTransparent = true
windowRef?.isRestorable = false
windowRef?.titleVisibility = .hidden
windowRef?.makeKeyAndOrderFront(nil)
windowRef?.isReleasedWhenClosed = false
// if let curWindow = windowRef {
// print("Window exists, reopening...")
// let frame = self.resetWindowSettings(mini: mini)
// curWindow.contentView = NSHostingView(rootView: view())
// curWindow.setFrame(frame, display: true, animate: true)
// curWindow.titlebarAppearsTransparent = true
// curWindow.isMovableByWindowBackground = true
// curWindow.title = "Pearcleaner"
// curWindow.isRestorable = false
// curWindow.titleVisibility = .hidden
// curWindow.makeKeyAndOrderFront(nil)
// curWindow.isReleasedWhenClosed = false
// return
// }
// print("Window doesn't exist, creating...")
//
// // Close existing window
// findAndHideWindows(named: ["Pearcleaner"])
// // Create new window using defaults
// let frame = self.resetWindowSettings(mini: mini)
// let newWindow = NSWindow(
// contentRect: .zero,
// styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
// backing: .buffered, defer: false)
// newWindow.contentView = NSHostingView(rootView: view())
// newWindow.setFrame(frame, display: true, animate: true)
// newWindow.titlebarAppearsTransparent = true
// newWindow.isMovableByWindowBackground = true
// newWindow.title = "Pearcleaner"
// newWindow.isRestorable = false
//// self.windows.append(newWindow)
// newWindow.titleVisibility = .hidden
// newWindow.makeKeyAndOrderFront(nil)
// newWindow.isReleasedWhenClosed = false
// windowRef = newWindow
}
// Register default sizes if the AppStorage keys are invalid
func registerDefaultWindowSettings(completion: @escaping () -> Void = {}) {
@@ -78,25 +149,7 @@ class WindowSettings {
return NSRect(x: defaultX, y: defaultY, width: defaultWidth, height: defaultHeight)
}
// Launch new app windows on demand
func newWindow<V: View>(mini: Bool, withView view: @escaping () -> V) {
// Close existing window
findAndHideWindows(named: ["Pearcleaner"])
// Create new window using defaults
let contentView = view
let frame = self.resetWindowSettings(mini: mini)
let newWindow = NSWindow(
contentRect: frame,
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
newWindow.contentView = NSHostingView(rootView: contentView())
newWindow.titlebarAppearsTransparent = true
newWindow.isMovableByWindowBackground = true
newWindow.title = "Pearcleaner"
newWindow.isRestorable = false
self.windows.append(newWindow)
newWindow.titleVisibility = .hidden
newWindow.makeKeyAndOrderFront(nil)
}
}