From 292a1443cdaad6c01dfa69bb8f06c642cda2c22a Mon Sep 17 00:00:00 2001 From: Bao Nguyen Date: Sun, 19 Feb 2023 11:13:22 +0700 Subject: [PATCH] auto nudging newly launched windows to prevent them from completely overlapping previous ones. --- src/lib/components/xp/Window.svelte | 35 +++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/lib/components/xp/Window.svelte b/src/lib/components/xp/Window.svelte index a2d2565..96d9142 100644 --- a/src/lib/components/xp/Window.svelte +++ b/src/lib/components/xp/Window.svelte @@ -26,9 +26,13 @@ onMount(async () => { if(options.exec_path != null){ let rect = await get(options.exec_path); - console.log(rect); if(rect){ + rect = {top: rect.top, left: rect.left, width: rect.width, height: rect.height}; let workspace = document.querySelector('#work-space'); + let nudge = calc_nudges(rect); + rect.top = rect.top + nudge.top; + rect.left = rect.left + nudge.left; + if(rect.left + rect.width <= workspace.offsetWidth && rect.top + rect.height <= workspace.offsetHeight){ options.top = rect.top; @@ -45,7 +49,7 @@ if(options.left == null){ options.left = (node_ref.parentNode.offsetWidth - node_ref.offsetWidth)/2; } - set_position({top: options.top, left: options.left, width: node_ref.width, height: node_ref.height}); + set_position({top: options.top, left: options.left, width: options.width, height: options.height}); if(options.resizable == null){ options.resizable = true; @@ -115,8 +119,6 @@ } } - - export function loose_focus(){ if(z_index == $zIndex){ console.log('loose focus'); @@ -124,6 +126,27 @@ } } + function calc_nudges({top, left, width, height}){ + let existing_window = $runningPrograms.findLast(el => { + return el.options.id != options.id + && el.options.exec_path == options.exec_path; + }); + if(existing_window == null) return {top: 0, left: 0} + + let pad = 10; + let nudges = [[pad, pad], [pad, -pad], [-pad, pad], [-pad, -pad], [0, 0]]; + let workspace = document.querySelector('#work-space'); + for(let nudge of nudges){ + if(top + nudge[0] >= 0 + && left + nudge[1] >= 0 + && top + height + nudge[0] <= workspace.offsetHeight + && left + width + nudge[1] <= workspace.offsetWidth){ + return {top: nudge[0], left: nudge[1]} + } + } + return {top: 0, left: 0}; + } + function get_center_point(rect){ if(rect == null){ return {x: document.body.offsetWidth*0.5, y: document.body.offsetHeight*0.5} @@ -139,6 +162,10 @@ node_ref.style.left = `${left}px`; node_ref.style.width = `${width}px`; node_ref.style.height = `${height}px`; + + if(options.exec_path){ + set(options.exec_path, node_ref.getBoundingClientRect()) + } }