From 64b59dd227203e968bd95f482fd8ac2830dc3bd1 Mon Sep 17 00:00:00 2001 From: RanXom Date: Sat, 13 Jun 2026 17:41:18 +0530 Subject: add timer, control buttons and tasks ui compenents --- desktop/src/components/Controls.tsx | 59 ++++++++++++ desktop/src/components/PixelTimer.tsx | 136 +++++++++++++++++++++++++++ desktop/src/components/TaskItem.tsx | 112 ++++++++++++++++++++++ desktop/src/components/TaskList.tsx | 172 ++++++++++++++++++++++++++++++++++ 4 files changed, 479 insertions(+) create mode 100644 desktop/src/components/Controls.tsx create mode 100644 desktop/src/components/PixelTimer.tsx create mode 100644 desktop/src/components/TaskItem.tsx create mode 100644 desktop/src/components/TaskList.tsx (limited to 'desktop/src') diff --git a/desktop/src/components/Controls.tsx b/desktop/src/components/Controls.tsx new file mode 100644 index 0000000..8df050d --- /dev/null +++ b/desktop/src/components/Controls.tsx @@ -0,0 +1,59 @@ +import type { TimerState } from "../lib/commands"; + +interface ControlsProps { + timer: TimerState; + onToggle: () => void; + onReset: () => void; + onSkip: () => void; +} + +export default function Controls({ timer, onToggle, onReset, onSkip }: ControlsProps) { + return ( +
+ + + + + +
+ ); +} diff --git a/desktop/src/components/PixelTimer.tsx b/desktop/src/components/PixelTimer.tsx new file mode 100644 index 0000000..b3310d1 --- /dev/null +++ b/desktop/src/components/PixelTimer.tsx @@ -0,0 +1,136 @@ +import { useState, useRef, useEffect } from "react"; +import type { TimerState } from "../lib/commands"; + +interface PixelTimerProps { + timer: TimerState; + onSetDuration: (mins: number) => void; +} + +const ASCII_MAP: Record = { + "0": [" ██████ ", "██ ██", "██ ██", "██ ██", " ██████ "], + "1": [" ██ ", " ███ ", " ██ ", " ██ ", " ██████ "], + "2": [" ██████ ", "██ ██", " ███ ", " ███ ", "████████"], + "3": [" ██████ ", " ██", " █████ ", " ██", " ██████ "], + "4": ["██ ██", "██ ██", "████████", " ██", " ██"], + "5": ["████████", "██ ", "███████ ", " ██", "███████ "], + "6": [" ██████ ", "██ ", "███████ ", "██ ██", " ██████ "], + "7": ["████████", " ██", " ██ ", " ██ ", " ██ "], + "8": [" ██████ ", "██ ██", " ██████ ", "██ ██", " ██████ "], + "9": [" ██████ ", "██ ██", " ███████", " ██", " ██████ "], + ":": [" ", " █ ", " ", " █ ", " "], +}; + +function formatMonolithicAscii(timeStr: string): string { + const lines = ["", "", "", "", ""]; + for (let i = 0; i < timeStr.length; i++) { + const char = timeStr[i]; + const art = ASCII_MAP[char] || ASCII_MAP[":"]; + for (let j = 0; j < 5; j++) { + lines[j] += art[j]; + if (i < timeStr.length - 1) { + lines[j] += " "; + } + } + } + return lines.join("\n"); +} + +function formatTimeStr(secs: number): string { + const m = Math.floor(secs / 60); + const s = secs % 60; + return `${String(m).padStart(2, "0")}:${String(s).padStart(2, "0")}`; +} + +export default function PixelTimer({ timer, onSetDuration }: PixelTimerProps) { + const [isEditing, setIsEditing] = useState(false); + const [editValue, setEditValue] = useState(""); + const inputRef = useRef(null); + + useEffect(() => { + if (isEditing && inputRef.current) { + inputRef.current.focus(); + } + }, [isEditing]); + + const timeStr = formatTimeStr(timer.time_remaining_secs); + const asciiArt = formatMonolithicAscii(timeStr); + + const [currentAscii, setCurrentAscii] = useState(asciiArt); + const [prevAscii, setPrevAscii] = useState(""); + const [isFading, setIsFading] = useState(false); + + useEffect(() => { + if (asciiArt !== currentAscii) { + setPrevAscii(currentAscii); + setCurrentAscii(asciiArt); + setIsFading(true); + const timeout = setTimeout(() => setIsFading(false), 300); + return () => clearTimeout(timeout); + } + }, [asciiArt, currentAscii]); + + function handleStartEdit() { + setEditValue(String(Math.floor(timer.time_remaining_secs / 60))); + setIsEditing(true); + } + + function handleSubmit() { + const mins = parseInt(editValue, 10); + if (!isNaN(mins) && mins >= 0) { + onSetDuration(mins); + } + setIsEditing(false); + } + + function handleKeyDown(e: React.KeyboardEvent) { + if (e.key === "Enter") { + handleSubmit(); + } else if (e.key === "Escape") { + setIsEditing(false); + } + } + + if (isEditing) { + return ( +
+ setEditValue(e.target.value)} + onBlur={handleSubmit} + onKeyDown={handleKeyDown} + min="0" + max="999" + aria-label="Set minutes" + /> + minutes +
+ ); + } + + return ( +
{ + if (e.key === "Enter" || e.key === " ") handleStartEdit(); + }} + > +
+ {isFading && ( +
+            {prevAscii}
+          
+ )} +
+          {currentAscii}
+        
+
+
+ ); +} diff --git a/desktop/src/components/TaskItem.tsx b/desktop/src/components/TaskItem.tsx new file mode 100644 index 0000000..bb4b148 --- /dev/null +++ b/desktop/src/components/TaskItem.tsx @@ -0,0 +1,112 @@ +import { useState, useRef, useEffect } from "react"; +import type { Task } from "../lib/commands"; + +interface TaskItemProps { + task: Task; + index: number; + total: number; + onEdit: (index: number, title: string) => void; + onDelete: (index: number) => void; + onToggle: (index: number) => void; + onReorder: (index: number, direction: "up" | "down") => void; +} + +export default function TaskItem({ + task, + index, + total: _total, + onEdit, + onDelete, + onToggle, + onReorder: _onReorder, +}: TaskItemProps) { + const [isEditing, setIsEditing] = useState(false); + const [editTitle, setEditTitle] = useState(task.title); + const inputRef = useRef(null); + + useEffect(() => { + if (isEditing && inputRef.current) { + inputRef.current.focus(); + } + }, [isEditing]); + + function handleSubmit() { + if (editTitle.trim() && editTitle !== task.title) { + onEdit(index, editTitle.trim()); + } else { + setEditTitle(task.title); + } + setIsEditing(false); + } + + function handleKeyDown(e: React.KeyboardEvent) { + if (e.key === "Enter") { + handleSubmit(); + } else if (e.key === "Escape") { + setEditTitle(task.title); + setIsEditing(false); + } + } + + return ( +
+ + + {isEditing ? ( + setEditTitle(e.target.value)} + onBlur={handleSubmit} + onKeyDown={handleKeyDown} + /> + ) : ( +
setIsEditing(true)}> + {task.title} +
+ )} + +
+ + +
+
+ ); +} diff --git a/desktop/src/components/TaskList.tsx b/desktop/src/components/TaskList.tsx new file mode 100644 index 0000000..8de41fb --- /dev/null +++ b/desktop/src/components/TaskList.tsx @@ -0,0 +1,172 @@ +import { useState } from "react"; +import type { Task } from "../lib/commands"; +import TaskItem from "./TaskItem"; + +interface TaskListProps { + tasks: Task[]; + onAdd: (title: string) => void; + onEdit: (index: number, title: string) => void; + onDelete: (index: number) => void; + onToggle: (index: number) => void; + onReorder: (index: number, direction: "up" | "down") => void; + onMove: (from: number, to: number) => void; +} + +export default function TaskList({ + tasks, + onAdd, + onEdit, + onDelete, + onToggle, + onReorder, + onMove, +}: TaskListProps) { + const [newTaskTitle, setNewTaskTitle] = useState(""); + const [draggedIndex, setDraggedIndex] = useState(null); + const [dragOverIndex, setDragOverIndex] = useState(null); + + function handleSubmit(e: React.FormEvent) { + e.preventDefault(); + if (newTaskTitle.trim()) { + onAdd(newTaskTitle.trim()); + setNewTaskTitle(""); + } + } + + function handleDragStart(e: React.DragEvent, index: number) { + setDraggedIndex(index); + if (e.dataTransfer) { + e.dataTransfer.effectAllowed = "move"; + e.dataTransfer.setData("text/plain", index.toString()); + + // Generate a small, clean drag ghost without relying on the browser's + // unpredictable DOM snapshotting on High DPI displays. + const task = tasks[index]; + const ghost = document.createElement("div"); + ghost.textContent = task.title; + ghost.style.position = "absolute"; + ghost.style.top = "-1000px"; + ghost.style.left = "-1000px"; + ghost.style.backgroundColor = "var(--surface)"; + ghost.style.color = "var(--text)"; + ghost.style.padding = "4px 8px"; + ghost.style.borderRadius = "var(--radius-sm)"; + // WebKit scaling fix: Make it 12px natively so it stays small even if scaled 2x + ghost.style.fontSize = "12px"; + ghost.style.fontFamily = "inherit"; + ghost.style.whiteSpace = "nowrap"; + ghost.style.boxShadow = "0 4px 12px rgba(0,0,0,0.5)"; + ghost.style.zIndex = "9999"; + + document.body.appendChild(ghost); + e.dataTransfer.setDragImage(ghost, 10, 10); + + setTimeout(() => { + if (document.body.contains(ghost)) { + document.body.removeChild(ghost); + } + }, 0); + } + } + + function handleDragOver(e: React.DragEvent, index: number) { + e.preventDefault(); + if (e.dataTransfer) { + e.dataTransfer.dropEffect = "move"; + } + if (draggedIndex !== null && draggedIndex !== index) { + setDragOverIndex(index); + } + } + + function handleDrop(e: React.DragEvent, dropIndex: number) { + e.preventDefault(); + if (draggedIndex !== null && draggedIndex !== dropIndex) { + onMove(draggedIndex, dropIndex); + } + setDraggedIndex(null); + setDragOverIndex(null); + } + + function handleDragEnd() { + setDraggedIndex(null); + setDragOverIndex(null); + } + + return ( +
+
+

Focus Priorities

+ + {tasks.filter((t) => !t.is_done).length}/{tasks.length} + +
+ +
+ setNewTaskTitle(e.target.value)} + /> + +
+ +
+ {tasks.map((task, index) => { + let transform = "none"; + if (draggedIndex !== null && dragOverIndex !== null) { + if (draggedIndex < dragOverIndex && index > draggedIndex && index <= dragOverIndex) { + transform = "translateY(-44px)"; + } else if (draggedIndex > dragOverIndex && index >= dragOverIndex && index < draggedIndex) { + transform = "translateY(44px)"; + } + } + + return ( +
handleDragStart(e, index)} + onDragOver={(e) => handleDragOver(e, index)} + onDrop={(e) => handleDrop(e, index)} + onDragEnd={handleDragEnd} + className={`task-drag-wrapper ${draggedIndex === index ? "dragging" : ""}`} + style={{ + transform, + transition: draggedIndex !== null ? "transform 0.2s cubic-bezier(0.2, 0, 0, 1)" : "none", + }} + > + +
+ ); + })} + {tasks.length === 0 && ( +
+

No tasks yet

+ Time to focus! +
+ )} +
+
+ ); +} -- cgit v1.2.3