Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable app list lag issue #347

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions Easydict/NewApp/View/SettingView/Tabs/DisabledAppTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
Expand Down