Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(grid): simplify image preview controls

t8y2f0a1d39a861a
diff --git a/apps/desktop/src/components/grid/ImagePreviewDialog.vue b/apps/desktop/src/components/grid/ImagePreviewDialog.vue
index 4449194003..4630df1d24 100644
--- a/apps/desktop/src/components/grid/ImagePreviewDialog.vue
+++ b/apps/desktop/src/components/grid/ImagePreviewDialog.vue
@@ -1,10 +1,10 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
-import { ExternalLink, Maximize2, Minimize2, RotateCw, X, ZoomIn, ZoomOut } from "lucide-vue-next";
+import { ExternalLink, Maximize2, X, ZoomIn, ZoomOut } from "lucide-vue-next";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
-import { imagePreviewTransform, nextImagePreviewScale } from "@/lib/imagePreviewViewer";
+import { imagePreviewFitScale, imagePreviewTransform, nextImagePreviewScale } from "@/lib/imagePreviewViewer";
const props = defineProps<{
open: boolean;
@@ -18,11 +18,13 @@ const emit = defineEmits<{
const { t } = useI18n();
const scale = ref(1);
-const rotation = ref(0);
const offsetX = ref(0);
const offsetY = ref(0);
const imageLoaded = ref(false);
const imageError = ref(false);
+const stageRef = ref<HTMLElement>();
+const naturalWidth = ref(0);
+const naturalHeight = ref(0);
const dragStart = ref<{ x: number; y: number; offsetX: number; offsetY: number } | null>(null);
const hostLabel = computed(() => {
@@ -37,9 +39,10 @@ const hostLabel = computed(() => {
const imageTitle = computed(() => props.title || t("grid.imagePreview"));
const zoomLabel = computed(() => `${Math.round(scale.value * 100)}%`);
const imageStyle = computed(() => ({
+ width: naturalWidth.value ? `${naturalWidth.value}px` : "auto",
+ height: naturalHeight.value ? `${naturalHeight.value}px` : "auto",
transform: `translate(-50%, -50%) ${imagePreviewTransform({
scale: scale.value,
- rotation: rotation.value,
offsetX: offsetX.value,
offsetY: offsetY.value,
})}`,
@@ -47,11 +50,12 @@ const imageStyle = computed(() => ({
function resetViewer() {
scale.value = 1;
- rotation.value = 0;
offsetX.value = 0;
offsetY.value = 0;
imageLoaded.value = false;
imageError.value = false;
+ naturalWidth.value = 0;
+ naturalHeight.value = 0;
dragStart.value = null;
}
@@ -68,19 +72,28 @@ function zoomOut() {
}
function fitImage() {
- scale.value = 1;
+ scale.value = currentFitScale();
offsetX.value = 0;
offsetY.value = 0;
}
-function actualSize() {
- scale.value = 1;
- offsetX.value = 0;
- offsetY.value = 0;
+function currentFitScale(): number {
+ const stage = stageRef.value;
+ if (!stage) return 1;
+ return imagePreviewFitScale({
+ imageWidth: naturalWidth.value,
+ imageHeight: naturalHeight.value,
+ viewportWidth: stage.clientWidth,
+ viewportHeight: stage.clientHeight,
+ });
}
-function rotateImage() {
- rotation.value = (rotation.value + 90) % 360;
+function onImageLoad(event: Event) {
+ const img = event.currentTarget as HTMLImageElement;
+ naturalWidth.value = img.naturalWidth;
+ naturalHeight.value = img.naturalHeight;
+ imageLoaded.value = true;
+ fitImage();
}
function onWheel(event: WheelEvent) {
@@ -111,8 +124,13 @@ function onDoubleClick() {
offsetY.value = 0;
}
-function openExternal() {
- window.open(props.src, "_blank", "noopener,noreferrer");
+async function openExternal() {
+ try {
+ const { open } = await import("@tauri-apps/plugin-shell");
+ await open(props.src);
+ } catch {
+ window.open(props.src, "_blank", "noopener,noreferrer");
+ }
}
watch(
@@ -164,24 +182,6 @@ watch(
>
<Maximize2 class="h-3.5 w-3.5" />
</Button>
- <Button
- variant="ghost"
- size="icon"
- class="h-7 w-7 text-white/75 hover:bg-white/10 hover:text-white"
- :title="t('grid.actualSize')"
- @click="actualSize"
- >
- <Minimize2 class="h-3.5 w-3.5" />
- </Button>
- <Button
- variant="ghost"
- size="icon"
- class="h-7 w-7 text-white/75 hover:bg-white/10 hover:text-white"
- :title="t('grid.rotateImage')"
- @click="rotateImage"
- >
- <RotateCw class="h-3.5 w-3.5" />
- </Button>
<Button
variant="ghost"
size="icon"
@@ -204,6 +204,7 @@ watch(
</div>
<div
+ ref="stageRef"
class="image-preview-stage relative min-h-0 flex-1 overflow-hidden"
:class="{ 'cursor-grabbing': dragStart, 'cursor-grab': !dragStart && imageLoaded && !imageError }"
@wheel="onWheel"
@@ -226,10 +227,10 @@ watch(
draggable="false"
decoding="async"
referrerpolicy="no-referrer"
- class="absolute left-1/2 top-1/2 max-h-[78vh] max-w-[88vw] select-none object-contain transition-opacity duration-150"
+ class="absolute left-1/2 top-1/2 max-w-none select-none object-contain transition-opacity duration-150"
:class="{ 'opacity-100': imageLoaded, 'opacity-0': !imageLoaded }"
:style="imageStyle"
- @load="imageLoaded = true"
+ @load="onImageLoad"
@error="imageError = true"
@pointerdown="onImagePointerDown"
@pointermove="onImagePointerMove"
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index dc328c8c16..9fa4e95d9b 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -347,8 +347,6 @@ export default {
zoomIn: "Zoom In",
zoomOut: "Zoom Out",
fitImage: "Fit to Window",
- actualSize: "Actual Size",
- rotateImage: "Rotate",
openImage: "Open Image",
columnName: "Column",
columnType: "Type",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index 9f1f881d91..5f33c6d0db 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -323,8 +323,6 @@ export default {
zoomIn: "Acercar",
zoomOut: "Alejar",
fitImage: "Ajustar a ventana",
- actualSize: "Tamaño real",
- rotateImage: "Rotar",
openImage: "Abrir imagen",
columnName: "Columna",
columnType: "Tipo",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index 074c654c2b..68377c7511 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -343,8 +343,6 @@ export default {
zoomIn: "放大",
zoomOut: "缩小",
fitImage: "适应窗口",
- actualSize: "原始大小",
- rotateImage: "旋转",
openImage: "打开图片",
columnName: "列名",
columnType: "类型",
diff --git a/apps/desktop/src/lib/imagePreviewViewer.ts b/apps/desktop/src/lib/imagePreviewViewer.ts
index cbe3f73fe6..7e6b942c14 100644
--- a/apps/desktop/src/lib/imagePreviewViewer.ts
+++ b/apps/desktop/src/lib/imagePreviewViewer.ts
@@ -14,11 +14,27 @@ export function nextImagePreviewScale(scale: number, direction: ImagePreviewZoom
return clampImagePreviewScale(scale + delta);
}
-export function imagePreviewTransform(options: {
- scale: number;
- rotation: number;
- offsetX: number;
- offsetY: number;
-}): string {
- return `translate(${options.offsetX}px, ${options.offsetY}px) rotate(${options.rotation}deg) scale(${options.scale})`;
+export function imagePreviewFitScale(options: {
+ imageWidth: number;
+ imageHeight: number;
+ viewportWidth: number;
+ viewportHeight: number;
+ paddingRatio?: number;
+}): number {
+ if (
+ options.imageWidth <= 0 ||
+ options.imageHeight <= 0 ||
+ options.viewportWidth <= 0 ||
+ options.viewportHeight <= 0
+ ) {
+ return 1;
+ }
+ const paddingRatio = options.paddingRatio ?? 0.9;
+ const widthScale = (options.viewportWidth * paddingRatio) / options.imageWidth;
+ const heightScale = (options.viewportHeight * paddingRatio) / options.imageHeight;
+ return clampImagePreviewScale(Math.min(widthScale, heightScale, 1));
+}
+
+export function imagePreviewTransform(options: { scale: number; offsetX: number; offsetY: number }): string {
+ return `translate(${options.offsetX}px, ${options.offsetY}px) scale(${options.scale})`;
}
diff --git a/packages/app-tests/imagePreviewViewer.test.ts b/packages/app-tests/imagePreviewViewer.test.ts
index 1a89addbba..914a4f6835 100644
--- a/packages/app-tests/imagePreviewViewer.test.ts
+++ b/packages/app-tests/imagePreviewViewer.test.ts
@@ -1,6 +1,7 @@
import { strict as assert } from "node:assert";
import test from "node:test";
import {
+ imagePreviewFitScale,
clampImagePreviewScale,
imagePreviewTransform,
nextImagePreviewScale,
@@ -20,7 +21,28 @@ test("zooms in fixed steps from wheel direction", () => {
test("builds a stable CSS transform for image previews", () => {
assert.equal(
- imagePreviewTransform({ scale: 1.5, rotation: 90, offsetX: 12, offsetY: -8 }),
- "translate(12px, -8px) rotate(90deg) scale(1.5)",
+ imagePreviewTransform({ scale: 1.5, offsetX: 12, offsetY: -8 }),
+ "translate(12px, -8px) scale(1.5)",
+ );
+});
+
+test("calculates fit scale from image and viewport dimensions", () => {
+ assert.equal(
+ imagePreviewFitScale({
+ imageWidth: 2000,
+ imageHeight: 1000,
+ viewportWidth: 1000,
+ viewportHeight: 800,
+ }),
+ 0.45,
+ );
+ assert.equal(
+ imagePreviewFitScale({
+ imageWidth: 200,
+ imageHeight: 100,
+ viewportWidth: 1000,
+ viewportHeight: 800,
+ }),
+ 1,
);
});