|
| 1 | +import FileMintCore |
| 2 | +import SwiftUI |
| 3 | + |
| 4 | +/// The settings surface also runs in an isolated native QA fixture, without a store. |
| 5 | +struct FileToolsSettingsView: View { |
| 6 | + @Binding var preferences: FileToolsPreferences |
| 7 | + let language: AppLanguage |
| 8 | + |
| 9 | + private func text(_ key: FileMintTextKey) -> String { |
| 10 | + FileMintStrings.text(key, language: language) |
| 11 | + } |
| 12 | + |
| 13 | + private var placementWidth: CGFloat { language.resolved() == .chinese ? 138 : 164 } |
| 14 | + |
| 15 | + var body: some View { |
| 16 | + ScrollView { |
| 17 | + VStack(alignment: .leading, spacing: 26) { |
| 18 | + VStack(alignment: .leading, spacing: 12) { |
| 19 | + heading(.fileTools) |
| 20 | + VStack(alignment: .leading, spacing: 8) { |
| 21 | + Toggle(text(.enableFileTools), isOn: $preferences.isEnabled) |
| 22 | + .font(.system(size: 13, weight: .semibold)) |
| 23 | + .accessibilityIdentifier("fileTools.enabled") |
| 24 | + Text(text(.fileToolsOffHint)) |
| 25 | + .font(.caption).foregroundStyle(.secondary) |
| 26 | + .fixedSize(horizontal: false, vertical: true) |
| 27 | + .padding(.leading, 20) |
| 28 | + }.toolSettingsGroup() |
| 29 | + } |
| 30 | + |
| 31 | + VStack(alignment: .leading, spacing: 12) { |
| 32 | + VStack(alignment: .leading, spacing: 5) { |
| 33 | + heading(.fileToolsActions) |
| 34 | + Text(text(.fileToolsActionsHint)) |
| 35 | + .font(.caption).foregroundStyle(.secondary) |
| 36 | + } |
| 37 | + ForEach(FileTool.allCases, id: \.self) { tool in |
| 38 | + toolSection(tool) |
| 39 | + } |
| 40 | + } |
| 41 | + // Disable the whole subtree, not just the visible pointer targets. |
| 42 | + // This also disables keyboard and accessibility actions on its controls. |
| 43 | + .disabled(!preferences.isEnabled) |
| 44 | + .saturation(preferences.isEnabled ? 1 : 0) |
| 45 | + .opacity(preferences.isEnabled ? 1 : 0.6) |
| 46 | + } |
| 47 | + .toggleStyle(.checkbox) |
| 48 | + .tint(.mint) |
| 49 | + .padding(1) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private func heading(_ key: FileMintTextKey) -> some View { |
| 54 | + Text(text(key)).font(.headline).accessibilityAddTraits(.isHeader) |
| 55 | + } |
| 56 | + |
| 57 | + private func toolSection(_ tool: FileTool) -> some View { |
| 58 | + let enabled = preferences.isToolEnabled(tool) |
| 59 | + return VStack(alignment: .leading, spacing: 14) { |
| 60 | + HStack(alignment: .top, spacing: 18) { |
| 61 | + VStack(alignment: .leading, spacing: 7) { |
| 62 | + Toggle(isOn: Binding( |
| 63 | + get: { preferences.isToolEnabled(tool) }, |
| 64 | + set: { preferences.setEnabled($0, for: tool) } |
| 65 | + )) { |
| 66 | + HStack(spacing: 9) { |
| 67 | + if let image = FileToolAppearance.image(for: tool, size: 20) { |
| 68 | + Image(nsImage: image).resizable().scaledToFit() |
| 69 | + .frame(width: 20, height: 20) |
| 70 | + .saturation(enabled ? 1 : 0) |
| 71 | + .opacity(enabled ? 1 : 0.5) |
| 72 | + .accessibilityHidden(true) |
| 73 | + } |
| 74 | + Text(text(tool.title)) |
| 75 | + .font(.system(size: 13, weight: .semibold)) |
| 76 | + .fixedSize(horizontal: false, vertical: true) |
| 77 | + } |
| 78 | + } |
| 79 | + .accessibilityLabel(text(tool.title)) |
| 80 | + .accessibilityIdentifier("fileTools.\(tool.rawValue)") |
| 81 | + Text(text(hint(for: tool))) |
| 82 | + .font(.caption).foregroundStyle(.secondary) |
| 83 | + .fixedSize(horizontal: false, vertical: true) |
| 84 | + .padding(.leading, 20) |
| 85 | + }.frame(maxWidth: .infinity, alignment: .leading) |
| 86 | + |
| 87 | + VStack(alignment: .leading, spacing: 6) { |
| 88 | + Text(text(.toolMenuPosition)).font(.caption).foregroundStyle(.secondary) |
| 89 | + positionPicker(selection: Binding( |
| 90 | + get: { preferences.mainMenuTools.contains(tool) }, |
| 91 | + set: { value in |
| 92 | + if value { preferences.mainMenuTools.insert(tool) } |
| 93 | + else { preferences.mainMenuTools.remove(tool) } |
| 94 | + } |
| 95 | + ), label: "\(text(tool.title)) — \(text(.toolMenuPosition))") |
| 96 | + .accessibilityIdentifier("fileTools.\(tool.rawValue).mainMenu") |
| 97 | + } |
| 98 | + .frame(width: placementWidth) |
| 99 | + .disabled(!enabled) |
| 100 | + } |
| 101 | + |
| 102 | + if tool == .move || tool == .permanentDelete { |
| 103 | + VStack(alignment: .leading, spacing: 12) { |
| 104 | + Divider() |
| 105 | + if tool == .move { |
| 106 | + HStack(spacing: 16) { |
| 107 | + Text(text(.moveHereMenuPosition)) |
| 108 | + .fixedSize(horizontal: false, vertical: true) |
| 109 | + Spacer(minLength: 0) |
| 110 | + positionPicker(selection: $preferences.moveHereInMainMenu, |
| 111 | + label: text(.moveHereMenuPosition)) |
| 112 | + .frame(width: placementWidth) |
| 113 | + .accessibilityIdentifier("fileTools.moveHere.mainMenu") |
| 114 | + } |
| 115 | + .help(text(.moveItemsHint)) |
| 116 | + } else { |
| 117 | + HStack(spacing: 16) { |
| 118 | + Text(text(.deleteConfirmation)) |
| 119 | + Spacer(minLength: 0) |
| 120 | + Picker(text(.deleteConfirmation), selection: $preferences.deleteConfirmation) { |
| 121 | + Text(text(.deleteRequireConfirmation)).tag(DeleteConfirmation.required) |
| 122 | + Text(text(.deleteSilently)).tag(DeleteConfirmation.silent) |
| 123 | + } |
| 124 | + .labelsHidden().pickerStyle(.segmented) |
| 125 | + .frame(width: language.resolved() == .chinese ? 250 : 290) |
| 126 | + .accessibilityIdentifier("fileTools.deleteConfirmation") |
| 127 | + } |
| 128 | + .help(text(.permanentDeleteHint)) |
| 129 | + } |
| 130 | + } |
| 131 | + .font(.system(size: 12)) |
| 132 | + .padding(.leading, 20) |
| 133 | + .disabled(!enabled) |
| 134 | + .opacity(enabled ? 1 : 0.6) |
| 135 | + } |
| 136 | + }.toolSettingsGroup() |
| 137 | + } |
| 138 | + |
| 139 | + private func positionPicker(selection: Binding<Bool>, label: String) -> some View { |
| 140 | + Picker(label, selection: selection) { |
| 141 | + Text(text(.toolSubmenu)).tag(false) |
| 142 | + Text(text(.toolMainMenu)).tag(true) |
| 143 | + } |
| 144 | + .labelsHidden().pickerStyle(.menu) |
| 145 | + // Menu placement is a regular choice, so keep its value in the |
| 146 | + // system control text color instead of the page's mint accent. |
| 147 | + .tint(.primary) |
| 148 | + .foregroundStyle(.primary) |
| 149 | + .accessibilityLabel(label) |
| 150 | + } |
| 151 | + |
| 152 | + private func hint(for tool: FileTool) -> FileMintTextKey { |
| 153 | + switch tool { |
| 154 | + case .copyNames: .copyNamesSettingsHint |
| 155 | + case .copyPaths: .copyPathsSettingsHint |
| 156 | + case .move: .moveSettingsHint |
| 157 | + case .permanentDelete: .deleteSettingsHint |
| 158 | + case .airDrop: .airDropHint |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +private extension View { |
| 164 | + func toolSettingsGroup() -> some View { |
| 165 | + padding(16) |
| 166 | + .frame(maxWidth: .infinity, alignment: .leading) |
| 167 | + .background(Color(nsColor: .controlBackgroundColor), |
| 168 | + in: RoundedRectangle(cornerRadius: 10, style: .continuous)) |
| 169 | + .overlay { |
| 170 | + RoundedRectangle(cornerRadius: 10, style: .continuous) |
| 171 | + .strokeBorder(Color(nsColor: .separatorColor).opacity(0.45), lineWidth: 0.5) |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments