ToastModule Fixes in the styles and small tweaks, also lib renamed to MWinLib

This commit is contained in:
2026-07-02 00:55:54 +02:00
parent afd663b6af
commit c7d1610648
6 changed files with 75 additions and 80 deletions
@@ -1,17 +1,17 @@
import {
getWrapperManager,
MOVABLE_MANAGER,
MWIN_MANAGER,
resolveElement,
type MovableAPI,
type MovableOptions,
type MovableState,
type NormalisedOptions,
type MWinAPI,
type MWinNormalizedParams,
type MWinParams,
type MWinState,
type ResizeDirection
} from './util';
} from './mwin_util';
export function movable(node: HTMLElement, options: MovableOptions) {
const controller = new MovableController(node, options);
const api: MovableAPI = {
export function mwin_movable(node: HTMLElement, options: MWinParams) {
const controller = new MWinController(node, options);
const api: MWinAPI = {
state: controller.state,
minimize: () => controller.minimize(),
maximize: () => controller.maximize(),
@@ -24,13 +24,13 @@ export function movable(node: HTMLElement, options: MovableOptions) {
};
}
class MovableController {
class MWinController {
#rafID: number | null = null;
#pendingApply = false;
#node: HTMLElement;
#wrapper: HTMLElement;
#opts: NormalisedOptions;
#opts: MWinNormalizedParams;
#savedTransition: string;
#savedStyles: string;
#wrapperObserver: ResizeObserver | null = null;
@@ -39,6 +39,8 @@ class MovableController {
#wrapperHeight: number = 0;
#wrapperLeft: number = 0;
#wrapperTop: number = 0;
#wrapperBorderLeft: number = 0;
#wrapperBorderTop: number = 0;
#dragOffsetX: number = 0;
#dragOffsetY: number = 0;
@@ -47,7 +49,7 @@ class MovableController {
#resizeStartPos = { x: 0, y: 0 };
#restoreRect: { x: number; y: number; width: number; height: number } | null = null;
state = $state<MovableState>({
state = $state<MWinState>({
x: 0,
y: 0,
width: 0,
@@ -59,13 +61,17 @@ class MovableController {
zIndex: 0
});
constructor(node: HTMLElement, options: MovableOptions) {
constructor(node: HTMLElement, options: MWinParams) {
this.#node = node;
const wrapper = resolveElement(options.wrapper, node);
if (!wrapper) throw new Error('[Movable] - Wrapper element not found!');
this.#wrapper = wrapper;
const wrapperStyle = getComputedStyle(this.#wrapper);
this.#wrapperBorderLeft = parseFloat(wrapperStyle.borderLeftWidth);
this.#wrapperBorderTop = parseFloat(wrapperStyle.borderTopWidth);
const resizableOpts = typeof options.resizable === 'object' ? options.resizable : {};
this.#opts = {
@@ -78,17 +84,15 @@ class MovableController {
resizeHandle: resolveElement(options.resizeHandle, node),
resizable: !!options.resizable,
threshold: resizableOpts.threshold ?? 8,
minWidth: resizableOpts.minWidth ?? 0,
minHeight: resizableOpts.minHeight ?? 0,
minWidth: resizableOpts.minWidth ?? 24, //MinWidth should be min-content
minHeight: resizableOpts.minHeight ?? 24, //MinHeight should be min-content
maxWidth: resizableOpts.maxWidth ?? Infinity,
maxHeight: resizableOpts.maxHeight ?? Infinity,
onClose: options.onClose,
onMinimize: options.onMinimize,
onMaximize: options.onMaximize
};
this.#savedStyles = this.#node.style.cssText;
this.#savedTransition = this.#node.style.transition;
this.#node.style.transition = 'none';
@@ -121,7 +125,6 @@ class MovableController {
node.addEventListener('pointerdown', this.#onPointerDown);
node.addEventListener('pointermove', this.#updateCursor);
node.addEventListener('pointerleave', this.#resetCursor);
//node.addEventListener('keydown', this.#onKeyDown);
this.#clampAndApply();
this.#bringToFront();
@@ -129,6 +132,7 @@ class MovableController {
this.#wrapperObserver = new ResizeObserver(() => {
this.#wrapperWidth = this.#wrapper.clientWidth;
this.#wrapperHeight = this.#wrapper.clientHeight;
this.#onWrapperResize();
});
this.#wrapperObserver.observe(this.#wrapper);
@@ -136,10 +140,9 @@ class MovableController {
#cacheWrapperRect() {
const wrapperRect = this.#wrapper.getBoundingClientRect();
const style = getComputedStyle(this.#wrapper);
this.#wrapperLeft = wrapperRect.left + parseFloat(style.borderLeftWidth);
this.#wrapperTop = wrapperRect.top + parseFloat(style.borderTopWidth);
this.#wrapperLeft = wrapperRect.left + this.#wrapperBorderLeft;
this.#wrapperTop = wrapperRect.top + this.#wrapperBorderTop;
this.#wrapperWidth = this.#wrapper.clientWidth;
this.#wrapperHeight = this.#wrapper.clientHeight;
}
@@ -183,17 +186,15 @@ class MovableController {
#getCornerDirection(e: PointerEvent): ResizeDirection {
if (!this.#opts.resizable) return null;
const rect = this.#node.getBoundingClientRect();
const t = this.#opts.threshold;
let dirY: string = '';
if (e.clientY - rect.top < t) dirY = 'n';
else if (rect.bottom - e.clientY < t) dirY = 's';
if (e.clientY - this.state.y < t) dirY = 'n';
else if (this.state.y + this.state.height - e.clientY < t) dirY = 's';
let dirX: string = '';
if (e.clientX - rect.left < t) dirX = 'w';
else if (rect.right - e.clientX < t) dirX = 'e';
if (e.clientX - this.state.x < t) dirX = 'w';
else if (this.state.x + this.state.width - e.clientX < t) dirX = 'e';
if (dirY && dirX) {
return (dirY + dirX) as ResizeDirection;
@@ -438,6 +439,10 @@ class MovableController {
};
#onWrapperResize(): void {
const style = getComputedStyle(this.#wrapper);
this.#wrapperBorderLeft = parseFloat(style.borderLeftWidth);
this.#wrapperBorderTop = parseFloat(style.borderTopWidth);
if (this.state.maximized) {
this.state.width = this.#wrapperWidth;
this.state.height = this.#wrapperHeight;
@@ -545,7 +550,7 @@ class MovableController {
const mgr = getWrapperManager(this.#wrapper);
mgr.refCount--;
if (mgr.refCount === 0) {
delete (this.#wrapper as any)[MOVABLE_MANAGER];
delete (this.#wrapper as any)[MWIN_MANAGER];
}
document.body.style.userSelect = '';
@@ -1,4 +1,4 @@
export interface MovableOptions {
export interface MWinParams {
wrapper: HTMLElement | (() => HTMLElement) | string; // changed
handle?: HTMLElement | (() => HTMLElement) | string;
initX?: number;
@@ -6,15 +6,15 @@ export interface MovableOptions {
initWidth?: number;
initHeight?: number;
baseZIndex?: number;
resizable?: boolean | ResizeOptions;
resizable?: boolean | MWinResizeParams;
resizeHandle?: HTMLElement | (() => HTMLElement) | string;
onClose?: () => void;
onMinimize?: () => void;
onMaximize?: () => void;
onAPI?: (api: MovableAPI) => void;
onAPI?: (api: MWinAPI) => void;
}
export interface NormalisedOptions {
export interface MWinNormalizedParams {
handle: HTMLElement | null;
initX?: number;
initY?: number;
@@ -33,7 +33,7 @@ export interface NormalisedOptions {
onMaximize?: () => void;
}
export interface ResizeOptions {
export interface MWinResizeParams {
threshold?: number;
minWidth?: number;
minHeight?: number;
@@ -41,7 +41,7 @@ export interface ResizeOptions {
maxHeight?: number;
}
export interface MovableState {
export interface MWinState {
x: number;
y: number;
width: number;
@@ -53,8 +53,8 @@ export interface MovableState {
zIndex: number;
}
export interface MovableAPI {
state: MovableState;
export interface MWinAPI {
state: MWinState;
minimize: () => void;
maximize: () => void;
close: () => void;
@@ -67,13 +67,13 @@ export class WrapperManager {
export type ResizeDirection = 'ne' | 'se' | 'sw' | 'nw' | null;
export const MOVABLE_MANAGER = Symbol('movable');
export const MWIN_MANAGER = Symbol('mwinlib');
export function getWrapperManager(wrapper: HTMLElement): WrapperManager {
let mgr = (wrapper as any)[MOVABLE_MANAGER] as WrapperManager | undefined;
let mgr = (wrapper as any)[MWIN_MANAGER] as WrapperManager | undefined;
if (!mgr) {
mgr = { zCounter: 0, refCount: 0 };
(wrapper as any)[MOVABLE_MANAGER] = mgr;
(wrapper as any)[MWIN_MANAGER] = mgr;
}
return mgr;
}
+3 -5
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import { movable } from '$lib/movable/movable_controller.svelte';
import type { MovableAPI } from '$lib/movable/util';
import { movable } from '$lib/mwinlib/mwin_controller.svelte';
import type { MovableAPI } from '$lib/mwinlib/mwin_util';
import Input from './terminal/Input.svelte';
let { username }: { username: string } = $props();
@@ -54,9 +54,7 @@
let inputValue = $state('');
</script>
<div
class="terminal-wrapper relative flex h-screen w-1/2 flex-col items-center justify-center bg-red-400"
>
<div class="terminal-wrapper relative flex h-screen w-full flex-col items-center justify-center">
<div
class="absolute flex size-full h-256 w-512 flex-col rounded-md shadow-subtle transition-discrete will-change-auto"
use:movable={{
+9 -12
View File
@@ -14,7 +14,7 @@
<div
transition:fly={{ x: 20, y: 0, duration: 300 }}
id={`toast-${toast.id.toString()}`}
class="w-md overflow-hidden rounded-md bg-bg-lighter-dark"
class="w-md overflow-hidden rounded-md bg-surface-2"
onclick={() => toast.onClick?.(toast)}
role="button"
tabindex={toast.onClick ? 0 : undefined}
@@ -30,18 +30,18 @@
<div class="flex items-center justify-start">
<div class="mx-4 my-8">
{#if toast.type === 'info'}
<Info class=" text-info-dark"></Info>
<Info class=" text-info"></Info>
{:else if toast.type === 'error'}
<CircleX class="text-danger-dark"></CircleX>
<CircleX class="text-error"></CircleX>
{:else if toast.type === 'warn'}
<TriangleAlert class="text-warning-dark"></TriangleAlert>
<TriangleAlert class="text-warning"></TriangleAlert>
{:else}
<CircleCheckBig class="text-success-dark"></CircleCheckBig>
<CircleCheckBig class="text-success"></CircleCheckBig>
{/if}
</div>
<div class="my-2 mr-auto mb-auto ml-2 flex flex-col items-start justify-start select-none">
<h5 class=" text- text-md font-bold text-text-dark">{toast.title}</h5>
<p class=" text-md mb-2 overflow-x-hidden overflow-y-scroll text-text-muted-dark">
<h5 class=" text- text-md text-text-dark font-bold">{toast.title}</h5>
<p class=" text-md mb-2 overflow-x-hidden overflow-y-scroll text-muted">
{toast.message}
</p>
</div>
@@ -56,16 +56,13 @@
<X
stroke-width={3}
size={18}
class=" transition-all duration-200 hover:text-text-muted-dark active:scale-90"
class=" transition-all duration-200 hover:text-muted active:scale-90"
></X>
</button>
</div>
</div>
{#if !toast.hideProgress && toast.tween}
<div
style:padding-right={`${toast.tween.current}%`}
class=" h-[2px] w-0 bg-primary-hover-dark"
></div>
<div style:padding-right={`${toast.tween.current}%`} class=" h-[2px] w-0 bg-primary"></div>
{/if}
</div>
{/each}
+17 -23
View File
@@ -1,27 +1,21 @@
<script lang="ts">
import { addToast } from '$lib/stores/toasts';
import { FileSystem } from '$lib/termos/filesystem/filesystem';
import { onMount } from 'svelte';
import Terminal from '../../modules/Terminal.svelte';
import type { ToastMessage } from '../admin/util';
import { mwin_movable } from '$lib/mwinlib/mwin_controller.svelte';
import type { MWinAPI } from '$lib/mwinlib/mwin_util';
function bye(tm: ToastMessage): void {
console.log('Funkcja wywolana');
}
let wrapper = $state<HTMLElement>();
let username = 'test';
onMount(async () => {
await FileSystem.init();
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
addToast('success', 'Hi~', '', 5000, undefined, undefined, true, false, bye, bye);
}
});
});
let API: MWinAPI;
</script>
<Terminal {username}></Terminal>
<div class="wrapper relative h-screen w-1/2 bg-blue-500">
<div
class="absolute h-128 w-5xl bg-red-400 will-change-auto"
use:mwin_movable={{
wrapper: '.wrapper',
baseZIndex: 10,
resizable: { threshold: 24 },
onAPI: (api) => (API = api)
}}
>
<div></div>
<button onclick={() => API.maximize()}>click me</button>
</div>
</div>