From 91be4dcc63b65f8de8f6774464f22110d072ad53 Mon Sep 17 00:00:00 2001 From: Triston Armstrong Date: Thu, 20 Jun 2024 14:32:14 -0500 Subject: [PATCH] start making window sizable --- src/ui/window.ts | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/ui/window.ts b/src/ui/window.ts index b75735f..4a4c562 100644 --- a/src/ui/window.ts +++ b/src/ui/window.ts @@ -7,6 +7,7 @@ export default class Window { header: HTMLDivElement; /** the os application that this window is for */ application: Application; + resize_start_pos: { x: number, y: number } = { x: 0, y: 0 }; constructor(application: Application) { this.element.classList.add("bg-[#ccc]", "absolute", "rounded-lg", "shadow-xl"); @@ -143,8 +144,31 @@ export default class Window { private _resize_start(e: MouseEvent, dir: 'l' | 'r' | 't' | 'b') { - // TODO finish - console.log({e, dir}) + console.log(e) + const { x, y } = this._get_mouse_position(e); + const { width: w, height: h } = this.element.getBoundingClientRect(); + + const new_size = { w, h }; + if (dir === 'l') { + new_size.w -= x - this.resize_start_pos.x; + } else if (dir === 'r') { + new_size.w += x - this.resize_start_pos.x; + } else if (dir === 't') { + new_size.h -= y - this.resize_start_pos.y; + } else if (dir === 'b') { + new_size.h += y - this.resize_start_pos.y; + } + this.element.style.width = `${new_size.w}px`; + this.element.style.height = `${new_size.h}px`; + this.resize_start_pos = { x, y }; + } + + private _get_mouse_position(e: MouseEvent): { x: number, y: number } { + const rect = this.element.getBoundingClientRect(); + return { + x: e.clientX - rect.left, + y: e.clientY - rect.top + } }