-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
175 lines (146 loc) · 5.66 KB
/
main.rs
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
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::{process::{Stdio, ExitStatus, ExitCode}, path::PathBuf};
use tauri::{Manager, App};
extern crate whoami;
#[derive(Default)]
pub struct MyState {
t: std::sync::Mutex<std::collections::HashMap<String, String>>,
}
#[tauri::command]
async fn prev(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<(), String> {
let curr_win_label = handle.get_focused_window().unwrap().label().to_owned();
let mut prev_win_label = "";
if curr_win_label == "screen-1" {
prev_win_label = ""
} else if curr_win_label == "screen-2" {
prev_win_label = "screen-1"
} else if curr_win_label == "screen-3" {
prev_win_label = "screen-2"
} else if curr_win_label == "screen-4" {
prev_win_label = "screen-3"
}
match handle.get_focused_window().unwrap().hide() {
Ok(_) => {},
Err(e) => println!("error from loading screen {:?}", e)
};
match handle.get_window(&prev_win_label).unwrap().show() {
Ok(_) => Ok(()),
Err(e) => Err(format!("error from loading screen {:?}", e))
}
}
#[tauri::command]
async fn next(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<(), String> {
let curr_win_label = handle.get_focused_window().unwrap().label().to_owned();
let mut next_win_label = "";
if curr_win_label == "screen-1" {
next_win_label = "screen-2"
} else if curr_win_label == "screen-2" {
next_win_label = "screen-3"
} else if curr_win_label == "screen-3" {
next_win_label = "screen-4"
}
let _ = match handle.get_focused_window().unwrap().hide() {
Ok(_) => Ok(()),
Err(e) => Err(format!("error from loading screen {:?}", e))
};
match handle.get_window(&next_win_label).unwrap().show() {
Ok(_) => Ok(()),
Err(e) => Err(format!("error from loading screen {:?}", e))
}
}
#[tauri::command]
async fn finish(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<(), String> {
handle.exit(0);
Ok(())
}
#[tauri::command]
async fn set_state(handle: tauri::AppHandle, state: tauri::State<'_, MyState>, key: String, value: String) -> Result<(), String> {
state.t.lock().unwrap().insert(key, value);
Ok(())
}
#[tauri::command]
async fn get_state(handle: tauri::AppHandle, state: tauri::State<'_, MyState>, key: String) -> Result<String, String> {
match state.t.lock().unwrap().get(&key) {
Some(val) => Ok(val.to_owned()),
None => Err("no-value-err".to_owned())
}
}
#[tauri::command]
async fn identify_curr_user(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<String, String> {
let username = whoami::username();
state.t.lock().unwrap().insert("install-user".to_owned(), username.clone());
Ok(username)
}
#[tauri::command]
async fn get_selected_install_user(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<String, String> {
Ok(state.t.lock().unwrap().get("install-user").unwrap().to_owned())
}
#[tauri::command]
async fn set_install_user(handle: tauri::AppHandle, state: tauri::State<'_, MyState>, name: String) -> Result<(), String> {
state.t.lock().unwrap().insert("install-user".to_owned(), name.clone());
Ok(())
}
mod macos;
mod win;
fn main() {
#[cfg(target_os = "macos")]
let app = tauri::Builder::default()
.manage(MyState::default())
.invoke_handler(tauri::generate_handler![prev, next, finish,
set_state, get_state, identify_curr_user, set_install_user, get_selected_install_user, elevate_re_run,
macos::get_default_install_folder,macos::get_user_list,
macos::stop_application, macos::uninstall_application, macos::start_install])
.build(tauri::generate_context!())
.expect("error while building tauri application");
#[cfg(windows)]
let app = tauri::Builder::default()
.manage(MyState::default())
.invoke_handler(tauri::generate_handler![prev, next, finish,
set_state, get_state, identify_curr_user, set_install_user, get_selected_install_user, elevate_re_run,
win::get_default_install_folder,win::get_user_list,
win::stop_application, win::uninstall_application, win::start_install])
.build(tauri::generate_context!())
.expect("error while building tauri application");
app.windows().into_iter().for_each(|(_, w)| {
w.hide().unwrap();
});
launch_window(&app);
// This will start the app and no other code below this will run.
app.run(|_, _| {});
}
#[cfg(target_os = "macos")]
fn launch_window(app: &App) {
let running_as = sudo::check();
dbg!("running_as check: ", &running_as);
if running_as != sudo::RunningAs::Root {
app.get_window("screen-0").unwrap().show().unwrap();
} else {
app.get_window("screen-1").unwrap().show().unwrap();
}
}
#[cfg(windows)]
fn launch_window(app: &App) {
app.get_window("screen-1").unwrap().show().unwrap();
}
#[cfg(target_os = "macos")]
#[tauri::command]
async fn elevate_re_run(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<(), String> {
handle.get_focused_window().unwrap().hide().unwrap();
match sudo::escalate_if_needed() {
Ok(as_user) => {
dbg!("running as user", as_user);
handle.exit(0);
Ok(())
},
Err(e) => {
dbg!("error elevating permissions", &e);
Err(format!("error elevating permissions: {:?}", e))
},
}
}
#[cfg(windows)]
#[tauri::command]
async fn elevate_re_run(handle: tauri::AppHandle, state: tauri::State<'_, MyState>) -> Result<(), String> {
Ok(())
}