refactoring some stuff

This commit is contained in:
Triston Armstrong 2024-06-25 22:08:51 -04:00
parent 0f4e13f6b0
commit 46d046b349
No known key found for this signature in database
GPG Key ID: FADE6AC6F956FC52
8 changed files with 60 additions and 37 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -27,13 +27,14 @@ export default class Application {
run() { run() {
console.log(`Running ${this.getName()}`); console.log(`Running ${this.getName()}`);
return this.ui_element.element
} }
remove_window() { remove_window() {
this.parent.end_application(this); this.parent.end_application(this);
} }
init(){ init() {
console.log(`Initializing ${this.getName()}`); console.log(`Initializing ${this.getName()}`);
} }
} }

View File

@ -1,10 +1,12 @@
import { el } from "../util/element"; import { el } from "../util/element";
export default class Component { export default class Component {
element: HTMLDivElement = el("div"); element: ReturnType<typeof el>;
constructor(id: string) { constructor(id: string, tag?: keyof HTMLElementTagNameMap) {
this.element.id = id this.element = el(tag ?? "div", {
id: id
})
} }
addChild(child: HTMLElement) { addChild(child: HTMLElement) {

View File

@ -1,17 +1,18 @@
import Component from "./component"; import Component from "./component";
import DesktopShortcut from "./desktop_shortcut"; import DesktopShortcut from "./desktop_shortcut";
import Window from "./window"; import Window from "./window";
import OS from "../os"
export default class Desktop extends Component { export default class Desktop extends Component {
shortcuts: DesktopShortcut[] = []; shortcuts: DesktopShortcut[] = [];
shortcuts_container: HTMLDivElement; shortcuts_container: Component;
windows_container: HTMLDivElement; windows_container: Component;
os: OS; os: OS;
constructor(os: OS) { constructor(os: OS) {
super(); super('desktop');
this.os = os this.os = os
this.element.classList.add("grid","grid-flow-col", "grid-cols-1"); this.element.classList.add("grid", "grid-flow-col", "grid-cols-1");
this.element.classList.add("w-full", "h-full", "gap-8", "p-2"); this.element.classList.add("w-full", "h-full", "gap-8", "p-2");
this.element.id = "desktop"; this.element.id = "desktop";
@ -36,8 +37,8 @@ export default class Desktop extends Component {
this.updateDesktop() this.updateDesktop()
} }
addOpenedWindow(w: Window) { addOpenedWindow(_w: Window) {
this.windows.push(w); // this.windows.push(w);
this.updateDesktop() this.updateDesktop()
} }

View File

@ -1,5 +1,6 @@
import OS from "../os"; import OS from "../os";
import Component from "./component"; import Component from "./component";
import { el } from "../util/element"
export default class DesktopShortcut extends Component { export default class DesktopShortcut extends Component {
icon: string; icon: string;
@ -7,22 +8,25 @@ export default class DesktopShortcut extends Component {
os: OS os: OS
constructor(icon: string, title: string, os: OS, id: string) { constructor(icon: string, title: string, os: OS, id: string) {
super(); super('desktop-shortcut');
this.icon = icon; this.icon = icon;
this.title = title; this.title = title;
this.id = id; this.element.id = id;
this.os = os; this.os = os;
this.element.classList.add("text-white", "cursor-pointer", "flex", "flex-col", "justify-center", "items-center", "p-2"); this.element.classList.add("text-white", "cursor-pointer", "flex");
this.element.classList.add("flex-col", "justify-center", "items-center", "p-2");
const _icon = document.createElement("img"); const _icon = el("img", {
const _title = document.createElement("p"); src: this.icon,
style: {
_icon.src = this.icon; width: "60px"
_icon.style.width = "60px"; }
});
_title.textContent = this.title; const _title = el("p", {
_title.classList.add( "font-bold", "text-md", "mt-1", "break-normal"); textContent: this.title
});
_title.classList.add("font-bold", "text-md", "mt-1", "break-normal");
this.addChild(_icon); this.addChild(_icon);
this.addChild(_title); this.addChild(_title);

View File

@ -3,6 +3,7 @@ import Component from "./component";
import NavBar from "./nav"; import NavBar from "./nav";
import TopBar from "./topbar"; import TopBar from "./topbar";
import Desktop from "./desktop"; import Desktop from "./desktop";
import { el } from "../util/element";
export default class UI extends Component { export default class UI extends Component {
os?: OS; os?: OS;
@ -11,7 +12,7 @@ export default class UI extends Component {
desktop?: Desktop; desktop?: Desktop;
constructor() { constructor() {
super(); super('ui');
this.element.style.height = "100vh"; this.element.style.height = "100vh";
this.element.style.width = "100vw"; this.element.style.width = "100vw";
@ -31,10 +32,13 @@ export default class UI extends Component {
this.navbar = new NavBar(); this.navbar = new NavBar();
this.desktop = new Desktop(this.os); this.desktop = new Desktop(this.os);
const desktop_container = document.createElement("div"); const desktop_container = el("div", {
desktop_container.id = "desktop_container"; id: "desktop_container",
desktop_container.style.display = "flex"; style: {
desktop_container.style.height = `calc(100vh - ${this.topbar.element.clientHeight}px)`; display: 'flex',
height: `calc(100vh - ${this.topbar.element.clientHeight}px)`
}
});
desktop_container.appendChild(this.navbar.start()); desktop_container.appendChild(this.navbar.start());
desktop_container.appendChild(this.desktop.start()); desktop_container.appendChild(this.desktop.start());
@ -44,6 +48,6 @@ export default class UI extends Component {
} }
updateDesktop() { updateDesktop() {
this.desktop.updateDesktop(); this.desktop?.updateDesktop();
} }
} }

View File

@ -1,14 +1,14 @@
import Component from "./component" import Component from "./component"
export default class NavBar extends Component { export default class NavBar extends Component {
applications: HTMLDivElement[] = []; applications: HTMLElement[] = [];
constructor() { constructor() {
super() super('navbar')
this.element.classList.add('flex', 'flex-column', 'w-14', 'h-full', 'bg-[#444]', 'justify-center','py-2', 'z-[12]') this.element.classList.add('flex', 'flex-column', 'w-14', 'h-full', 'bg-[#444]', 'justify-center', 'py-2', 'z-[12]')
} }
init(){ init() {
this.applications.push(new Application("../../assets/apps.svg").start()) this.applications.push(new Application("../../assets/apps.svg").start())
this.applications.forEach(app => this.element.appendChild(app)) this.applications.forEach(app => this.element.appendChild(app))
} }
@ -25,13 +25,13 @@ export default class NavBar extends Component {
class Application extends Component { class Application extends Component {
icon: string; icon: string;
constructor(icon: string) { constructor(icon: string) {
super(); super('application');
this.icon = icon; this.icon = icon;
this.element.classList.add('cursor-pointer') this.element.classList.add('cursor-pointer')
} }
setup_click_handler(){ setup_click_handler() {
this.element.addEventListener('click', () => { this.element.addEventListener('click', () => {
console.log('clicked') console.log('clicked')
}) })

View File

@ -1,4 +1,15 @@
export const el = <T extends keyof HTMLElementTagNameMap>(tag: T, props?: Partial<HTMLElementTagNameMap[T]>) => { /** Create a new element
return document.createElement(tag, props as ElementCreationOptions); * @param tag the html element to use
* @param props the standard html props you would normally set */
export const el = <T extends keyof HTMLElementTagNameMap>(tag: T, props?: CustomHtmlElementTagNameMap<T>): HTMLElementTagNameMap[T] => {
const element = document.createElement(tag);
if (!props) return element
Object.entries(props).forEach(([x, y]: [string, any]) => {
element[x as keyof CustomHtmlElementTagNameMap<T>] = y
})
return element
} }
type CustomHtmlElementTagNameMap<T extends keyof HTMLElementTagNameMap> = Partial<Omit<HTMLElementTagNameMap[T], 'style'>> & {
style?: Partial<CSSStyleDeclaration>
}