Skip to content

Commit

Permalink
feat: Windows and Linux systems automatically restore the window afte…
Browse files Browse the repository at this point in the history
…r opening.
  • Loading branch information
1943time committed Nov 4, 2023
1 parent 925e3dd commit 707cf29
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
42 changes: 14 additions & 28 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import {AppUpdate} from './update'
const isWindows = process.platform === 'win32'

type WinOptions = {
width?: number
height?: number
x?: number
y?: number
openFolder?: string
openFile?: string
openTabs?: string[]
index?: number
}
Expand All @@ -24,11 +19,9 @@ let fileChangedWindow: BrowserWindow | null = null
function createWindow(initial?: WinOptions): void {
const dark = isDark()
const {width, height} = screen.getPrimaryDisplay().workAreaSize
let openWidth = initial?.width ? initial.width < 800 ? 800 : initial.width : width
let openHeight = initial?.height ? initial.height < 400 ? 400 : initial.width : height
const window = new BrowserWindow({
width: openWidth,
height: openHeight,
width,
height,
titleBarStyle: 'hiddenInset',
backgroundColor: dark ? '#222222' : '#ffffff',
...windowOptions
Expand Down Expand Up @@ -77,9 +70,13 @@ function createWindow(initial?: WinOptions): void {
})
if (res !== 1) {
e.preventDefault()
return
}
}
windows.delete(window.id)
const id = window.id
setTimeout(() => {
windows.delete(id)
}, 500)
})
windows.set(window.id, initial || {})
window.show()
Expand All @@ -106,10 +103,10 @@ ipcMain.on('open-history-path', (e, path: string) => {
const openFiles = (filePath: string) => {
try {
const win = Array.from(windows).find(w => {
return (w[1].openFile === filePath || w[1].openFolder === filePath) || (w[1].openFolder && filePath.startsWith(w[1].openFolder))
return (w[1].openTabs?.includes(filePath) || w[1]?.openFolder === filePath) || (w[1].openFolder && filePath.startsWith(w[1].openFolder))
})
if (win) {
if (win[1].openFile === filePath || win[1].openFolder === filePath) {
if (win[1].openTabs?.includes(filePath) || win[1]?.openFolder === filePath) {
BrowserWindow.fromId(win[0])?.focus()
} else if (win[1].openFolder && filePath.startsWith(win[1].openFolder)) {
const w = BrowserWindow.fromId(win[0])
Expand All @@ -120,7 +117,7 @@ const openFiles = (filePath: string) => {
const stat = lstatSync(filePath)
createWindow({
openFolder: stat.isDirectory() ? filePath : undefined,
openFile: stat.isFile() ? filePath : undefined
openTabs: stat.isFile() ? [filePath] : undefined
})
}
} catch (e) {
Expand Down Expand Up @@ -155,6 +152,7 @@ app.whenReady().then(() => {
if (data.openFolder) windows.get(window.id)!.openFolder = data.openFolder
if (data.openTabs) windows.get(window.id)!.openTabs = data.openTabs
if (typeof data.index !== 'undefined') windows.get(window.id)!.index = data.index
console.log('set', data)
})
ipcMain.on('add-recent-path', (e, path) => {
store.set('recent-open-paths', Array.from(new Set([...(store.get('recent-open-paths') as any[] || []), path])))
Expand All @@ -177,24 +175,13 @@ app.whenReady().then(() => {
return {
openFolder: w.openFolder,
index: w.index,
// openFile will be abandoned
openTabs: w.openTabs
// openTabs: ['/Users/huangjie/Documents/t1/dir/test.md', '/Users/huangjie/Documents/t1/demo.md']
}
}
return null
})
app.on('before-quit', e => {
store.set('windows', BrowserWindow.getAllWindows().map(w => {
const bound = w.getBounds()
return {
...windows.get(w.id),
width: bound.width,
height: bound.height,
x: bound.x,
y: bound.y,
}
}))
store.set('windows', Array.from(windows).map(w => w[1]))
})
if (isWindows) electronApp.setAppUserModelId('me.bluemd')

Expand All @@ -208,7 +195,6 @@ app.whenReady().then(() => {
if (waitOpenFile === '.') waitOpenFile = ''
try {
const data = store.get('windows') as WinOptions[] || []
// console.log('data', data)
if (data?.length) {
for (let d of data) {
createWindow(typeof d === 'object' ? d : undefined)
Expand All @@ -221,15 +207,15 @@ app.whenReady().then(() => {
}
if (waitOpenFile) {
const win = Array.from(windows).find(w => {
return !w[1].openFolder && !w[1].openFile
return !w[1].openFolder
})
if (win) {
setTimeout(() => {
BrowserWindow.fromId(win[0])?.webContents.send('open-path', waitOpenFile)
}, 300)
} else{
createWindow({
openFile: waitOpenFile
openTabs: [waitOpenFile]
})
}
waitOpenFile = ''
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/editor/elements/media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ElementProps, MediaNode} from '../../el'
import {isAbsolute, join} from 'path'
import {ReactEditor} from 'slate-react'
import {useGetSetState} from 'react-use'
import Img from '../../svg/Img'
import Img from '../../icons/Img'
import {useEffect, useMemo, useRef} from 'react'
import {treeStore} from '../../store/tree'
import {Transforms} from 'slate'
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/editor/tools/FloatBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export const FloatBar = observer(() => {
}
if (e.key === '#') {
setTimeout(getAnchors)
} else if (!state.url?.includes('#') && state.anchors.length) {
} else if (state.anchors.length && !state.url?.includes('#')) {
setState({anchors: []})
}
}}
Expand Down
File renamed without changes.

0 comments on commit 707cf29

Please sign in to comment.