Skip to content

Commit 4d5f58a

Browse files
committed
try fix always on top
1 parent 56fc1e2 commit 4d5f58a

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

src/components/PomodoroTimer.tsx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
usePomodoroHistory,
1111
PomodoroHistoryProvider,
1212
} from "@/hooks/usePomodoroHistory.tsx";
13+
import { useToast } from "@/components/ui/use-toast";
1314

1415
const formatTime = (sec: number) => {
1516
const m = Math.floor(sec / 60)
@@ -56,6 +57,7 @@ const PomodoroTimer: React.FC<PomodoroTimerProps> = ({
5657
const { pomodoro, updatePomodoro, theme } = useSettings();
5758
const { addSession } = usePomodoroHistory();
5859
const { t } = useTranslation();
60+
const { toast } = useToast();
5961
const pipWindowRef = useRef<Window | null>(null);
6062
const [now, setNow] = useState(Date.now());
6163
const [position, setPosition] = useState<{ x: number; y: number }>(() => {
@@ -185,14 +187,45 @@ const PomodoroTimer: React.FC<PomodoroTimerProps> = ({
185187
};
186188
};
187189
if (w.documentPictureInPicture) {
188-
pip = await w.documentPictureInPicture.requestWindow({
189-
width: 200,
190-
height: 200,
191-
});
190+
try {
191+
pip = await w.documentPictureInPicture.requestWindow({
192+
width: 200,
193+
height: 200,
194+
});
195+
} catch (err) {
196+
console.error("Failed to open PiP window", err);
197+
toast({
198+
title: t("pomodoroTimer.pipErrorTitle", "Floating Window Error"),
199+
description: t(
200+
"pomodoroTimer.pipErrorDesc",
201+
"Could not open always-on-top window. Falling back to standard window.",
202+
),
203+
variant: "destructive",
204+
});
205+
pip = window.open("", "", "width=200,height=200");
206+
}
192207
} else {
208+
toast({
209+
title: t("pomodoroTimer.pipUnsupportedTitle", "Not Supported"),
210+
description: t(
211+
"pomodoroTimer.pipUnsupportedDesc",
212+
"Your browser does not support always-on-top floating windows. Using standard window instead.",
213+
),
214+
variant: "default",
215+
});
193216
pip = window.open("", "", "width=200,height=200");
194217
}
195-
if (!pip) return;
218+
if (!pip) {
219+
toast({
220+
title: t("pomodoroTimer.popupBlockedTitle", "Popup Blocked"),
221+
description: t(
222+
"pomodoroTimer.popupBlockedDesc",
223+
"Please allow popups for this site to use the floating timer.",
224+
),
225+
variant: "destructive",
226+
});
227+
return;
228+
}
196229
pipWindowRef.current = pip;
197230
if (!pip.document.body) {
198231
pip.document.write(
@@ -236,6 +269,12 @@ const PomodoroTimer: React.FC<PomodoroTimerProps> = ({
236269
pip.addEventListener("beforeunload", cleanup);
237270
} catch (err) {
238271
console.error("Failed to open floating window", err);
272+
toast({
273+
title: "Error",
274+
description:
275+
"An unexpected error occurred while opening the floating window.",
276+
variant: "destructive",
277+
});
239278
}
240279
};
241280
// We rely on store.startTime for both work and break now.

0 commit comments

Comments
 (0)