| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- import SwiftUI
- import AppKit
- import UniformTypeIdentifiers
- struct LauncherRootView: View {
- enum LayoutMode {
- case grid
- case panel
- }
- @State private var query = ""
- @State private var selectedApp: LauncherApp?
- @State private var showingPremiumScreen = false
- @State private var showingCreateAppSheet = false
- @State private var customApps: [LauncherApp] = []
- @State private var appOrder: [UUID] = []
- @State private var draggedAppID: UUID?
- @State private var layoutMode: LayoutMode = .grid
- @AppStorage("customLauncherAppsData") private var customAppsData = ""
- @AppStorage("launcherAppOrderData") private var appOrderData = ""
- @AppStorage("pinnedTileIDsData") private var pinnedTileIDsData = ""
- private var apps: [LauncherApp] {
- LauncherApp.sampleApps + customApps + [LauncherApp.createNewTile]
- }
- private var columns: [GridItem] {
- [GridItem(.adaptive(minimum: 120, maximum: 145), spacing: 22)]
- }
- private var filteredApps: [LauncherApp] {
- let q = query.trimmingCharacters(in: .whitespacesAndNewlines)
- guard !q.isEmpty else { return orderedApps }
- return orderedApps.filter { $0.name.localizedCaseInsensitiveContains(q) }
- }
- private var orderedApps: [LauncherApp] {
- guard !apps.isEmpty else { return [] }
- let defaultIndexByID = Dictionary(
- uniqueKeysWithValues: apps.enumerated().map { ($0.element.id, $0.offset) }
- )
- let rankByID = Dictionary(
- uniqueKeysWithValues: appOrder.enumerated().map { ($0.element, $0.offset) }
- )
- let pinnedIDs = decodeUUIDSet(from: pinnedTileIDsData)
- return apps.sorted { lhs, rhs in
- if lhs.isCreateNew != rhs.isCreateNew {
- return rhs.isCreateNew
- }
- let leftPinned = pinnedIDs.contains(lhs.id)
- let rightPinned = pinnedIDs.contains(rhs.id)
- if leftPinned != rightPinned {
- return leftPinned
- }
- let leftRank = rankByID[lhs.id] ?? Int.max
- let rightRank = rankByID[rhs.id] ?? Int.max
- if leftRank != rightRank {
- return leftRank < rightRank
- }
- return (defaultIndexByID[lhs.id] ?? Int.max) < (defaultIndexByID[rhs.id] ?? Int.max)
- }
- }
- var body: some View {
- ZStack {
- VisualEffectBlur(material: .hudWindow, blendingMode: .behindWindow)
- .ignoresSafeArea()
- Color.black.opacity(0.22)
- .ignoresSafeArea()
- VStack(spacing: 16) {
- SearchHeader(
- query: $query,
- isPanelMode: layoutMode == .panel,
- onToggleLayout: {
- layoutMode = layoutMode == .grid ? .panel : .grid
- },
- onOpenPremium: {
- showingPremiumScreen = true
- },
- onOpenGoogle: {
- if let url = URL(string: "https://www.google.com") {
- InAppBrowserWindowManager.shared.open(url: url, title: "Google")
- }
- }
- )
- .padding(.top, 12)
- PromoBanner(
- onCloseTap: { showingPremiumScreen = true },
- onUpgradeTap: { showingPremiumScreen = true }
- )
- .padding(.bottom, 8)
- .zIndex(1)
- if filteredApps.isEmpty {
- VStack(spacing: 10) {
- Image(systemName: "magnifyingglass")
- .font(.system(size: 28, weight: .semibold))
- .foregroundStyle(.white.opacity(0.75))
- Text("No apps found")
- .font(.title3.weight(.semibold))
- .foregroundStyle(.white.opacity(0.9))
- Text("Try a different search term.")
- .font(.subheadline)
- .foregroundStyle(.white.opacity(0.7))
- }
- .frame(maxWidth: .infinity, maxHeight: .infinity)
- } else {
- if layoutMode == .panel {
- IconPanelView(apps: filteredApps) { app in
- handleAppTap(app)
- }
- } else {
- ScrollView {
- LazyVGrid(columns: columns, spacing: 20) {
- ForEach(filteredApps) { app in
- AppTileView(app: app) {
- handleAppTap(app)
- }
- .onDrag {
- draggedAppID = app.id
- return NSItemProvider(object: app.id.uuidString as NSString)
- }
- .onDrop(
- of: [UTType.text],
- delegate: TileReorderDropDelegate(
- destinationAppID: app.id,
- draggedAppID: $draggedAppID,
- onMove: moveApp
- )
- )
- }
- }
- .padding(.top, 10)
- .padding(.horizontal, 14)
- .padding(.bottom, 18)
- }
- }
- }
- }
- .padding(.horizontal, 10)
- .padding(.bottom, 8)
- }
- .sheet(item: $selectedApp) { app in
- AppDetailView(app: app)
- .frame(minWidth: 360, minHeight: 220)
- }
- .sheet(isPresented: $showingPremiumScreen) {
- PremiumFeaturesView()
- .frame(minWidth: 560, minHeight: 690)
- }
- .sheet(isPresented: $showingCreateAppSheet) {
- CreateAppSheetView { input in
- addCustomApp(name: input.name, url: input.url)
- }
- .frame(minWidth: 760, minHeight: 420)
- }
- .onAppear {
- loadCustomAppsFromStorage()
- loadAppOrderFromStorage()
- normalizeAppOrderAndPersist()
- }
- }
- private func handleAppTap(_ app: LauncherApp) {
- if app.isCreateNew {
- showingCreateAppSheet = true
- return
- }
- if let webURL = app.webURL {
- InAppBrowserWindowManager.shared.open(url: webURL, title: app.name)
- return
- }
- selectedApp = app
- }
- private func addCustomApp(name: String, url: URL) {
- let newApp = LauncherApp(
- name: name,
- assetIconName: "",
- fallbackSymbolName: "globe",
- description: "Custom app shortcut.",
- isCreateNew: false,
- webURL: url
- )
- customApps.append(newApp)
- saveCustomAppsToStorage()
- normalizeAppOrderAndPersist()
- }
- private func loadCustomAppsFromStorage() {
- guard !customAppsData.isEmpty else {
- customApps = []
- return
- }
- guard let data = customAppsData.data(using: .utf8) else {
- customApps = []
- return
- }
- do {
- let persisted = try JSONDecoder().decode([PersistedCustomApp].self, from: data)
- customApps = persisted.compactMap { item in
- guard let parsedURL = URL(string: item.urlString) else { return nil }
- return LauncherApp(
- id: item.id,
- name: item.name,
- assetIconName: "",
- fallbackSymbolName: "globe",
- description: "Custom app shortcut.",
- isCreateNew: false,
- webURL: parsedURL
- )
- }
- } catch {
- customApps = []
- }
- }
- private func saveCustomAppsToStorage() {
- let persisted = customApps.compactMap { app -> PersistedCustomApp? in
- guard !app.isCreateNew, let urlString = app.webURL?.absoluteString else { return nil }
- return PersistedCustomApp(id: app.id, name: app.name, urlString: urlString)
- }
- do {
- let encoded = try JSONEncoder().encode(persisted)
- customAppsData = String(decoding: encoded, as: UTF8.self)
- } catch {
- // Keep UI responsive even if persistence fails.
- }
- }
- private func moveApp(from sourceID: UUID, to destinationID: UUID) {
- guard sourceID != destinationID else { return }
- guard let sourceIndex = appOrder.firstIndex(of: sourceID),
- let destinationIndex = appOrder.firstIndex(of: destinationID) else {
- return
- }
- var updated = appOrder
- let movedID = updated.remove(at: sourceIndex)
- let insertionIndex = destinationIndex > sourceIndex ? destinationIndex - 1 : destinationIndex
- updated.insert(movedID, at: insertionIndex)
- appOrder = updated
- saveAppOrderToStorage()
- }
- private func loadAppOrderFromStorage() {
- guard !appOrderData.isEmpty else {
- appOrder = []
- return
- }
- guard let data = appOrderData.data(using: .utf8) else {
- appOrder = []
- return
- }
- do {
- let decoded = try JSONDecoder().decode([String].self, from: data)
- appOrder = decoded.compactMap { UUID(uuidString: $0) }
- } catch {
- appOrder = []
- }
- }
- private func normalizeAppOrderAndPersist() {
- let currentIDs = apps.map(\.id)
- var seen = Set<UUID>()
- var normalized: [UUID] = []
- for id in appOrder where currentIDs.contains(id) {
- if seen.insert(id).inserted {
- normalized.append(id)
- }
- }
- for id in currentIDs where seen.insert(id).inserted {
- normalized.append(id)
- }
- appOrder = normalized
- saveAppOrderToStorage()
- }
- private func saveAppOrderToStorage() {
- do {
- let payload = appOrder.map(\.uuidString)
- let encoded = try JSONEncoder().encode(payload)
- appOrderData = String(decoding: encoded, as: UTF8.self)
- } catch {
- // Ignore persistence failure and keep local order.
- }
- }
- 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 struct PersistedCustomApp: Codable {
- let id: UUID
- let name: String
- let urlString: String
- }
- private struct TileReorderDropDelegate: DropDelegate {
- let destinationAppID: UUID
- @Binding var draggedAppID: UUID?
- let onMove: (UUID, UUID) -> Void
- func dropEntered(info: DropInfo) {
- guard let draggedAppID else { return }
- guard draggedAppID != destinationAppID else { return }
- onMove(draggedAppID, destinationAppID)
- }
- func performDrop(info: DropInfo) -> Bool {
- draggedAppID = nil
- return true
- }
- }
- private struct SearchHeader: View {
- @Binding var query: String
- let isPanelMode: Bool
- let onToggleLayout: () -> Void
- let onOpenPremium: () -> Void
- let onOpenGoogle: () -> Void
- var body: some View {
- HStack {
- HStack(spacing: 10) {
- Image(systemName: "magnifyingglass")
- .foregroundStyle(.white.opacity(0.82))
- TextField("Search", text: $query)
- .textFieldStyle(.plain)
- .foregroundStyle(.white.opacity(0.94))
- }
- .padding(.horizontal, 12)
- .padding(.vertical, 8)
- .background(
- RoundedRectangle(cornerRadius: 15, style: .continuous)
- .fill(Color.black.opacity(0.28))
- )
- .overlay(
- RoundedRectangle(cornerRadius: 15, style: .continuous)
- .stroke(Color.white.opacity(0.08), lineWidth: 1)
- )
- Spacer(minLength: 8)
- HStack(spacing: 12) {
- Button(action: onToggleLayout) {
- Image(systemName: isPanelMode ? "square.grid.2x2" : "list.bullet")
- }
- .buttonStyle(.plain)
- Menu {
- Button("Clear Search") {
- query = ""
- }
- Button("Open Premium") {
- onOpenPremium()
- }
- Button("Open Google") {
- onOpenGoogle()
- }
- } label: {
- Image(systemName: "ellipsis")
- }
- .menuStyle(.borderlessButton)
- }
- .font(.system(size: 15, weight: .semibold))
- .foregroundStyle(.white.opacity(0.82))
- .padding(.trailing, 4)
- }
- }
- }
- private struct IconPanelView: View {
- let apps: [LauncherApp]
- let onAppTap: (LauncherApp) -> Void
- private let actions = [
- "house", "magnifyingglass", "target", "map", "heart", "person.2", "clock",
- "star", "exclamationmark.circle", "trash", "paperplane", "doc", "lock",
- ]
- var body: some View {
- ScrollView {
- HStack(alignment: .top, spacing: 20) {
- VStack(alignment: .leading, spacing: 12) {
- ForEach(Array(apps.prefix(10))) { app in
- Button(action: { onAppTap(app) }) {
- HStack(spacing: 14) {
- AppIconGlyph(app: app)
- Text(app.name)
- .font(.system(size: 18, weight: .medium))
- .foregroundStyle(.white.opacity(0.94))
- .lineLimit(1)
- .truncationMode(.tail)
- Spacer(minLength: 0)
- }
- .padding(.vertical, 3)
- }
- .buttonStyle(.plain)
- }
- Spacer(minLength: 0)
- }
- .frame(width: 320, alignment: .topLeading)
- VStack(alignment: .trailing, spacing: 14) {
- ForEach(Array(apps.prefix(10).enumerated()), id: \.offset) { index, _ in
- HStack(spacing: 12) {
- ForEach(0..<8, id: \.self) { col in
- let symbol = actions[(index * 3 + col) % actions.count]
- ActionBubble(symbol: symbol, highlighted: col == 0 && index % 3 == 0)
- }
- Image(systemName: "info.circle")
- .foregroundStyle(.white.opacity(0.6))
- .font(.system(size: 17))
- .frame(width: 20)
- }
- }
- }
- .frame(maxWidth: .infinity, alignment: .topTrailing)
- }
- .padding(.horizontal, 10)
- .padding(.bottom, 18)
- }
- }
- }
- private struct AppIconGlyph: View {
- let app: LauncherApp
- var body: some View {
- ZStack {
- RoundedRectangle(cornerRadius: 12, style: .continuous)
- .fill(Color.white.opacity(0.08))
- .frame(width: 48, height: 48)
- if let webURL = app.webURL {
- WebSiteFaviconView(
- webURL: webURL,
- size: 32,
- assetIconName: app.assetIconName,
- fallbackSymbolName: app.fallbackSymbolName,
- iconPaddingFactor: 0.08
- )
- } else if let icon = NSImage(named: app.assetIconName) {
- Image(nsImage: icon)
- .resizable()
- .scaledToFit()
- .frame(width: 32, height: 32)
- } else {
- Image(systemName: app.fallbackSymbolName)
- .font(.system(size: 20, weight: .semibold))
- .foregroundStyle(.white)
- }
- }
- }
- }
- private struct ActionBubble: View {
- let symbol: String
- let highlighted: Bool
- var body: some View {
- Circle()
- .fill(highlighted ? Color.cyan.opacity(0.22) : Color.white.opacity(0.08))
- .overlay(
- Image(systemName: symbol)
- .foregroundStyle(.white.opacity(0.9))
- .font(.system(size: 15, weight: .medium))
- )
- .frame(width: 42, height: 42)
- }
- }
- private struct PromoBanner: View {
- let onCloseTap: () -> Void
- let onUpgradeTap: () -> Void
- var body: some View {
- HStack(spacing: 10) {
- Button(action: onCloseTap) {
- Image(systemName: "xmark")
- .foregroundStyle(.blue.opacity(0.95))
- .font(.system(size: 14, weight: .semibold))
- }
- .buttonStyle(.plain)
- Text("Update to Premium to unlock all features and remove ads")
- .font(.system(size: 13, weight: .medium))
- .foregroundStyle(.blue.opacity(0.95))
- .lineLimit(1)
- Spacer()
- Button(action: onUpgradeTap) {
- Text("Upgrade")
- .font(.system(size: 13, weight: .semibold))
- .foregroundStyle(.white)
- .padding(.horizontal, 12)
- .padding(.vertical, 6)
- .background(
- Capsule(style: .continuous)
- .fill(Color.blue.opacity(0.92))
- )
- }
- .buttonStyle(.plain)
- }
- .padding(.horizontal, 11)
- .padding(.vertical, 8)
- .background(
- RoundedRectangle(cornerRadius: 12, style: .continuous)
- .fill(Color.white.opacity(0.03))
- )
- .overlay(
- RoundedRectangle(cornerRadius: 12, style: .continuous)
- .stroke(Color.white.opacity(0.05), lineWidth: 1)
- )
- }
- }
- #Preview {
- LauncherRootView()
- }
- private struct VisualEffectBlur: NSViewRepresentable {
- let material: NSVisualEffectView.Material
- let blendingMode: NSVisualEffectView.BlendingMode
- func makeNSView(context: Context) -> NSVisualEffectView {
- let view = NSVisualEffectView()
- view.state = .active
- view.material = material
- view.blendingMode = blendingMode
- return view
- }
- func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
- nsView.material = material
- nsView.blendingMode = blendingMode
- }
- }
|