|
| 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