Skip to content

Commit

Permalink
feat: hot key to close tab is compatible with windows
Browse files Browse the repository at this point in the history
  • Loading branch information
1943time committed Oct 28, 2023
1 parent 2d734fa commit ccb662e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/main/appMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const getSystemMenus = () => {
]
},
] : [
{type: 'separator'},
{
id: 'open',
label: 'Open File',
Expand Down Expand Up @@ -190,6 +191,7 @@ const getSystemMenus = () => {
ipcMain.emit('create-window')
}
},
{type: 'separator'},
{
label: 'New Tab',
accelerator: `${cmd}+t`,
Expand All @@ -199,10 +201,7 @@ const getSystemMenus = () => {
},
{
label: 'Close current tab',
accelerator: `${cmd}+w`,
click: (menu, win) => {
win?.webContents.send('close-current-tab')
}
accelerator: `${cmd}+w`
},
...systemFileMenus,
{
Expand Down
12 changes: 11 additions & 1 deletion src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {app, BrowserWindow, dialog, ipcMain, screen, shell} from 'electron'
import {app, BrowserWindow, dialog, ipcMain, screen, shell, globalShortcut} from 'electron'
import {lstatSync} from 'fs'
import {electronApp, is, optimizer} from '@electron-toolkit/utils'
import {baseUrl, isDark, registerApi, windowOptions} from './api'
Expand Down Expand Up @@ -126,6 +126,16 @@ const openFiles = (filePath: string) => {
} catch (e) {
}
}

app.on('browser-window-focus', () => {
globalShortcut.register("CommandOrControl+W", () => {
BrowserWindow.getFocusedWindow()?.webContents.send('close-current-tab')
})
})

app.on('browser-window-blur', () => {
globalShortcut.unregister('CommandOrControl+W')
})
app.whenReady().then(() => {
createAppMenus()
registerMenus()
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/components/Empty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const Empty = observer(() => {
className={'cursor-default text-sky-600 duration-200 hover:text-sky-400'}
onClick={() => {
treeStore.openFolder(r.filePath)
treeStore.openFirst()
}}
>
<FolderOutlined />
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/src/hooks/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export const useSystemMenus = () => {
const filePath = res.filePaths[0]
const s = stat(filePath)
if (s) {
s.isDirectory() ? treeStore.openFolder(filePath) : treeStore.openNote(filePath)
if (s.isDirectory()) {
treeStore.openFolder(filePath)
treeStore.openFirst()
} else {
treeStore.openNote(filePath)
}
}
window.electron.ipcRenderer.send('add-recent-path', res.filePaths[0])
}
Expand Down
11 changes: 10 additions & 1 deletion src/renderer/src/store/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,16 @@ export class TreeStore {
this.watcher.openDirCheck()
this.parseFolder()
}

openFirst() {
if (!treeStore.currentTab.current || this.tabs.length === 1) {
const first = this.firstNote
if (first) {
this.currentTab.history.push(first)
this.currentTab.index = 0
this.openParentDir(first.filePath)
}
}
}
async parseFolder() {
const queue:IFileItem[] = []
const stack:IFileItem[] = this.root.children!.slice()
Expand Down

0 comments on commit ccb662e

Please sign in to comment.