Skip to content

Commit

Permalink
feat: add pref to keep main window open after timer or stopwatch launch
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatNerdSquared committed May 17, 2024
1 parent df92ab0 commit 20f0d63
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 21 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@
"label": "Copy elapsed time",
"default": false
},
{
"name": "closeWindowOnTimerStart",
"type": "checkbox",
"required": false,
"title": "Close Window on Timer Start",
"description": "Whether or not to close the Raycast window when a timer or stopwatch is started.",
"label": "Automatically close window on start",
"default": true
},
{
"name": "newTimerInputOrder",
"type": "dropdown",
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useStopwatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default function useStopwatches() {
setIsLoading(false);
};

const handleStartSW = (swName = "Untitled") => {
startStopwatch(swName);
const handleStartSW = (swName = "Untitled", launchedFromMenuBar = false) => {
startStopwatch(swName, launchedFromMenuBar);
refreshSWes();
};

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useTimers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function useTimers() {

const handleStartTimer = (seconds: number, name: string, launchedFromMenuBar = false) => {
if (!checkForOverlyLoudAlert(launchedFromMenuBar)) return;
startTimer(seconds, name);
startTimer(seconds, launchedFromMenuBar, name);
refreshTimers();
};

Expand All @@ -41,7 +41,7 @@ export default function useTimers() {

const handleStartCT = (customTimer: CustomTimer, launchedFromMenuBar = false) => {
if (!checkForOverlyLoudAlert(launchedFromMenuBar)) return;
startTimer(customTimer.timeInSeconds, customTimer.name, customTimer.selectedSound);
startTimer(customTimer.timeInSeconds, launchedFromMenuBar, customTimer.name, customTimer.selectedSound);
refreshTimers();
};

Expand Down
2 changes: 1 addition & 1 deletion src/manageTimers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function Command(props: LaunchProps<{ launchContext: CommandLinkP
title: "This custom timer no longer exists!",
});
} else {
startTimer(ct.timeInSeconds, ct.name, ct.selectedSound);
startTimer(ct.timeInSeconds, false, ct.name, ct.selectedSound);
return;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/startCustomTimer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Action, ActionPanel, closeMainWindow, Form, getPreferenceValues, Toast
import { useState } from "react";
import { soundData } from "./soundData";
import { checkForOverlyLoudAlert, createCustomTimer, ensureCTFileExists, startTimer } from "./timerUtils";
import { CTInlineArgs, InputField, RayFormEvent, Values } from "./types";
import { CTInlineArgs, InputField, Preferences, RayFormEvent, Values } from "./types";

export default function CustomTimerView(props: { arguments: CTInlineArgs }) {
const hasArgs = Object.values(props.arguments).some((x) => x !== "");
const [hourErr, setHourErr] = useState<string | undefined>();
const [minErr, setMinErr] = useState<string | undefined>();
const [secErr, setSecErr] = useState<string | undefined>();

const prefs = getPreferenceValues();
const prefs: Preferences = getPreferenceValues();

const handleSubmit = (values: Values) => {
ensureCTFileExists();
Expand All @@ -28,7 +28,7 @@ export default function CustomTimerView(props: { arguments: CTInlineArgs }) {
closeMainWindow();
const timerName = values.name ? values.name : "Untitled";
const timeInSeconds = 3600 * Number(values.hours) + 60 * Number(values.minutes) + Number(values.seconds);
startTimer(timeInSeconds, timerName, values.selectedSound);
startTimer(timeInSeconds, false, timerName, values.selectedSound);
if (values.willBeSaved)
createCustomTimer({
name: values.name,
Expand Down
7 changes: 4 additions & 3 deletions src/stopwatchUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { environment, popToRoot, showHUD } from "@raycast/api";
import { environment, popToRoot } from "@raycast/api";
import { execSync } from "child_process";
import { randomUUID } from "crypto";
import { existsSync, readdirSync, readFileSync, writeFileSync } from "fs";
import { extname } from "path";
import { secondsBetweenDates } from "./formatUtils";
import { Stopwatch } from "./types";
import { showHudOrToast } from "./utils";

const SWPATH = environment.supportPath + "/raycast-stopwatches.json";

Expand Down Expand Up @@ -48,15 +49,15 @@ const getStopwatches = () => {
return setOfStopwatches;
};

const startStopwatch = async (swName = "Untitled") => {
const startStopwatch = async (swName = "Untitled", launchedFromMenuBar: boolean) => {
ensureSWFileExists();
const swStore: Stopwatch[] = JSON.parse(readFileSync(SWPATH).toString());
const newTimer = initStopwatch(swName);
swStore.push(newTimer);
writeFileSync(SWPATH, JSON.stringify(swStore));

popToRoot();
await showHUD(`Stopwatch "${swName}" started! 🎉`);
showHudOrToast(`Stopwatch "${swName}" started!`, launchedFromMenuBar, false);
};

const pauseStopwatch = (swToPause: string) => {
Expand Down
21 changes: 11 additions & 10 deletions src/timerUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { environment, getPreferenceValues, popToRoot, showHUD, showToast, Toast } from "@raycast/api";
import { environment, getPreferenceValues } from "@raycast/api";
import { exec } from "child_process";
import { randomUUID } from "crypto";
import { existsSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from "fs";
import { extname } from "path";
import { CustomTimer, Preferences, Timer } from "./types";
import { formatTime, secondsBetweenDates } from "./formatUtils";
import { showHudOrToast } from "./utils";

const DATAPATH = environment.supportPath + "/customTimers.json";
const DEFAULT_PRESET_VISIBLES_FILE = environment.supportPath + "/defaultPresetVisibles.json";
Expand All @@ -21,19 +22,19 @@ const silentFileDeletion = (fp: string) => {
const checkForOverlyLoudAlert = (launchedFromMenuBar = false) => {
const prefs = getPreferenceValues<Preferences>();
if (parseFloat(prefs.volumeSetting) > 5.0) {
const errorMsg = "⚠️ Timer alert volume should not be louder than 5 (it can get quite loud!)";
if (launchedFromMenuBar) {
showHUD(errorMsg);
} else {
showToast({ style: Toast.Style.Failure, title: errorMsg });
}
const errorMsg = "Timer alert volume should not be louder than 5 (it can get quite loud!)";
showHudOrToast(errorMsg, launchedFromMenuBar, true);
return false;
}
return true;
};

async function startTimer(timeInSeconds: number, timerName = "Untitled", selectedSound = "default") {
popToRoot();
async function startTimer(
timeInSeconds: number,
launchedFromMenuBar: boolean,
timerName = "Untitled",
selectedSound = "default",
) {
const fileName = environment.supportPath + "/" + new Date().toISOString() + "---" + timeInSeconds + ".timer";
const masterName = fileName.replace(/:/g, "__");
writeFileSync(masterName, timerName);
Expand Down Expand Up @@ -68,7 +69,7 @@ async function startTimer(timeInSeconds: number, timerName = "Untitled", selecte
return;
}
});
await showHUD(`Timer "${timerName}" started for ${formatTime(timeInSeconds)}! 🎉`);
showHudOrToast(`Timer "${timerName}" started for ${formatTime(timeInSeconds)}!`, launchedFromMenuBar, false);
}

function stopTimer(timerFile: string) {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ export interface Preferences {
selectedSound: string;
ringContinuously: boolean;
copyOnSwStop: boolean;
closeWindowOnTimerStart: boolean;
volumeSetting: string;
showTitleInMenuBar: boolean;
newTimerInputOrder: string;
}

export interface CTInlineArgs {
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Toast, getPreferenceValues, popToRoot, showHUD, showToast } from "@raycast/api";
import { Preferences } from "./types";

const showHudOrToast = (msg: string, launchedFromMenuBar: boolean, isErr: boolean) => {
const prefs: Preferences = getPreferenceValues();
if (launchedFromMenuBar || prefs.closeWindowOnTimerStart) {
const msgEmoji = isErr ? "⚠️" : "🎉";
showHUD(`${msgEmoji} ${msg}`);
return popToRoot();
} else {
showToast({ style: isErr ? Toast.Style.Failure : Toast.Style.Success, title: msg });
}
};

export { showHudOrToast };

0 comments on commit 20f0d63

Please sign in to comment.