diff --git a/Cargo.lock b/Cargo.lock index 180d456e5f..fa8b58eadd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1859,6 +1859,8 @@ dependencies = [ "futures", "log", "mongodb", + "objc2", + "objc2-app-kit", "percent-encoding", "portpicker", "redis", diff --git a/apps/desktop/src/App.vue b/apps/desktop/src/App.vue index ab5bbef5bb..b215bc6384 100644 --- a/apps/desktop/src/App.vue +++ b/apps/desktop/src/App.vue @@ -28,7 +28,7 @@ import { useDialogSources } from "@/composables/useDialogSources"; import { useNavigationTargets } from "@/composables/useNavigationTargets"; import { useDataGridActions } from "@/composables/useDataGridActions"; import { useTauriEvents } from "@/composables/useTauriEvents"; -import { useCloseActionPrompt } from "@/composables/useCloseActionPrompt"; +import { useCloseActionPrompt, type AppCloseAction, type AppCloseRequestOptions } from "@/composables/useCloseActionPrompt"; import { useVisibilityChange } from "@/composables/useVisibilityChange"; import { useWebDavAutoUpload } from "@/composables/useWebDavAutoUpload"; import "@/i18n"; @@ -151,6 +151,9 @@ const saveSqlName = ref(""); const saveSqlFolderId = ref(""); const pendingSaveAndCloseTabId = ref(null); const pendingPrevActiveTabId = ref(null); +const pendingSaveShouldCloseTab = ref(true); +const pendingAppCloseAction = ref(null); +const pendingCloseActionChoice = ref(false); const ROOT_SAVED_SQL_FOLDER = "__root__"; const activeTab = computed(() => queryStore.tabs.find((t) => t.id === queryStore.activeTabId)); @@ -252,7 +255,7 @@ const { setupTauriListeners, cleanupTauriListeners } = useTauriEvents({ openDbFilePath, openConnectionDeepLink, }); -const { showCloseActionPrompt, chooseQuit, chooseMinimize, setupCloseActionPromptListener, cleanupCloseActionPromptListener } = useCloseActionPrompt(); +const { showCloseActionPrompt, chooseQuit, chooseMinimize, cancelCloseActionPrompt, performCloseAction, setupCloseActionPromptListener, cleanupCloseActionPromptListener } = useCloseActionPrompt({ requestClose: requestAppClose }); useVisibilityChange(); useWebDavAutoUpload(); @@ -506,7 +509,81 @@ function closePendingSavedTab() { pendingSaveAndCloseTabId.value = null; if (pendingPrevActiveTabId.value) queryStore.activeTabId = pendingPrevActiveTabId.value; pendingPrevActiveTabId.value = null; - queryStore.closeTab(closeId, { force: true }); + const shouldCloseTab = pendingSaveShouldCloseTab.value; + pendingSaveShouldCloseTab.value = true; + if (shouldCloseTab) queryStore.closeTab(closeId, { force: true }); +} + +function cancelPendingSaveAndClose() { + showSaveSqlDialog.value = false; + pendingSaveAndCloseTabId.value = null; + pendingPrevActiveTabId.value = null; + pendingSaveShouldCloseTab.value = true; + cancelPendingAppClose(); +} + +function cancelPendingAppClose() { + pendingAppCloseAction.value = null; + pendingCloseActionChoice.value = false; + pendingSaveShouldCloseTab.value = true; +} + +function finishPendingAppClose(action: AppCloseAction) { + if (pendingCloseActionChoice.value) { + pendingCloseActionChoice.value = false; + showCloseActionPrompt.value = true; + return; + } + pendingAppCloseAction.value = null; + pendingSaveShouldCloseTab.value = true; + queryStore.flushPendingPersist(); + void performCloseAction(action); +} + +function continuePendingAppCloseAfterSave() { + const action = pendingAppCloseAction.value; + if (!action) return; + if (queryStore.hasDirtyTabs) { + pendingSaveShouldCloseTab.value = false; + if (queryStore.requestAppCloseConfirmation()) return; + } + finishPendingAppClose(action); +} + +function requestAppClose(action: AppCloseAction, options: AppCloseRequestOptions = {}) { + pendingCloseActionChoice.value = !!options.requireCloseActionChoice; + if (queryStore.hasDirtyTabs) { + pendingAppCloseAction.value = action; + pendingSaveShouldCloseTab.value = false; + if (queryStore.requestAppCloseConfirmation()) return; + } + finishPendingAppClose(action); +} + +function completePendingTabSave(tabId: string) { + if (pendingAppCloseAction.value) { + continuePendingAppCloseAfterSave(); + return; + } + queryStore.closeTab(tabId, { force: true }); +} + +function handleDiscardPendingTabClose() { + if (!pendingAppCloseAction.value) return; + continuePendingAppCloseAfterSave(); +} + +function handleDiscardAllPendingTabClose() { + if (!pendingAppCloseAction.value) return; + continuePendingAppCloseAfterSave(); +} + +function handleCloseActionPromptOpenChange(open: boolean) { + showCloseActionPrompt.value = open; + if (!open) { + cancelCloseActionPrompt(); + cancelPendingAppClose(); + } } async function saveExternalSqlPath(tab: QueryTab, options: { closeAfterSave?: boolean } = {}): Promise { @@ -523,15 +600,69 @@ async function saveExternalSqlPath(tab: QueryTab, options: { closeAfterSave?: bo } } +async function saveTabForCloseAll(tabId: string): Promise { + const tab = queryStore.tabs.find((t) => t.id === tabId); + if (!tab || !canSaveSqlTab(tab)) return true; + queryStore.activeTabId = tabId; + + if (tab.objectSource) return saveActiveObjectSource(tab); + + if (await saveExternalSqlPath(tab)) return !queryStore.isTabDirty(tab); + + const existing = tab.savedSqlId ? savedSqlStore.getFile(tab.savedSqlId) : undefined; + try { + const saved = await savedSqlStore.saveFile({ + id: existing?.id, + connectionId: tab.connectionId, + folderId: existing?.folderId, + name: existing?.name || defaultSavedSqlName(tab.title), + database: tab.database, + schema: tab.schema, + sql: tab.sql, + }); + queryStore.linkSavedSql(tab.id, saved.id, saved.name); + queryStore.markTabClean(tab); + return true; + } catch (e: any) { + toast(t("savedSql.saveFailed", { message: e?.message || String(e) }), 5000); + return false; + } +} + +async function handleSaveAllPendingTabClose() { + const ids = [...queryStore.closeConfirmDirtyTabIds]; + if (!ids.length) return; + queryStore.suspendCloseConfirm(); + + for (const id of ids) { + const saved = await saveTabForCloseAll(id); + if (!saved) break; + } + + if (queryStore.closeConfirmDirtyTabIds.length > 0) { + queryStore.resumeCloseConfirm(); + return; + } + + const result = queryStore.completePendingCloseAfterSaveAll(); + if (result === "app") continuePendingAppCloseAfterSave(); +} + async function handleSaveTab(tabId: string) { const tab = queryStore.tabs.find((t) => t.id === tabId); if (!tab || !canSaveSqlTab(tab)) return; + const closeAfterSave = pendingAppCloseAction.value === null; + pendingSaveShouldCloseTab.value = closeAfterSave; if (tab.objectSource) { const saved = await saveActiveObjectSource(tab); - if (saved) queryStore.closeTab(tabId, { force: true }); + if (saved) completePendingTabSave(tabId); + else if (pendingAppCloseAction.value) cancelPendingAppClose(); + return; + } + if (await saveExternalSqlPath(tab, { closeAfterSave })) { + if (!closeAfterSave) continuePendingAppCloseAfterSave(); return; } - if (await saveExternalSqlPath(tab, { closeAfterSave: true })) return; const existing = tab.savedSqlId ? savedSqlStore.getFile(tab.savedSqlId) : undefined; if (existing) { const updated = await savedSqlStore.saveFile({ @@ -546,7 +677,7 @@ async function handleSaveTab(tabId: string) { queryStore.linkSavedSql(tab.id, updated.id, updated.name); queryStore.markTabClean(tab); toast(t("savedSql.saved"), 2000); - queryStore.closeTab(tabId, { force: true }); + completePendingTabSave(tabId); return; } // No existing saved SQL — open save dialog, then close after save @@ -1599,6 +1730,10 @@ onUnmounted(() => { @close-driver-store="closeDriverStorePage" @close-settings-page="closeSettingsPage" @save-tab="handleSaveTab" + @discard-tab-close="handleDiscardPendingTabClose" + @save-all-tab-close="handleSaveAllPendingTabClose" + @discard-all-tab-close="handleDiscardAllPendingTabClose" + @cancel-tab-close="cancelPendingAppClose" /> { @download-and-install="downloadAndInstallUpdate" @restart="restartApp" /> - + @@ -1813,7 +1948,7 @@ onUnmounted(() => { @update:open=" (open: boolean) => { showSaveSqlDialog = open; - if (!open) pendingSaveAndCloseTabId = null; + if (!open && pendingSaveAndCloseTabId) cancelPendingSaveAndClose(); } " > @@ -1843,14 +1978,7 @@ onUnmounted(() => { - + diff --git a/apps/desktop/src/components/editor/EditorSettingsDialog.vue b/apps/desktop/src/components/editor/EditorSettingsDialog.vue index 703ab6fe9d..0aef9a8f4a 100644 --- a/apps/desktop/src/components/editor/EditorSettingsDialog.vue +++ b/apps/desktop/src/components/editor/EditorSettingsDialog.vue @@ -214,6 +214,7 @@ const editWordWrap = ref(settingsStore.editorSettings.wordWrap); const editSqlSemanticDiagnosticsMode = ref(settingsStore.editorSettings.sqlSemanticDiagnosticsMode); const editSqlSemanticDiagnosticsEnabled = ref(settingsStore.editorSettings.sqlSemanticDiagnosticsEnabled); const editConfirmDangerousSqlExecution = ref(settingsStore.editorSettings.confirmDangerousSqlExecution); +const editConfirmUnsavedSqlClose = ref(settingsStore.editorSettings.confirmUnsavedSqlClose); const editAppLayout = ref(settingsStore.editorSettings.appLayout); const editShowTrayIcon = ref(settingsStore.desktopSettings.show_tray_icon); const editQuitOnClose = ref(settingsStore.desktopSettings.quit_on_close); @@ -495,6 +496,7 @@ watch( editSqlSemanticDiagnosticsMode.value = settingsStore.editorSettings.sqlSemanticDiagnosticsMode; editSqlSemanticDiagnosticsEnabled.value = settingsStore.editorSettings.sqlSemanticDiagnosticsEnabled; editConfirmDangerousSqlExecution.value = settingsStore.editorSettings.confirmDangerousSqlExecution; + editConfirmUnsavedSqlClose.value = settingsStore.editorSettings.confirmUnsavedSqlClose; editAppLayout.value = settingsStore.editorSettings.appLayout; editShowTrayIcon.value = settingsStore.desktopSettings.show_tray_icon; editQuitOnClose.value = settingsStore.desktopSettings.quit_on_close; @@ -562,6 +564,7 @@ function hasChanges(): boolean { editSqlSemanticDiagnosticsMode.value !== settingsStore.editorSettings.sqlSemanticDiagnosticsMode || editSqlSemanticDiagnosticsEnabled.value !== settingsStore.editorSettings.sqlSemanticDiagnosticsEnabled || editConfirmDangerousSqlExecution.value !== settingsStore.editorSettings.confirmDangerousSqlExecution || + editConfirmUnsavedSqlClose.value !== settingsStore.editorSettings.confirmUnsavedSqlClose || editAppLayout.value !== settingsStore.editorSettings.appLayout || editShowTrayIcon.value !== settingsStore.desktopSettings.show_tray_icon || editQuitOnClose.value !== settingsStore.desktopSettings.quit_on_close || @@ -614,6 +617,7 @@ async function persistSettings() { wordWrap: editWordWrap.value, sqlSemanticDiagnosticsMode: editSqlSemanticDiagnosticsMode.value, confirmDangerousSqlExecution: editConfirmDangerousSqlExecution.value, + confirmUnsavedSqlClose: editConfirmUnsavedSqlClose.value, appLayout: editAppLayout.value, showColumnCommentsInHeader: editShowColumnCommentsInHeader.value, showColumnTypesInHeader: editShowColumnTypesInHeader.value, @@ -678,6 +682,7 @@ function resetDefaultsForTab(tab: SettingsCategory) { editSqlSemanticDiagnosticsMode.value = DEFAULT_EDITOR_SETTINGS.sqlSemanticDiagnosticsMode; editSqlSemanticDiagnosticsEnabled.value = DEFAULT_EDITOR_SETTINGS.sqlSemanticDiagnosticsEnabled; editConfirmDangerousSqlExecution.value = DEFAULT_EDITOR_SETTINGS.confirmDangerousSqlExecution; + editConfirmUnsavedSqlClose.value = DEFAULT_EDITOR_SETTINGS.confirmUnsavedSqlClose; } else if (tab === "formatter") { editSqlFormatter.value = normalizeSqlFormatterSettings(DEFAULT_EDITOR_SETTINGS.sqlFormatter); sqlFormatterConfigValid.value = true; @@ -740,6 +745,7 @@ function resetAllDefaults() { editSqlSemanticDiagnosticsMode.value = DEFAULT_EDITOR_SETTINGS.sqlSemanticDiagnosticsMode; editSqlSemanticDiagnosticsEnabled.value = DEFAULT_EDITOR_SETTINGS.sqlSemanticDiagnosticsEnabled; editConfirmDangerousSqlExecution.value = DEFAULT_EDITOR_SETTINGS.confirmDangerousSqlExecution; + editConfirmUnsavedSqlClose.value = DEFAULT_EDITOR_SETTINGS.confirmUnsavedSqlClose; editAppLayout.value = DEFAULT_EDITOR_SETTINGS.appLayout; editShowTrayIcon.value = DEFAULT_DESKTOP_SETTINGS.show_tray_icon; editQuitOnClose.value = DEFAULT_DESKTOP_SETTINGS.quit_on_close; @@ -1834,6 +1840,12 @@ async function ensureCodexMcpStatus() { const previewRef = ref(); const previewView = shallowRef(null); +interface PreviewSqlDiagnostic { + from: number; + to: number; + message: string; +} + function getPreviewCustomThemeColors(): CustomThemeColors | undefined { if (editTheme.value !== "custom") return undefined; const activeTheme = editCustomThemes.value.find((t) => t.id === editActiveCustomThemeId.value); @@ -1854,13 +1866,46 @@ const previewSettings = computed<{ customColors: getPreviewCustomThemeColors(), })); -const previewSql = `SELECT u.id, u.name +const previewSqlNormal = `SELECT u.id, u.name FROM users u ORDER BY u.id LIMIT 5;`; +const previewSqlWithSyntaxError = `SELECT u.id, u.name +FOM users u +ORDER BY u.id LIMIT 5;`; let fontThemeComp: import("@codemirror/state").Compartment | null = null; let themeComp: import("@codemirror/state").Compartment | null = null; +let diagnosticComp: import("@codemirror/state").Compartment | null = null; +let setPreviewDiagnosticsEffect: import("@codemirror/state").StateEffectType | null = null; let editorViewModule: typeof import("@codemirror/view") | null = null; +let previewSqlDiagnostics: PreviewSqlDiagnostic[] = []; + +function currentPreviewSql(): string { + return editSqlSemanticDiagnosticsEnabled.value ? previewSqlWithSyntaxError : previewSqlNormal; +} + +function previewDiagnosticsForSql(sql: string): PreviewSqlDiagnostic[] { + if (!editSqlSemanticDiagnosticsEnabled.value) return []; + const from = sql.indexOf("FOM"); + return from >= 0 ? [{ from, to: from + 3, message: "Syntax error: expected FROM" }] : []; +} + +function updatePreviewSqlDiagnostics() { + const view = previewView.value; + if (!view || !setPreviewDiagnosticsEffect) return; + const nextSql = currentPreviewSql(); + const currentSql = view.state.doc.toString(); + previewSqlDiagnostics = previewDiagnosticsForSql(nextSql); + const effects = setPreviewDiagnosticsEffect.of(previewSqlDiagnostics); + if (currentSql === nextSql) { + view.dispatch({ effects }); + return; + } + view.dispatch({ + changes: { from: 0, to: currentSql.length, insert: nextSql }, + effects, + }); +} watch( [previewSettings, editCustomThemes, editActiveCustomThemeId], @@ -1875,6 +1920,10 @@ watch( { deep: true }, ); +watch(editSqlSemanticDiagnosticsEnabled, () => { + updatePreviewSqlDiagnostics(); +}); + let previewInitialized = false; function cleanupPreviewEditor() { @@ -1884,7 +1933,10 @@ function cleanupPreviewEditor() { previewInitialized = false; fontThemeComp = null; themeComp = null; + diagnosticComp = null; + setPreviewDiagnosticsEffect = null; editorViewModule = null; + previewSqlDiagnostics = []; } watch(activeSettingsTab, (tab) => { @@ -1898,18 +1950,51 @@ watch(previewRef, async (el) => { previewInitialized = true; if (previewView.value) return; - const [{ EditorView }, { EditorState, Compartment }, { sql, MySQL }, { basicSetup }] = await Promise.all([import("@codemirror/view"), import("@codemirror/state"), import("@codemirror/lang-sql"), import("codemirror")]); + const [{ EditorView, Decoration }, { EditorState, Compartment, StateEffect, StateField }, { sql, MySQL }, { basicSetup }] = await Promise.all([import("@codemirror/view"), import("@codemirror/state"), import("@codemirror/lang-sql"), import("codemirror")]); editorViewModule = { EditorView } as typeof import("@codemirror/view"); fontThemeComp = new Compartment(); themeComp = new Compartment(); + diagnosticComp = new Compartment(); + setPreviewDiagnosticsEffect = StateEffect.define(); + previewSqlDiagnostics = previewDiagnosticsForSql(currentPreviewSql()); const ss = previewSettings.value; const themeExt = await loadEditorTheme(ss.theme, ss.appAppearance, ss.customColors); + const diagnosticTheme = EditorView.baseTheme({ + ".cm-settings-preview-sql-error": { + textDecoration: "underline wavy var(--destructive)", + textUnderlineOffset: "3px", + }, + }); + const buildPreviewDiagnosticExtension = () => { + const diagnosticEffect = setPreviewDiagnosticsEffect; + const buildDecorations = () => + Decoration.set( + previewSqlDiagnostics.map((diagnostic) => + Decoration.mark({ + class: "cm-settings-preview-sql-error", + attributes: { title: diagnostic.message }, + }).range(diagnostic.from, diagnostic.to), + ), + true, + ); + + const field = StateField.define({ + create: buildDecorations, + update(value, transaction) { + const diagnosticsChanged = !!diagnosticEffect && transaction.effects.some((effect) => effect.is(diagnosticEffect)); + return transaction.docChanged || diagnosticsChanged ? buildDecorations() : value; + }, + provide: (field) => EditorView.decorations.from(field), + }); + + return [field, diagnosticTheme]; + }; const state = EditorState.create({ - doc: previewSql, - extensions: [basicSetup, sql({ dialect: MySQL }), themeComp.of(themeExt), fontThemeComp.of(editorFontTheme(EditorView, ss.fontSize, ss.fontFamily))], + doc: currentPreviewSql(), + extensions: [basicSetup, sql({ dialect: MySQL }), themeComp.of(themeExt), fontThemeComp.of(editorFontTheme(EditorView, ss.fontSize, ss.fontFamily)), diagnosticComp.of(buildPreviewDiagnosticExtension())], }); previewView.value = new EditorView({ state, parent: previewRef.value }); @@ -2080,6 +2165,16 @@ onUnmounted(cleanupPreviewEditor); + +
+
+ +

+ {{ t("settings.confirmUnsavedSqlCloseDescription") }} +

+
+ +
diff --git a/apps/desktop/src/components/layout/AppTabBar.vue b/apps/desktop/src/components/layout/AppTabBar.vue index 95ceaa7979..701266d751 100644 --- a/apps/desktop/src/components/layout/AppTabBar.vue +++ b/apps/desktop/src/components/layout/AppTabBar.vue @@ -1,10 +1,11 @@