From 5e478f694737eff3d7510df4acdb3f69ba76b3ae Mon Sep 17 00:00:00 2001 From: phlpsong Date: Sat, 20 Jan 2024 20:18:12 +0800 Subject: [PATCH] fix: disable app list lag after add too many apps --- .../SettingView/Tabs/DisabledAppTab.swift | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift index a121652c7..7d70402ff 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift @@ -179,42 +179,20 @@ private struct ListButton: View { private struct BlockAppItemView: View { @EnvironmentObject var disabledAppViewModel: DisabledAppViewModel - let appIcon: NSImage - let appName: String + @StateObject private var appItemViewModel: AppItemViewModel init(with appModel: EZAppModel) { - let appBundleId = appModel.appBundleID - let workspace = NSWorkspace.shared - let appURL = workspace.urlForApplication(withBundleIdentifier: appBundleId) - guard let appURL else { - appIcon = .init() - appName = "" - return - } - - let appPath = NSWorkspace.shared.urlForApplication(withBundleIdentifier: appBundleId) - guard let appPath else { - appIcon = .init() - appName = "" - return - } - appIcon = workspace.icon(forFile: appPath.path(percentEncoded: false)) - - guard let appBundle = Bundle(url: appURL) else { - appName = "" - return - } - appName = appBundle.applicationName + _appItemViewModel = StateObject(wrappedValue: AppItemViewModel(appModel: appModel)) } var body: some View { HStack(alignment: .center) { - Image(nsImage: appIcon) + Image(nsImage: appItemViewModel.appIcon) .resizable() .scaledToFit() .frame(width: 24, height: 24) - Text(appName) + Text(appItemViewModel.appName) Spacer() } @@ -225,6 +203,34 @@ private struct BlockAppItemView: View { } } +@available(macOS 13.0, *) +private class AppItemViewModel: ObservableObject { + @Published var appIcon = NSImage() + + @Published var appName = "" + + var appModel: EZAppModel + + init(appModel: EZAppModel) { + self.appModel = appModel + getAppBundleInfo() + } + + func getAppBundleInfo() { + let appBundleId = appModel.appBundleID + let workspace = NSWorkspace.shared + let appURL = workspace.urlForApplication(withBundleIdentifier: appBundleId) + guard let appURL else { return } + + let appPath = NSWorkspace.shared.urlForApplication(withBundleIdentifier: appBundleId) + guard let appPath else { return } + appIcon = workspace.icon(forFile: appPath.path(percentEncoded: false)) + + guard let appBundle = Bundle(url: appURL) else { return } + appName = appBundle.applicationName + } +} + @available(macOS 13.0, *) #Preview { DisabledAppTab()