Merge pull request #710 from hydralauncher/fix/window-auto-launch-on-startup

fix: windows auto launch on startup
This commit is contained in:
Zamitto 2024-06-28 11:16:32 -03:00 committed by GitHub
commit ec8ccf7728
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 60 additions and 14 deletions

View file

@ -8,6 +8,7 @@ extraResources:
- from: node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe - from: node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe
to: fastlist.exe to: fastlist.exe
- from: node_modules/create-desktop-shortcuts/src/windows.vbs - from: node_modules/create-desktop-shortcuts/src/windows.vbs
- from: resources/hydralauncher.vbs
files: files:
- "!**/.vscode/*" - "!**/.vscode/*"
- "!src/*" - "!src/*"

View file

@ -0,0 +1,3 @@
Set WshShell = CreateObject("WScript.Shell" )
WshShell.Run """%localappdata%\Programs\Hydra\Hydra.exe""", 0 'Must quote command if it has spaces; must escape quotes
Set WshShell = Nothing

View file

@ -14,3 +14,12 @@ export const logsPath = path.join(app.getPath("appData"), "hydra", "logs");
export const seedsPath = app.isPackaged export const seedsPath = app.isPackaged
? path.join(process.resourcesPath, "seeds") ? path.join(process.resourcesPath, "seeds")
: path.join(__dirname, "..", "..", "seeds"); : path.join(__dirname, "..", "..", "seeds");
export const windowsStartupPath = path.join(
app.getPath("appData"),
"Microsoft",
"Windows",
"Start Menu",
"Programs",
"Startup"
);

View file

@ -49,4 +49,8 @@ import "./profile/update-profile";
ipcMain.handle("ping", () => "pong"); ipcMain.handle("ping", () => "pong");
ipcMain.handle("getVersion", () => app.getVersion()); ipcMain.handle("getVersion", () => app.getVersion());
ipcMain.handle(
"isPortableVersion",
() => process.env.PORTABLE_EXECUTABLE_FILE != null
);
ipcMain.handle("getDefaultDownloadsPath", () => defaultDownloadsPath); ipcMain.handle("getDefaultDownloadsPath", () => defaultDownloadsPath);

View file

@ -1,11 +1,27 @@
import { windowsStartupPath } from "@main/constants";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import AutoLaunch from "auto-launch"; import AutoLaunch from "auto-launch";
import { app } from "electron"; import { app } from "electron";
import fs from "node:fs";
import path from "node:path";
const autoLaunch = async ( const autoLaunch = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
enabled: boolean enabled: boolean
) => { ) => {
if (!app.isPackaged) return;
if (process.platform == "win32") {
const destination = path.join(windowsStartupPath, "hydralauncher.vbs");
if (enabled) {
const scriptPath = path.join(process.resourcesPath, "hydralauncher.vbs");
fs.copyFileSync(scriptPath, destination);
} else {
fs.rmSync(destination);
}
} else {
const appLauncher = new AutoLaunch({ const appLauncher = new AutoLaunch({
name: app.getName(), name: app.getName(),
}); });
@ -14,6 +30,7 @@ const autoLaunch = async (
} else { } else {
appLauncher.disable().catch(); appLauncher.disable().catch();
} }
}
}; };
registerEvent("autoLaunch", autoLaunch); registerEvent("autoLaunch", autoLaunch);

View file

@ -110,6 +110,7 @@ contextBridge.exposeInMainWorld("electron", {
ping: () => ipcRenderer.invoke("ping"), ping: () => ipcRenderer.invoke("ping"),
getVersion: () => ipcRenderer.invoke("getVersion"), getVersion: () => ipcRenderer.invoke("getVersion"),
getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"), getDefaultDownloadsPath: () => ipcRenderer.invoke("getDefaultDownloadsPath"),
isPortableVersion: () => ipcRenderer.invoke("isPortableVersion"),
openExternal: (src: string) => ipcRenderer.invoke("openExternal", src), openExternal: (src: string) => ipcRenderer.invoke("openExternal", src),
isUserLoggedIn: () => ipcRenderer.invoke("isUserLoggedIn"), isUserLoggedIn: () => ipcRenderer.invoke("isUserLoggedIn"),
showOpenDialog: (options: Electron.OpenDialogOptions) => showOpenDialog: (options: Electron.OpenDialogOptions) =>

View file

@ -104,6 +104,7 @@ declare global {
getVersion: () => Promise<string>; getVersion: () => Promise<string>;
ping: () => string; ping: () => string;
getDefaultDownloadsPath: () => Promise<string>; getDefaultDownloadsPath: () => Promise<string>;
isPortableVersion: () => Promise<boolean>;
showOpenDialog: ( showOpenDialog: (
options: Electron.OpenDialogOptions options: Electron.OpenDialogOptions
) => Promise<Electron.OpenDialogReturnValue>; ) => Promise<Electron.OpenDialogReturnValue>;

View file

@ -10,6 +10,8 @@ export function SettingsBehavior() {
(state) => state.userPreferences.value (state) => state.userPreferences.value
); );
const [showRunAtStartup, setShowRunAtStartup] = useState(false);
const { updateUserPreferences } = useContext(settingsContext); const { updateUserPreferences } = useContext(settingsContext);
const [form, setForm] = useState({ const [form, setForm] = useState({
@ -28,6 +30,12 @@ export function SettingsBehavior() {
} }
}, [userPreferences]); }, [userPreferences]);
useEffect(() => {
window.electron.isPortableVersion().then((isPortableVersion) => {
setShowRunAtStartup(!isPortableVersion);
});
}, []);
const handleChange = (values: Partial<typeof form>) => { const handleChange = (values: Partial<typeof form>) => {
setForm((prev) => ({ ...prev, ...values })); setForm((prev) => ({ ...prev, ...values }));
updateUserPreferences(values); updateUserPreferences(values);
@ -45,6 +53,7 @@ export function SettingsBehavior() {
} }
/> />
{showRunAtStartup && (
<CheckboxField <CheckboxField
label={t("launch_with_system")} label={t("launch_with_system")}
onChange={() => { onChange={() => {
@ -53,6 +62,7 @@ export function SettingsBehavior() {
}} }}
checked={form.runAtStartup} checked={form.runAtStartup}
/> />
)}
</> </>
); );
} }