Jotley / app / macOS / Views / NoteDetailPlaceholderView+ActionsPopoverMenuItem.swift
| 1 | import SwiftUI |
| 2 | import AppKit |
| 3 | |
| 4 | extension NoteDetailPlaceholderView { |
| 5 | struct MenuItemView: View { |
| 6 | let icon: String |
| 7 | let title: String |
| 8 | var assetIconName: String? = nil |
| 9 | var subtitle: String? = nil |
| 10 | var iconColor: Color? = nil |
| 11 | var hasSubmenu: Bool = false |
| 12 | var submenuExpanded: Bool = false |
| 13 | var shortcut: String? = nil |
| 14 | var badge: String? = nil |
| 15 | var disabled: Bool = false |
| 16 | var isDestructive: Bool = false |
| 17 | var onHover: ((Bool) -> Void)? = nil |
| 18 | let action: () -> Void |
| 19 | |
| 20 | @State private var isHovered = false |
| 21 | |
| 22 | var body: some View { |
| 23 | Button { |
| 24 | if !disabled { |
| 25 | action() |
| 26 | } |
| 27 | } label: { |
| 28 | HStack(spacing: 12) { |
| 29 | if let assetIconName { |
| 30 | Image(assetIconName) |
| 31 | .resizable() |
| 32 | .scaledToFit() |
| 33 | .frame(width: 16, height: 16) |
| 34 | .opacity(iconColor == nil ? 0.8 : 1.0) |
| 35 | .frame(width: 20) |
| 36 | } else { |
| 37 | Image(systemName: icon) |
| 38 | .font(.system(size: 16)) |
| 39 | .foregroundStyle(iconColor ?? (isDestructive ? Color.red : Color.themePrimaryAccent)) |
| 40 | .frame(width: 20) |
| 41 | } |
| 42 | |
| 43 | Text(title) |
| 44 | .font(.system(size: 13)) |
| 45 | .foregroundStyle(isDestructive ? Color.red : Color.themeTextPrimary) |
| 46 | .lineLimit(1) |
| 47 | .truncationMode(.tail) |
| 48 | |
| 49 | if let badge = badge { |
| 50 | Text(badge) |
| 51 | .font(.system(size: 10, weight: .medium)) |
| 52 | .foregroundStyle(Color.primary) |
| 53 | .padding(.horizontal, 6) |
| 54 | .padding(.vertical, 2) |
| 55 | .background(Color.primary.opacity(0.07)) |
| 56 | .cornerRadius(4) |
| 57 | } |
| 58 | |
| 59 | Spacer() |
| 60 | |
| 61 | if let subtitle = subtitle { |
| 62 | Text(subtitle) |
| 63 | .font(.system(size: 12)) |
| 64 | .foregroundColor(.secondary) |
| 65 | } |
| 66 | |
| 67 | if hasSubmenu { |
| 68 | Image(systemName: submenuExpanded ? "chevron.down" : "chevron.right") |
| 69 | .font(.system(size: 10)) |
| 70 | .foregroundColor(.secondary) |
| 71 | } |
| 72 | |
| 73 | if let shortcut = shortcut { |
| 74 | Text(shortcut) |
| 75 | .font(.system(size: 12)) |
| 76 | .foregroundColor(.secondary) |
| 77 | } |
| 78 | } |
| 79 | .padding(.horizontal, 12) |
| 80 | .padding(.vertical, 8) |
| 81 | .background(isHovered ? Color.themeSelectionBackground : Color.clear) |
| 82 | .cornerRadius(8) |
| 83 | .contentShape(Rectangle()) |
| 84 | } |
| 85 | .buttonStyle(PlainButtonStyle()) |
| 86 | .opacity(disabled ? 0.5 : 1.0) |
| 87 | .onHover { hovering in |
| 88 | withAnimation(.easeInOut(duration: 0.1)) { |
| 89 | isHovered = hovering |
| 90 | } |
| 91 | onHover?(hovering) |
| 92 | if hovering { |
| 93 | NSCursor.pointingHand.set() |
| 94 | } else { |
| 95 | NSCursor.arrow.set() |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// Shared material for the main More menu and its template submenu. |
| 103 | struct MacNoteMenuSurface: ViewModifier { |
| 104 | @Environment(\.accessibilityReduceTransparency) private var reduceTransparency |
| 105 | |
| 106 | func body(content: Content) -> some View { |
| 107 | content.background { |
| 108 | let shape = RoundedRectangle(cornerRadius: 14, style: .continuous) |
| 109 | if reduceTransparency { |
| 110 | shape.fill(Color(NSColor.controlBackgroundColor)) |
| 111 | } else { |
| 112 | Color.clear.glassEffect(.regular, in: shape) |
| 113 | .overlay { |
| 114 | shape.strokeBorder(LinearGradient(colors: [.white.opacity(0.6), .white.opacity(0.08), .white.opacity(0.3)], |
| 115 | startPoint: .topLeading, endPoint: .bottomTrailing), lineWidth: 1) |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | .shadow(color: .black.opacity(0.14), radius: 12, y: 5) |
| 120 | } |
| 121 | } |