Atoll/DynamicIsland/helpers/AppIcons.swift
2026-02-04 00:00:50 +05:30

87 lines
2.7 KiB
Swift

/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import SwiftUI
import AppKit
import Defaults
struct AppIcons {
func getIcon(file path: String) -> NSImage? {
guard FileManager.default.fileExists(atPath: path)
else { return nil }
return NSWorkspace.shared.icon(forFile: path)
}
func getIcon(bundleID: String) -> NSImage? {
guard let path = NSWorkspace.shared.urlForApplication(
withBundleIdentifier: bundleID
)?.absoluteString
else { return nil }
return getIcon(file: path)
}
/// Easily read Info.plist as a Dictionary from any bundle by accessing .infoDictionary on Bundle
func bundle(forBundleID: String) -> Bundle? {
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: forBundleID)
else { return nil }
return Bundle(url: url)
}
}
func AppIcon(for bundleID: String) -> Image {
let workspace = NSWorkspace.shared
if let appURL = workspace.urlForApplication(withBundleIdentifier: bundleID) {
let appIcon = workspace.icon(forFile: appURL.path)
return Image(nsImage: appIcon)
}
return Image(nsImage: workspace.icon(for: .applicationBundle))
}
func AppIconAsNSImage(for bundleID: String) -> NSImage? {
let workspace = NSWorkspace.shared
if let appURL = workspace.urlForApplication(withBundleIdentifier: bundleID) {
let appIcon = workspace.icon(forFile: appURL.path)
return appIcon
}
return nil
}
func applySelectedAppIcon() {
let customIcons = Defaults[.customAppIcons]
if let selectedID = Defaults[.selectedAppIconID],
let icon = customIcons.first(where: { $0.id.uuidString == selectedID }),
let image = NSImage(contentsOf: icon.fileURL) {
NSApp.applicationIconImage = image
return
}
let fallbackName = Bundle.main.iconFileName ?? "AppIcon"
if let image = NSImage(named: fallbackName) {
NSApp.applicationIconImage = image
}
}