Keep the window draggable while a modal is open

Co-Authored-By: Rūdolfs Ošiņš <rudolfs@osins.org>
This commit is contained in:
Brandon Oxendine
2026-09-09 12:23:16 +02:00
committed by Rūdolfs Ošiņš
co-authored by Rūdolfs Ošiņš
parent 982a568538
commit 55e18a45f0
3 changed files with 91 additions and 27 deletions
+2 -27
View File
@@ -4,7 +4,6 @@
import type { RepoInfo } from "@bindings/repo/RepoInfo";
import { listen } from "@tauri-apps/api/event";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { onDestroy, onMount } from "svelte";
import { get } from "svelte/store";
@@ -27,6 +26,7 @@
unlistenNodeEvents,
} from "@app/lib/startup.svelte";
import { isMac, unreachable } from "@app/lib/utils";
import { dragWindow } from "@app/lib/window";
import AppSidebar from "@app/components/AppSidebar.svelte";
import { codeFont } from "@app/components/CodeFontSwitch.svelte";
@@ -60,27 +60,6 @@
return isLoadedRepoRoute(route) ? route.params.repo : undefined;
});
const DRAG_REGION_HEIGHT = 32;
const INTERACTIVE_TAGS = new Set([
"a",
"button",
"input",
"select",
"textarea",
]);
function isDraggableArea(e: MouseEvent): boolean {
if (e.clientY > DRAG_REGION_HEIGHT) return false;
let el = e.target as HTMLElement | null;
while (el && el !== document.body) {
if (INTERACTIVE_TAGS.has(el.tagName.toLowerCase())) return false;
if (el.getAttribute("role") === "button") return false;
if (el.classList.contains("txt-selectable")) return false;
el = el.parentElement;
}
return true;
}
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", ({ matches }) => {
@@ -165,11 +144,7 @@
</style>
<svelte:document
onmousedown={e => {
if (window.__TAURI_INTERNALS__ && isDraggableArea(e)) {
void getCurrentWindow().startDragging();
}
}}
onmousedown={dragWindow}
onkeydown={e => {
const auxiliarKey = isMac() ? e.metaKey : e.ctrlKey;
// Handles the position of the plus key on different keyboard layouts.
@@ -1,5 +1,10 @@
<script lang="ts">
import { hide, modalStore } from "@app/lib/modal";
import { DRAG_REGION_HEIGHT } from "@app/lib/window";
// Only Tauri drags the window, and outside it the strip would do nothing but
// swallow the scrim clicks that dismiss the modal.
const dragRegion = Boolean(window.__TAURI_INTERNALS__);
</script>
<style>
@@ -20,6 +25,14 @@
position: fixed;
}
.drag-region {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 100;
}
.content {
z-index: 200;
margin: auto;
@@ -36,6 +49,14 @@
onclick={$modalStore.disableScrimClose ? undefined : hide}
style:cursor={$modalStore.disableHide ? "not-allowed" : "default"}>
</div>
{#if dragRegion}
<div
class="drag-region"
style:height="{DRAG_REGION_HEIGHT}px"
style:cursor={$modalStore.disableHide ? "not-allowed" : "default"}
data-tauri-drag-region>
</div>
{/if}
<div class="content">
<svelte:component this={$modalStore.component} {...$modalStore.props} />
</div>
+68
View File
@@ -0,0 +1,68 @@
import { getCurrentWindow } from "@tauri-apps/api/window";
// The strip along the top edge of the window that drags it. Deliberately a
// fixed pixel height rather than rem: the root font size is user-configurable,
// and scaling the strip with it would swallow clicks at the top of every view.
export const DRAG_REGION_HEIGHT = 32;
const INTERACTIVE_TAGS = new Set([
"a",
"button",
"input",
"select",
"textarea",
]);
// A press in the top strip on something that isn't itself interactive. Anything
// carrying `data-tauri-drag-region` is excluded because Tauri's own script
// already owns it -- that script drags on mousedown and maximizes on double
// click, and doing either here as well would fight it.
export function isDraggableArea(e: MouseEvent): boolean {
if (e.clientY > DRAG_REGION_HEIGHT) return false;
let el = e.target as HTMLElement | null;
while (el && el !== document.body) {
if (INTERACTIVE_TAGS.has(el.tagName.toLowerCase())) return false;
if (el.getAttribute("role") === "button") return false;
if (el.classList.contains("txt-selectable")) return false;
if (el.hasAttribute("data-tauri-drag-region")) return false;
el = el.parentElement;
}
return true;
}
// A window drag suppresses the mouse events that follow it, so the second press
// of a double click can arrive with `detail` back at 1 and `dblclick` never
// arrives at all. Pair the presses by time and position instead, the way the
// sidebar's resize edge does.
const DOUBLE_PRESS_MS = 500;
const DOUBLE_PRESS_SLOP_PX = 6;
let lastPressAt = 0;
let lastPressX = 0;
let lastPressY = 0;
function isDoublePress(e: MouseEvent): boolean {
const paired =
e.timeStamp - lastPressAt < DOUBLE_PRESS_MS &&
Math.abs(e.clientX - lastPressX) <= DOUBLE_PRESS_SLOP_PX &&
Math.abs(e.clientY - lastPressY) <= DOUBLE_PRESS_SLOP_PX;
// Cleared so a third press starts a new pair instead of toggling again.
lastPressAt = paired ? 0 : e.timeStamp;
lastPressX = e.clientX;
lastPressY = e.clientY;
return paired;
}
// Drag the window by its top strip, and maximize it when that strip is double
// clicked, matching what Tauri's own drag regions do elsewhere in the app.
export function dragWindow(e: MouseEvent): void {
// Primary button only; a right-click here would otherwise start a drag.
if (!window.__TAURI_INTERNALS__ || e.button !== 0 || !isDraggableArea(e)) {
return;
}
if (isDoublePress(e)) {
void getCurrentWindow().toggleMaximize();
} else {
void getCurrentWindow().startDragging();
}
}