DesktopWidgetView.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import SwiftUI
  2. import AppKit
  3. struct DesktopWidgetView: View {
  4. let app: LauncherApp
  5. let variant: WidgetVariant
  6. var isPreview: Bool = false
  7. /// Gmail: account slot for `/mail/u/{{u}}/…` in URLs. Ignored for other apps.
  8. var gmailAccountSlot: Int = 0
  9. private var actions: [WidgetAction] { variant.actions }
  10. private var layoutMode: WidgetLayoutMode { variant.layoutMode }
  11. private var showHeader: Bool { variant.showHeader }
  12. private var size: WidgetSize { variant.size }
  13. var body: some View {
  14. Group {
  15. if case .gmail(let gmailMode) = layoutMode {
  16. GmailDesktopWidgetView(app: app, mode: gmailMode, gmailAccountSlot: gmailAccountSlot, isPreview: isPreview)
  17. } else if case .drive(let driveMode) = layoutMode {
  18. DriveDesktopWidgetView(app: app, mode: driveMode, isPreview: isPreview)
  19. } else if case .keep(let keepMode) = layoutMode {
  20. KeepDesktopWidgetView(app: app, mode: keepMode, isPreview: isPreview)
  21. } else if case .translate(let translateMode) = layoutMode {
  22. TranslateDesktopWidgetView(app: app, mode: translateMode, isPreview: isPreview)
  23. } else if case .earth(let earthMode) = layoutMode {
  24. EarthDesktopWidgetView(app: app, mode: earthMode, isPreview: isPreview)
  25. } else if case .googleSearch(let searchMode) = layoutMode {
  26. GoogleSearchDesktopWidgetView(app: app, mode: searchMode, isPreview: isPreview)
  27. } else if case .calendar(let calendarMode) = layoutMode {
  28. CalendarDesktopWidgetView(app: app, mode: calendarMode, isPreview: isPreview)
  29. } else if case .interactiveMap = layoutMode {
  30. MapsInteractiveWidgetView(app: app, widgetSize: size, isPreview: isPreview)
  31. } else {
  32. ZStack {
  33. RoundedRectangle(cornerRadius: 22, style: .continuous)
  34. .fill(
  35. LinearGradient(
  36. colors: [Color(red: 0.18, green: 0.24, blue: 0.31), Color(red: 0.18, green: 0.47, blue: 0.46)],
  37. startPoint: .topLeading,
  38. endPoint: .bottomTrailing
  39. )
  40. )
  41. .shadow(color: .black.opacity(0.25), radius: 14, x: 0, y: 8)
  42. .overlay(
  43. RoundedRectangle(cornerRadius: 22, style: .continuous)
  44. .stroke(Color.white.opacity(0.12), lineWidth: 1)
  45. )
  46. VStack(alignment: .leading, spacing: 12) {
  47. switch layoutMode {
  48. case .iconOnly(let title):
  49. iconOnly(title: title)
  50. case .actionsRow:
  51. if showHeader {
  52. header
  53. }
  54. rowActions(maxActions: 3)
  55. case .actionsGrid(let columns):
  56. if showHeader {
  57. header
  58. }
  59. gridActions(columns: columns, maxActions: maxActionsForSize)
  60. case .interactiveMap, .gmail, .drive, .keep, .translate, .earth, .googleSearch, .calendar:
  61. EmptyView()
  62. }
  63. }
  64. .padding(16)
  65. }
  66. .clipShape(RoundedRectangle(cornerRadius: 22, style: .continuous))
  67. }
  68. }
  69. }
  70. private var header: some View {
  71. HStack(spacing: 10) {
  72. AppIconView(app: app, size: 34, showAppBackground: false, iconPaddingFactor: 0.07)
  73. VStack(alignment: .leading, spacing: 2) {
  74. Text(app.name)
  75. .font(.system(size: 16, weight: .bold))
  76. .foregroundStyle(.white.opacity(0.92))
  77. .lineLimit(1)
  78. }
  79. Spacer(minLength: 0)
  80. }
  81. }
  82. @ViewBuilder
  83. private func iconOnly(title: String) -> some View {
  84. let fontSize: CGFloat = {
  85. switch size {
  86. case .small: return 16
  87. case .medium: return 22
  88. case .large: return 18
  89. }
  90. }()
  91. let content = VStack(spacing: 10) {
  92. AppIconView(app: app, size: 44, showAppBackground: false, iconPaddingFactor: 0.05)
  93. Text(title)
  94. .font(.system(size: fontSize, weight: .bold))
  95. .foregroundStyle(.white.opacity(0.92))
  96. .lineLimit(1)
  97. }
  98. if isPreview || app.webURL == nil {
  99. content
  100. } else {
  101. Button(action: openBase) {
  102. content
  103. }
  104. .buttonStyle(.plain)
  105. }
  106. }
  107. private var maxActionsForSize: Int {
  108. switch size {
  109. case .small: return 3
  110. case .medium: return 4
  111. case .large: return 8
  112. }
  113. }
  114. private func rowActions(maxActions: Int) -> some View {
  115. let prefix = actions.prefix(maxActions)
  116. return HStack(spacing: 10) {
  117. ForEach(prefix) { action in
  118. actionChip(action: action)
  119. }
  120. }
  121. }
  122. private func gridActions(columns: Int, maxActions: Int) -> some View {
  123. let grid = Array(repeating: GridItem(.flexible(minimum: 0), spacing: 10), count: columns)
  124. return LazyVGrid(columns: grid, spacing: 10) {
  125. ForEach(actions.prefix(maxActions)) { action in
  126. actionTile(action: action)
  127. }
  128. }
  129. }
  130. private func actionChip(action: WidgetAction) -> some View {
  131. Button(action: { open(action: action) }) {
  132. HStack(spacing: 8) {
  133. Image(systemName: action.systemImage)
  134. .font(.system(size: 12, weight: .semibold))
  135. .foregroundStyle(foregroundColor(for: action))
  136. Text(action.title)
  137. .font(.system(size: 12, weight: .bold))
  138. .foregroundStyle(foregroundColor(for: action))
  139. .lineLimit(1)
  140. }
  141. .padding(.horizontal, 12)
  142. .padding(.vertical, 9)
  143. .background(
  144. RoundedRectangle(cornerRadius: 999, style: .continuous)
  145. .fill(backgroundColor(for: action))
  146. )
  147. .overlay(
  148. RoundedRectangle(cornerRadius: 999, style: .continuous)
  149. .stroke(Color.white.opacity(isPrimary(action) ? 0.22 : 0.12), lineWidth: 1)
  150. )
  151. }
  152. .buttonStyle(.plain)
  153. .disabled(isPreview || app.webURL == nil)
  154. // In the widget picker, we want clicks to toggle the whole card.
  155. // Ensure preview buttons do not intercept hit testing.
  156. .allowsHitTesting(!(isPreview || app.webURL == nil))
  157. .opacity((isPreview || app.webURL == nil) ? 0.6 : 1)
  158. }
  159. private func actionTile(action: WidgetAction) -> some View {
  160. Button(action: { open(action: action) }) {
  161. HStack(spacing: 10) {
  162. Image(systemName: action.systemImage)
  163. .font(.system(size: 12, weight: .semibold))
  164. .foregroundStyle(foregroundColor(for: action))
  165. Text(action.title)
  166. .font(.system(size: 12, weight: .bold))
  167. .foregroundStyle(foregroundColor(for: action))
  168. .lineLimit(1)
  169. Spacer(minLength: 0)
  170. }
  171. .padding(.horizontal, 12)
  172. .padding(.vertical, 10)
  173. .background(
  174. RoundedRectangle(cornerRadius: 14, style: .continuous)
  175. .fill(backgroundColor(for: action))
  176. )
  177. .overlay(
  178. RoundedRectangle(cornerRadius: 14, style: .continuous)
  179. .stroke(Color.white.opacity(isPrimary(action) ? 0.22 : 0.12), lineWidth: 1)
  180. )
  181. }
  182. .buttonStyle(.plain)
  183. .disabled(isPreview || app.webURL == nil)
  184. // In the widget picker, we want clicks to toggle the whole card.
  185. // Ensure preview buttons do not intercept hit testing.
  186. .allowsHitTesting(!(isPreview || app.webURL == nil))
  187. .opacity((isPreview || app.webURL == nil) ? 0.6 : 1)
  188. }
  189. private func openBase() {
  190. guard !isPreview, let url = app.webURL else { return }
  191. InAppBrowserWindowManager.shared.open(url: url, title: app.name)
  192. }
  193. private func open(action: WidgetAction) {
  194. guard !isPreview, let base = app.webURL else { return }
  195. let target = WidgetDeepLinkURL.resolved(base: base, actionPath: action.urlPath, accountSlot: gmailAccountSlot)
  196. if target.scheme?.lowercased() == "mailto" {
  197. NSWorkspace.shared.open(target)
  198. } else {
  199. InAppBrowserWindowManager.shared.open(url: target, title: app.name)
  200. }
  201. }
  202. private func isPrimary(_ action: WidgetAction) -> Bool {
  203. actions.first?.id == action.id
  204. }
  205. private func backgroundColor(for action: WidgetAction) -> Color {
  206. isPrimary(action) ? Color.white.opacity(0.38) : Color.black.opacity(0.66)
  207. }
  208. private func foregroundColor(for action: WidgetAction) -> Color {
  209. isPrimary(action) ? Color.white.opacity(0.95) : Color.white.opacity(0.88)
  210. }
  211. }