Skip to content

Commit 10433f1

Browse files
authored
Improve resizing
1 parent 5c7cc83 commit 10433f1

File tree

1 file changed

+29
-47
lines changed

1 file changed

+29
-47
lines changed

usergui.js

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class UserGui {
4848
body {
4949
background-color: #ffffff;
5050
overflow: hidden;
51+
width: 100% !important;
5152
}
5253
5354
form {
@@ -100,6 +101,7 @@ class UserGui {
100101
bottom: 0;
101102
right: 0;
102103
}
104+
103105
.formbuilder-button {
104106
width: fit-content;
105107
}
@@ -296,9 +298,6 @@ class UserGui {
296298
<style>
297299
${bootstrapStyling}
298300
${this.settings.gui.internal.style}
299-
body {
300-
width: ${this.settings.window.size.width}px
301-
}
302301
${
303302
this.settings.gui.centeredItems
304303
? `.form-group {
@@ -364,8 +363,8 @@ class UserGui {
364363

365364
// Sets the iFrame's size
366365
function setFrameSize(x, y) {
367-
iFrame.style.width = x + "px";
368-
iFrame.style.height = y + "px";
366+
iFrame.style.width = `${x}px`;
367+
iFrame.style.height = `${y}px`;
369368
}
370369

371370
// Gets the iFrame's size
@@ -377,8 +376,8 @@ class UserGui {
377376

378377
// Sets the iFrame's position relative to the main window's document
379378
function setFramePos(x, y) {
380-
iFrame.style.left = x + "px";
381-
iFrame.style.top = y + "px";
379+
iFrame.style.left = `${x}px`;
380+
iFrame.style.top = `${y}px`;
382381
}
383382

384383
// Gets the iFrame's position relative to the main document
@@ -388,14 +387,6 @@ class UserGui {
388387
return { "x": frameBounds.x, "y" : frameBounds.y };
389388
}
390389

391-
// Sets the frame body's offsetHeight
392-
function setInnerFrameSize(x, y) {
393-
const innerFrameElem = iFrame.contentDocument.querySelector("#gui");
394-
395-
innerFrameElem.style.width = `${x}px`;
396-
innerFrameElem.style.height = `${y}px`;
397-
}
398-
399390
// Gets the frame body's offsetHeight
400391
function getInnerFrameSize() {
401392
const innerFrameElem = iFrame.contentDocument.querySelector("#gui");
@@ -416,46 +407,31 @@ class UserGui {
416407

417408
// Variables for resizer
418409
let resizing = false,
419-
lastFrameResizePos = { "x" : 0, "y" : 0 },
420-
lastInnerFrameResizePos = { "x" : 0, "y" : 0 };
410+
mousePos = { "x" : undefined, "y" : undefined },
411+
lastFrame;
421412

422413
function handleResize(isInsideFrame, e) {
423-
let framePos, framePos2;
414+
if(mousePos.x == undefined && mousePos.y == undefined) {
415+
mousePos.x = e.clientX;
416+
mousePos.y = e.clientY;
424417

425-
if(isInsideFrame) {
426-
framePos = lastInnerFrameResizePos;
427-
framePos2 = lastFrameResizePos;
428-
} else {
429-
framePos = lastFrameResizePos;
430-
framePos2 = lastInnerFrameResizePos;
418+
lastFrame = isInsideFrame;
431419
}
432420

433-
// Reset the variable to avoid a leap between changing from main frame to iframe
434-
framePos2 = { "x" : 0, "y" : 0 };
421+
const deltaX = mousePos.x - e.clientX,
422+
deltaY = mousePos.y - e.clientY;
435423

436424
const frameSize = getFrameSize();
437-
const innerFrameSize = getInnerFrameSize();
425+
const allowedSize = frameSize.width - deltaX > 160 && frameSize.height - deltaY > 90;
438426

439-
if(framePos.x == 0 && framePos.y == 0) {
440-
framePos.x = e.clientX;
441-
framePos.y = e.clientY;
442-
}
443-
444-
const deltaX = framePos.x - e.clientX;
445-
const deltaY = framePos.y - e.clientY;
446-
447-
// Delta [-50,50] (to avoid a huge leap between sizes)
448-
// Slows the speed of resizing a bit, though
449-
if((deltaX >= -50 && deltaX <= 50) && (deltaY >= -2 && deltaY <= 2)) {
450-
// Current size - delta
427+
if(isInsideFrame == lastFrame && allowedSize) {
451428
setFrameSize(frameSize.width - deltaX, frameSize.height - deltaY);
452-
453-
// Current size - delta
454-
setInnerFrameSize(innerFrameSize.x - deltaX, innerFrameSize.y - deltaY);
455429
}
456430

457-
framePos.x = e.clientX;
458-
framePos.y = e.clientY;
431+
mousePos.x = e.clientX;
432+
mousePos.y = e.clientY;
433+
434+
lastFrame = isInsideFrame;
459435
}
460436

461437
function handleDrag(isInsideFrame, e) {
@@ -546,16 +522,22 @@ class UserGui {
546522
resizing = false;
547523
});
548524

549-
// Adjust the frame size every time the user clicks
550-
this.document.addEventListener('click', adjustFrameSize);
551-
552525
// Listener for the close button, closes the internal GUI
553526
this.document.querySelector("#button-close-gui").addEventListener('click', e => {
554527
e.preventDefault();
555528

556529
this.close();
557530
});
558531

532+
const guiObserver = new MutationObserver(adjustFrameSize);
533+
const guiElement = this.document.querySelector("#gui");
534+
535+
guiObserver.observe(guiElement, {
536+
childList: true,
537+
subtree: true,
538+
attributes: true
539+
});
540+
559541
adjustFrameSize();
560542
}
561543

0 commit comments

Comments
 (0)