| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- import SwiftUI
- import AppKit
- struct AppTileView: View {
- let app: LauncherApp
- let canAddWidget: Bool
- let onTap: () -> Void
- let onHide: (() -> Void)?
- let onPremiumRequired: (() -> Void)?
- @State private var isHovering = false
- @AppStorage("pinnedTileIDsData") private var pinnedTileIDsData = ""
- @AppStorage("statusBarAppIDsData") private var statusBarAppIDsData = ""
- @AppStorage("widgetAppIDsData") private var widgetAppIDsData = ""
- private let tileCornerRadius: CGFloat = 16
- var body: some View {
- ZStack(alignment: .topTrailing) {
- Button(action: onTap) {
- tileContent
- }
- .buttonStyle(.plain)
- if isPinned {
- Image(systemName: "pin.fill")
- .font(.system(size: 12, weight: .semibold))
- .foregroundStyle(.white.opacity(0.92))
- .frame(width: 26, height: 26)
- .background(
- Circle()
- .fill(Color.black.opacity(isHovering ? 0.35 : 0.2))
- )
- .overlay(
- Circle()
- .stroke(Color.white.opacity(isHovering ? 0.18 : 0.12), lineWidth: 1)
- )
- .shadow(color: .black.opacity(0.22), radius: 10, x: 0, y: 6)
- .padding(8)
- .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
- .allowsHitTesting(false)
- }
- if !app.isCreateNew {
- Menu {
- Button(isPinned ? "Unpin" : "Pin") { togglePin() }
- Divider()
- Button("Add to StatusBar") { addToStatusBar() }
- Button("Add to desktop") { addToDesktop() }
- Button("Add Widget") { addWidget() }
- Divider()
- Button("Remove from View") { onHide?() }
- } label: {
- Image(systemName: "ellipsis")
- .font(.system(size: 13, weight: .semibold))
- .rotationEffect(.degrees(90))
- .foregroundStyle(.white.opacity(isHovering ? 0.92 : 0.75))
- .frame(width: 26, height: 26)
- .background(
- Circle()
- .fill(Color.black.opacity(isHovering ? 0.35 : 0.2))
- )
- .overlay(
- Circle()
- .stroke(Color.white.opacity(isHovering ? 0.18 : 0.12), lineWidth: 1)
- )
- .shadow(color: .black.opacity(0.25), radius: 10, x: 0, y: 6)
- }
- .menuStyle(.borderlessButton)
- .modifier(HideMenuIndicatorIfAvailable())
- .padding(8)
- .opacity(isHovering ? 1 : 0.0)
- .animation(.easeOut(duration: 0.12), value: isHovering)
- }
- }
- .contextMenu {
- if !app.isCreateNew {
- Button(isPinned ? "Unpin" : "Pin") { togglePin() }
- Divider()
- Button("Add to StatusBar") { addToStatusBar() }
- Button("Add to desktop") { addToDesktop() }
- Button("Add Widget") { addWidget() }
- Divider()
- Button("Remove from View") { onHide?() }
- }
- }
- .onHover { hovering in
- isHovering = hovering
- }
- }
- private var tileContent: some View {
- VStack(spacing: 8) {
- AppIconView(
- app: app,
- size: 48,
- showAppBackground: false,
- iconPaddingFactor: 0.07
- )
- // Subtle depth like an app-store tile.
- .shadow(color: .black.opacity(0.14), radius: 8, x: 0, y: 4)
- Text(app.name)
- .font(.system(size: 14, weight: .semibold))
- .foregroundStyle(.white.opacity(0.95))
- .lineLimit(1)
- .truncationMode(.tail)
- .frame(maxWidth: .infinity)
- }
- .padding(.horizontal, 10)
- .padding(.vertical, 12)
- .background(
- RoundedRectangle(cornerRadius: tileCornerRadius, style: .continuous)
- .fill(Color.white.opacity(isHovering ? 0.06 : 0.025))
- )
- .overlay(
- RoundedRectangle(cornerRadius: tileCornerRadius, style: .continuous)
- .stroke(Color.white.opacity(isHovering ? 0.18 : 0.08), lineWidth: 1)
- )
- .shadow(
- color: .black.opacity(isHovering ? 0.18 : 0.10),
- radius: isHovering ? 16 : 10,
- x: 0,
- y: isHovering ? 10 : 6
- )
- // Ensure tiles look consistent across the adaptive grid.
- .frame(maxWidth: .infinity, minHeight: 94)
- .contentShape(RoundedRectangle(cornerRadius: tileCornerRadius, style: .continuous))
- .animation(.easeOut(duration: 0.12), value: isHovering)
- }
- private var isPinned: Bool {
- pinnedIDs.contains(app.id)
- }
- private var pinnedIDs: Set<UUID> {
- decodeUUIDSet(from: pinnedTileIDsData)
- }
- private func togglePin() {
- var updated = pinnedIDs
- if updated.contains(app.id) {
- updated.remove(app.id)
- } else {
- updated.insert(app.id)
- }
- pinnedTileIDsData = encodeUUIDSet(updated)
- }
- private func addToStatusBar() {
- var updated = decodeUUIDSet(from: statusBarAppIDsData)
- updated.insert(app.id)
- statusBarAppIDsData = encodeUUIDSet(updated)
- showAlert(title: "Added", message: "Added “\(app.name)” to Status Bar shortcuts.")
- }
- private func addWidget() {
- if canAddWidget {
- var updated = decodeUUIDSet(from: widgetAppIDsData)
- updated.insert(app.id)
- widgetAppIDsData = encodeUUIDSet(updated)
- }
- NotificationCenter.default.post(
- name: .openWidgetsPage,
- object: nil,
- userInfo: ["appID": app.id]
- )
- }
- private func addToDesktop() {
- let widgetVariants = WidgetTemplates.variants(for: app)
- guard let variant = widgetVariants.first else {
- showAlert(title: "Unavailable", message: "No desktop widget is available for this app.")
- return
- }
- var updated = decodeUUIDSet(from: widgetAppIDsData)
- updated.insert(app.id)
- widgetAppIDsData = encodeUUIDSet(updated)
- let instance = WidgetInstance(
- appID: app.id,
- variantID: variant.id,
- size: variant.size,
- origin: nil
- )
- DesktopWidgetWindowManager.shared.show(instance: instance, appProvider: { id in
- id == app.id ? app : nil
- })
- }
- private func decodeUUIDSet(from dataString: String) -> Set<UUID> {
- guard !dataString.isEmpty, let data = dataString.data(using: .utf8) else { return [] }
- do {
- let strings = try JSONDecoder().decode([String].self, from: data)
- return Set(strings.compactMap(UUID.init(uuidString:)))
- } catch {
- return []
- }
- }
- private func encodeUUIDSet(_ set: Set<UUID>) -> String {
- do {
- let strings = set.map(\.uuidString).sorted()
- let data = try JSONEncoder().encode(strings)
- return String(decoding: data, as: UTF8.self)
- } catch {
- return ""
- }
- }
- private func showAlert(title: String, message: String) {
- let alert = NSAlert()
- alert.messageText = title
- alert.informativeText = message
- alert.addButton(withTitle: "OK")
- alert.alertStyle = .informational
- alert.runModal()
- }
- }
- extension AppTileView {
- init(app: LauncherApp, onTap: @escaping () -> Void) {
- self.app = app
- self.canAddWidget = false
- self.onTap = onTap
- self.onHide = nil
- self.onPremiumRequired = nil
- }
- init(app: LauncherApp, onTap: @escaping () -> Void, onHide: @escaping () -> Void) {
- self.app = app
- self.canAddWidget = false
- self.onTap = onTap
- self.onHide = onHide
- self.onPremiumRequired = nil
- }
- init(
- app: LauncherApp,
- canAddWidget: Bool,
- onTap: @escaping () -> Void,
- onHide: @escaping () -> Void,
- onPremiumRequired: @escaping () -> Void
- ) {
- self.app = app
- self.canAddWidget = canAddWidget
- self.onTap = onTap
- self.onHide = onHide
- self.onPremiumRequired = onPremiumRequired
- }
- }
- private struct HideMenuIndicatorIfAvailable: ViewModifier {
- func body(content: Content) -> some View {
- if #available(macOS 13.0, *) {
- content.menuIndicator(.hidden)
- } else {
- content
- }
- }
- }
- struct AppDetailView: View {
- let app: LauncherApp
- @Environment(\.dismiss) private var dismiss
- var body: some View {
- ZStack {
- Color(red: 0.09, green: 0.09, blue: 0.11)
- .ignoresSafeArea()
- VStack(spacing: 14) {
- AppIconView(app: app, size: 76)
- Text(app.name)
- .font(.system(size: 22, weight: .semibold))
- .foregroundStyle(.white)
- Text(app.description)
- .font(.system(size: 14))
- .foregroundStyle(.white.opacity(0.82))
- Button("Close") {
- dismiss()
- }
- .buttonStyle(.borderedProminent)
- }
- .padding(24)
- }
- }
- }
|