Skip to content

Commit 940d980

Browse files
feat: add FileMint file drawer
Add the native drawer with scoped folder browsing, keyboard navigation, Open With, and persistent opening preferences. Include the drawer specification and signed native acceptance fixtures.
1 parent 53be850 commit 940d980

51 files changed

Lines changed: 6826 additions & 68 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎App/FileMint/AppDelegate.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
4747

4848
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false }
4949

50-
func applicationWillTerminate(_ notification: Notification) { PreferencesModel.shared.globalShortcuts.shutdown() }
50+
func applicationWillTerminate(_ notification: Notification) { PreferencesModel.shared.stopGlobalShortcuts() }
5151

5252
func applicationShouldOpenUntitledFile(_ sender: NSApplication) -> Bool { false }
5353

‎App/FileMint/ContentView.swift‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ struct ContentView: View {
182182
case .fileTools: FileToolsPane()
183183
case .resourceTools: ResourceToolsPane(launchTool: launchResourceTool)
184184
case .openWith: OpenWithPane()
185+
case .fileDrawer: FileDrawerSettingsPane()
185186
case .favoriteLocations: FavoriteLocationsPane(favorites: favoriteLocations)
186187
case .about: AboutPane()
187188
}
@@ -247,7 +248,7 @@ extension PreferencesModel.Pane {
247248
func groupTitle(_ language: AppLanguage) -> String {
248249
switch self {
249250
case .fileTypes, .creation: InterfaceText.fileCreation.text(language)
250-
case .fileTools, .resourceTools, .openWith, .favoriteLocations, .shortcuts, .launchers: FileMintStrings.text(.extensions, language: language)
251+
case .fileTools, .resourceTools, .openWith, .favoriteLocations, .fileDrawer, .shortcuts, .launchers: FileMintStrings.text(.extensions, language: language)
251252
default: InterfaceText.preferences.text(language)
252253
}
253254
}
@@ -264,6 +265,7 @@ extension PreferencesModel.Pane {
264265
case .fileTools: InterfaceText.filesSubtitle.text(language)
265266
case .resourceTools: InterfaceText.resourceSubtitle.text(language)
266267
case .openWith: FileMintStrings.text(.openWithAppsHint, language: language)
268+
case .fileDrawer: FileMintStrings.text(.fileDrawerHint, language: language)
267269
case .favoriteLocations: FavoriteText.subtitle.text(language)
268270
case .about: FileMintStrings.text(.productTagline, language: language)
269271
}
@@ -280,6 +282,7 @@ extension PreferencesModel.Pane {
280282
case .fileTools: .fileTools
281283
case .resourceTools: .resourceTools
282284
case .openWith: .openWithApps
285+
case .fileDrawer: .fileDrawer
283286
case .favoriteLocations: .favoriteLocations
284287
case .about: .about
285288
}
@@ -296,6 +299,7 @@ extension PreferencesModel.Pane {
296299
case .fileTools: .fileToolsHint
297300
case .resourceTools: .resourceToolsHint
298301
case .openWith: .openWithAppsHint
302+
case .fileDrawer: .fileDrawerHint
299303
case .favoriteLocations: .favoriteLocations
300304
case .about: .productTagline
301305
}
@@ -313,6 +317,7 @@ extension PreferencesModel.Pane {
313317
case .fileTools: "wrench.and.screwdriver"
314318
case .resourceTools: "photo.on.rectangle"
315319
case .openWith: FileToolAppearance.openWithSymbol
320+
case .fileDrawer: "sidebar.right"
316321
case .favoriteLocations: "star"
317322
case .about: "info.circle"
318323
}

‎App/FileMint/FavoriteQuickPanelController.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ struct QuickSearchInputField: NSViewRepresentable {
152152
// Opt in only for the command palette; the favorite picker retains its
153153
// compact native field and the same editing/IME delegate.
154154
var paletteStyle = false
155+
var onAttach: ((NSSearchField) -> Void)? = nil
155156

156157
func makeCoordinator() -> Coordinator { Coordinator(self) }
157158

@@ -174,7 +175,10 @@ struct QuickSearchInputField: NSViewRepresentable {
174175
(field.cell as? NSSearchFieldCell)?.searchButtonCell = nil
175176
field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
176177
}
177-
if paletteStyle {
178+
if let onAttach {
179+
onCreate?(field)
180+
field.didAttach = onAttach
181+
} else if paletteStyle {
178182
// Hosted views can mount after the panel becomes key. Notify the
179183
// owner when the field actually belongs to its native window.
180184
field.didAttach = onCreate
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import AppKit
2+
import SwiftUI
3+
4+
@MainActor
5+
struct FileDrawerActionPalette: View {
6+
@ObservedObject var model: FileDrawerModel
7+
let session: FileDrawerActionSession
8+
let chinese: Bool
9+
let showsHints: Bool
10+
let availableHeight: CGFloat
11+
@State private var query = ""
12+
@State private var highlighted: FileDrawerAction?
13+
14+
private var matches: [FileDrawerAction] {
15+
let words = query.split(whereSeparator: \.isWhitespace)
16+
return FileDrawerAction.allCases.filter { action in
17+
guard action.enabled(model) else { return false }
18+
let names = action.title(false) + " " + action.title(true)
19+
return words.allSatisfy { names.localizedStandardContains(String($0)) }
20+
}
21+
}
22+
23+
var body: some View {
24+
let title = session.items.count == 1 ? session.items[0].name :
25+
model.text("\(session.items.count) selected items", "已选 \(session.items.count) 项")
26+
VStack(alignment: .leading, spacing: 5) {
27+
HStack {
28+
if model.applicationList != nil {
29+
Button(action: model.backFromApplications) { Image(systemName: "chevron.backward") }
30+
.buttonStyle(.borderless).accessibilityLabel(model.text("Back to Actions", "返回操作"))
31+
.accessibilityIdentifier("drawer.openWithBack")
32+
Text(model.text("Open With", "打开方式")).font(.headline)
33+
}
34+
Text(verbatim: title).lineLimit(1).truncationMode(.middle)
35+
.font(.system(size: 13, weight: .semibold)).foregroundStyle(.secondary)
36+
.accessibilityAddTraits(.isHeader)
37+
Spacer(minLength: 4)
38+
Button(action: model.closeActionPalette) { Image(systemName: "xmark.circle") }
39+
.buttonStyle(.borderless).accessibilityLabel(model.text("Close Actions", "关闭操作面板"))
40+
}.padding(.horizontal, 5)
41+
if let page = model.applicationList {
42+
FileDrawerApplicationPicker(model: model, page: page, availableHeight: availableHeight,
43+
showsHints: showsHints, focus: { focus($0, page: page.id) })
44+
.id(page.id)
45+
} else {
46+
actionRows
47+
}
48+
}
49+
.padding(9)
50+
.font(.system(size: 14))
51+
.background {
52+
RoundedRectangle(cornerRadius: 11).fill(FileMintStyle.surface)
53+
.shadow(color: .black.opacity(0.20), radius: 9, y: 3)
54+
}
55+
.overlay(RoundedRectangle(cornerRadius: 11).strokeBorder(FileMintStyle.line))
56+
.onExitCommand {
57+
if model.applicationList != nil { model.backFromApplications() }
58+
else { model.closeActionPalette() }
59+
}
60+
}
61+
62+
@ViewBuilder private var actionRows: some View {
63+
let rows = matches
64+
let selected = highlighted ?? rows.first
65+
Group {
66+
if rows.isEmpty {
67+
Text(model.text("No matching actions", "没有匹配的操作"))
68+
.foregroundStyle(.secondary).frame(maxWidth: .infinity, minHeight: 48)
69+
.accessibilityIdentifier("drawer.noMatchingActions")
70+
} else {
71+
ScrollViewReader { reader in
72+
ScrollView {
73+
VStack(spacing: 2) {
74+
ForEach(rows, id: \.self) { action in
75+
Button { run(action) } label: {
76+
HStack(spacing: 8) {
77+
Image(systemName: action.symbol).frame(width: 20)
78+
Text(action.title(chinese)).lineLimit(1).truncationMode(.tail)
79+
Spacer(minLength: 2)
80+
if showsHints {
81+
Group {
82+
if selected == action { Text("↩") }
83+
else if let binding = FileDrawerCommand.bindings.first(where: { $0.paletteAction == action }) {
84+
Text(binding.keys)
85+
}
86+
}.font(.system(size: 12)).foregroundStyle(.secondary).accessibilityHidden(true)
87+
}
88+
}
89+
.padding(.horizontal, 7).frame(minHeight: 31)
90+
.contentShape(Rectangle())
91+
.background(selected == action ? FileMintStyle.selection : .clear,
92+
in: RoundedRectangle(cornerRadius: 5))
93+
}
94+
.buttonStyle(.plain).id(action)
95+
.accessibilityAddTraits(selected == action ? .isSelected : [])
96+
.accessibilityIdentifier("drawer.action." + action.identifier)
97+
}
98+
}
99+
}
100+
.onChange(of: selected) { if let action = $0 { reader.scrollTo(action) } }
101+
}
102+
.frame(height: min(CGFloat(rows.count) * 33 - 2, 260, max(32, availableHeight - 94)))
103+
}
104+
Divider()
105+
QuickSearchInputField(text: Binding(get: { query }, set: { query = $0; highlighted = nil }),
106+
placeholder: model.text("Search actions", "搜索操作"), move: move, submit: submit,
107+
cancel: model.closeActionPalette, onCreate: { field in
108+
field.setAccessibilityIdentifier("drawer.actionSearch")
109+
field.setAccessibilityLabel(model.text("Search actions", "搜索操作"))
110+
}, onAttach: { focus($0, page: nil) })
111+
.frame(height: 28)
112+
.accessibilityIdentifier("drawer.actionSearch")
113+
}
114+
}
115+
116+
private func focus(_ field: NSSearchField, page: UUID?) {
117+
guard let window = field.window, window === model.window, model.actionPalette?.id == session.id,
118+
model.applicationList?.id == page else { return }
119+
window.makeFirstResponder(field)
120+
let id = session.id
121+
DispatchQueue.main.async { [weak field, weak model, weak window] in
122+
guard model?.actionPalette?.id == id, model?.applicationList?.id == page,
123+
let window, window.isKeyWindow, window.attachedSheet == nil,
124+
let field, field.window === window, field.currentEditor() == nil else { return }
125+
window.makeFirstResponder(field)
126+
}
127+
}
128+
129+
private func move(_ delta: Int) {
130+
let rows = matches
131+
guard !rows.isEmpty else { return }
132+
let index = highlighted.flatMap { rows.firstIndex(of: $0) } ?? 0
133+
highlighted = rows[min(rows.count - 1, max(0, index + delta))]
134+
}
135+
136+
private func submit() {
137+
let rows = matches
138+
if let action = highlighted ?? rows.first, rows.contains(action) { run(action) }
139+
}
140+
141+
private func run(_ action: FileDrawerAction) {
142+
model.performPaletteAction(action, session: session.id)
143+
}
144+
}

0 commit comments

Comments
 (0)