Skip to content

Commit beb1572

Browse files
committed
Update Menu And ListView
1 parent 0a6b5ec commit beb1572

File tree

2 files changed

+28
-62
lines changed

2 files changed

+28
-62
lines changed

CodeEdit/Features/Welcome/Views/RecentProjectsListView.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ struct RecentProjectsListView: View {
1919
init(openDocument: @escaping (URL?, @escaping () -> Void) -> Void, dismissWindow: @escaping () -> Void) {
2020
self.openDocument = openDocument
2121
self.dismissWindow = dismissWindow
22-
self._recentProjects = .init(initialValue: RecentProjectsStore.recentProjectURLs())
23-
self._selection = .init(initialValue: Set(RecentProjectsStore.recentProjectURLs().prefix(1)))
22+
self._recentProjects = .init(initialValue: RecentProjectsStore.default.recentURLs())
23+
self._selection = .init(initialValue: Set(RecentProjectsStore.default.recentURLs().prefix(1)))
2424
}
2525

2626
var listEmptyView: some View {
@@ -81,16 +81,20 @@ struct RecentProjectsListView: View {
8181
}
8282
}
8383
}
84-
.onReceive(NotificationCenter.default.publisher(for: RecentProjectsStore.didUpdateNotification)) { _ in
84+
.onReceive(
85+
NotificationCenter
86+
.default
87+
.publisher(for: RecentProjectsStore.didUpdateNotification).receive(on: RunLoop.main)
88+
) { _ in
8589
updateRecentProjects()
8690
}
8791
}
8892

8993
func removeRecentProjects() {
90-
recentProjects = RecentProjectsStore.removeRecentProjects(selection)
94+
recentProjects = RecentProjectsStore.default.removeRecentProjects(selection)
9195
}
9296

9397
func updateRecentProjects() {
94-
recentProjects = RecentProjectsStore.recentProjectURLs()
98+
recentProjects = RecentProjectsStore.default.recentURLs()
9599
}
96100
}

CodeEdit/Features/WindowCommands/Utils/RecentProjectsMenu.swift

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
import AppKit
99

1010
class RecentProjectsMenu: NSObject {
11+
let projectsStore: RecentProjectsStore
12+
13+
init(projectsStore: RecentProjectsStore = .default) {
14+
self.projectsStore = projectsStore
15+
}
16+
1117
func makeMenu() -> NSMenu {
1218
let menu = NSMenu(title: NSLocalizedString("Open Recent", comment: "Open Recent menu title"))
1319

14-
projectItems(menu: menu)
20+
addFileURLs(to: menu, fileURLs: projectsStore.recentProjectURLs().prefix(10))
1521
menu.addItem(NSMenuItem.separator())
16-
fileItems(menu: menu)
17-
22+
addFileURLs(to: menu, fileURLs: projectsStore.recentFileURLs().prefix(10))
1823
menu.addItem(NSMenuItem.separator())
24+
1925
let clearMenuItem = NSMenuItem(
2026
title: NSLocalizedString("Clear Menu", comment: "Recent project menu clear button"),
2127
action: #selector(clearMenuItemClicked(_:)),
@@ -27,67 +33,23 @@ class RecentProjectsMenu: NSObject {
2733
return menu
2834
}
2935

30-
private func projectItems( menu: NSMenu) {
31-
let projectPaths = RecentProjectsStore.recentProjectURLs().prefix(10)
32-
33-
for projectPath in projectPaths {
34-
let icon = NSWorkspace.shared.icon(forFile: projectPath.path())
35-
icon.size = NSSize(width: 16, height: 16)
36-
let alternateTitle = alternateTitle(for: projectPath)
37-
38-
let primaryItem = NSMenuItem(
39-
title: projectPath.lastPathComponent,
40-
action: #selector(recentProjectItemClicked(_:)),
41-
keyEquivalent: ""
42-
)
43-
primaryItem.target = self
44-
primaryItem.image = icon
45-
primaryItem.representedObject = projectPath
46-
47-
let containsDuplicate = projectPaths.contains { url in
48-
url != projectPath && url.lastPathComponent == projectPath.lastPathComponent
49-
}
50-
51-
// If there's a duplicate, add the path.
52-
if containsDuplicate {
53-
primaryItem.attributedTitle = alternateTitle
54-
}
55-
56-
let alternateItem = NSMenuItem(
57-
title: "",
58-
action: #selector(recentProjectItemClicked(_:)),
59-
keyEquivalent: ""
60-
)
61-
alternateItem.attributedTitle = alternateTitle
62-
alternateItem.target = self
63-
alternateItem.image = icon
64-
alternateItem.representedObject = projectPath
65-
alternateItem.isAlternate = true
66-
alternateItem.keyEquivalentModifierMask = [.option]
67-
68-
menu.addItem(primaryItem)
69-
menu.addItem(alternateItem)
70-
}
71-
}
72-
73-
private func fileItems( menu: NSMenu) {
74-
let filePaths = RecentProjectsStore.recentFileURLs().prefix(10)
75-
for filePath in filePaths {
76-
let icon = NSWorkspace.shared.icon(forFile: filePath.path())
36+
private func addFileURLs(to menu: NSMenu, fileURLs: ArraySlice<URL>) {
37+
for url in fileURLs {
38+
let icon = NSWorkspace.shared.icon(forFile: url.path())
7739
icon.size = NSSize(width: 16, height: 16)
78-
let alternateTitle = alternateTitle(for: filePath)
40+
let alternateTitle = alternateTitle(for: url)
7941

8042
let primaryItem = NSMenuItem(
81-
title: filePath.lastPathComponent,
43+
title: url.lastPathComponent,
8244
action: #selector(recentProjectItemClicked(_:)),
8345
keyEquivalent: ""
8446
)
8547
primaryItem.target = self
8648
primaryItem.image = icon
87-
primaryItem.representedObject = filePath
49+
primaryItem.representedObject = url
8850

89-
let containsDuplicate = filePaths.contains { url in
90-
url != filePath && url.lastPathComponent == filePath.lastPathComponent
51+
let containsDuplicate = fileURLs.contains { otherURL in
52+
url != otherURL && url.lastPathComponent == otherURL.lastPathComponent
9153
}
9254

9355
// If there's a duplicate, add the path.
@@ -103,7 +65,7 @@ class RecentProjectsMenu: NSObject {
10365
alternateItem.attributedTitle = alternateTitle
10466
alternateItem.target = self
10567
alternateItem.image = icon
106-
alternateItem.representedObject = filePath
68+
alternateItem.representedObject = url
10769
alternateItem.isAlternate = true
10870
alternateItem.keyEquivalentModifierMask = [.option]
10971

@@ -141,6 +103,6 @@ class RecentProjectsMenu: NSObject {
141103

142104
@objc
143105
func clearMenuItemClicked(_ sender: NSMenuItem) {
144-
RecentProjectsStore.clearList()
106+
projectsStore.clearList()
145107
}
146108
}

0 commit comments

Comments
 (0)