|
1 | | -// DirectoryContextMenu.swift |
2 | | -// MiMiNavigator |
3 | | -// |
4 | | -// Created by Iakov Senatov on 08.10.2025. |
5 | | -// Copyright © 2025-2026 Senatov. All rights reserved. |
6 | | -// Description: Context menu for directories - Finder-style layout |
| 1 | + // DirectoryContextMenu.swift |
| 2 | + // MiMiNavigator |
| 3 | + // |
| 4 | + // Created by Iakov Senatov on 08.10.2025. |
| 5 | + // Copyright © 2025-2026 Senatov. All rights reserved. |
| 6 | + // Description: Context menu for directories - Finder-style layout |
7 | 7 |
|
8 | | -import FavoritesKit |
9 | | -import SwiftUI |
10 | | -import FileModelKit |
| 8 | + import FavoritesKit |
| 9 | + import SwiftUI |
| 10 | + import FileModelKit |
11 | 11 |
|
12 | | -/// Context menu for directory items. |
13 | | -/// Matches Finder's context menu structure for folders. |
14 | | -struct DirectoryContextMenu: View { |
15 | | - let file: CustomFile |
16 | | - let panelSide: PanelSide |
17 | | - let onAction: (DirectoryAction) -> Void |
18 | | - @State private var userFavorites = UserFavoritesStore.shared |
| 12 | + /// Context menu for directory items. |
| 13 | + /// Matches Finder's context menu structure for folders. |
| 14 | + struct DirectoryContextMenu: View { |
| 15 | + let file: CustomFile |
| 16 | + let panelSide: PanelSide |
| 17 | + let onAction: (DirectoryAction) -> Void |
| 18 | + @State private var userFavorites = UserFavoritesStore.shared |
19 | 19 |
|
20 | | - init(file: CustomFile, panelSide: PanelSide, onAction: @escaping (DirectoryAction) -> Void) { |
21 | | - self.file = file |
22 | | - self.panelSide = panelSide |
23 | | - self.onAction = onAction |
24 | | - } |
25 | | - |
26 | | - var body: some View { |
27 | | - Group { |
28 | | - // ═══════════════════════════════════════════ |
29 | | - // SECTION 1: Navigation (directory-specific) |
30 | | - // ═══════════════════════════════════════════ |
31 | | - menuButton(.open) |
32 | | - menuButton(.openInNewTab) |
33 | | - menuButton(.openInFinder) |
34 | | - menuButton(.openInTerminal) |
35 | | - menuButton(.viewLister) |
36 | | - |
37 | | - Divider() |
38 | | - |
39 | | - // ═══════════════════════════════════════════ |
40 | | - // SECTION 2: Edit actions (clipboard) |
41 | | - // ═══════════════════════════════════════════ |
42 | | - menuButton(.cut) |
43 | | - menuButton(.copy) |
44 | | - menuButton(.copyAsPathname) |
45 | | - menuButton(.paste) |
46 | | - menuButton(.duplicate) |
47 | | - |
48 | | - Divider() |
49 | | - |
50 | | - // ═══════════════════════════════════════════ |
51 | | - // SECTION 3: Operations |
52 | | - // ═══════════════════════════════════════════ |
53 | | - menuButton(.compress) |
54 | | - menuButton(.share) |
55 | | - |
56 | | - Divider() |
57 | | - |
58 | | - // ═══════════════════════════════════════════ |
59 | | - // SECTION 4: Rename & Delete (danger zone) |
60 | | - // ═══════════════════════════════════════════ |
61 | | - menuButton(.rename) |
62 | | - menuButton(.delete) |
63 | | - |
64 | | - Divider() |
65 | | - |
66 | | - // ═══════════════════════════════════════════ |
67 | | - // SECTION 5: Info |
68 | | - // ═══════════════════════════════════════════ |
69 | | - menuButton(.getInfo) |
| 20 | + init(file: CustomFile, panelSide: PanelSide, onAction: @escaping (DirectoryAction) -> Void) { |
| 21 | + self.file = file |
| 22 | + self.panelSide = panelSide |
| 23 | + self.onAction = onAction |
| 24 | + } |
| 25 | + |
| 26 | + var body: some View { |
| 27 | + Group { |
| 28 | + // ═══════════════════════════════════════════ |
| 29 | + // SECTION 1: Navigation (directory-specific) |
| 30 | + // ═══════════════════════════════════════════ |
| 31 | + menuButton(.open) |
| 32 | + menuButton(.openInNewTab) |
| 33 | + menuButton(.openInFinder) |
| 34 | + menuButton(.openInTerminal) |
| 35 | + menuButton(.viewLister) |
| 36 | + |
| 37 | + Divider() |
| 38 | + |
| 39 | + // ═══════════════════════════════════════════ |
| 40 | + // SECTION 2: Edit actions (clipboard) |
| 41 | + // ═══════════════════════════════════════════ |
| 42 | + menuButton(.cut) |
| 43 | + menuButton(.copy) |
| 44 | + menuButton(.copyAsPathname) |
| 45 | + menuButton(.paste) |
| 46 | + menuButton(.duplicate) |
| 47 | + |
| 48 | + Divider() |
| 49 | + |
| 50 | + // ═══════════════════════════════════════════ |
| 51 | + // SECTION 3: Operations |
| 52 | + // ═══════════════════════════════════════════ |
| 53 | + menuButton(.compress) |
| 54 | + menuButton(.share) |
| 55 | + |
| 56 | + Divider() |
| 57 | + |
| 58 | + // ═══════════════════════════════════════════ |
| 59 | + // SECTION 4: Rename & Delete (danger zone) |
| 60 | + // ═══════════════════════════════════════════ |
| 61 | + menuButton(.rename) |
| 62 | + menuButton(.delete) |
| 63 | + |
| 64 | + Divider() |
| 65 | + |
| 66 | + // ═══════════════════════════════════════════ |
| 67 | + // SECTION 5: Info |
| 68 | + // ═══════════════════════════════════════════ |
| 69 | + menuButton(.getInfo) |
70 | 70 |
|
71 | | - Divider() |
| 71 | + Divider() |
72 | 72 |
|
73 | | - // ═══════════════════════════════════════════ |
74 | | - // SECTION 6: Cross-panel |
75 | | - // ═══════════════════════════════════════════ |
76 | | - menuButton(.openOnOtherPanel) |
| 73 | + // ═══════════════════════════════════════════ |
| 74 | + // SECTION 6: Cross-panel |
| 75 | + // ═══════════════════════════════════════════ |
| 76 | + menuButton(.openOnOtherPanel) |
77 | 77 |
|
78 | | - Divider() |
| 78 | + Divider() |
79 | 79 |
|
80 | | - // ═══════════════════════════════════════════ |
81 | | - // SECTION 7: Favorites |
82 | | - // ═══════════════════════════════════════════ |
83 | | - favoritesToggleButton |
| 80 | + // ═══════════════════════════════════════════ |
| 81 | + // SECTION 7: Favorites |
| 82 | + // ═══════════════════════════════════════════ |
| 83 | + favoritesToggleButton |
| 84 | + } |
84 | 85 | } |
85 | | - } |
86 | 86 |
|
87 | | - // MARK: - Add / Remove Favorites toggle |
| 87 | + // MARK: - Add / Remove Favorites toggle |
88 | 88 |
|
89 | | - @ViewBuilder |
90 | | - private var favoritesToggleButton: some View { |
91 | | - let isInFavorites = userFavorites.contains(path: file.pathStr) |
92 | | - if isInFavorites { |
93 | | - Button(role: .destructive) { |
94 | | - userFavorites.remove(path: file.pathStr) |
95 | | - log.info("[Favorites] directory removed via context menu: \(file.pathStr)") |
96 | | - } label: { |
97 | | - Label("Remove from Favorites", systemImage: "star.slash.fill") |
| 89 | + @ViewBuilder |
| 90 | + private var favoritesToggleButton: some View { |
| 91 | + let isInFavorites = userFavorites.contains(url: file.urlValue) |
| 92 | + if isInFavorites { |
| 93 | + Button(role: .destructive) { |
| 94 | + userFavorites.remove(url: file.urlValue) |
| 95 | + log.info("[Favorites] directory removed via context menu: \(file.urlValue.path)") |
| 96 | + } label: { |
| 97 | + Label("Remove from Favorites", systemImage: "star.slash.fill") |
| 98 | + } |
| 99 | + } else { |
| 100 | + menuButton(.addToFavorites) |
98 | 101 | } |
99 | | - } else { |
100 | | - menuButton(.addToFavorites) |
101 | 102 | } |
102 | | - } |
103 | 103 |
|
104 | | - // MARK: - Menu Button Builder |
| 104 | + // MARK: - Menu Button Builder |
105 | 105 |
|
106 | | - @ViewBuilder |
107 | | - private func menuButton(_ action: DirectoryAction) -> some View { |
108 | | - Button { |
109 | | - log.debug("\(#function) action=\(action.rawValue) dir='\(file.nameStr)'") |
110 | | - onAction(action) |
111 | | - } label: { |
112 | | - Label { |
113 | | - HStack { |
114 | | - Text(action.title) |
115 | | - Spacer() |
116 | | - if let shortcut = action.shortcutHint { |
117 | | - Text(shortcut) |
118 | | - .font(.caption) |
119 | | - .foregroundStyle(.secondary) |
| 106 | + @ViewBuilder |
| 107 | + private func menuButton(_ action: DirectoryAction) -> some View { |
| 108 | + Button { |
| 109 | + log.debug("\(#function) action=\(action.rawValue) dir='\(file.nameStr)'") |
| 110 | + onAction(action) |
| 111 | + } label: { |
| 112 | + Label { |
| 113 | + HStack { |
| 114 | + Text(action.title) |
| 115 | + Spacer() |
| 116 | + if let shortcut = action.shortcutHint { |
| 117 | + Text(shortcut) |
| 118 | + .font(.caption) |
| 119 | + .foregroundStyle(.secondary) |
| 120 | + } |
120 | 121 | } |
| 122 | + } icon: { |
| 123 | + Image(systemName: action.systemImage) |
121 | 124 | } |
122 | | - } icon: { |
123 | | - Image(systemName: action.systemImage) |
124 | 125 | } |
| 126 | + .disabled(isActionDisabled(action)) |
125 | 127 | } |
126 | | - .disabled(isActionDisabled(action)) |
127 | | - } |
128 | | - |
129 | | - // MARK: - Action State |
130 | | - |
131 | | - private func isActionDisabled(_ action: DirectoryAction) -> Bool { |
132 | | - switch action { |
133 | | - case .paste: |
134 | | - return !ClipboardManager.shared.hasContent |
135 | | - default: |
136 | | - return false |
| 128 | + |
| 129 | + // MARK: - Action State |
| 130 | + |
| 131 | + private func isActionDisabled(_ action: DirectoryAction) -> Bool { |
| 132 | + switch action { |
| 133 | + case .paste: |
| 134 | + return !ClipboardManager.shared.hasContent |
| 135 | + default: |
| 136 | + return false |
| 137 | + } |
137 | 138 | } |
138 | 139 | } |
139 | | -} |
140 | 140 |
|
141 | | -// MARK: - Preview |
142 | | -#Preview { |
143 | | - VStack { |
144 | | - Text("Right-click for directory menu") |
145 | | - } |
146 | | - .frame(width: 300, height: 200) |
147 | | - .contextMenu { |
148 | | - DirectoryContextMenu( |
149 | | - file: CustomFile(path: "/Users"), |
150 | | - panelSide: .left, |
151 | | - onAction: { action in |
152 | | - log.debug("Action: \(action)") |
153 | | - } |
154 | | - ) |
| 141 | + // MARK: - Preview |
| 142 | + #Preview { |
| 143 | + VStack { |
| 144 | + Text("Right-click for directory menu") |
| 145 | + } |
| 146 | + .frame(width: 300, height: 200) |
| 147 | + .contextMenu { |
| 148 | + DirectoryContextMenu( |
| 149 | + file: CustomFile(path: "/Users"), |
| 150 | + panelSide: .left, |
| 151 | + onAction: { action in |
| 152 | + log.debug("Action: \(action)") |
| 153 | + } |
| 154 | + ) |
| 155 | + } |
155 | 156 | } |
156 | | -} |
0 commit comments