forked from CapSoftware/Cap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneral_settings.rs
More file actions
265 lines (238 loc) · 8.17 KB
/
general_settings.rs
File metadata and controls
265 lines (238 loc) · 8.17 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use crate::window_exclusion::WindowExclusion;
use serde::{Deserialize, Serialize};
use serde_json::json;
use specta::Type;
use tauri::{AppHandle, Wry};
use tauri_plugin_store::StoreExt;
use tracing::{error, instrument};
use uuid::Uuid;
#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum PostStudioRecordingBehaviour {
#[default]
OpenEditor,
ShowOverlay,
}
#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum MainWindowRecordingStartBehaviour {
#[default]
Close,
Minimise,
}
#[derive(Default, Serialize, Deserialize, Type, Debug, Clone, Copy)]
#[serde(rename_all = "camelCase")]
pub enum PostDeletionBehaviour {
#[default]
DoNothing,
ReopenRecordingWindow,
}
impl MainWindowRecordingStartBehaviour {
pub fn perform(&self, window: &tauri::WebviewWindow) -> tauri::Result<()> {
match self {
Self::Close => window.close(),
Self::Minimise => window.minimize(),
}
}
}
const DEFAULT_EXCLUDED_WINDOW_TITLES: &[&str] = &[
"Cap",
"Cap Settings",
"Cap Recording Controls",
"Cap Camera",
];
pub fn default_excluded_windows() -> Vec<WindowExclusion> {
DEFAULT_EXCLUDED_WINDOW_TITLES
.iter()
.map(|title| WindowExclusion {
bundle_identifier: None,
owner_name: None,
window_title: Some((*title).to_string()),
})
.collect()
}
// When adding fields here, #[serde(default)] defines the value to use for existing configurations,
// and `Default::default` defines the value to use for new configurations.
// Things that affect the user experience should only be enabled by default for new configurations.
#[derive(Serialize, Deserialize, Type, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GeneralSettingsStore {
#[serde(default = "uuid::Uuid::new_v4")]
pub instance_id: Uuid,
#[serde(default)]
pub upload_individual_files: bool,
#[serde(default)]
pub hide_dock_icon: bool,
#[serde(default)]
pub auto_create_shareable_link: bool,
#[serde(default = "true_b")]
pub enable_notifications: bool,
#[serde(default)]
pub disable_auto_open_links: bool,
// first launch: store won't exist so show startup
#[serde(default = "true_b")]
pub has_completed_startup: bool,
#[serde(default)]
pub theme: AppTheme,
#[serde(default)]
pub commercial_license: Option<CommercialLicense>,
#[serde(default)]
pub last_version: Option<String>,
#[serde(default)]
pub window_transparency: bool,
#[serde(default)]
pub post_studio_recording_behaviour: PostStudioRecordingBehaviour,
#[serde(default)]
pub main_window_recording_start_behaviour: MainWindowRecordingStartBehaviour,
// Renamed from `custom_cursor_capture` to `custom_cursor_capture2` so we can change the default.
#[serde(default = "default_true", rename = "custom_cursor_capture2")]
pub custom_cursor_capture: bool,
#[serde(default = "default_server_url")]
pub server_url: String,
#[serde(default)]
pub recording_countdown: Option<u32>,
// #[deprecated = "can be removed when native camera preview is ready"]
#[serde(
default = "default_enable_native_camera_preview",
skip_serializing_if = "no"
)]
pub enable_native_camera_preview: bool,
#[serde(default)]
pub auto_zoom_on_clicks: bool,
// #[deprecated = "can be removed when new recording flow is the default"]
#[serde(
default = "default_enable_new_recording_flow",
skip_serializing_if = "no"
)]
pub enable_new_recording_flow: bool,
#[serde(default)]
pub post_deletion_behaviour: PostDeletionBehaviour,
#[serde(default = "default_excluded_windows")]
pub excluded_windows: Vec<WindowExclusion>,
#[serde(default)]
pub delete_instant_recordings_after_upload: bool,
#[serde(default = "default_instant_mode_max_resolution")]
pub instant_mode_max_resolution: u32,
#[serde(default)]
pub default_project_name_template: Option<String>,
}
fn default_enable_native_camera_preview() -> bool {
// This will help us with testing it
cfg!(all(debug_assertions, target_os = "macos"))
}
fn default_enable_new_recording_flow() -> bool {
cfg!(debug_assertions)
}
fn no(_: &bool) -> bool {
false
}
fn default_true() -> bool {
true
}
fn default_instant_mode_max_resolution() -> u32 {
1920
}
fn default_server_url() -> String {
std::option_env!("VITE_SERVER_URL")
.unwrap_or("https://cap.so")
.to_string()
}
#[derive(Serialize, Deserialize, Type, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CommercialLicense {
license_key: String,
expiry_date: Option<f64>,
refresh: f64,
activated_on: f64,
}
impl Default for GeneralSettingsStore {
fn default() -> Self {
Self {
instance_id: uuid::Uuid::new_v4(),
upload_individual_files: false,
hide_dock_icon: false,
auto_create_shareable_link: false,
enable_notifications: true,
disable_auto_open_links: false,
has_completed_startup: false,
theme: AppTheme::System,
commercial_license: None,
last_version: None,
window_transparency: false,
post_studio_recording_behaviour: PostStudioRecordingBehaviour::OpenEditor,
main_window_recording_start_behaviour: MainWindowRecordingStartBehaviour::Close,
custom_cursor_capture: true,
server_url: default_server_url(),
recording_countdown: Some(3),
enable_native_camera_preview: default_enable_native_camera_preview(),
auto_zoom_on_clicks: false,
enable_new_recording_flow: default_enable_new_recording_flow(),
post_deletion_behaviour: PostDeletionBehaviour::DoNothing,
excluded_windows: default_excluded_windows(),
delete_instant_recordings_after_upload: false,
instant_mode_max_resolution: 1920,
default_project_name_template: None,
}
}
}
#[derive(Default, Debug, Copy, Clone, Serialize, Deserialize, Type)]
#[serde(rename_all = "camelCase")]
pub enum AppTheme {
#[default]
System,
Light,
Dark,
}
fn true_b() -> bool {
true
}
impl GeneralSettingsStore {
pub fn get(app: &AppHandle<Wry>) -> Result<Option<Self>, String> {
match app.store("store").map(|s| s.get("general_settings")) {
Ok(Some(store)) => {
// Handle potential deserialization errors gracefully
match serde_json::from_value(store) {
Ok(settings) => Ok(Some(settings)),
Err(e) => Err(format!("Failed to deserialize general settings store: {e}")),
}
}
_ => Ok(None),
}
}
// i don't trust anyone to not overwrite the whole store lols
pub fn update(app: &AppHandle, update: impl FnOnce(&mut Self)) -> Result<(), String> {
let Ok(store) = app.store("store") else {
return Err("Store not found".to_string());
};
let mut settings = Self::get(app)?.unwrap_or_default();
update(&mut settings);
store.set("general_settings", json!(settings));
store.save().map_err(|e| e.to_string())
}
fn save(&self, app: &AppHandle) -> Result<(), String> {
let Ok(store) = app.store("store") else {
return Err("Store not found".to_string());
};
store.set("general_settings", json!(self));
store.save().map_err(|e| e.to_string())
}
}
pub fn init(app: &AppHandle) {
println!("Initializing GeneralSettingsStore");
let store = match GeneralSettingsStore::get(app) {
Ok(Some(store)) => store,
Ok(None) => GeneralSettingsStore::default(),
Err(e) => {
error!("Failed to deserialize general settings store: {}", e);
GeneralSettingsStore::default()
}
};
store.save(app).unwrap();
println!("GeneralSettingsState managed");
}
#[tauri::command]
#[specta::specta]
#[instrument]
pub fn get_default_excluded_windows() -> Vec<WindowExclusion> {
default_excluded_windows()
}