Skip to content

Commit

Permalink
feat: insert interval on setup
Browse files Browse the repository at this point in the history
  • Loading branch information
SARDONYX-sard committed Jan 21, 2025
1 parent 2669417 commit 362e898
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions gui/backend/src/cmd/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static INTERVAL_ID: AtomicU64 = AtomicU64::new(0);
/// The callback fn cannot return a Result, so write only error log.
#[tauri::command]
pub async fn restart_interval(app: AppHandle) {
tracing::trace!("`restart_interval` was called.");
tracing::debug!("`restart_interval` was called.");
let id = INTERVAL_ID.load(Ordering::Acquire);
if id != 0 {
clear_interval(id).await;
Expand All @@ -34,8 +34,8 @@ pub async fn restart_interval(app: AppHandle) {

let id = set_interval(
move || {
// Callbacks in the interval may survive until program termination in the worst case.
// Therefore, they become 'static' and must be cloned.
tracing::debug!("`restart_interval` closure was called.");

let app = app.clone();
let address = config.address;
let window = app.get_webview_window("main").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion gui/backend/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod config;
mod interval;
mod supports;
mod system_tray;

pub(super) mod device_watcher;
pub(super) mod interval;

use crate::err_log_to_string;
use tauri::{Builder, Wry};
Expand Down
3 changes: 1 addition & 2 deletions gui/backend/src/cmd/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ use tauri::image::Image;
///
/// # Panics
/// 0 <= battery_level <= 100
#[tracing::instrument(level = "trace")]
pub fn update_tray_inner(
device_name: &str,
battery_level: u64,
is_connected: bool,
) -> tauri::Result<()> {
tracing::debug!("Change to {battery_level} battery icon");

let battery_icon = if is_connected {
match battery_level {
0 => include_bytes!("../../icons/battery/battery-0.png"),
Expand Down
6 changes: 5 additions & 1 deletion gui/backend/src/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ mod window_event;

use self::tray_menu::new_tray_menu;
use self::window_event::window_event;
use crate::{cmd::device_watcher::restart_device_watcher_inner, err_log};
use crate::{
cmd::{device_watcher::restart_device_watcher_inner, interval::restart_interval},
err_log,
};
use tauri::{Builder, Manager, Wry};
pub use tray_menu::TRAY_ICON;

Expand All @@ -23,6 +26,7 @@ impl SetupsRegister for Builder<Wry> {
tauri::async_runtime::spawn(async move {
let app = app;
err_log!(restart_device_watcher_inner(&app).await);
restart_interval(app).await;
});

Ok(())
Expand Down

0 comments on commit 362e898

Please sign in to comment.