DesktopWidgetView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 .books(let booksMode) = layoutMode {
  30. BooksDesktopWidgetView(app: app, mode: booksMode, isPreview: isPreview)
  31. } else if case .travel(let travelMode) = layoutMode {
  32. TravelDesktopWidgetView(app: app, mode: travelMode, isPreview: isPreview)
  33. } else if case .shopping(let shoppingMode) = layoutMode {
  34. ShoppingDesktopWidgetView(app: app, mode: shoppingMode, isPreview: isPreview)
  35. } else if case .youtube(let youtubeMode) = layoutMode {
  36. YouTubeDesktopWidgetView(app: app, mode: youtubeMode, isPreview: isPreview)
  37. } else if case .meet(let meetMode) = layoutMode {
  38. MeetDesktopWidgetView(app: app, mode: meetMode, isPreview: isPreview)
  39. } else if case .news(let newsMode) = layoutMode {
  40. NewsDesktopWidgetView(app: app, mode: newsMode, isPreview: isPreview)
  41. } else if case .forms(let formsMode) = layoutMode {
  42. FormsDesktopWidgetView(app: app, mode: formsMode, isPreview: isPreview)
  43. } else if case .docs(let docsMode) = layoutMode {
  44. DocsDesktopWidgetView(app: app, mode: docsMode, isPreview: isPreview)
  45. } else if case .sheets(let sheetsMode) = layoutMode {
  46. SheetsDesktopWidgetView(app: app, mode: sheetsMode, isPreview: isPreview)
  47. } else if case .slides(let slidesMode) = layoutMode {
  48. SlidesDesktopWidgetView(app: app, mode: slidesMode, isPreview: isPreview)
  49. } else if case .googlePlay(let playMode) = layoutMode {
  50. GooglePlayDesktopWidgetView(app: app, mode: playMode, isPreview: isPreview)
  51. } else if case .gemini(let geminiMode) = layoutMode {
  52. GeminiDesktopWidgetView(app: app, mode: geminiMode, isPreview: isPreview)
  53. } else if case .interactiveMap = layoutMode {
  54. MapsInteractiveWidgetView(app: app, widgetSize: size, isPreview: isPreview)
  55. } else {
  56. ZStack {
  57. RoundedRectangle(cornerRadius: 22, style: .continuous)
  58. .fill(
  59. LinearGradient(
  60. colors: [Color(red: 0.18, green: 0.24, blue: 0.31), Color(red: 0.18, green: 0.47, blue: 0.46)],
  61. startPoint: .topLeading,
  62. endPoint: .bottomTrailing
  63. )
  64. )
  65. .shadow(color: .black.opacity(0.25), radius: 14, x: 0, y: 8)
  66. .overlay(
  67. RoundedRectangle(cornerRadius: 22, style: .continuous)
  68. .stroke(Color.white.opacity(0.12), lineWidth: 1)
  69. )
  70. VStack(alignment: .leading, spacing: 12) {
  71. switch layoutMode {
  72. case .iconOnly(let title):
  73. iconOnly(title: title)
  74. case .actionsRow:
  75. if showHeader {
  76. header
  77. }
  78. rowActions(maxActions: 3)
  79. case .actionsGrid(let columns):
  80. if showHeader {
  81. header
  82. }
  83. gridActions(columns: columns, maxActions: maxActionsForSize)
  84. case .interactiveMap, .gmail, .drive, .keep, .translate, .earth, .googleSearch, .calendar, .books, .travel, .shopping, .youtube, .meet, .news, .forms, .docs, .sheets, .slides, .googlePlay, .gemini:
  85. EmptyView()
  86. }
  87. }
  88. .padding(16)
  89. }
  90. .clipShape(RoundedRectangle(cornerRadius: 22, style: .continuous))
  91. }
  92. }
  93. }
  94. private var header: some View {
  95. HStack(spacing: 10) {
  96. AppIconView(app: app, size: 34, showAppBackground: false, iconPaddingFactor: 0.07)
  97. VStack(alignment: .leading, spacing: 2) {
  98. Text(app.name)
  99. .font(.system(size: 16, weight: .bold))
  100. .foregroundStyle(.white.opacity(0.92))
  101. .lineLimit(1)
  102. }
  103. Spacer(minLength: 0)
  104. }
  105. }
  106. @ViewBuilder
  107. private func iconOnly(title: String) -> some View {
  108. let fontSize: CGFloat = {
  109. switch size {
  110. case .small: return 16
  111. case .medium: return 22
  112. case .large: return 18
  113. }
  114. }()
  115. let content = VStack(spacing: 10) {
  116. AppIconView(app: app, size: 44, showAppBackground: false, iconPaddingFactor: 0.05)
  117. Text(title)
  118. .font(.system(size: fontSize, weight: .bold))
  119. .foregroundStyle(.white.opacity(0.92))
  120. .lineLimit(1)
  121. }
  122. if isPreview || app.webURL == nil {
  123. content
  124. } else {
  125. Button(action: openBase) {
  126. content
  127. }
  128. .buttonStyle(.plain)
  129. }
  130. }
  131. private var maxActionsForSize: Int {
  132. switch size {
  133. case .small: return 3
  134. case .medium: return 4
  135. case .large: return 8
  136. }
  137. }
  138. private func rowActions(maxActions: Int) -> some View {
  139. let prefix = actions.prefix(maxActions)
  140. return HStack(spacing: 10) {
  141. ForEach(prefix) { action in
  142. actionChip(action: action)
  143. }
  144. }
  145. }
  146. private func gridActions(columns: Int, maxActions: Int) -> some View {
  147. let grid = Array(repeating: GridItem(.flexible(minimum: 0), spacing: 10), count: columns)
  148. return LazyVGrid(columns: grid, spacing: 10) {
  149. ForEach(actions.prefix(maxActions)) { action in
  150. actionTile(action: action)
  151. }
  152. }
  153. }
  154. private func actionChip(action: WidgetAction) -> some View {
  155. Button(action: { open(action: action) }) {
  156. HStack(spacing: 8) {
  157. Image(systemName: action.systemImage)
  158. .font(.system(size: 12, weight: .semibold))
  159. .foregroundStyle(foregroundColor(for: action))
  160. Text(action.title)
  161. .font(.system(size: 12, weight: .bold))
  162. .foregroundStyle(foregroundColor(for: action))
  163. .lineLimit(1)
  164. }
  165. .padding(.horizontal, 12)
  166. .padding(.vertical, 9)
  167. .background(
  168. RoundedRectangle(cornerRadius: 999, style: .continuous)
  169. .fill(backgroundColor(for: action))
  170. )
  171. .overlay(
  172. RoundedRectangle(cornerRadius: 999, style: .continuous)
  173. .stroke(Color.white.opacity(isPrimary(action) ? 0.22 : 0.12), lineWidth: 1)
  174. )
  175. }
  176. .buttonStyle(.plain)
  177. .disabled(isPreview || app.webURL == nil)
  178. // In the widget picker, we want clicks to toggle the whole card.
  179. // Ensure preview buttons do not intercept hit testing.
  180. .allowsHitTesting(!(isPreview || app.webURL == nil))
  181. .opacity((isPreview || app.webURL == nil) ? 0.6 : 1)
  182. }
  183. private func actionTile(action: WidgetAction) -> some View {
  184. Button(action: { open(action: action) }) {
  185. HStack(spacing: 10) {
  186. Image(systemName: action.systemImage)
  187. .font(.system(size: 12, weight: .semibold))
  188. .foregroundStyle(foregroundColor(for: action))
  189. Text(action.title)
  190. .font(.system(size: 12, weight: .bold))
  191. .foregroundStyle(foregroundColor(for: action))
  192. .lineLimit(1)
  193. Spacer(minLength: 0)
  194. }
  195. .padding(.horizontal, 12)
  196. .padding(.vertical, 10)
  197. .background(
  198. RoundedRectangle(cornerRadius: 14, style: .continuous)
  199. .fill(backgroundColor(for: action))
  200. )
  201. .overlay(
  202. RoundedRectangle(cornerRadius: 14, style: .continuous)
  203. .stroke(Color.white.opacity(isPrimary(action) ? 0.22 : 0.12), lineWidth: 1)
  204. )
  205. }
  206. .buttonStyle(.plain)
  207. .disabled(isPreview || app.webURL == nil)
  208. // In the widget picker, we want clicks to toggle the whole card.
  209. // Ensure preview buttons do not intercept hit testing.
  210. .allowsHitTesting(!(isPreview || app.webURL == nil))
  211. .opacity((isPreview || app.webURL == nil) ? 0.6 : 1)
  212. }
  213. private func openBase() {
  214. guard !isPreview, let url = app.webURL else { return }
  215. InAppBrowserWindowManager.shared.open(url: url, title: app.name)
  216. }
  217. private func open(action: WidgetAction) {
  218. guard !isPreview, let base = app.webURL else { return }
  219. let target = WidgetDeepLinkURL.resolved(base: base, actionPath: action.urlPath, accountSlot: gmailAccountSlot)
  220. if target.scheme?.lowercased() == "mailto" {
  221. NSWorkspace.shared.open(target)
  222. } else {
  223. InAppBrowserWindowManager.shared.open(url: target, title: app.name)
  224. }
  225. }
  226. private func isPrimary(_ action: WidgetAction) -> Bool {
  227. actions.first?.id == action.id
  228. }
  229. private func backgroundColor(for action: WidgetAction) -> Color {
  230. isPrimary(action) ? Color.white.opacity(0.38) : Color.black.opacity(0.66)
  231. }
  232. private func foregroundColor(for action: WidgetAction) -> Color {
  233. isPrimary(action) ? Color.white.opacity(0.95) : Color.white.opacity(0.88)
  234. }
  235. }