fix(ui): clamp wheel zoom
cx
This commit is contained in:
1 parent
4e0327635e
commit
b1803e4d85
3 files changed
+100
-15
No files matched your search
@@ -11,6 +11,7 @@ import { goBack, goForward } from '$lib/stores/navigationStore';
|
||||
import { loadToastsPreference } from '$lib/stores/toastStore';
|
||||
import { loadSystemNotificationsPreference, flushScrobbleQueue } from '$lib/services/playbackService';
|
||||
import { initOfflineStore, cleanupOfflineStore, onOnlineTransition } from '$lib/stores/offlineStore';
|
||||
import { clampZoom, getNextZoomLevel } from '$lib/utils/zoom';
|
||||
|
||||
// ============ Theme Management ============
|
||||
|
||||
@@ -34,12 +35,50 @@ export async function applySavedZoom(): Promise<void> {
|
||||
if (!Number.isFinite(zoom) || zoom <= 0) return;
|
||||
|
||||
try {
|
||||
await getCurrentWebview().setZoom(zoom);
|
||||
const clamped = clampZoom(zoom);
|
||||
if (clamped !== zoom) {
|
||||
localStorage.setItem('qbz-zoom-level', String(clamped));
|
||||
}
|
||||
await getCurrentWebview().setZoom(clamped);
|
||||
} catch (err) {
|
||||
console.warn('Failed to apply saved zoom:', err);
|
||||
}
|
||||
}
|
||||
|
||||
function getStoredZoom(): number {
|
||||
const savedZoom = localStorage.getItem('qbz-zoom-level');
|
||||
const parsed = Number.parseFloat(savedZoom ?? '');
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) {
|
||||
return 1;
|
||||
}
|
||||
return clampZoom(parsed);
|
||||
}
|
||||
|
||||
async function applyZoomLevel(zoom: number): Promise<void> {
|
||||
const clamped = clampZoom(zoom);
|
||||
localStorage.setItem('qbz-zoom-level', String(clamped));
|
||||
try {
|
||||
await getCurrentWebview().setZoom(clamped);
|
||||
} catch (err) {
|
||||
console.warn('Failed to set zoom level:', err);
|
||||
}
|
||||
}
|
||||
|
||||
function handleZoomWheel(event: WheelEvent): void {
|
||||
if (!event.ctrlKey && !event.metaKey) return;
|
||||
if (event.deltaY === 0) return;
|
||||
|
||||
event.preventDefault();
|
||||
const direction = event.deltaY < 0 ? 'in' : 'out';
|
||||
const nextZoom = getNextZoomLevel(getStoredZoom(), direction);
|
||||
void applyZoomLevel(nextZoom);
|
||||
}
|
||||
|
||||
export function setupZoomControls(): () => void {
|
||||
window.addEventListener('wheel', handleZoomWheel, { passive: false });
|
||||
return () => window.removeEventListener('wheel', handleZoomWheel);
|
||||
}
|
||||
|
||||
// ============ Last.fm Session ============
|
||||
|
||||
/**
|
||||
@@ -105,6 +144,7 @@ export function bootstrapApp(): BootstrapResult {
|
||||
|
||||
// Setup mouse navigation
|
||||
const cleanupMouse = setupMouseNavigation();
|
||||
const cleanupZoom = setupZoomControls();
|
||||
|
||||
// Restore Last.fm session (async, fire-and-forget)
|
||||
restoreLastfmSession();
|
||||
@@ -125,6 +165,7 @@ export function bootstrapApp(): BootstrapResult {
|
||||
return {
|
||||
cleanup: () => {
|
||||
cleanupMouse();
|
||||
cleanupZoom();
|
||||
cleanupOfflineStore();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
import { get } from 'svelte/store';
|
||||
import MigrationModal from '../MigrationModal.svelte';
|
||||
import { getDevicePrettyName } from '$lib/utils/audioDeviceNames';
|
||||
import { ZOOM_OPTIONS, findZoomOption, getZoomLevelFromOption } from '$lib/utils/zoom';
|
||||
import {
|
||||
subscribe as subscribeOffline,
|
||||
getStatus as getOfflineStatus,
|
||||
@@ -350,17 +351,7 @@
|
||||
let normalizeVolume = $state(false);
|
||||
|
||||
// UI scale settings
|
||||
const zoomOptions = ['80%', '90%', '100%', '110%', '125%', '150%', '175%', '200%'];
|
||||
const zoomMap: Record<string, number> = {
|
||||
'80%': 0.8,
|
||||
'90%': 0.9,
|
||||
'100%': 1,
|
||||
'110%': 1.1,
|
||||
'125%': 1.25,
|
||||
'150%': 1.5,
|
||||
'175%': 1.75,
|
||||
'200%': 2,
|
||||
};
|
||||
const zoomOptions = [...ZOOM_OPTIONS];
|
||||
let zoomLevel = $state('100%');
|
||||
|
||||
// Appearance settings
|
||||
@@ -416,8 +407,8 @@
|
||||
const savedZoom = localStorage.getItem('qbz-zoom-level');
|
||||
if (savedZoom) {
|
||||
const parsed = Number.parseFloat(savedZoom);
|
||||
const match = zoomOptions.find(option => Math.abs((zoomMap[option] ?? 1) - parsed) < 0.01);
|
||||
zoomLevel = match || '100%';
|
||||
const match = findZoomOption(parsed);
|
||||
zoomLevel = match ?? '100%';
|
||||
}
|
||||
|
||||
// Load library settings
|
||||
@@ -1365,7 +1356,7 @@
|
||||
|
||||
async function handleZoomChange(value: string) {
|
||||
zoomLevel = value;
|
||||
const zoom = zoomMap[value] ?? 1;
|
||||
const zoom = getZoomLevelFromOption(value);
|
||||
localStorage.setItem('qbz-zoom-level', String(zoom));
|
||||
try {
|
||||
await getCurrentWebview().setZoom(zoom);
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
export const ZOOM_OPTIONS = ['80%', '90%', '100%', '110%', '125%', '150%', '175%', '200%'] as const;
|
||||
|
||||
export const ZOOM_LEVELS = ZOOM_OPTIONS.map((option) => {
|
||||
const parsed = Number.parseFloat(option);
|
||||
return Number.isFinite(parsed) ? parsed / 100 : 1;
|
||||
});
|
||||
|
||||
export const ZOOM_MAP: Record<string, number> = Object.fromEntries(
|
||||
ZOOM_OPTIONS.map((option, index) => [option, ZOOM_LEVELS[index]])
|
||||
);
|
||||
|
||||
export const ZOOM_MIN = ZOOM_LEVELS[0];
|
||||
export const ZOOM_MAX = ZOOM_LEVELS[ZOOM_LEVELS.length - 1];
|
||||
|
||||
export function getZoomLevelFromOption(option: string): number {
|
||||
return ZOOM_MAP[option] ?? 1;
|
||||
}
|
||||
|
||||
export function findZoomOption(level: number): string | null {
|
||||
const epsilon = 0.01;
|
||||
for (const option of ZOOM_OPTIONS) {
|
||||
if (Math.abs((ZOOM_MAP[option] ?? 1) - level) < epsilon) {
|
||||
return option;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function clampZoom(level: number): number {
|
||||
return Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, level));
|
||||
}
|
||||
|
||||
export function getNextZoomLevel(level: number, direction: 'in' | 'out'): number {
|
||||
const clamped = clampZoom(level);
|
||||
const epsilon = 0.001;
|
||||
|
||||
if (direction === 'in') {
|
||||
for (const value of ZOOM_LEVELS) {
|
||||
if (value > clamped + epsilon) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return ZOOM_MAX;
|
||||
}
|
||||
|
||||
for (let index = ZOOM_LEVELS.length - 1; index >= 0; index -= 1) {
|
||||
if (ZOOM_LEVELS[index] < clamped - epsilon) {
|
||||
return ZOOM_LEVELS[index];
|
||||
}
|
||||
}
|
||||
|
||||
return ZOOM_MIN;
|
||||
}
|
||||
Reference in new issue
Block a user