Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

5.9 KB
Raw Blame
"use client";

import { createElement, useEffect, useRef, useState, type CSSProperties } from "react";
import { Check, LayoutGrid, Trash2 } from "lucide-react";
import { ContextMenu, useContextMenu } from "../../../components/shells/context-menu";
import type { MenuItem } from "../../../lib/context-menu";
import { cn } from "@/lib/utils";
import {
  setPickerOpen,
  setWidgetPos,
  setWidgetSize,
  toggleWidget,
  useWidgetState,
  type WidgetPos,
  type WidgetSize,
} from "../widget-registry";
import { WIDGET_RENDER } from "./widgets-defs";
import { WidgetPicker } from "./widget-picker";

// Desktop widgets — a glanceable, EDITABLE set free-positioned on the wallpaper
// layer (behind windows). Drag a widget to move it; right-click sets its size or
// removes it. Layout + membership live in the widget-registry store; the
// WidgetPicker (live-preview gallery) edits membership.

const SIZE_W: Record<WidgetSize, string> = { s: "w-44", m: "w-60", l: "w-72" };
const SIZE_PX: Record<WidgetSize, number> = { s: 176, m: 240, l: 288 };

// One free-dragged widget. Drag from any non-interactive part (its own inputs /
// buttons keep working); the move commits once on drop (not every frame). Right-
// click sets its size or removes it.
function DesktopWidget({ id, size, pos }: { id: string; size: WidgetSize; pos: WidgetPos }) {
  const element = useRef<HTMLDivElement>(null);
  const [bounds, setBounds] = useState({ w: 0, h: 0, cardH: 0 });
  useEffect(() => {
    const card = element.current, parent = card?.parentElement;
    if (!card || !parent) return;
    const measure = () => setBounds({ w: parent.clientWidth, h: parent.clientHeight, cardH: card.offsetHeight });
    const observer = new ResizeObserver(measure);
    observer.observe(parent); observer.observe(card);
    measure();
    return () => observer.disconnect();
  }, []);
  const ctx = useContextMenu();
  const Render = WIDGET_RENDER[id];
  const drag = useRef<{ x: number; y: number } | null>(null);
  const [offset, setOffset] = useState<{ dx: number; dy: number } | null>(null);
  if (!Render) return null;

  const onPointerDown = (e: React.PointerEvent) => {
    if (e.button !== 0) return;
    if ((e.target as HTMLElement).closest("input,textarea,button,a,[contenteditable],iframe,summary")) return;
    e.stopPropagation();
    drag.current = { x: e.clientX, y: e.clientY };
    setOffset({ dx: 0, dy: 0 });
    (e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId);
  };
  const onPointerMove = (e: React.PointerEvent) => {
    if (!drag.current) return;
    setOffset({ dx: e.clientX - drag.current.x, dy: e.clientY - drag.current.y });
  };
  const onPointerUp = (e: React.PointerEvent) => {
    if (drag.current) setWidgetPos(id, origin.x + e.clientX - drag.current.x, origin.y + e.clientY - drag.current.y);
    drag.current = null;
    setOffset(null);
  };

  const origin = { x: Math.max(0, Math.min(pos.x, bounds.w - SIZE_PX[size])), y: Math.max(0, Math.min(pos.y, bounds.h - bounds.cardH)) };
  const cancelDrag = () => { drag.current = null; setOffset(null); };
  const items: MenuItem[] = [
    { label: "Small", icon: size === "s" ? Check : undefined, onClick: () => setWidgetSize(id, "s") },
    { label: "Medium", icon: size === "m" ? Check : undefined, onClick: () => setWidgetSize(id, "m") },
    { label: "Large", icon: size === "l" ? Check : undefined, onClick: () => setWidgetSize(id, "l") },
    { type: "sep" },
    { label: "Remove widget", icon: Trash2, onClick: () => toggleWidget(id), danger: true },
    { label: "Desktop widgets…", icon: LayoutGrid, onClick: () => setPickerOpen(true) },
  ];

  return (
    <div
      ref={element}
      className={cn(
        // Elevation so a widget reads as the same floating surface family as
        // windows (which carry --shadow-win). filter drop-shadow — not box-shadow
        // — follows the inner card's rounded alpha; this square wrapper has no radius.
        "pointer-events-auto absolute touch-none cursor-grab active:cursor-grabbing drop-shadow-[0_8px_24px_rgba(0,0,0,0.28)]",
        SIZE_W[size],
      )}
      style={{ left: origin.x + (offset?.dx ?? 0), top: origin.y + (offset?.dy ?? 0), maxWidth: "100%", maxHeight: "100%", overflowY: "auto" }}
      onPointerDown={onPointerDown}
      onPointerMove={onPointerMove}
      onPointerUp={onPointerUp}
      onPointerCancel={cancelDrag}
      onLostPointerCapture={cancelDrag}
      onContextMenu={(e) => { e.stopPropagation(); ctx.open(e); }}
    >
      {createElement(Render)}
      <ContextMenu pos={ctx.pos} items={items} onClose={ctx.close} />
    </div>
  );
}

export function DesktopWidgets() {
  const { on, enabled, sizes, positions } = useWidgetState();
  const ref = useRef<HTMLDivElement>(null);

  // Auto-place any enabled widget that lacks a saved position — a top-right
  // column, matching the previous stack so the migration is seamless.
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const W = el.clientWidth;
    let slot = 0;
    for (const id of enabled) {
      if (positions[id]) continue;
      setWidgetPos(id, Math.max(0, W - SIZE_PX[sizes[id] ?? "m"] - 16), 16 + slot * 128);
      slot++;
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [enabled.join(","), on]);

  if (!on) return <WidgetPicker />;
  return (
    <>
      <WidgetPicker />
      <div
        ref={ref}
        className="pointer-events-none absolute inset-0 z-[5]"
        // Desktop-only: point the shared widget Card's radius at the WINDOW token
        // so widgets read as the same floating surface family as windows. Mobile
        // Today never sets this var → its Cards keep the 1rem fallback → intact.
        style={{ "--widget-radius": "var(--shell-radius-win)" } as CSSProperties}
      >
        {enabled.map((id) => {
          const pos = positions[id];
          return pos ? <DesktopWidget key={id} id={id} size={sizes[id] ?? "m"} pos={pos} /> : null;
        })}
      </div>
    </>
  );
}