-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsScripts.js
More file actions
174 lines (158 loc) · 5.14 KB
/
Copy pathsettingsScripts.js
File metadata and controls
174 lines (158 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const path = require("path"); // Import Node.js path module
const os = require("os"); // Import Node.js os module
const ws = require("windows-shortcuts"); // Import windows-shortcuts module
const fs = require("fs"); // Import Node.js file system module
const Store = require("electron-store"); // Import electron-store module
const { app, BrowserWindow, dialog } = require("electron");
const store = new Store(); // Initialize electron-store
// Function to initialize settings with default values if not already set
function initializeSettings() {
if (store.get("runOnStartup") == undefined) {
store.set("runOnStartup", false); // Set default value for runOnStartup
}
if (store.get("startMinimised") == undefined) {
store.set("startMinimised", false); // Set default value for startMinimised
}
if (store.get("closeToTray") == undefined) {
store.set("closeToTray", false); // Set default value for closeToTray
}
// Return the current settings
return {
runOnStartup: store.get("runOnStartup"),
startMinimised: store.get("startMinimised"),
closeToTray: store.get("closeToTray"),
};
}
// Function to toggle the run on startup setting
async function toggleRunOnStartup() {
const startUpFolder = path.join(
os.homedir(),
"AppData",
"Roaming",
"Microsoft",
"Windows",
"Start Menu",
"Programs",
"Startup"
); // Path to the startup folder
const exePath = path.join(__dirname, "../../ScreenDiary.exe"); // Path to the executable
const targetPath = path.join(startUpFolder, "ScreenDiary.lnk"); // Path to the shortcut
if (!fs.existsSync(targetPath)) {
// Create a shortcut if it doesn't exist
ws.create(targetPath, exePath, (err) => {
if (err) {
console.error("Failed to create shortcut: ", err); // Log any errors
}
});
store.set("runOnStartup", true); // Update the setting to true
} else {
// Remove the shortcut if it exists
fs.rmSync(targetPath);
store.set("runOnStartup", false); // Update the setting to false
}
}
// Function to toggle the start minimized setting
function toggleStartMinimised() {
store.set("startMinimised", !store.get("startMinimised")); // Toggle the start minimized setting
}
// Function to toggle the close to tray setting
function toggleCloseToTray() {
store.set("closeToTray", !store.get("closeToTray")); // Toggle the close to tray setting
}
app.whenReady().then(() => {
setTimeout(() => {
const win = BrowserWindow.getAllWindows()[0]; // Get the focused window
win.on("close", function (event) {
if (store.get("closeToTray") == true) {
event.preventDefault(); // Prevent the default window close action
win.hide(); // Hide the window
}
});
}, 1000);
});
function exportSettings() {
dialog
.showSaveDialog({
title: "Export Settings",
defaultPath: path.join(
os.homedir(),
`Screen Diary Settings ${new Date()
.toISOString()
.replaceAll(":", "-")}.json`
),
filters: [{ name: "JSON", extensions: ["json"] }],
})
.then((result) => {
if (!result.canceled) {
fs.writeFileSync(result.filePath, JSON.stringify(store.store, null, 4));
}
})
.catch((err) => {
console.log(err);
});
}
function importSettings() {
dialog
.showOpenDialog({
title: "Import Settings",
filters: [{ name: "JSON", extensions: ["json"] }],
properties: ["openFile"],
})
.then((result) => {
if (!result.canceled) {
const data = JSON.parse(fs.readFileSync(result.filePaths[0]));
// Check if data contains the required settings
if (
data.hasOwnProperty("runOnStartup") &&
data.hasOwnProperty("startMinimised") &&
data.hasOwnProperty("closeToTray")
) {
store.store = data;
app.relaunch(); // Reload the window
app.exit(); // Exit the application to apply changes and relaunch
} else {
dialog.showErrorBox(
"Invalid Settings",
"The selected file does not contain valid settings for this application."
);
}
}
})
.catch((err) => {
console.log(err);
});
}
function clearIconCache() {
const iconPath = path.join(app.getPath("userData"), "/Icons");
if (fs.existsSync(iconPath)) {
fs.rmSync(iconPath, { recursive: true });
}
}
function factoryReset() {
if (store.get("runOnStartup") == true) {
toggleRunOnStartup(); // Toggle the run on startup setting
}
store.clear(); // Clear all settings
app.relaunch(); // Reload the window
app.exit(); // Exit the application to apply changes and relaunch
}
function clearHistory() {
const historyPath = path.join(app.getPath("userData"), "/Save Data");
if (fs.existsSync(historyPath)) {
fs.rmSync(historyPath, { recursive: true });
}
app.relaunch(); // Reload the window
app.exit(); // Exit the application to apply changes and relaunch
}
// Export the functions for use in other modules
module.exports = {
toggleRunOnStartup,
initializeSettings,
toggleStartMinimised,
toggleCloseToTray,
exportSettings,
importSettings,
clearIconCache,
factoryReset,
clearHistory,
};