#Updating tray title with temperature const from main.rs

3 messages · Page 1 of 1 (latest)

fervent zodiac
#

Im having a deathlock on this one seems passing tauri stuff around methods keep biting me.

I would like to update title of tray with a temperature and i do this from the react frontend with a tauri invoke. thus created a command for it.

fn update_title(app_handle: AppHandle, temperature: f64) -> Result<(), String> {

    #[cfg(debug_assertions)]
    println!("update_title");
    println!("temp: {}", temperature);

    let title = temperature;

    // Assuming `tray_handle_arc` is a global or accessible otherwise
    // if let Err(err) = tray_handle_arc.set_title(&title) {
    //     eprintln!("Error updating tray title: {:?}", err);
    //     return Err(format!("Error updating tray title: {:?}", err));
    // }

    Ok(())
}```
#

My main.rs ```
fn main() {
#[cfg(debug_assertions)]
println!("Main function started");
let mut use_autostart_plugin = true;
let (tx, mut rx): (tokio::sync::mpsc::Sender<()>, tokio::sync::mpsc::Receiver<()>) = tokio::sync::mpsc::channel(32);
let rx = Arc::new(Mutex::new(rx));
tauri::Builder::default()
.menu(tauri::Menu::new())
.setup(|app| {
tauri::updater::builder(app.handle()).should_install(|_current, _latest| true);
// tauri::updater::check_update();
let app_handle = app.handle();
let cloned_app_handle = app.handle().clone();
let app_handle_clone = app_handle.clone(); // Clone the AppHandle before using it in the async block
let app_handle = app_handle.clone();

        let main_window = ui::setup_main_window(&app.handle()).unwrap();

        // TRAY IS CREATED HERE from /src/ui/tray.rs
        Tray::setup_tray(app);

        Ok(())
    })
    .invoke_handler(tauri::generate_handler![
         update_title
    ])
    .build(tauri::generate_context!())
    .unwrap()
    .run(move |app_handle, event| {
    });

}

#[tauri::command]
fn update_title(app_handle: AppHandle, temperature: f64) -> Result<(), String> {

#[cfg(debug_assertions)]
println!("update_title");
println!("temp: {}", temperature);

let title = temperature;

// Assuming `tray_handle_arc` is a global or accessible otherwise
// if let Err(err) = tray_handle_arc.set_title(&title) {
//     eprintln!("Error updating tray title: {:?}", err);
//     return Err(format!("Error updating tray title: {:?}", err));
// }

Ok(())

}

#

how could i make this work? i have tray.rs defined in tray.rs like so ```pub struct Tray {
pub launch_at_login: Mutex<bool>,
}

impl Tray {

pub fn new() -> Self {
    Self {
        launch_at_login: Mutex::new(false),
    }
}

pub fn setup_tray<R: Runtime>(app: &mut App<R>) {
    let tray_instance = Tray::new();
    //pub fn setup_tray<R: Runtime>(app: &mut App<R>) {
    let app_handle_clone = app.handle().clone(); // Define app_handle_clone
    let menu = Tray::create_menu::<R>(&app_handle_clone, activated);
    let mut system_tray = SystemTray::new().with_menu(menu);

    Tray::setup_main_window_event(app);

    #[cfg(target_os = "macos")]
    {
        system_tray = system_tray.with_menu_on_left_click(false);
        //Tray::update_macos_title(&system_tray, "Initial Title"); // Provide an initial title
    }
    let system_tray_handle = system_tray
        .on_event(Tray::create_window_event_handler(app.handle()))
        .build(app)
        .expect("Failed to build system tray");
}

}