generated from JulianKominovic/tauri-ts-react-vite-tailwind-extras-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
157 lines (140 loc) · 5.5 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
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use log::{error, trace};
use once_cell::sync::Lazy;
#[cfg(target_os = "macos")]
use plugins::mac_os::traffic_lights::setup_traffic_light_positioner;
use rdev::{listen, EventType, Key};
use std::sync::Arc;
use std::thread;
use std::{env, path::PathBuf};
use tauri::async_runtime::Mutex;
use tauri::Manager;
use tauri_plugin_log::LogTarget;
use window_vibrancy::{apply_vibrancy, NSVisualEffectMaterial};
pub mod constants;
pub mod events;
pub mod keypress;
pub mod plugins;
pub mod soundpacks;
pub mod utils;
use soundpacks::{download_soundpack_if_necessary, Soundpack};
use specta::collect_types;
use tauri_specta::ts;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate cocoa;
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
static SOUNDPACK: Lazy<Arc<Mutex<Soundpack>>> =
Lazy::new(|| Arc::new(Mutex::new(Soundpack::new())));
static KEYS_PRESSED: Lazy<std::sync::Mutex<Vec<Key>>> =
Lazy::new(|| std::sync::Mutex::new(Vec::new()));
#[specta::specta]
#[tauri::command]
async fn set_volume_level(volume: f32) -> Result<(), String> {
if volume < 0.0 || volume > 1.0 {
return Err("For good sake of your ears volume must be between 0.0 and 1.0. If you see this, probably a bug happened.".to_string());
}
trace!("Setting volume to: {}", volume);
let mut soundpack_lock = SOUNDPACK.lock().await;
soundpack_lock.set_volume(volume);
Ok(())
}
#[specta::specta]
#[tauri::command]
async fn choose_soundpack(soundpack_id: String, soundpack_folder: String) -> Result<(), String> {
trace!(
"Choosing soundpack: {} with folder located in: {}",
soundpack_id,
soundpack_folder
);
let new_soundpack_folder = PathBuf::from(soundpack_folder).join(&soundpack_id);
trace!("New soundpack folder will be: {:?}", new_soundpack_folder);
let download_result =
download_soundpack_if_necessary(soundpack_id, &new_soundpack_folder).await;
if download_result.is_err() {
return Err(download_result.unwrap_err());
}
let new_soundpack_sound_data = soundpacks::prepare_sound_data(&new_soundpack_folder);
let new_soundpack_slices = soundpacks::prepare_sound_slices(&new_soundpack_folder);
trace!("New soundpack slices: {:?}", new_soundpack_slices);
trace!("New soundpack sound data: {:?}", new_soundpack_sound_data);
let mut soundpack_lock = SOUNDPACK.lock().await;
if new_soundpack_sound_data.is_err() {
return Err("Error preparing sounds".to_string());
}
if new_soundpack_slices.is_empty() {
return Err("Error preparing sound slices".to_string());
}
soundpack_lock.set_sound_data(Some(new_soundpack_sound_data.unwrap()));
soundpack_lock.set_sound_slices(new_soundpack_slices);
Ok(())
}
fn main() {
#[cfg(debug_assertions)]
ts::export(
collect_types![choose_soundpack, set_volume_level],
"../src/bindings.ts",
)
.unwrap();
/*
Here we listen for key events and play sounds accordingly.
We also keep track of keys that are currently pressed to avoid playing the same sound multiple times when a key is held down.
*/
thread::spawn(|| {
if let Err(error) = listen(move |event| match event.event_type {
EventType::KeyPress(key) => {
let mut keys_pressed_lock = KEYS_PRESSED.lock().unwrap();
if keys_pressed_lock.contains(&key) {
drop(keys_pressed_lock);
return;
} else {
keys_pressed_lock.push(key);
drop(keys_pressed_lock);
}
tauri::async_runtime::block_on(async {
SOUNDPACK.lock().await.play_sound(key, false);
});
}
EventType::KeyRelease(key) => {
let mut keys_pressed_lock = KEYS_PRESSED.lock().unwrap();
keys_pressed_lock.retain(|&x| x != key);
tauri::async_runtime::block_on(async {
SOUNDPACK.lock().await.play_sound(key, true);
});
}
_ => {}
}) {
error!("Error: {:?}", error)
}
});
tauri::Builder::default()
.setup(move |app| {
let window = app.get_window("main").unwrap();
let window_clone = window.clone();
#[cfg(target_os = "macos")]
apply_vibrancy(&window, NSVisualEffectMaterial::Sidebar, None, None)
.expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS");
#[cfg(target_os = "windows")]
apply_blur(&window, Some((18, 18, 18, 125)))
.expect("Unsupported platform! 'apply_blur' is only supported on Windows");
#[cfg(target_os = "macos")]
window.on_window_event(move |event| {
if let tauri::WindowEvent::ThemeChanged(_theme) = event {
setup_traffic_light_positioner(window_clone.clone())
}
});
Ok(())
})
.invoke_handler(tauri::generate_handler![choose_soundpack, set_volume_level])
.plugin(plugins::mac_os::traffic_lights::init())
.plugin(
tauri_plugin_log::Builder::default()
.targets([LogTarget::LogDir, LogTarget::Stdout, LogTarget::Webview])
.build(),
)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}