Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal-plugins/setting/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,17 @@ declare global {
getCurrentShortcut: () => Promise<string>
registerGlobalShortcut: (
shortcut: string,
target: string
target: string,
autoCopy?: boolean
) => Promise<{ success: boolean; error?: string }>
unregisterGlobalShortcut: (shortcut: string) => Promise<{
success: boolean
error?: string
}>
updateGlobalShortcutConfig: (
shortcut: string,
config: { autoCopy: boolean }
) => Promise<{ success: boolean; error?: string }>
registerAppShortcut: (
shortcut: string,
target: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface GlobalShortcut {
enabled: boolean
configurable?: boolean
configKey?: BuiltInShortcutKey
autoCopy?: boolean
}

type BuiltInShortcutKey = 'search' | 'closePlugin' | 'killPlugin'
Expand Down Expand Up @@ -324,7 +325,10 @@ async function saveAliasMappings(nextStore: CommandAliasStore): Promise<boolean>
async function loadGlobalShortcuts(): Promise<void> {
try {
const data = await window.ztools.internal.dbGet('global-shortcuts')
globalShortcuts.value = data || []
globalShortcuts.value = (data || []).map((shortcut: any) => ({
...shortcut,
autoCopy: shortcut.autoCopy ?? false // 默认禁用
}))
} catch (err) {
console.error('加载全局快捷键失败:', err)
}
Expand Down Expand Up @@ -670,6 +674,7 @@ async function handleSaveGlobalShortcut(
}

const oldShortcut = editingShortcut.value.shortcut
const autoCopy = editingShortcut.value.autoCopy ?? false // 保留原有 autoCopy 配置

try {
if (oldShortcut !== recordedShortcut) {
Expand All @@ -678,14 +683,16 @@ async function handleSaveGlobalShortcut(

const result = await window.ztools.internal.registerGlobalShortcut(
recordedShortcut,
targetCommand
targetCommand,
autoCopy
)

if (result.success) {
const index = globalShortcuts.value.findIndex((s) => s.id === editingShortcut.value!.id)
if (index >= 0) {
globalShortcuts.value[index].shortcut = recordedShortcut
globalShortcuts.value[index].target = targetCommand
// autoCopy 保持不变
}

await saveGlobalShortcuts()
Expand All @@ -695,7 +702,8 @@ async function handleSaveGlobalShortcut(
if (oldShortcut !== recordedShortcut) {
await window.ztools.internal.registerGlobalShortcut(
oldShortcut,
editingShortcut.value.target
editingShortcut.value.target,
autoCopy
)
}
error(`快捷键注册失败: ${result.error}`)
Expand All @@ -704,7 +712,8 @@ async function handleSaveGlobalShortcut(
if (oldShortcut !== recordedShortcut) {
await window.ztools.internal.registerGlobalShortcut(
oldShortcut,
editingShortcut.value.target
editingShortcut.value.target,
autoCopy
)
}
console.error('更新快捷键失败:', err)
Expand All @@ -723,7 +732,8 @@ async function handleSaveGlobalShortcut(
id: Date.now().toString(),
shortcut: recordedShortcut,
target: targetCommand,
enabled: true
enabled: true,
autoCopy: false // 新建快捷键默认禁用自动复制
}

globalShortcuts.value.push(newShortcut)
Expand All @@ -732,7 +742,8 @@ async function handleSaveGlobalShortcut(
try {
const result = await window.ztools.internal.registerGlobalShortcut(
recordedShortcut,
targetCommand
targetCommand,
false // 新建快捷键默认禁用自动复制
)
if (result.success) {
success('快捷键添加成功!')
Expand Down Expand Up @@ -880,6 +891,70 @@ async function handleDelete(id: string): Promise<void> {
}
}

// 处理自动复制开关切换
async function handleAutoCopyToggle(shortcut: any, event: Event): Promise<void> {
console.log('[AutoCopy] 开关切换触发', { shortcut, event })

const target = event.target as HTMLInputElement | null
if (!target) {
console.error('[AutoCopy] 无法获取 target 元素')
return
}

const newAutoCopy = target.checked
console.log('[AutoCopy] 新状态:', newAutoCopy, '旧状态:', shortcut.autoCopy)

try {
// 1. 更新本地数据
const index = globalShortcuts.value.findIndex((s) => s.id === shortcut.id)
console.log('[AutoCopy] 找到快捷键索引:', index)

if (index >= 0) {
globalShortcuts.value[index].autoCopy = newAutoCopy
console.log('[AutoCopy] 本地数据已更新:', globalShortcuts.value[index])
}

// 2. 保存到数据库(清理数据,只保留可序列化的字段)
console.log('[AutoCopy] 开始保存到数据库...')
const dataToSave = globalShortcuts.value.map((s) => ({
id: s.id,
shortcut: s.shortcut,
target: s.target,
enabled: s.enabled,
autoCopy: s.autoCopy,
...(s.configurable !== undefined && { configurable: s.configurable }),
...(s.configKey !== undefined && { configKey: s.configKey })
}))
await window.ztools.internal.dbPut('global-shortcuts', dataToSave)
console.log('[AutoCopy] 数据库保存成功')

// 3. 通知主进程更新配置
console.log('[AutoCopy] 通知主进程更新配置:', {
shortcut: shortcut.shortcut,
autoCopy: newAutoCopy
})
const result = await window.ztools.internal.updateGlobalShortcutConfig(shortcut.shortcut, {
autoCopy: newAutoCopy
})
console.log('[AutoCopy] 主进程配置更新结果:', result)

if (!result.success) {
throw new Error(result.error || '更新配置失败')
}

console.log('[AutoCopy] 自动复制开关切换完成')
} catch (err: any) {
console.error('[AutoCopy] 更新自动复制开关失败:', err)
// 回滚
target.checked = !newAutoCopy
const index = globalShortcuts.value.findIndex((s) => s.id === shortcut.id)
if (index >= 0) {
globalShortcuts.value[index].autoCopy = !newAutoCopy
console.log('[AutoCopy] 已回滚本地数据')
}
}
}

onMounted(() => {
const userAgent = navigator.userAgent.toLowerCase()
const platform = navigator.platform.toLowerCase()
Expand Down Expand Up @@ -1152,6 +1227,20 @@ useJumpFunction<ShortcutsSettingJumpFunction>(async (state) => {
</div>

<div class="shortcut-meta">
<!-- 自动复制开关(仅全局快捷键) -->
<label
v-if="activeTab === 'global'"
class="toggle auto-copy-toggle"
:title="(shortcut.autoCopy ?? false) ? '已启用自动复制' : '已禁用自动复制'"
>
<input
type="checkbox"
:checked="shortcut.autoCopy ?? false"
@change="handleAutoCopyToggle(shortcut, $event)"
/>
<span class="toggle-slider"></span>
</label>

<button
class="icon-btn edit-btn"
title="编辑"
Expand Down Expand Up @@ -1633,6 +1722,12 @@ useJumpFunction<ShortcutsSettingJumpFunction>(async (state) => {
justify-content: center;
}

.auto-copy-toggle {
margin-right: 8px;
transform: scale(0.85);
transform-origin: center;
}

.edit-btn {
color: var(--primary-color);
}
Expand Down
13 changes: 7 additions & 6 deletions resources/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,14 +914,15 @@ window.ztools = {
await electron.ipcRenderer.invoke('internal:get-plugin-memory-info', pluginPath),

// ==================== 全局快捷键 API ====================
registerGlobalShortcut: async (shortcut, target) =>
await electron.ipcRenderer.invoke('internal:register-global-shortcut', shortcut, target),
registerGlobalShortcut: async (shortcut, target, autoCopy) =>
await electron.ipcRenderer.invoke('register-global-shortcut', shortcut, target, autoCopy),
unregisterGlobalShortcut: async (shortcut) =>
await electron.ipcRenderer.invoke('internal:unregister-global-shortcut', shortcut),
startHotkeyRecording: async () =>
await electron.ipcRenderer.invoke('internal:start-hotkey-recording'),
await electron.ipcRenderer.invoke('unregister-global-shortcut', shortcut),
updateGlobalShortcutConfig: async (shortcut, config) =>
await electron.ipcRenderer.invoke('update-global-shortcut-config', shortcut, config),
startHotkeyRecording: async () => await electron.ipcRenderer.invoke('start-hotkey-recording'),
updateShortcut: async (shortcut) =>
await electron.ipcRenderer.invoke('internal:update-shortcut', shortcut),
await electron.ipcRenderer.invoke('update-shortcut', shortcut),
getCurrentShortcut: async () => await electron.ipcRenderer.invoke('get-current-shortcut'),
onHotkeyRecorded: (callback) => {
if (callback && typeof callback === 'function') {
Expand Down
Loading