mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-27 06:39:24 +00:00

some of the requested changes warrant discussion so I haven't applied all of them here. Making a draft PR for now, we can discuss the rest later # Description of Changes See Matts todo list, the crossed out items are what were addressed in this PR --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details. --------- Co-authored-by: James Brunton <jbrunton96@gmail.com>
150 lines
4.4 KiB
TypeScript
150 lines
4.4 KiB
TypeScript
import React, { useState, useCallback } from "react";
|
|
import { SegmentedControl, Loader } from "@mantine/core";
|
|
import { useRainbowThemeContext } from "./RainbowThemeProvider";
|
|
import rainbowStyles from '../../styles/rainbow.module.css';
|
|
import VisibilityIcon from "@mui/icons-material/Visibility";
|
|
import EditNoteIcon from "@mui/icons-material/EditNote";
|
|
import FolderIcon from "@mui/icons-material/Folder";
|
|
import { ModeType, isValidMode } from '../../contexts/NavigationContext';
|
|
import { Tooltip } from "./Tooltip";
|
|
|
|
const viewOptionStyle = {
|
|
display: 'inline-flex',
|
|
flexDirection: 'row',
|
|
alignItems: 'center',
|
|
gap: 6,
|
|
whiteSpace: 'nowrap',
|
|
paddingTop: '0.3rem',
|
|
}
|
|
|
|
|
|
// Build view options showing text only for current view; others icon-only with tooltip
|
|
const createViewOptions = (currentView: ModeType, switchingTo: ModeType | null) => [
|
|
{
|
|
label: (
|
|
<div style={viewOptionStyle as React.CSSProperties}>
|
|
{switchingTo === "viewer" ? (
|
|
<Loader size="xs" />
|
|
) : (
|
|
<VisibilityIcon fontSize="small" />
|
|
)}
|
|
<span>Viewer</span>
|
|
</div>
|
|
),
|
|
value: "viewer",
|
|
},
|
|
{
|
|
label: (
|
|
<Tooltip content="Page Editor" position="bottom" arrow={true}>
|
|
<div style={viewOptionStyle as React.CSSProperties}>
|
|
{currentView === "pageEditor" ? (
|
|
<>
|
|
{switchingTo === "pageEditor" ? <Loader size="xs" /> : <EditNoteIcon fontSize="small" />}
|
|
<span>Page Editor</span>
|
|
</>
|
|
) : (
|
|
switchingTo === "pageEditor" ? <Loader size="xs" /> : <EditNoteIcon fontSize="small" />
|
|
)}
|
|
</div>
|
|
</Tooltip>
|
|
),
|
|
value: "pageEditor",
|
|
},
|
|
{
|
|
label: (
|
|
<Tooltip content="Active Files" position="bottom" arrow={true}>
|
|
<div style={viewOptionStyle as React.CSSProperties}>
|
|
{currentView === "fileEditor" ? (
|
|
<>
|
|
{switchingTo === "fileEditor" ? <Loader size="xs" /> : <FolderIcon fontSize="small" />}
|
|
<span>Active Files</span>
|
|
</>
|
|
) : (
|
|
switchingTo === "fileEditor" ? <Loader size="xs" /> : <FolderIcon fontSize="small" />
|
|
)}
|
|
</div>
|
|
</Tooltip>
|
|
),
|
|
value: "fileEditor",
|
|
},
|
|
];
|
|
|
|
interface TopControlsProps {
|
|
currentView: ModeType;
|
|
setCurrentView: (view: ModeType) => void;
|
|
selectedToolKey?: string | null;
|
|
}
|
|
|
|
const TopControls = ({
|
|
currentView,
|
|
setCurrentView,
|
|
selectedToolKey,
|
|
}: TopControlsProps) => {
|
|
const { isRainbowMode } = useRainbowThemeContext();
|
|
const [switchingTo, setSwitchingTo] = useState<ModeType | null>(null);
|
|
|
|
const isToolSelected = selectedToolKey !== null;
|
|
|
|
const handleViewChange = useCallback((view: string) => {
|
|
if (!isValidMode(view)) {
|
|
// Ignore invalid values defensively
|
|
return;
|
|
}
|
|
const mode = view as ModeType;
|
|
|
|
// Show immediate feedback
|
|
setSwitchingTo(mode as ModeType);
|
|
|
|
// Defer the heavy view change to next frame so spinner can render
|
|
requestAnimationFrame(() => {
|
|
// Give the spinner one more frame to show
|
|
requestAnimationFrame(() => {
|
|
setCurrentView(mode as ModeType);
|
|
|
|
// Clear the loading state after view change completes
|
|
setTimeout(() => setSwitchingTo(null), 300);
|
|
});
|
|
});
|
|
}, [setCurrentView]);
|
|
|
|
return (
|
|
<div className="absolute left-0 w-full top-0 z-[100] pointer-events-none">
|
|
{!isToolSelected && (
|
|
<div className="flex justify-center mt-[0.5rem]">
|
|
<SegmentedControl
|
|
data={createViewOptions(currentView, switchingTo)}
|
|
value={currentView}
|
|
onChange={handleViewChange}
|
|
color="blue"
|
|
fullWidth
|
|
className={isRainbowMode ? rainbowStyles.rainbowSegmentedControl : ''}
|
|
style={{
|
|
transition: 'all 0.2s ease',
|
|
opacity: switchingTo ? 0.8 : 1,
|
|
pointerEvents: 'auto'
|
|
}}
|
|
styles={{
|
|
root: {
|
|
borderRadius: 9999,
|
|
maxHeight: '2.6rem',
|
|
},
|
|
control: {
|
|
borderRadius: 9999,
|
|
},
|
|
indicator: {
|
|
borderRadius: 9999,
|
|
maxHeight: '2rem',
|
|
},
|
|
label: {
|
|
paddingTop: '0rem',
|
|
}
|
|
}}
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default TopControls;
|