Skip to content

Commit

Permalink
bugfix: fixing pause unpause status in the menu (#543)
Browse files Browse the repository at this point in the history
* cleanup from previous branch

* show pause/unpause status in menu again
  • Loading branch information
a5huynh authored Nov 14, 2024
1 parent de762fd commit 95934b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
1 change: 0 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ on:
push:
branches:
- release
- tweak/enable-cuda-in-windows-release-build
jobs:
publish-tauri:
strategy:
Expand Down
19 changes: 8 additions & 11 deletions apps/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::Arc;

use auto_launch::AutoLaunchBuilder;
use constants::SEARCH_WIN_NAME;
use menu::MenuState;
use rpc::RpcMutex;
use tauri::image::Image;
use tauri::process::current_binary;
Expand Down Expand Up @@ -199,11 +200,6 @@ pub fn run() -> Result<(), Box<dyn std::error::Error>> {
.on_tray_icon_event(menu::handle_tray_icon_events)
.build(app)?;

// Copy default plugins to data directory to be picked up by the backend
// if let Err(e) = copy_plugins(&config, app.path()) {
// log::error!("Unable to copy default plugins: {}", e);
// }

let bar = get_searchbar(app_handle);
bar.show()?;

Expand Down Expand Up @@ -351,7 +347,7 @@ pub fn update_auto_launch(user_settings: &UserSettings) {
}
}

async fn pause_crawler(app: AppHandle, _menu_id: String) {
async fn pause_crawler(app: AppHandle) {
if let Some(rpc) = app.try_state::<RpcMutex>() {
let pause_state = app.state::<Arc<PauseState>>().inner();
let rpc = rpc.lock().await;
Expand All @@ -366,16 +362,17 @@ async fn pause_crawler(app: AppHandle, _menu_id: String) {
let is_paused = !pause_state.load(Ordering::Relaxed);
pause_state.store(is_paused, Ordering::Relaxed);

let _new_label = if is_paused {
let new_label = if is_paused {
"▶️ Resume indexing"
} else {
"⏸ Pause indexing"
};

// if let Some(tray) = app.tray_by_id("main-tray"){
// let _ = item_handle.set_title(new_label);
// let _ = item_handle.set_enabled(true);
// }
// Update menu item label.
if let Some(state) = app.try_state::<MenuState>() {
let _ = state.pause_toggle.set_text(new_label);
let _ = state.pause_toggle.set_enabled(true);
}
}
Err(err) => log::warn!("Error sending RPC: {}", err),
}
Expand Down
38 changes: 24 additions & 14 deletions apps/tauri/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ use strum_macros::{Display, EnumString};
use tauri::{
menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu},
tray::{TrayIcon, TrayIconEvent},
AppHandle, Manager, PackageInfo,
AppHandle, Manager, PackageInfo, Wry,
};

use crate::{pause_crawler, platform::os_open, window};

#[derive(Clone)]
pub struct MenuState {
pub pause_toggle: MenuItem<Wry>,
}

#[derive(Display, Debug, EnumString)]
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
pub enum MenuID {
Expand Down Expand Up @@ -64,6 +69,18 @@ pub fn get_tray_menu(
],
)?;

let pause_status = MenuItem::with_id(
app,
MenuID::CRAWL_STATUS.to_string(),
"⏸ Pause indexing",
true,
None::<&str>,
)?;
// manage the pause status menu item so we can update it later.
app.manage(MenuState {
pause_toggle: pause_status.clone(),
});

tray.append_items(&[
&MenuItem::with_id(
app,
Expand All @@ -72,13 +89,7 @@ pub fn get_tray_menu(
true,
Some(user_settings.shortcut.clone()),
)?,
&MenuItem::with_id(
app,
MenuID::CRAWL_STATUS.to_string(),
"⏸ Pause indexing",
true,
None::<&str>,
)?,
&pause_status,
&PredefinedMenuItem::separator(app)?,
&MenuItem::with_id(
app,
Expand Down Expand Up @@ -219,12 +230,11 @@ pub fn handle_tray_menu_events(app: &AppHandle, event: MenuEvent) {
match menu_id {
MenuID::CRAWL_STATUS => {
// Don't block main thread when pausing the crawler.
tauri::async_runtime::spawn(pause_crawler(app.clone(), menu_id.to_string()));
// if let Some(tray) = app.tray_by_id("main-tray") {
// let _ = item_handle.set_title("Handling request...");
// let _ = item_handle.set_enabled(false);
// tauri::async_runtime::spawn(pause_crawler(app.clone(), menu_id.to_string()));
// }
tauri::async_runtime::spawn(pause_crawler(app.clone()));
if let Some(state) = app.try_state::<MenuState>() {
let _ = state.pause_toggle.set_text("Handling request...");
let _ = state.pause_toggle.set_enabled(false);
}
}
MenuID::DISCOVER => {
window::navigate_to_tab(app, &crate::constants::TabLocation::Discover);
Expand Down

0 comments on commit 95934b1

Please sign in to comment.