File size: 6,707 Bytes
4e1096a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | import clsx from 'clsx';
import React, { useEffect, useRef } from 'react';
import { useEnv } from '@/context/EnvContext';
import { tauriHandleMinimize, tauriHandleToggleMaximize, tauriHandleClose } from '@/utils/window';
import { isTauriAppPlatform } from '@/services/environment';
import { useTranslation } from '@/hooks/useTranslation';
interface WindowButtonsProps {
className?: string;
headerRef?: React.RefObject<HTMLDivElement | null>;
showMinimize?: boolean;
showMaximize?: boolean;
showClose?: boolean;
closeButtonLabel?: string;
onMinimize?: () => void;
onToggleMaximize?: () => void;
onClose?: () => void;
}
interface WindowButtonProps {
id: string;
onClick: () => void;
label: string;
children: React.ReactNode;
}
const WindowButton: React.FC<WindowButtonProps> = ({ onClick, label, id, children }) => (
<button
id={id}
onClick={onClick}
className='window-button bg-base-200/35 hover:bg-base-200 text-base-content/85 hover:text-base-content'
aria-label={label}
>
{children}
</button>
);
const WindowButtons: React.FC<WindowButtonsProps> = ({
className,
headerRef,
showMinimize = true,
showMaximize = true,
showClose = true,
closeButtonLabel,
onMinimize,
onToggleMaximize,
onClose,
}) => {
const _ = useTranslation();
const parentRef = useRef<HTMLDivElement>(null);
const { appService } = useEnv();
const touchState = useRef({
lastPointerTime: 0,
pointerStartPosition: { x: 0, y: 0 },
isDragging: false,
});
const isExcludedElement = (target: HTMLElement) => {
return (
target.closest('.btn') ||
target.closest('.window-button') ||
target.closest('.dropdown-container') ||
target.closest('.exclude-title-bar-mousedown')
);
};
const handleMouseDown = async (e: MouseEvent) => {
const target = e.target as HTMLElement;
if (isExcludedElement(target)) {
return;
}
const { getCurrentWindow } = await import('@tauri-apps/api/window');
if (e.buttons === 1) {
if (e.detail === 2) {
getCurrentWindow().toggleMaximize();
} else {
getCurrentWindow().startDragging();
}
}
};
const handlePointerDown = async (e: PointerEvent) => {
const target = e.target as HTMLElement;
if (isExcludedElement(target)) {
return;
}
if (e.pointerType === 'mouse') {
return;
}
e.preventDefault();
const currentTime = Date.now();
const timeDiff = currentTime - touchState.current.lastPointerTime;
touchState.current.pointerStartPosition = {
x: e.clientX,
y: e.clientY,
};
if (timeDiff < 300) {
const { getCurrentWindow } = await import('@tauri-apps/api/window');
getCurrentWindow().toggleMaximize();
return;
}
touchState.current.lastPointerTime = currentTime;
touchState.current.isDragging = false;
};
const handlePointerMove = async (e: PointerEvent) => {
const target = e.target as HTMLElement;
if (isExcludedElement(target) || touchState.current.isDragging) {
return;
}
if (e.pointerType === 'mouse') {
return;
}
e.preventDefault();
const deltaX = Math.abs(e.clientX - touchState.current.pointerStartPosition.x);
const deltaY = Math.abs(e.clientY - touchState.current.pointerStartPosition.y);
if (deltaX > 5 || deltaY > 5) {
touchState.current.isDragging = true;
try {
const { getCurrentWindow } = await import('@tauri-apps/api/window');
await getCurrentWindow().startDragging();
} catch (error) {
console.warn('Failed to start window dragging:', error);
}
}
};
const handlePointerUp = () => {
touchState.current.isDragging = false;
};
useEffect(() => {
if (!isTauriAppPlatform()) return;
const headerElement = headerRef?.current;
if (!headerElement) return;
headerElement.addEventListener('mousedown', handleMouseDown);
headerElement.addEventListener('pointerdown', handlePointerDown);
headerElement.addEventListener('pointermove', handlePointerMove);
headerElement.addEventListener('pointerup', handlePointerUp);
headerElement.addEventListener('pointercancel', handlePointerUp);
return () => {
headerElement.removeEventListener('mousedown', handleMouseDown);
headerElement.removeEventListener('pointerdown', handlePointerDown);
headerElement.removeEventListener('pointermove', handlePointerMove);
headerElement.removeEventListener('pointerup', handlePointerUp);
headerElement.removeEventListener('pointercancel', handlePointerUp);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const handleMinimize = async () => {
if (onMinimize) {
onMinimize();
} else {
tauriHandleMinimize();
}
};
const handleMaximize = async () => {
if (onToggleMaximize) {
onToggleMaximize();
} else {
tauriHandleToggleMaximize();
}
};
const handleClose = async () => {
if (onClose) {
onClose();
} else {
tauriHandleClose();
}
};
return (
<div
ref={parentRef}
className={clsx(
'window-buttons flex h-8 items-center justify-end space-x-2',
showClose || showMaximize || showMinimize ? 'visible' : 'hidden',
className,
)}
>
{showMinimize && appService?.hasWindowBar && (
<WindowButton onClick={handleMinimize} label={_('Minimize')} id='titlebar-minimize'>
<svg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' viewBox='0 0 24 24'>
<path fill='currentColor' d='M20 14H4v-2h16' />
</svg>
</WindowButton>
)}
{showMaximize && appService?.hasWindowBar && (
<WindowButton
onClick={handleMaximize}
label={_('Maximize or Restore')}
id='titlebar-maximize'
>
<svg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' viewBox='0 0 24 24'>
<path fill='currentColor' d='M4 4h16v16H4zm2 4v10h12V8z' />
</svg>
</WindowButton>
)}
{showClose && (appService?.hasWindowBar || onClose) && (
<WindowButton
onClick={handleClose}
label={closeButtonLabel || _('Close')}
id='titlebar-close'
>
<svg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' viewBox='0 0 24 24'>
<path
fill='currentColor'
d='M19 6.41L17.59 5L12 10.59L6.41 5L5 6.41L10.59 12L5 17.59L6.41 19L12 13.41L17.59 19L19 17.59L13.41 12z'
/>
</svg>
</WindowButton>
)}
</div>
);
};
export default WindowButtons;
|