Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 112 additions & 2 deletions cardinal/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cardinal/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ fswalk.path = "../../fswalk"
fs-icon.path = "../../fs-icon"
search-cancel.path = "../../search-cancel"
camino = "1.2.1"
axum = "0.8.9"
tokio = { version = "1.52.1", features = ["rt-multi-thread", "macros"] }

[features]
default = []
Expand Down
74 changes: 70 additions & 4 deletions cardinal/src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use search_cache::{SearchOptions, SearchOutcome, SearchResultNode, SlabIndex, Sl
use search_cancel::CancellationToken;
use serde::{Deserialize, Serialize};
use std::{cell::LazyCell, process::Command};
use tauri::{ActivationPolicy, AppHandle, State};
use tauri::{ActivationPolicy, AppHandle, Emitter, Manager, State};
use tracing::{error, info, warn};

#[derive(Debug, Clone)]
Expand All @@ -47,6 +47,31 @@ impl From<SearchOptionsPayload> for SearchOptions {
}
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ServerConfig {
pub enabled: bool,
pub endpoint: String,
}

impl Default for ServerConfig {
fn default() -> Self {
Self {
enabled: false,
endpoint: "127.0.0.1:3388".to_string(),
}
}
}

/// Load `ServerConfig` from a JSON file previously written by `set_server_config`.
/// Returns `ServerConfig::default()` if the file is absent or malformed.
pub(crate) fn load_server_config_from_file(path: &std::path::Path) -> ServerConfig {
let Ok(contents) = std::fs::read_to_string(path) else {
return ServerConfig::default();
};
serde_json::from_str::<ServerConfig>(&contents).unwrap_or_default()
}

#[derive(Debug, Clone)]
pub struct SearchJob {
pub query: String,
Expand All @@ -68,22 +93,27 @@ struct SortedViewCache {
}

pub struct SearchState {
search_tx: Sender<SearchJob>,
node_info_tx: Sender<NodeInfoRequest>,
pub(crate) search_tx: Sender<SearchJob>,
pub(crate) node_info_tx: Sender<NodeInfoRequest>,
icon_viewport_tx: Sender<(u64, Vec<SlabIndex>)>,
rescan_tx: Sender<CancellationToken>,
watch_config_tx: Sender<WatchConfigUpdate>,
pub(crate) server_config: Mutex<ServerConfig>,
pub(crate) server_config_tx: Sender<ServerConfig>,
sorted_view_cache: Mutex<Option<SortedViewCache>>,
pub(crate) update_window_state_tx: Sender<()>,
}

impl SearchState {
#[allow(clippy::too_many_arguments)]
pub fn new(
search_tx: Sender<SearchJob>,
node_info_tx: Sender<NodeInfoRequest>,
icon_viewport_tx: Sender<(u64, Vec<SlabIndex>)>,
rescan_tx: Sender<CancellationToken>,
watch_config_tx: Sender<WatchConfigUpdate>,
server_config: ServerConfig,
server_config_tx: Sender<ServerConfig>,
update_window_state_tx: Sender<()>,
) -> Self {
Self {
Expand All @@ -92,6 +122,8 @@ impl SearchState {
icon_viewport_tx,
rescan_tx,
watch_config_tx,
server_config: Mutex::new(server_config),
server_config_tx,
sorted_view_cache: Mutex::new(None),
update_window_state_tx,
}
Expand Down Expand Up @@ -230,7 +262,7 @@ impl SearchResponse {
pub const CANCELLED: u8 = 1;
}

#[derive(Serialize)]
#[derive(Clone, Serialize)]
pub struct NodeInfoMetadata {
pub r#type: u8,
pub size: i64,
Expand Down Expand Up @@ -430,6 +462,40 @@ pub fn set_watch_config(
}
}

#[tauri::command]
pub fn get_server_config(state: State<'_, SearchState>) -> ServerConfig {
state.server_config.lock().clone()
}

#[tauri::command(async)]
pub fn set_server_config(app: AppHandle, config: ServerConfig, state: State<'_, SearchState>) {
{
let mut current = state.server_config.lock();
*current = config.clone();
}

// Persist so the server can start from the correct config on next launch.
if let Ok(config_dir) = app.path().app_config_dir() {
let path = config_dir.join("server_config.json");
match serde_json::to_string(&config) {
Ok(json) => {
if let Err(e) = std::fs::write(&path, json) {
error!("Failed to persist server config: {e:?}");
}
}
Err(e) => error!("Failed to serialize server config: {e:?}"),
}
}

if let Err(e) = state.server_config_tx.send(config.clone()) {
error!("Failed to request server config change: {e:?}");
}

if let Err(e) = app.emit("server-config-changed", config) {
error!("Failed to emit server-config-changed event: {e:?}");
}
}

#[tauri::command]
pub async fn open_in_finder(path: String) {
if let Err(e) = Command::new("open").arg("-R").arg(&path).spawn() {
Expand Down
Loading
Loading