From e8e32352ed18ef1417b4b7eb11c29e25ce41c220 Mon Sep 17 00:00:00 2001 From: meetqy Date: Sun, 17 Mar 2024 22:52:34 +0800 Subject: [PATCH 1/2] fix: Automatic Adjustment of Puter Windows to Stay Within Viewport on Browser Resize #12 --- src/globals.js | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/globals.js b/src/globals.js index 5e31a7c3..fa5e966f 100644 --- a/src/globals.js +++ b/src/globals.js @@ -141,19 +141,37 @@ if (window.location !== window.parent.location) { window.desktop_height = window.innerHeight - window.toolbar_height - window.taskbar_height; window.desktop_width = window.innerWidth; +let isResize = false; + // recalculate desktop height and width on window resize $( window ).on( "resize", function() { - const radio = window.desktop_width / window.innerWidth; - - window.desktop_height = window.innerHeight - window.toolbar_height - window.taskbar_height; - window.desktop_width = window.innerWidth; - - const { top } = $(".window-login").position(); - const width = $(".window-login").width(); - $(".window-login").css({ - left: (window.desktop_width - width) / 2, - top: top / radio, - }); + const new_desktop_height = window.innerHeight - window.toolbar_height - window.taskbar_height; + const new_desktop_width = window.innerWidth; + + $('.window').each((_, el) => { + const pos = $(el).position(); + const leftRadio = pos.left / window.desktop_width; + const topRadio = pos.top / window.desktop_height; + + let left = new_desktop_width * leftRadio; + let top = new_desktop_height * topRadio; + const maxLeft = new_desktop_width - $(el).width(); + const maxTop = new_desktop_height - $(el).height(); + + if(left < 0) left = 0; + else if(left > maxLeft) left = maxLeft; + + if(top < window.toolbar_height) top = window.toolbar_height; + else if(top > maxTop) top = maxTop + window.toolbar_height; + + $(el).css({ + left, + top + }) + }) + + window.desktop_height = new_desktop_height; + window.desktop_width = new_desktop_width; }); // for now `active_element` is basically the last element that was clicked, From ad319988160c414644ffcc405f00e52c82b52eba Mon Sep 17 00:00:00 2001 From: meetqy Date: Tue, 19 Mar 2024 21:59:48 +0800 Subject: [PATCH 2/2] add original position --- src/UI/UIWindow.js | 3 +++ src/globals.js | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/UI/UIWindow.js b/src/UI/UIWindow.js index e1d80a17..b7b42465 100644 --- a/src/UI/UIWindow.js +++ b/src/UI/UIWindow.js @@ -1369,6 +1369,9 @@ async function UIWindow(options) { } $(el_window).addClass('window-dragging'); + + // rm window from original_window_position + window.original_window_position[$(el_window).attr('id')] = undefined; // since jquery draggable sets the z-index automatically we need this to // bring windows to the front when they are clicked. diff --git a/src/globals.js b/src/globals.js index 15d5a05b..fb69918b 100644 --- a/src/globals.js +++ b/src/globals.js @@ -142,6 +142,9 @@ if (window.location !== window.parent.location) { window.desktop_height = window.innerHeight - window.toolbar_height - window.taskbar_height; window.desktop_width = window.innerWidth; +// {id: {left: 0, top: 0}} +window.original_window_position = {}; + // recalculate desktop height and width on window resize $( window ).on( "resize", function() { const new_desktop_height = window.innerHeight - window.toolbar_height - window.taskbar_height; @@ -149,19 +152,33 @@ $( window ).on( "resize", function() { $('.window').each((_, el) => { const pos = $(el).position(); + const id = $(el).attr('id'); + + if(!window.original_window_position[id]){ + window.original_window_position[id] = { + left: pos.left, + top: pos.top + } + } + const leftRadio = pos.left / window.desktop_width; const topRadio = pos.top / window.desktop_height; let left = new_desktop_width * leftRadio; let top = new_desktop_height * topRadio; + const maxLeft = new_desktop_width - $(el).width(); const maxTop = new_desktop_height - $(el).height(); + // first move the window to the original position + const originalLeft = window.original_window_position[id].left; + const originalTop = window.original_window_position[id].top; + if(left < 0) left = 0; - else if(left > maxLeft) left = maxLeft; + else if(left > maxLeft || originalLeft > maxLeft) left = maxLeft; if(top < window.toolbar_height) top = window.toolbar_height; - else if(top > maxTop) top = maxTop + window.toolbar_height; + else if(top > maxTop || originalTop > maxTop) top = maxTop + window.toolbar_height; $(el).css({ left,