importCarbonclassHotkeyManager{privatevarhotkeyRef:EventHotKeyRef?privatelethotkeyID=EventHotKeyID(signature:OSType(UInt32(0x4F4E4543)),id:1)funcregisterHotkey(keyCode:UInt32,modifiers:UInt32){varref:EventHotKeyRef?letstatus=RegisterEventHotKey(keyCode,modifiers,hotkeyID,GetApplicationEventTarget(),0,&ref)ifstatus==noErr{hotkeyRef=refprint("✅ 快捷键注册成功")}else{print("❌ 快捷键注册失败: \(status)")}}funcunregisterHotkey(){ifletref=hotkeyRef{UnregisterEventHotKey(ref)}}}// 快捷键码对照表letHOTKEY_CODES=["V":9,// V 键"R":15,// R 键"C":8,// C 键"D":2,// D 键]letMODIFIER_KEYS=["cmd":UInt32(cmdKey),// Command"option":UInt32(optionKey),// Option"shift":UInt32(shiftKey),// Shift"control":UInt32(controlKey),// Control]
// SQLite 数据库封装classClipboardDatabase{privatevardb:OpaquePointer?init(atpath:String)throws{// 打开数据库连接guardsqlite3_open(path,&db)==SQLITE_OKelse{throwClipboardError.databaseNotReady}// 创建表结构trycreateTables()}// 保存项目funcsaveItem(_item:ClipboardItem)throws{letsql=""" INSERT OR REPLACE INTO clipboard_items (id, content, type, timestamp, source_app, is_favorite, is_pinned, content_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?) """// 执行 SQL}// 加载最近项目funcloadHotData(limit:Int)throws->[ClipboardItem]{letsql="SELECT * FROM clipboard_items ORDER BY timestamp DESC LIMIT ?"// 执行查询并返回结果}}
性能优化:
1 2 3 4 5 6 7 8 9101112131415161718192021222324
// 使用索引加速查询funccreateTables()throws{letsql=""" CREATE TABLE IF NOT EXISTS clipboard_items ( id TEXT PRIMARY KEY, content TEXT, type TEXT NOT NULL, timestamp REAL NOT NULL, source_app TEXT, is_favorite INTEGER DEFAULT 0, is_pinned INTEGER DEFAULT 0, content_hash TEXT ); CREATE INDEX IF NOT EXISTS idx_timestamp ON clipboard_items(timestamp DESC); CREATE INDEX IF NOT EXISTS idx_content_hash ON clipboard_items(content_hash); """// 执行 SQL}// 使用哈希索引快速去重 - O(1) 时间复杂度funcfindItemByHash(_hash:String)->UUID?{letsql="SELECT id FROM clipboard_items WHERE content_hash = ? LIMIT 1"// 执行查询}
classSearchOptimizer{// 搜索防抖privatevarsearchDebounceTimer:Timer?funcsearchWithDebounce(_query:String){searchDebounceTimer?.invalidate()searchDebounceTimer=Timer.scheduledTimer(withTimeInterval:0.3,repeats:false){[weakself]_inself?.performSearch(query)}}privatefuncperformSearch(_query:String){// 使用 SQLite 查询letsql=""" SELECT * FROM clipboard_items WHERE content LIKE ? OR summary LIKE ? ORDER BY timestamp DESC LIMIT 50 """DispatchQueue.global(qos:.userInitiated).async{// 执行 SQL 查询letresults=self.database.executeQuery(sql,with:query)DispatchQueue.main.async{self.updateSearchResults(results)}}}}