Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复在macOS m1 中点击全屏幕后,点击关闭后黑屏的问题 #627

Merged
merged 4 commits into from
Jan 4, 2025
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
34 changes: 29 additions & 5 deletions src/main/services/WindowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { configManager } from './ConfigManager'
export class WindowService {
private static instance: WindowService | null = null
private mainWindow: BrowserWindow | null = null
private isQuitting: boolean = false
private wasFullScreen: boolean = false

public static getInstance(): WindowService {
if (!WindowService.instance) {
Expand Down Expand Up @@ -42,7 +44,7 @@ export class WindowService {
height: mainWindowState.height,
minWidth: 1080,
minHeight: 600,
show: true,
show: false, // 初始不显示
autoHideMenuBar: true,
transparent: isMac,
vibrancy: 'under-window',
Expand Down Expand Up @@ -118,9 +120,20 @@ export class WindowService {
}

private setupWindowEvents(mainWindow: BrowserWindow) {
mainWindow.on('ready-to-show', () => {
mainWindow.once('ready-to-show', () => {
mainWindow.show()
})

// 处理全屏相关事件
mainWindow.on('enter-full-screen', () => {
this.wasFullScreen = true
mainWindow.webContents.send('fullscreen-status-changed', true)
})

mainWindow.on('leave-full-screen', () => {
this.wasFullScreen = false
mainWindow.webContents.send('fullscreen-status-changed', false)
})
}

private setupWebContentsHandlers(mainWindow: BrowserWindow) {
Expand Down Expand Up @@ -182,6 +195,11 @@ export class WindowService {
}

private setupWindowLifecycleEvents(mainWindow: BrowserWindow) {
// 监听应用退出事件
app.on('before-quit', () => {
this.isQuitting = true
})

mainWindow.on('close', (event) => {
const notInTray = !configManager.isTray()

Expand All @@ -191,9 +209,15 @@ export class WindowService {
}

// Mac
if (!app.isQuitting) {
event.preventDefault()
mainWindow.hide()
if (!this.isQuitting) {
if (this.wasFullScreen) {
// 如果是全屏状态,直接退出
this.isQuitting = true
app.quit()
} else {
event.preventDefault()
mainWindow.hide()
}
}
})
}
Expand Down
28 changes: 28 additions & 0 deletions src/renderer/src/hooks/useMermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,32 @@ export const useMermaid = () => {

setTimeout(renderMermaid, 100)
}, [generating])

useEffect(() => {
const handleWheel = (e: WheelEvent) => {
if (e.ctrlKey || e.metaKey) {
e.preventDefault()
const mermaidElement = (e.target as HTMLElement).closest('.mermaid')
if (!mermaidElement) return

const svg = mermaidElement.querySelector('svg')
if (!svg) return

const currentScale = parseFloat(svg.style.transform?.match(/scale\((.*?)\)/)?.[1] || '1')
const delta = e.deltaY < 0 ? 0.1 : -0.1
const newScale = Math.max(0.1, Math.min(3, currentScale + delta))

const container = svg.parentElement
if (container) {
container.style.overflow = 'auto'
container.style.position = 'relative'
svg.style.transformOrigin = 'top left'
svg.style.transform = `scale(${newScale})`
}
}
}

document.addEventListener('wheel', handleWheel, { passive: false })
return () => document.removeEventListener('wheel', handleWheel)
}, [])
}
4 changes: 4 additions & 0 deletions src/renderer/src/i18n/locales/en-us.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
"png": "Download PNG",
"svg": "Download SVG"
},
"resize": {
"zoom-in": "Zoom In",
"zoom-out": "Zoom Out"
},
"tabs": {
"preview": "Preview",
"source": "Source"
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/i18n/locales/ja-jp.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
"png": "PNGをダウンロード",
"svg": "SVGをダウンロード"
},
"resize": {
"zoom-in": "拡大する",
"zoom-out": "ズームアウト"
},
"tabs": {
"preview": "プレビュー",
"source": "ソース"
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/i18n/locales/ru-ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
"png": "Скачать PNG",
"svg": "Скачать SVG"
},
"resize": {
"zoom-in": "Yвеличить",
"zoom-out": "Yменьшить масштаб"
},
"tabs": {
"preview": "Предпросмотр",
"source": "Исходный код"
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/i18n/locales/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@
"png": "下载 PNG",
"svg": "下载 SVG"
},
"resize": {
"zoom-in": "放大",
"zoom-out": "缩小"
},
"tabs": {
"preview": "预览",
"source": "源码"
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/src/i18n/locales/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@
"png": "下載 PNG",
"svg": "下載 SVG"
},
"resize": {
"zoom-in": "放大",
"zoom-out": "縮小"
},
"tabs": {
"preview": "預覽",
"source": "原始碼"
Expand Down
22 changes: 22 additions & 0 deletions src/renderer/src/pages/home/Markdown/MermaidPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const PopupContainer: React.FC<Props> = ({ resolve, chart }) => {
const { t } = useTranslation()
const mermaidId = `mermaid-popup-${Date.now()}`
const [activeTab, setActiveTab] = useState('preview')
const [scale, setScale] = useState(1)

const onOk = () => {
setOpen(false)
Expand All @@ -31,6 +32,25 @@ const PopupContainer: React.FC<Props> = ({ resolve, chart }) => {
resolve({})
}

const handleZoom = (delta: number) => {
const newScale = Math.max(0.1, Math.min(3, scale + delta))
setScale(newScale)

const element = document.getElementById(mermaidId)
if (!element) return

const svg = element.querySelector('svg')
if (!svg) return

const container = svg.parentElement
if (container) {
container.style.overflow = 'auto'
container.style.position = 'relative'
svg.style.transformOrigin = 'top left'
svg.style.transform = `scale(${newScale})`
}
}

const handleDownload = async (format: 'svg' | 'png') => {
try {
const element = document.getElementById(mermaidId)
Expand Down Expand Up @@ -110,6 +130,8 @@ const PopupContainer: React.FC<Props> = ({ resolve, chart }) => {
{activeTab === 'source' && <Button onClick={() => handleCopy()}>{t('common.copy')}</Button>}
{activeTab === 'preview' && (
<>
<Button onClick={() => handleZoom(0.1)}>{t('mermaid.resize.zoom-in')}</Button>
<Button onClick={() => handleZoom(-0.1)}>{t('mermaid.resize.zoom-out')}</Button>
<Button onClick={() => handleDownload('svg')}>{t('mermaid.download.svg')}</Button>
<Button onClick={() => handleDownload('png')}>{t('mermaid.download.png')}</Button>
</>
Expand Down