visual window picker with marching ants overlay
This commit is contained in:
+34
-140
@@ -4,14 +4,6 @@
|
||||
import { listen } from "@tauri-apps/api/event"
|
||||
import { invoke } from "@tauri-apps/api/core"
|
||||
|
||||
type WindowInfo = {
|
||||
title: string
|
||||
x: number
|
||||
y: number
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
const win = getCurrentWebviewWindow()
|
||||
|
||||
let width: number = $state(0)
|
||||
@@ -20,9 +12,6 @@
|
||||
let fps: number = $state(30)
|
||||
let elapsed: number = $state(0)
|
||||
let timer: number | null = null
|
||||
let pickerOpen: boolean = $state(false)
|
||||
let windows: WindowInfo[] = $state([])
|
||||
let loadingWindows: boolean = $state(false)
|
||||
|
||||
onMount(() => {
|
||||
const updateSize = async () => {
|
||||
@@ -78,36 +67,46 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function togglePicker() {
|
||||
if (pickerOpen) {
|
||||
pickerOpen = false
|
||||
return
|
||||
}
|
||||
loadingWindows = true
|
||||
pickerOpen = true
|
||||
async function openPicker() {
|
||||
try {
|
||||
windows = await invoke<WindowInfo[]>("list_windows")
|
||||
await invoke("open_window_picker")
|
||||
} catch (e) {
|
||||
console.error("list_windows", e)
|
||||
windows = []
|
||||
} finally {
|
||||
loadingWindows = false
|
||||
console.error("open_window_picker", e)
|
||||
}
|
||||
}
|
||||
|
||||
async function pickWindow(w: WindowInfo) {
|
||||
try {
|
||||
await invoke("set_overlay_to_window", {
|
||||
x: w.x,
|
||||
y: w.y,
|
||||
width: w.width,
|
||||
height: w.height,
|
||||
onMount(() => {
|
||||
let unlistenPick: (() => void) | null = null
|
||||
let unlistenCancel: (() => void) | null = null
|
||||
void (async () => {
|
||||
unlistenPick = await listen<{ x: number; y: number; width: number; height: number }>(
|
||||
"picker-pick",
|
||||
async (e) => {
|
||||
try {
|
||||
await invoke("set_overlay_to_window", e.payload)
|
||||
} catch (err) {
|
||||
console.error("set_overlay_to_window", err)
|
||||
}
|
||||
try {
|
||||
await invoke("close_window_picker")
|
||||
} catch (err) {
|
||||
console.error("close_window_picker", err)
|
||||
}
|
||||
},
|
||||
)
|
||||
unlistenCancel = await listen("picker-cancel", async () => {
|
||||
try {
|
||||
await invoke("close_window_picker")
|
||||
} catch (err) {
|
||||
console.error("close_window_picker", err)
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.error("set_overlay_to_window", e)
|
||||
})()
|
||||
return () => {
|
||||
unlistenPick?.()
|
||||
unlistenCancel?.()
|
||||
}
|
||||
pickerOpen = false
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<div class="frame">
|
||||
@@ -121,7 +120,7 @@
|
||||
<span class="dim" data-tauri-drag-region>{width} x {height}</span>
|
||||
<button
|
||||
class="pick-btn"
|
||||
onclick={togglePicker}
|
||||
onclick={openPicker}
|
||||
onmousedown={(e) => e.stopPropagation()}
|
||||
title="snap to window"
|
||||
type="button"
|
||||
@@ -132,31 +131,6 @@
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if pickerOpen}
|
||||
<div class="picker" role="dialog" tabindex="-1" onmousedown={(e) => e.stopPropagation()}>
|
||||
<div class="picker-head">
|
||||
<span>pick a window</span>
|
||||
<button onclick={() => (pickerOpen = false)} type="button">×</button>
|
||||
</div>
|
||||
{#if loadingWindows}
|
||||
<div class="picker-empty">listing...</div>
|
||||
{:else if windows.length === 0}
|
||||
<div class="picker-empty">no windows found</div>
|
||||
{:else}
|
||||
<ul class="picker-list">
|
||||
{#each windows as w}
|
||||
<li>
|
||||
<button onclick={() => pickWindow(w)} type="button">
|
||||
<span class="w-title">{w.title}</span>
|
||||
<span class="w-dim">{w.width}x{w.height}</span>
|
||||
</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
@@ -229,84 +203,4 @@
|
||||
background: rgba(0, 0, 0, 0.35);
|
||||
color: #fff;
|
||||
}
|
||||
.picker {
|
||||
background: #1a1a1a;
|
||||
border: 1px solid #4a8;
|
||||
border-radius: 4px;
|
||||
margin: 4px;
|
||||
max-height: calc(100vh - 32px);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
font: 12px ui-monospace, monospace;
|
||||
color: #ddd;
|
||||
}
|
||||
.picker-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 4px 8px;
|
||||
background: #222;
|
||||
border-bottom: 1px solid #2a2a2a;
|
||||
color: #aaa;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
font-size: 10px;
|
||||
}
|
||||
.picker-head button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #888;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
padding: 0 4px;
|
||||
}
|
||||
.picker-head button:hover {
|
||||
color: #fff;
|
||||
}
|
||||
.picker-empty {
|
||||
padding: 12px;
|
||||
text-align: center;
|
||||
color: #888;
|
||||
}
|
||||
.picker-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 4px;
|
||||
overflow-y: auto;
|
||||
flex: 1;
|
||||
}
|
||||
.picker-list li {
|
||||
margin: 0;
|
||||
}
|
||||
.picker-list button {
|
||||
width: 100%;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #ddd;
|
||||
font: inherit;
|
||||
text-align: left;
|
||||
padding: 4px 6px;
|
||||
border-radius: 2px;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
.picker-list button:hover {
|
||||
background: #2a3a35;
|
||||
color: #fff;
|
||||
}
|
||||
.w-title {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
}
|
||||
.w-dim {
|
||||
color: #888;
|
||||
font-size: 10px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte"
|
||||
import { listen } from "@tauri-apps/api/event"
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"
|
||||
|
||||
type Rect = {
|
||||
x: number
|
||||
y: number
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
let hover: Rect | null = $state(null)
|
||||
let scale: number = $state(1)
|
||||
let originX: number = $state(0)
|
||||
let originY: number = $state(0)
|
||||
|
||||
onMount(() => {
|
||||
const win = getCurrentWebviewWindow()
|
||||
let unlistenHover: (() => void) | null = null
|
||||
|
||||
void (async () => {
|
||||
scale = await win.scaleFactor()
|
||||
const pos = await win.outerPosition()
|
||||
originX = pos.x
|
||||
originY = pos.y
|
||||
|
||||
unlistenHover = await listen<Rect>("picker-hover", (e) => {
|
||||
hover = e.payload
|
||||
})
|
||||
})()
|
||||
|
||||
return () => {
|
||||
unlistenHover?.()
|
||||
}
|
||||
})
|
||||
|
||||
let style = $derived.by(() => {
|
||||
if (!hover) return ""
|
||||
const left = (hover.x - originX) / scale
|
||||
const top = (hover.y - originY) / scale
|
||||
const width = hover.width / scale
|
||||
const height = hover.height / scale
|
||||
return `left: ${left}px; top: ${top}px; width: ${width}px; height: ${height}px;`
|
||||
})
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<div class="dim"></div>
|
||||
{#if hover}
|
||||
<div class="frame" style={style}>
|
||||
<div class="dims">{hover.width} x {hover.height}</div>
|
||||
</div>
|
||||
{/if}
|
||||
<div class="hint">click a window to snap. Esc to cancel.</div>
|
||||
</main>
|
||||
|
||||
<style>
|
||||
:global(html), :global(body), :global(#picker) {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
height: 100vh;
|
||||
width: 100vw;
|
||||
overflow: hidden;
|
||||
cursor: crosshair;
|
||||
}
|
||||
main {
|
||||
position: relative;
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
}
|
||||
.dim {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
background: rgba(10, 14, 12, 0.18);
|
||||
}
|
||||
.frame {
|
||||
position: absolute;
|
||||
box-sizing: border-box;
|
||||
border: 2px dashed #4a8;
|
||||
background: rgba(74, 170, 136, 0.08);
|
||||
animation: march 0.6s linear infinite;
|
||||
pointer-events: none;
|
||||
}
|
||||
@keyframes march {
|
||||
to { background-position: 16px 0; }
|
||||
}
|
||||
.dims {
|
||||
position: absolute;
|
||||
top: -22px;
|
||||
left: 0;
|
||||
padding: 1px 6px;
|
||||
background: #4a8;
|
||||
color: #0e1a16;
|
||||
font: 11px ui-monospace, monospace;
|
||||
border-radius: 3px;
|
||||
}
|
||||
.hint {
|
||||
position: absolute;
|
||||
bottom: 20px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
padding: 6px 14px;
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
color: #fff;
|
||||
border-radius: 999px;
|
||||
font: 13px ui-monospace, monospace;
|
||||
pointer-events: none;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user