-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathcommands.rs
More file actions
253 lines (213 loc) · 6.3 KB
/
commands.rs
File metadata and controls
253 lines (213 loc) · 6.3 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
use crate::AppExt;
#[derive(Debug, Clone, serde::Serialize, specta::Type)]
#[serde(rename_all = "camelCase")]
pub struct PluginManifestEntry {
pub id: String,
pub name: String,
pub version: String,
pub main_path: String,
}
#[derive(Debug, Clone, serde::Deserialize)]
struct PluginManifestFile {
id: String,
name: String,
version: String,
main: String,
}
#[tauri::command]
#[specta::specta]
pub async fn get_onboarding_needed<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
) -> Result<bool, String> {
app.get_onboarding_needed().map_err(|e| e.to_string())
}
#[tauri::command]
#[specta::specta]
pub async fn set_onboarding_needed<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
v: bool,
) -> Result<(), String> {
app.set_onboarding_needed(v).map_err(|e| e.to_string())
}
#[tauri::command]
#[specta::specta]
pub async fn get_dismissed_toasts<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
) -> Result<Vec<String>, String> {
app.get_dismissed_toasts()
}
#[tauri::command]
#[specta::specta]
pub async fn set_dismissed_toasts<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
v: Vec<String>,
) -> Result<(), String> {
app.set_dismissed_toasts(v)
}
#[tauri::command]
#[specta::specta]
pub async fn get_env<R: tauri::Runtime>(_app: tauri::AppHandle<R>, key: String) -> String {
std::env::var(&key).unwrap_or_default()
}
#[tauri::command]
#[specta::specta]
pub fn show_devtool() -> bool {
if cfg!(debug_assertions) {
return true;
}
#[cfg(feature = "devtools")]
{
return true;
}
#[cfg(not(feature = "devtools"))]
{
false
}
}
#[tauri::command]
#[specta::specta]
pub async fn resize_window_for_chat<R: tauri::Runtime>(
window: tauri::Window<R>,
) -> Result<(), String> {
const CHAT_PANEL_EXPANSION_WIDTH: u32 = 400;
let outer_size = window.outer_size().map_err(|e| e.to_string())?;
let outer_position = window.outer_position().map_err(|e| e.to_string())?;
let monitor = window.current_monitor().map_err(|e| e.to_string())?;
if let Some(monitor) = monitor {
let monitor_position = monitor.position();
let monitor_size = monitor.size();
let window_right = i64::from(outer_position.x) + i64::from(outer_size.width);
let monitor_right = i64::from(monitor_position.x) + i64::from(monitor_size.width);
if monitor_right - window_right < i64::from(CHAT_PANEL_EXPANSION_WIDTH) {
return Ok(());
}
}
let new_size = tauri::PhysicalSize {
width: outer_size.width + CHAT_PANEL_EXPANSION_WIDTH,
height: outer_size.height,
};
window
.set_size(tauri::Size::Physical(new_size))
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
#[specta::specta]
pub async fn resize_window_for_sidebar<R: tauri::Runtime>(
window: tauri::Window<R>,
) -> Result<(), String> {
let outer_size = window.outer_size().map_err(|e| e.to_string())?;
if outer_size.width < 840 {
let new_size = tauri::PhysicalSize {
width: outer_size.width + 280,
height: outer_size.height,
};
window
.set_size(tauri::Size::Physical(new_size))
.map_err(|e| e.to_string())?;
}
Ok(())
}
#[tauri::command]
#[specta::specta]
pub async fn get_tinybase_values<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
) -> Result<Option<String>, String> {
app.get_tinybase_values()
}
#[tauri::command]
#[specta::specta]
pub async fn set_tinybase_values<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
v: String,
) -> Result<(), String> {
app.set_tinybase_values(v)
}
#[tauri::command]
#[specta::specta]
pub async fn get_pinned_tabs<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
) -> Result<Option<String>, String> {
app.get_pinned_tabs()
}
#[tauri::command]
#[specta::specta]
pub async fn set_pinned_tabs<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
v: String,
) -> Result<(), String> {
app.set_pinned_tabs(v)
}
#[tauri::command]
#[specta::specta]
pub async fn get_recently_opened_sessions<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
) -> Result<Option<String>, String> {
app.get_recently_opened_sessions()
}
#[tauri::command]
#[specta::specta]
pub async fn set_recently_opened_sessions<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
v: String,
) -> Result<(), String> {
app.set_recently_opened_sessions(v)
}
#[tauri::command]
#[specta::specta]
pub async fn list_plugins<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
) -> Result<Vec<PluginManifestEntry>, String> {
use tauri_plugin_settings::SettingsPluginExt;
let base = app.settings().vault_base().map_err(|e| e.to_string())?;
let plugins_dir = base.join("plugins").into_std_path_buf();
if !plugins_dir.exists() {
std::fs::create_dir_all(&plugins_dir).map_err(|e| e.to_string())?;
return Ok(Vec::new());
}
let mut plugins = Vec::new();
for entry in std::fs::read_dir(&plugins_dir).map_err(|e| e.to_string())? {
let entry = match entry {
Ok(entry) => entry,
Err(_) => continue,
};
let Ok(file_type) = entry.file_type() else {
continue;
};
if !file_type.is_dir() {
continue;
}
let root = entry.path();
let manifest_path = root.join("plugin.json");
if !manifest_path.exists() {
continue;
}
let manifest: PluginManifestFile = match std::fs::read_to_string(&manifest_path)
.ok()
.and_then(|raw| serde_json::from_str::<PluginManifestFile>(&raw).ok())
{
Some(manifest) => manifest,
None => continue,
};
let main_relative = std::path::Path::new(&manifest.main);
if main_relative.is_absolute()
|| main_relative
.components()
.any(|c| c == std::path::Component::ParentDir)
{
continue;
}
let main_path = root.join(main_relative);
if !main_path.exists() {
continue;
}
plugins.push(PluginManifestEntry {
id: manifest.id,
name: manifest.name,
version: manifest.version,
main_path: main_path.to_string_lossy().to_string(),
});
}
plugins.sort_by(|a, b| a.id.cmp(&b.id));
Ok(plugins)
}