Skip to content

Commit

Permalink
fix: disable app list lag after add too many apps (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong authored Jan 21, 2024
1 parent 1f6fdc2 commit 5384b39
Showing 1 changed file with 32 additions and 26 deletions.
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

0 comments on commit 5384b39

Please sign in to comment.