mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
Feature: Favourites
This commit is contained in:
parent
79b16570b5
commit
5321352184
7 changed files with 627 additions and 32 deletions
|
|
@ -18,6 +18,7 @@ struct ClipboardItem: Identifiable, Codable {
|
|||
let type: ClipboardItemType
|
||||
let timestamp: Date
|
||||
let preview: String
|
||||
var isPinned: Bool = false
|
||||
|
||||
// Store different types of data - avoid large binary data in UserDefaults
|
||||
let stringData: String?
|
||||
|
|
@ -90,6 +91,14 @@ struct ClipboardItem: Identifiable, Codable {
|
|||
return try? Data(contentsOf: fileURL)
|
||||
}
|
||||
|
||||
// Helper to check if this item has the same content as another
|
||||
func isSameContent(as other: ClipboardItem) -> Bool {
|
||||
return stringData == other.stringData &&
|
||||
imageFileName == other.imageFileName &&
|
||||
fileURLs == other.fileURLs &&
|
||||
type == other.type
|
||||
}
|
||||
|
||||
static func generatePreview(stringData: String, type: ClipboardItemType) -> String {
|
||||
switch type {
|
||||
case .text:
|
||||
|
|
@ -149,6 +158,7 @@ class ClipboardManager: ObservableObject {
|
|||
static let shared = ClipboardManager()
|
||||
|
||||
@Published var clipboardHistory: [ClipboardItem] = []
|
||||
@Published var pinnedItems: [ClipboardItem] = []
|
||||
@Published var isMonitoring: Bool = false
|
||||
|
||||
private var timer: Timer?
|
||||
|
|
@ -159,6 +169,15 @@ class ClipboardManager: ObservableObject {
|
|||
return Defaults[.clipboardHistorySize]
|
||||
}
|
||||
|
||||
// Computed properties for filtered lists
|
||||
var regularHistory: [ClipboardItem] {
|
||||
clipboardHistory.filter { !$0.isPinned }
|
||||
}
|
||||
|
||||
var pinnedHistory: [ClipboardItem] {
|
||||
pinnedItems
|
||||
}
|
||||
|
||||
// Directory for storing clipboard data files
|
||||
static let clipboardDataDirectory: URL = {
|
||||
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
||||
|
|
@ -254,6 +273,58 @@ class ClipboardManager: ObservableObject {
|
|||
saveHistoryToDefaults()
|
||||
}
|
||||
|
||||
func pinItem(_ item: ClipboardItem) {
|
||||
// Update the item to be pinned
|
||||
var pinnedItem = item
|
||||
pinnedItem.isPinned = true
|
||||
|
||||
// Remove from regular history if it exists there
|
||||
clipboardHistory.removeAll { $0.id == item.id }
|
||||
|
||||
// Add to pinned items if not already there
|
||||
if !pinnedItems.contains(where: { $0.id == item.id }) {
|
||||
pinnedItems.append(pinnedItem)
|
||||
}
|
||||
|
||||
saveHistoryToDefaults()
|
||||
savePinnedItemsToDefaults()
|
||||
}
|
||||
|
||||
func unpinItem(_ item: ClipboardItem) {
|
||||
// Remove from pinned items
|
||||
pinnedItems.removeAll { $0.id == item.id }
|
||||
|
||||
// Update the item to be unpinned and add back to regular history
|
||||
var unpinnedItem = item
|
||||
unpinnedItem.isPinned = false
|
||||
|
||||
// Add back to regular history at the top
|
||||
clipboardHistory.insert(unpinnedItem, at: 0)
|
||||
|
||||
// Maintain history size limit
|
||||
if clipboardHistory.count > maxHistoryItems {
|
||||
let itemsToDelete = Array(clipboardHistory.dropFirst(maxHistoryItems))
|
||||
for oldItem in itemsToDelete {
|
||||
if let fileName = oldItem.imageFileName {
|
||||
let fileURL = ClipboardManager.clipboardDataDirectory.appendingPathComponent(fileName)
|
||||
try? FileManager.default.removeItem(at: fileURL)
|
||||
}
|
||||
}
|
||||
clipboardHistory = Array(clipboardHistory.prefix(maxHistoryItems))
|
||||
}
|
||||
|
||||
saveHistoryToDefaults()
|
||||
savePinnedItemsToDefaults()
|
||||
}
|
||||
|
||||
func togglePin(for item: ClipboardItem) {
|
||||
if item.isPinned || pinnedItems.contains(where: { $0.id == item.id }) {
|
||||
unpinItem(item)
|
||||
} else {
|
||||
pinItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private func checkClipboard() {
|
||||
|
|
@ -438,10 +509,21 @@ class ClipboardManager: ObservableObject {
|
|||
}
|
||||
}
|
||||
|
||||
func savePinnedItemsToDefaults() {
|
||||
if let encoded = try? JSONEncoder().encode(pinnedItems) {
|
||||
UserDefaults.standard.set(encoded, forKey: "ClipboardPinnedItems")
|
||||
}
|
||||
}
|
||||
|
||||
private func loadHistoryFromDefaults() {
|
||||
if let data = UserDefaults.standard.data(forKey: "ClipboardHistory"),
|
||||
let history = try? JSONDecoder().decode([ClipboardItem].self, from: data) {
|
||||
clipboardHistory = history
|
||||
}
|
||||
|
||||
if let data = UserDefaults.standard.data(forKey: "ClipboardPinnedItems"),
|
||||
let pinned = try? JSONDecoder().decode([ClipboardItem].self, from: data) {
|
||||
pinnedItems = pinned
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
104
DynamicIsland/managers/ClipboardWindowManager.swift
Normal file
104
DynamicIsland/managers/ClipboardWindowManager.swift
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
//
|
||||
// ClipboardWindowManager.swift
|
||||
// DynamicIsland
|
||||
//
|
||||
// Created by GitHub Copilot on 12/08/25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
|
||||
class ClipboardWindowManager: ObservableObject {
|
||||
static let shared = ClipboardWindowManager()
|
||||
|
||||
private var clipboardWindow: NSWindow?
|
||||
|
||||
private init() {}
|
||||
|
||||
func showClipboardWindow() {
|
||||
if let existingWindow = clipboardWindow {
|
||||
existingWindow.makeKeyAndOrderFront(nil)
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
return
|
||||
}
|
||||
|
||||
// Create the window
|
||||
let window = NSWindow(
|
||||
contentRect: NSRect(x: 0, y: 0, width: 400, height: 300),
|
||||
styleMask: [.titled, .closable, .resizable],
|
||||
backing: .buffered,
|
||||
defer: false
|
||||
)
|
||||
|
||||
window.title = "Clipboard Manager"
|
||||
window.isMovableByWindowBackground = true
|
||||
window.level = .floating
|
||||
window.isReleasedWhenClosed = false
|
||||
window.hasShadow = true
|
||||
window.titlebarAppearsTransparent = true
|
||||
window.styleMask.insert(.fullSizeContentView)
|
||||
|
||||
// Set minimum and maximum sizes
|
||||
window.minSize = NSSize(width: 350, height: 250)
|
||||
window.maxSize = NSSize(width: 600, height: 500)
|
||||
|
||||
// Center the window
|
||||
if let screen = NSScreen.main {
|
||||
let screenFrame = screen.visibleFrame
|
||||
let windowFrame = window.frame
|
||||
let x = (screenFrame.width - windowFrame.width) / 2 + screenFrame.minX
|
||||
let y = (screenFrame.height - windowFrame.height) / 2 + screenFrame.minY
|
||||
window.setFrameOrigin(NSPoint(x: x, y: y))
|
||||
}
|
||||
|
||||
// Set the content view
|
||||
let contentView = ClipboardWindow()
|
||||
let hostingView = NSHostingView(rootView: contentView)
|
||||
window.contentView = hostingView
|
||||
|
||||
// Handle window closing
|
||||
window.delegate = WindowDelegate { [weak self] in
|
||||
self?.clipboardWindow = nil
|
||||
}
|
||||
|
||||
self.clipboardWindow = window
|
||||
window.makeKeyAndOrderFront(nil)
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
}
|
||||
|
||||
func hideClipboardWindow() {
|
||||
clipboardWindow?.close()
|
||||
}
|
||||
|
||||
func toggleClipboardWindow() {
|
||||
if let window = clipboardWindow, window.isVisible {
|
||||
hideClipboardWindow()
|
||||
} else {
|
||||
showClipboardWindow()
|
||||
}
|
||||
}
|
||||
|
||||
var isWindowVisible: Bool {
|
||||
return clipboardWindow?.isVisible ?? false
|
||||
}
|
||||
}
|
||||
|
||||
private class WindowDelegate: NSObject, NSWindowDelegate {
|
||||
private let onClose: () -> Void
|
||||
|
||||
init(onClose: @escaping () -> Void) {
|
||||
self.onClose = onClose
|
||||
super.init()
|
||||
}
|
||||
|
||||
func windowWillClose(_ notification: Notification) {
|
||||
onClose()
|
||||
}
|
||||
|
||||
func windowShouldClose(_ sender: NSWindow) -> Bool {
|
||||
// Hide instead of close when user clicks close button
|
||||
sender.orderOut(nil)
|
||||
onClose()
|
||||
return false
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue