Files
kiss-translator/src/views/Action/ContentFab.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-10-21 02:07:33 +08:00
import Fab from "@mui/material/Fab";
import TranslateIcon from "@mui/icons-material/Translate";
import ThemeProvider from "../../hooks/Theme";
import Draggable from "./Draggable";
import { useState, useMemo, useCallback } from "react";
import { SettingProvider } from "../../hooks/Setting";
2025-10-22 01:50:49 +08:00
import { MSG_TRANS_TOGGLE, MSG_POPUP_TOGGLE } from "../../config";
2025-10-21 02:07:33 +08:00
import useWindowSize from "../../hooks/WindowSize";
export default function ContentFab({
fabConfig: { x: fabX, y: fabY, fabClickAction = 0 } = {},
2025-10-22 01:50:49 +08:00
processActions,
2025-10-21 02:07:33 +08:00
}) {
const fabWidth = 40;
const windowSize = useWindowSize();
const [moved, setMoved] = useState(false);
const handleStart = useCallback(() => {
setMoved(false);
}, []);
const handleMove = useCallback(() => {
setMoved(true);
}, []);
const handleClick = useCallback(() => {
if (!moved) {
if (fabClickAction === 1) {
2025-10-30 22:01:08 +08:00
processActions({ action: MSG_TRANS_TOGGLE });
2025-10-21 02:07:33 +08:00
} else {
2025-10-22 01:50:49 +08:00
processActions({ action: MSG_POPUP_TOGGLE });
2025-10-21 02:07:33 +08:00
}
}
2025-10-30 22:01:08 +08:00
}, [moved, fabClickAction, processActions]);
2025-10-21 02:07:33 +08:00
const fabProps = useMemo(
() => ({
windowSize,
width: fabWidth,
height: fabWidth,
left: fabX ?? -fabWidth,
top: fabY ?? windowSize.h / 2,
}),
[windowSize, fabWidth, fabX, fabY]
);
return (
<SettingProvider>
<ThemeProvider>
<Draggable
key="fab"
snapEdge
{...fabProps}
onStart={handleStart}
onMove={handleMove}
handler={
<Fab size="small" color="primary" onClick={handleClick}>
<TranslateIcon
sx={{
width: 24,
height: 24,
}}
/>
</Fab>
}
/>
</ThemeProvider>
</SettingProvider>
);
}