Skip to content

Commit ded5008

Browse files
committed
fix: fix config
1 parent 52de472 commit ded5008

21 files changed

+381
-627
lines changed

Cargo.lock

Lines changed: 183 additions & 411 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cspell.jsonc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"PCWSTR",
1111
"repr",
1212
"tauri",
13-
"Uncategorized"
13+
"Uncategorized",
14+
"unlisten"
1415
]
1516
}

gui/backend/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "bluetooth_battery_monitor_gui"
2+
name = "bluetooth_battery_monitor"
33
version.workspace = true
44
description = "Bluetooth battery monitor GUI"
55
authors = ["SARDONYX-sard"]

gui/backend/src/cmd/battery_reporter.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use super::bluetooth_info_cache::write_bt_cache;
21
use super::config::write_config;
32
use super::supports::{notify, update_tray};
43
use super::{config::read_config, find_bluetooth_devices};
@@ -7,20 +6,22 @@ use std::{
76
sync::atomic::{AtomicU64, Ordering},
87
time::Duration,
98
};
10-
use tauri::AppHandle;
9+
use tauri::{Manager, Window};
1110
use timer::{clear_interval, set_interval};
1211

1312
static INTERVAL_ID: AtomicU64 = AtomicU64::new(0);
1413

1514
/// # NOTE
1615
/// The callback fn cannot return a Result, so write only error log.
1716
#[tauri::command]
18-
pub async fn restart_interval(app: AppHandle) {
17+
pub async fn restart_interval(window: Window) {
18+
tracing::trace!("`restart_interval` was called.");
1919
let id = INTERVAL_ID.load(Ordering::Acquire);
2020
if id != 0 {
2121
clear_interval(id).await;
2222
};
2323

24+
let app = window.app_handle();
2425
let config = read_config(app.clone()).unwrap_or_else(|_| {
2526
let _ = err_log!(write_config(app.clone(), Default::default()));
2627
Default::default()
@@ -33,15 +34,15 @@ pub async fn restart_interval(app: AppHandle) {
3334
// Therefore, they become 'static' and must be cloned.
3435
let app = app.clone();
3536
let instance_id = config.instance_id.clone();
37+
let window = window.clone();
38+
3639
async move {
3740
// NOTE: The callback fn cannot return a Result, so write only error log.
3841
match find_bluetooth_devices().await {
3942
Ok(devices) => {
40-
if let Ok(json) = err_log!(serde_json::to_string_pretty(&devices)) {
41-
let _ = err_log!(write_bt_cache(app.clone(), &json));
42-
};
43+
tracing::debug!("Got devices: {:#?}", devices);
4344

44-
for dev in devices {
45+
for dev in &devices {
4546
if instance_id.is_empty() {
4647
if !dev.is_connected {
4748
continue;
@@ -59,6 +60,9 @@ pub async fn restart_interval(app: AppHandle) {
5960
let dev_name = &dev.friendly_name;
6061
let _ = err_log!(update_tray(&app, dev_name, battery_level).await);
6162
}
63+
if let Err(err) = window.emit("bt_monitor://restart_interval", devices) {
64+
tracing::error!("{err}");
65+
};
6266
}
6367
Err(e) => tracing::error!(e),
6468
};

gui/backend/src/cmd/bluetooth_info_cache.rs

Lines changed: 0 additions & 22 deletions
This file was deleted.

gui/backend/src/cmd/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
pub(crate) mod battery_reporter;
2-
mod bluetooth_info_cache;
32
mod config;
43
mod supports;
54

65
use crate::err_log;
76
use bluetooth::{device::device_info::FindBluetooth as _, BluetoothDeviceInfo};
7+
use supports::{default_tray, update_tray};
8+
use tauri::{AppHandle, Builder, Wry};
89

910
#[tauri::command]
1011
pub(crate) async fn update_tray_icon(
@@ -15,9 +16,19 @@ pub(crate) async fn update_tray_icon(
1516
err_log!(update_tray(&app, device_name, battery_level).await)
1617
}
1718

19+
#[tauri::command]
20+
pub(crate) async fn set_default_tray_icon(
21+
app: AppHandle,
22+
) -> Result<(), String> {
23+
err_log!(default_tray(&app).await)
24+
}
25+
1826
#[tauri::command]
1927
pub(crate) async fn find_bluetooth_devices() -> Result<Vec<BluetoothDeviceInfo>, String> {
20-
err_log!(BluetoothDeviceInfo::find_devices())
28+
tracing::trace!("`find_bluetooth_devices` was called.");
29+
let devices = err_log!(BluetoothDeviceInfo::find_devices())?;
30+
tracing::debug!("Got devices: {:#?}", devices);
31+
Ok(devices)
2132
}
2233

2334
#[tauri::command]
@@ -35,9 +46,6 @@ pub(crate) async fn write_file(path: &str, content: &str) -> Result<(), String>
3546
err_log!(std::fs::write(path, content))
3647
}
3748

38-
use supports::update_tray;
39-
use tauri::{AppHandle, Builder, Wry};
40-
4149
pub(crate) trait CommandsRegister {
4250
/// Implements custom commands.
4351
fn impl_commands(self) -> Self;
@@ -48,11 +56,10 @@ impl CommandsRegister for Builder<Wry> {
4856
self.invoke_handler(tauri::generate_handler![
4957
change_log_level,
5058
find_bluetooth_devices,
59+
set_default_tray_icon,
5160
update_tray_icon,
5261
write_file,
5362
battery_reporter::restart_interval,
54-
bluetooth_info_cache::read_bt_cache,
55-
bluetooth_info_cache::write_bt_cache,
5663
config::read_config,
5764
config::write_config,
5865
])

gui/backend/src/cmd/supports/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ mod system_tray;
44

55
pub(super) use logger::change_log_level;
66
pub(super) use notify::notify;
7-
pub(super) use system_tray::update_tray;
7+
pub(super) use system_tray::{default_tray, update_tray};

gui/backend/src/cmd/supports/system_tray.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,14 @@ pub async fn update_tray(
2525
tray_handle.set_icon(tauri::Icon::Raw(battery_icon.to_vec()))?;
2626
tray_handle.set_tooltip(&format!("{device_name} {battery_level}%"))
2727
}
28+
29+
/// Update application tray icon & name
30+
pub async fn default_tray(
31+
app: &tauri::AppHandle,
32+
) -> tauri::Result<()> {
33+
let battery_icon = include_bytes!("../../../icons/icon.png").as_slice();
34+
35+
let tray_handle = app.tray_handle();
36+
tray_handle.set_icon(tauri::Icon::Raw(battery_icon.to_vec()))?;
37+
tray_handle.set_tooltip("Getting bluetooth battery...")
38+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use crate::cmd::battery_reporter::restart_interval;
2+
use tauri::{AppHandle, Manager as _};
23

34
/// # Note
45
/// Interval callback fn cannot return Result. So relies on internal error log.
5-
pub fn start_interval(app: &tauri::AppHandle) {
6+
pub fn start_interval(app: &AppHandle) {
67
let app = app.clone();
8+
let window = app.get_window("main").unwrap();
79
tauri::async_runtime::spawn(async move {
8-
let _ = restart_interval(app).await;
10+
let _ = restart_interval(window).await;
911
});
1012
}

gui/frontend/src/backend_api/bluetooth_finder.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,6 @@ export async function FindBluetoothDevices() {
4040
* Restart interval to get bluetooth device information.
4141
* @throws `Error`
4242
*/
43-
export async function restartBtGetter() {
44-
return await invoke<BluetoothDeviceInfo[]>('restart_interval');
43+
export async function restartInterval() {
44+
await invoke('restart_interval');
4545
}
46-
47-
export const btCache = {
48-
/**
49-
* Get bluetooth device information from cache.
50-
* @throws `Error`
51-
*/
52-
async read() {
53-
return await invoke<BluetoothDeviceInfo[]>('read_bt_cache');
54-
},
55-
56-
/**
57-
* Write bluetooth device information to cache.
58-
* @throws `Error`
59-
*/
60-
async write(devices: BluetoothDeviceInfo[]) {
61-
// biome-ignore lint/style/useNamingConvention: <explanation>
62-
await invoke('write_bt_cache', { devices_json: JSON.stringify(devices) });
63-
},
64-
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { listen } from '@tauri-apps/api/event';
2+
3+
import type { BluetoothDeviceInfo } from '@/backend_api';
4+
import { notify } from '@/components/notifications';
5+
6+
import type { EventCallback } from '@tauri-apps/api/event';
7+
import type { JSX } from 'react/jsx-runtime';
8+
9+
type ListenerProps = {
10+
setDev: (devices: BluetoothDeviceInfo[]) => void;
11+
/** @default Error */
12+
error?: string | JSX.Element;
13+
};
14+
15+
export async function deviceListener({ setDev, error }: ListenerProps) {
16+
let unlisten: (() => void) | null = null;
17+
const eventHandler: EventCallback<BluetoothDeviceInfo[]> = (event) => {
18+
setDev(event.payload);
19+
};
20+
21+
try {
22+
// Setup before run Promise(For event hook)
23+
unlisten = await listen<BluetoothDeviceInfo[]>('bt_monitor://restart_interval', eventHandler);
24+
return unlisten;
25+
} catch (err) {
26+
notify.error(error ?? `${err}`);
27+
if (unlisten) {
28+
unlisten();
29+
}
30+
}
31+
}

gui/frontend/src/backend_api/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @index('./*', f => `export * from '${f.path}'`)
2-
export * from './backup';
32
export * from './backend_config';
3+
export * from './backup';
44
export * from './bluetooth_finder';
55
export * from './default_api_wrapper';
6+
export * from './device_listener';
67
export * from './lang';
78
export * from './log';
8-
export * from './progress_listener';
99
export * from './sys_tray';

gui/frontend/src/backend_api/progress_listener.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

gui/frontend/src/backend_api/sys_tray.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ import { invoke } from '@tauri-apps/api';
33
export async function updateTrayIcon(deviceName: string, batteryLevel: number) {
44
await invoke('update_tray_icon', { deviceName, batteryLevel });
55
}
6+
7+
export async function defaultTrayIcon() {
8+
await invoke('set_default_tray_icon');
9+
}

0 commit comments

Comments
 (0)