export const useWindowManager = () => {
const context = useContext(WindowManagerContext);
if (!context) {
throw new Error('useWindowManager must be used within a WindowManagerProvider');
}
return context;
};
TASK: I want it so that there is a new prop called pinnedToTaskbar either true or false and that will mean if it is pinned to taskbar e.g. always displayed even is window is closed. if that is false window will still display in taskbar but only is opened or minimsied.
To implement the pinnedToTaskbar feature, we'll need to update both the WindowManagerContext and the Taskbar component. Below are the detailed steps and code modifications to achieve the desired functionality:
1. Update WindowProps in WindowManagerContext.tsx:
Add the pinnedToTaskbar property to the WindowProps type definition.
2. Ensure pinnedToTaskbar is accessible in window initialization:
When dynamically importing windows, make sure that windowProps includes pinnedToTaskbar.
// contexts/WindowManagerContext.tsx
// Inside the importWindows function
const windowProps: WindowProps = mod.windowProps || {};
// No changes needed here since windowProps will now include pinnedToTaskbar if specified
3. Modify the Taskbar component to account for pinnedToTaskbar:
Update the mapping logic in the Taskbar.tsx to decide whether to show a window in the taskbar based on pinnedToTaskbar, visible, and minimized state.
// app/Taskbar.tsx
import React from 'react';
import { useWindowManager } from '../contexts/WindowManagerContext';
import { FaWindows } from 'react-icons/fa';
const Taskbar: React.FC = () => {
const { windows, windowStates, openWindow, restoreWindow, minimizeWindow } = useWindowManager();
return (
<div className="fixed bottom-0 left-0 right-0 bg-gray-800 h-[50px] flex items-center px-2 taskbar">
{/* Meta Icon Button */}
<button className="text-white p-2 hover:bg-gray-700 rounded">
<FaWindows size={24} />
</button>
{/* Divider */}
<div className="h-6 border-l border-gray-600 mx-2"></div>
{/* Window Buttons */}
{Object.keys(windows).map((name) => {
const state = windowStates[name];
const windowData = windows[name];
const windowProps = windowData.windowProps;
if (!state) return null;
const isPinned = windowProps.pinnedToTaskbar;
const isVisible = state.visible;
const isMinimized = state.minimized;
// Decide whether to render the button
if (!isPinned && !isVisible && !isMinimized) {
// Not pinned and not open/minimized, so don't render
return null;
}
let buttonStyle = 'bg-gray-600 text-white px-4 py-1 mx-1 rounded hover:bg-gray-500';
if (isVisible && !isMinimized) {
// Window is open
buttonStyle = 'bg-blue-600 text-white px-4 py-1 mx-1 rounded hover:bg-blue-500';
} else if (isMinimized) {
// Window is minimized
buttonStyle = 'bg-yellow-500 text-white px-4 py-1 mx-1 rounded hover:bg-yellow-400';
} else {
// Window is closed but pinned
buttonStyle = 'bg-gray-600 text-white px-4 py-1 mx-1 rounded hover:bg-gray-500';
}
const handleClick = () => {
if (isVisible && !isMinimized) {
// Window is open and not minimized, minimize it
minimizeWindow(name);
} else if (isMinimized) {
// Restore minimized window
restoreWindow(name);
} else {
// Open window
openWindow(name);
}
};
return (
<button key={name} onClick={handleClick} className={buttonStyle}>
{name}
</button>
);
})}
</div>
);
};
export default Taskbar;
4. Specify pinnedToTaskbar in individual window components:
In your window components (e.g., windows/MyWindow.tsx), define the windowProps and set pinnedToTaskbar as needed.
// windows/MyWindow.tsx
import React from 'react';
export const windowProps = {
width: '400px',
height: '300px',
initialState: 'closed',
windowTitle: 'My Window',
pinnedToTaskbar: true, // Set to true or false according to your preference
};
const MyWindow: React.FC = () => {
return <div>Your window content here</div>;
};
export default MyWindow;
5. Ensure that windowProps are correctly imported in WindowManagerContext:
No changes are needed since windowProps are already being imported and used when initializing windows.
6. Test the functionality:
Windows with pinnedToTaskbar: true should always appear on the taskbar, even when closed.
Clicking on a pinned window's taskbar button should open or restore the window.
Windows without pinnedToTaskbar or with pinnedToTaskbar: false should only appear on the taskbar when opened or minimized.
Summary:
By adding the pinnedToTaskbar property to WindowProps and modifying the logic in the Taskbar component, we can control whether a window's button is always displayed in the taskbar or only when the window is open or minimized. This provides the flexibility to pin certain applications to the taskbar, mimicking the behavior of modern operating systems.
Final Taskbar.tsx Code with Changes:
// app/Taskbar.tsx
'use client';
import React from 'react';
import { useWindowManager } from '../contexts/WindowManagerContext';
import { FaWindows } from 'react-icons/fa';
const Taskbar: React.FC = () => {
const { windows, windowStates, openWindow, restoreWindow, minimizeWindow } = useWindowManager();
return (
<div className="fixed bottom-0 left-0 right-0 bg-gray-800 h-[50px] flex items-center px-2 taskbar">
{/* Meta Icon Button */}
<button className="text-white p-2 hover:bg-gray-700 rounded">
<FaWindows size={24} />
</button>
{/* Divider */}
<div className="h-6 border-l border-gray-600 mx-2"></div>
{/* Window Buttons */}
{Object.keys(windows).map((name) => {
const state = windowStates[name];
const windowData = windows[name];
const windowProps = windowData.windowProps;
if (!state) return null;
const isPinned = windowProps.pinnedToTaskbar;
const isVisible = state.visible;
const isMinimized = state.minimized;
// Decide whether to render the button
if (!isPinned && !isVisible && !isMinimized) {
// Not pinned and not open/minimized, so don't render
return null;
}
let buttonStyle = 'bg-gray-600 text-white px-4 py-1 mx-1 rounded hover:bg-gray-500';
if (isVisible && !isMinimized) {
// Window is open
buttonStyle = 'bg-blue-600 text-white px-4 py-1 mx-1 rounded hover:bg-blue-500';
} else if (isMinimized) {
// Window is minimized
buttonStyle = 'bg-yellow-500 text-white px-4 py-1 mx-1 rounded hover:bg-yellow-400';
} else {
// Window is closed but pinned
buttonStyle = 'bg-gray-600 text-white px-4 py-1 mx-1 rounded hover:bg-gray-500';
}
const handleClick = () => {
if (isVisible && !isMinimized) {
// Window is open and not minimized, minimize it
minimizeWindow(name);
} else if (isMinimized) {
// Restore minimized window
restoreWindow(name);
} else {
// Open window
openWindow(name);
}
};
return (
<button key={name} onClick={handleClick} className={buttonStyle}>
{name}
</button>
);
})}
</div>
);
};
export default Taskbar;
Final WindowManagerContext.tsx Code with Changes:
// contexts/WindowManagerContext.tsx
'use client';
import React, { createContext, useContext, useState, useEffect } from 'react';
type WindowProps = {
width?: string;
height?: string;
initialState?: 'open' | 'closed' | 'minimized';
gravity?: number;
initialX?: string;
initialY?: string;
windowTitle?: string;
pinnedToTaskbar?: boolean; // Added pinnedToTaskbar property
};
// Rest of the code remains the same
By following these steps, you've successfully added the pinnedToTaskbar feature to your application.