Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alienator88 committed Sep 30, 2024
1 parent d8f6501 commit dbcf0ba
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 145 deletions.
30 changes: 21 additions & 9 deletions Pearcleaner/Logic/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Foundation
import SwiftUI
import AlinFoundation
//import FinderSync

let home = FileManager.default.homeDirectoryForCurrentUser.path
Expand All @@ -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
}
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion Pearcleaner/Logic/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
99 changes: 76 additions & 23 deletions Pearcleaner/Logic/WindowSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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)
}

}



38 changes: 19 additions & 19 deletions Pearcleaner/PearcleanerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct PearcleanerApp: App {
@StateObject private var updater = Updater(owner: "alienator88", repo: "Pearcleaner")
@StateObject private var themeManager = ThemeManager.shared
@StateObject private var permissionManager = PermissionManager.shared
@State private var windowSettings = WindowSettings()
@StateObject private var windowSettings = WindowSettings.shared
@AppStorage("settings.permissions.hasLaunched") private var hasLaunched: Bool = false
@AppStorage("settings.general.mini") private var mini: Bool = false
@AppStorage("settings.general.miniview") private var miniView: Bool = true
Expand Down Expand Up @@ -48,6 +48,8 @@ struct PearcleanerApp: App {
}

var body: some Scene {


WindowGroup {
Group {
if mini {
Expand Down Expand Up @@ -79,12 +81,6 @@ struct PearcleanerApp: App {
}
return true
}
// Save window size on window dimension change
// .onChange(of: NSApplication.shared.windows.first?.frame) { newFrame in
// if let newFrame = newFrame {
// windowSettings.saveWindowSettings(frame: newFrame)
// }
// }
.alert(isPresented: $appState.showUninstallAlert) {
Alert(
title: Text("Warning!"),
Expand All @@ -108,7 +104,6 @@ struct PearcleanerApp: App {
appState.currentView = .empty
}


// Disable tabbing
NSWindow.allowsAutomaticWindowTabbing = false

Expand All @@ -127,6 +122,10 @@ struct PearcleanerApp: App {
.environmentObject(permissionManager)
.preferredColorScheme(themeManager.displayMode.colorScheme)
})

findAndHideWindows(named: ["Pearcleaner"])
NSApplication.shared.setActivationPolicy(.accessory)

}


Expand Down Expand Up @@ -158,6 +157,7 @@ struct PearcleanerApp: App {
.environmentObject(themeManager)
.environmentObject(updater)
.environmentObject(permissionManager)
.environmentObject(windowSettings)
.preferredColorScheme(themeManager.displayMode.colorScheme)
.toolbarBackground(.clear)
.movableByWindowBackground()
Expand All @@ -170,7 +170,7 @@ struct PearcleanerApp: App {


class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
var windowSettings = WindowSettings()
let windowSettings = WindowSettings.shared
var themeManager = ThemeManager.shared
var windowCloseObserver: NSObjectProtocol?
var windowFrameObserver: NSObjectProtocol?
Expand All @@ -182,16 +182,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

func applicationDidFinishLaunching(_ notification: Notification) {
let menubarEnabled = UserDefaults.standard.bool(forKey: "settings.menubar.enabled")
// UserDefaults.standard.register(defaults: ["NSQuitAlwaysKeepsWindows" : false])

findAndSetWindowFrame(named: ["Pearcleaner"], windowSettings: windowSettings)
if !menubarEnabled {
findAndSetWindowFrame(named: ["Pearcleaner"], windowSettings: windowSettings)
}

themeManager.setupAppearance()

if menubarEnabled {
findAndHideWindows(named: ["Pearcleaner"])
NSApplication.shared.setActivationPolicy(.accessory)
}
// if menubarEnabled {
// findAndHideWindows(named: ["Pearcleaner"])
// NSApplication.shared.setActivationPolicy(.accessory)
// }

windowFrameObserver = NotificationCenter.default.addObserver(forName: nil, object: nil, queue: nil) { notification in
if let window = notification.object as? NSWindow, window.title == "Pearcleaner" {
Expand All @@ -203,6 +204,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {

windowCloseObserver = NotificationCenter.default.addObserver(forName: NSWindow.willCloseNotification, object: nil, queue: nil) { notification in
if let window = notification.object as? NSWindow, window.title == "Pearcleaner" {
// Save window settings before removal (existing logic)
self.windowSettings.saveWindowSettings(frame: window.frame)
}
}
Expand All @@ -225,16 +227,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {


func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
let windowSettings = WindowSettings()
let windowSettings = WindowSettings.shared

if !flag {
// No visible windows, so let's open a new one
for window in sender.windows {
window.title = "Pearcleaner"
window.makeKeyAndOrderFront(self)
updateOnMain(after: 0.1, {
resizeWindowAuto(windowSettings: windowSettings, title: "Pearcleaner")
})
window.setFrame(windowSettings.loadWindowSettings(), display: true, animate: true)
}
return true // Indicates you've handled the re-open
}
Expand Down
49 changes: 24 additions & 25 deletions Pearcleaner/Settings/Interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct InterfaceSettingsTab: View {
@EnvironmentObject var permissionManager: PermissionManager
@EnvironmentObject var fsm: FolderSettingsManager
@EnvironmentObject var themeManager: ThemeManager
@State private var windowSettings = WindowSettings()
@EnvironmentObject var windowSettings: WindowSettings
@AppStorage("settings.menubar.enabled") private var menubarEnabled: Bool = false
@AppStorage("settings.general.mini") private var mini: Bool = false
@AppStorage("settings.general.glass") private var glass: Bool = false
Expand Down Expand Up @@ -287,30 +287,29 @@ struct InterfaceSettingsTab: View {
} else {
MenuBarExtraManager.shared.removeMenuBarExtra()
NSApplication.shared.setActivationPolicy(.regular)
if !hasWindowOpen() {
if mini {
windowSettings.newWindow(mini: true, withView: {
MiniMode(search: $search, showPopover: $showPopover)
.environmentObject(appState)
.environmentObject(locations)
.environmentObject(fsm)
.environmentObject(themeManager)
.environmentObject(updater)
.environmentObject(permissionManager)
.preferredColorScheme(themeManager.displayMode.colorScheme)
})
} else {
windowSettings.newWindow(mini: false, withView: {
RegularMode(search: $search, showPopover: $showPopover)
.environmentObject(appState)
.environmentObject(locations)
.environmentObject(fsm)
.environmentObject(themeManager)
.environmentObject(updater)
.environmentObject(permissionManager)
.preferredColorScheme(themeManager.displayMode.colorScheme)
})
}

if mini {
windowSettings.newWindow(mini: true, withView: {
MiniMode(search: $search, showPopover: $showPopover)
.environmentObject(appState)
.environmentObject(locations)
.environmentObject(fsm)
.environmentObject(themeManager)
.environmentObject(updater)
.environmentObject(permissionManager)
.preferredColorScheme(themeManager.displayMode.colorScheme)
})
} else {
windowSettings.newWindow(mini: false, withView: {
RegularMode(search: $search, showPopover: $showPopover)
.environmentObject(appState)
.environmentObject(locations)
.environmentObject(fsm)
.environmentObject(themeManager)
.environmentObject(updater)
.environmentObject(permissionManager)
.preferredColorScheme(themeManager.displayMode.colorScheme)
})
}

}
Expand Down
2 changes: 2 additions & 0 deletions Pearcleaner/Settings/SettingsWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct SettingsView: View {
@EnvironmentObject var fsm: FolderSettingsManager
@EnvironmentObject var themeManager: ThemeManager
@EnvironmentObject var updater: Updater
@EnvironmentObject var windowSettings: WindowSettings
@Binding var showPopover: Bool
@Binding var search: String
@AppStorage("settings.general.glass") private var glass: Bool = false
Expand Down Expand Up @@ -93,6 +94,7 @@ struct SettingsView: View {
case .interface:
InterfaceSettingsTab(showPopover: $showPopover, search: $search)
.environmentObject(themeManager)
.environmentObject(windowSettings)
case .folders:
FolderSettingsTab()
.environmentObject(themeManager)
Expand Down
12 changes: 2 additions & 10 deletions Pearcleaner/Views/AppListItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,8 @@ struct AppListItems: View {
}
.onAppear {
if self.bundleSize == 0 {
DispatchQueue.global(qos: .userInitiated).async {
let size = totalSizeOnDisk(for: appInfo.path).logical
DispatchQueue.main.async {
print("Sizing up")
self.bundleSize = size
// Update the appInfo in the appState array
if let index = appState.sortedApps.firstIndex(where: { $0.path == appInfo.path }) {
appState.sortedApps[index].bundleSize = size
}
}
appState.getBundleSize(for: appInfo) { size in
self.bundleSize = size
}
}
}
Expand Down
Loading

0 comments on commit dbcf0ba

Please sign in to comment.