mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-16 03:27:43 +01:00
refactor: avoid GLOBAL_TAURI_APP_HANLE if possible (#796)
This commit fixes the Windows panic issue. Coco panicked because it accessed `GLOBAL_TAURI_APP_HANDLE` when this global variable wasn't initialized. I removed all the uses of this variable except for the one use in `src-tauri/src/server/http_client.rs`, which I don't have a good way to refactor. If you are wondering why this didn't happen in the past, the access was triggered by the frontend code, something there likely changed. Regardless, this global variable is still dangerous and error-prone, so we should avoid it. Also, this commit fixes the issue that the panic hook does not work on Windows because the log filename contains ":", which is not allowed by the Windows file system.
This commit is contained in:
@@ -8,7 +8,7 @@ fn main() {
|
||||
}
|
||||
|
||||
// Notify `rustc` of this `cfg` attribute to suppress unknown attribute warnings.
|
||||
//
|
||||
//
|
||||
// unexpected condition name: `ci`
|
||||
println!("cargo::rustc-check-cfg=cfg(ci)");
|
||||
}
|
||||
|
||||
@@ -30,4 +30,4 @@ pub struct Session {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct SessionContext {
|
||||
pub attachments: Option<Vec<String>>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug,Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Connector {
|
||||
pub id: String,
|
||||
pub created: Option<String>,
|
||||
@@ -13,7 +13,7 @@ pub struct Connector {
|
||||
pub url: Option<String>,
|
||||
pub assets: Option<ConnectorAssets>,
|
||||
}
|
||||
#[derive(Debug,Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ConnectorAssets {
|
||||
pub icons: Option<std::collections::HashMap<String, String>>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,4 +18,4 @@ pub struct DataSource {
|
||||
pub struct ConnectorConfig {
|
||||
pub id: Option<String>,
|
||||
pub config: Option<serde_json::Value>, // Using serde_json::Value to handle any type of config
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Runtime;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RichLabel {
|
||||
@@ -62,23 +64,21 @@ impl OnOpened {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn open(on_opened: OnOpened) -> Result<(), String> {
|
||||
pub(crate) async fn open<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
on_opened: OnOpened,
|
||||
) -> Result<(), String> {
|
||||
log::debug!("open({})", on_opened.url());
|
||||
|
||||
use crate::util::open as homemade_tauri_shell_open;
|
||||
use crate::GLOBAL_TAURI_APP_HANDLE;
|
||||
use std::process::Command;
|
||||
|
||||
let global_tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
match on_opened {
|
||||
OnOpened::Application { app_path } => {
|
||||
homemade_tauri_shell_open(global_tauri_app_handle.clone(), app_path).await?
|
||||
homemade_tauri_shell_open(tauri_app_handle.clone(), app_path).await?
|
||||
}
|
||||
OnOpened::Document { url } => {
|
||||
homemade_tauri_shell_open(global_tauri_app_handle.clone(), url).await?
|
||||
homemade_tauri_shell_open(tauri_app_handle.clone(), url).await?
|
||||
}
|
||||
OnOpened::Command { action } => {
|
||||
let mut cmd = Command::new(action.exec);
|
||||
|
||||
@@ -15,7 +15,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ErrorCause {
|
||||
|
||||
@@ -38,7 +38,6 @@ pub async fn get_response_body_text(response: Response) -> Result<String, String
|
||||
return Err(fallback_error);
|
||||
}
|
||||
|
||||
|
||||
match serde_json::from_str::<common::error::ErrorResponse>(&body) {
|
||||
Ok(parsed_error) => {
|
||||
dbg!(&parsed_error);
|
||||
@@ -57,7 +56,6 @@ pub async fn get_response_body_text(response: Response) -> Result<String, String
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn convert_query_params_to_strings(
|
||||
query_params: Option<HashMap<String, JsonValue>>,
|
||||
) -> Option<Vec<String>> {
|
||||
@@ -68,13 +66,10 @@ pub fn convert_query_params_to_strings(
|
||||
JsonValue::Number(n) => Some(format!("{}={}", k, n)),
|
||||
JsonValue::Bool(b) => Some(format!("{}={}", k, b)),
|
||||
_ => {
|
||||
eprintln!(
|
||||
"Skipping unsupported query value for key '{}': {:?}",
|
||||
k, v
|
||||
);
|
||||
eprintln!("Skipping unsupported query value for key '{}': {:?}", k, v);
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,4 +13,4 @@ pub struct UserProfile {
|
||||
pub email: String,
|
||||
pub avatar: Option<String>,
|
||||
pub preferences: Option<Preferences>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,15 @@ use crate::common::error::SearchError;
|
||||
use crate::common::search::SearchQuery;
|
||||
use crate::common::search::{QueryResponse, QuerySource};
|
||||
use async_trait::async_trait;
|
||||
use tauri::AppHandle;
|
||||
|
||||
#[async_trait]
|
||||
pub trait SearchSource: Send + Sync {
|
||||
fn get_type(&self) -> QuerySource;
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError>;
|
||||
async fn search(
|
||||
&self,
|
||||
tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError>;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use super::super::Extension;
|
||||
use super::super::pizza_engine_runtime::RUNTIME_TX;
|
||||
use super::super::pizza_engine_runtime::SearchSourceState;
|
||||
use super::super::pizza_engine_runtime::Task;
|
||||
use super::super::pizza_engine_runtime::RUNTIME_TX;
|
||||
use super::super::Extension;
|
||||
use super::AppMetadata;
|
||||
use crate::GLOBAL_TAURI_APP_HANDLE;
|
||||
use crate::common::document::{DataSourceReference, Document, OnOpened};
|
||||
use crate::common::error::SearchError;
|
||||
use crate::common::search::{QueryResponse, QuerySource, SearchQuery};
|
||||
@@ -10,7 +11,6 @@ use crate::common::traits::SearchSource;
|
||||
use crate::extension::ExtensionType;
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::util::open;
|
||||
use crate::GLOBAL_TAURI_APP_HANDLE;
|
||||
use applications::{App, AppTrait};
|
||||
use async_trait::async_trait;
|
||||
use log::{error, warn};
|
||||
@@ -23,12 +23,12 @@ use pizza_engine::error::PizzaEngineError;
|
||||
use pizza_engine::search::{OriginalQuery, QueryContext, SearchResult, Searcher};
|
||||
use pizza_engine::store::{DiskStore, DiskStoreSnapshot};
|
||||
use pizza_engine::writer::Writer;
|
||||
use pizza_engine::{doc, Engine, EngineBuilder};
|
||||
use pizza_engine::{Engine, EngineBuilder, doc};
|
||||
use serde_json::Value as Json;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use tauri::{async_runtime, AppHandle, Manager, Runtime};
|
||||
use tauri_plugin_fs_pro::{icon, metadata, name, IconOptions};
|
||||
use tauri::{AppHandle, Manager, Runtime, async_runtime};
|
||||
use tauri_plugin_fs_pro::{IconOptions, icon, metadata, name};
|
||||
use tauri_plugin_global_shortcut::GlobalShortcutExt;
|
||||
use tauri_plugin_global_shortcut::Shortcut;
|
||||
use tauri_plugin_global_shortcut::ShortcutEvent;
|
||||
@@ -246,8 +246,8 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
|
||||
if !index_exists {
|
||||
let search_path = {
|
||||
let disabled_app_list_and_search_path_store = tauri_app_handle
|
||||
.store(TAURI_STORE_DISABLED_APP_LIST_AND_SEARCH_PATH)?;
|
||||
let disabled_app_list_and_search_path_store =
|
||||
tauri_app_handle.store(TAURI_STORE_DISABLED_APP_LIST_AND_SEARCH_PATH)?;
|
||||
let search_path_json = disabled_app_list_and_search_path_store
|
||||
.get(TAURI_STORE_KEY_SEARCH_PATH)
|
||||
.unwrap_or_else(|| {
|
||||
@@ -255,14 +255,14 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
});
|
||||
|
||||
let search_path: Vec<String> = match search_path_json {
|
||||
Json::Array(array) => array
|
||||
.into_iter()
|
||||
.map(|json| match json {
|
||||
Json::String(str) => str,
|
||||
_ => unreachable!("search path is stored in a string"),
|
||||
})
|
||||
.collect(),
|
||||
_ => unreachable!("search path is stored in an array"),
|
||||
Json::Array(array) => array
|
||||
.into_iter()
|
||||
.map(|json| match json {
|
||||
Json::String(str) => str,
|
||||
_ => unreachable!("search path is stored in a string"),
|
||||
})
|
||||
.collect(),
|
||||
_ => unreachable!("search path is stored in an array"),
|
||||
};
|
||||
|
||||
search_path
|
||||
@@ -294,8 +294,9 @@ async fn index_applications_if_not_indexed<R: Runtime>(
|
||||
// We don't error out because one failure won't break the whole thing
|
||||
if let Err(e) = writer.create_document(document).await {
|
||||
warn!(
|
||||
"failed to index application [app name: '{}', app path: '{}'] due to error [{}]", app_name, app_path, e
|
||||
)
|
||||
"failed to index application [app name: '{}', app path: '{}'] due to error [{}]",
|
||||
app_name, app_path, e
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,7 +402,9 @@ impl<R: Runtime> Task for SearchApplicationsTask<R> {
|
||||
|
||||
let rx_dropped_error = callback.send(Ok(empty_hits)).is_err();
|
||||
if rx_dropped_error {
|
||||
warn!("failed to send local app search result back because the corresponding channel receiver end has been unexpected dropped, which could happen due to a low query timeout")
|
||||
warn!(
|
||||
"failed to send local app search result back because the corresponding channel receiver end has been unexpected dropped, which could happen due to a low query timeout"
|
||||
)
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -422,7 +425,9 @@ impl<R: Runtime> Task for SearchApplicationsTask<R> {
|
||||
// It will be passed to Pizza like "Google\nChrome". Using Display impl would result
|
||||
// in an invalid query DSL and serde will complain.
|
||||
let dsl = format!(
|
||||
"{{ \"query\": {{ \"bool\": {{ \"should\": [ {{ \"match\": {{ \"{FIELD_APP_NAME}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME}\": {:?} }} }} ] }} }} }}", self.query_string, self.query_string);
|
||||
"{{ \"query\": {{ \"bool\": {{ \"should\": [ {{ \"match\": {{ \"{FIELD_APP_NAME}\": {:?} }} }}, {{ \"prefix\": {{ \"{FIELD_APP_NAME}\": {:?} }} }} ] }} }} }}",
|
||||
self.query_string, self.query_string
|
||||
);
|
||||
|
||||
let state = state
|
||||
.as_mut_any()
|
||||
@@ -453,7 +458,9 @@ impl<R: Runtime> Task for SearchApplicationsTask<R> {
|
||||
|
||||
let rx_dropped_error = callback.send(Ok(search_result)).is_err();
|
||||
if rx_dropped_error {
|
||||
warn!("failed to send local app search result back because the corresponding channel receiver end has been unexpected dropped, which could happen due to a low query timeout")
|
||||
warn!(
|
||||
"failed to send local app search result back because the corresponding channel receiver end has been unexpected dropped, which could happen due to a low query timeout"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -524,8 +531,8 @@ impl ApplicationSearchSource {
|
||||
.set(TAURI_STORE_KEY_DISABLED_APP_LIST, Json::Array(Vec::new()));
|
||||
}
|
||||
|
||||
// IndexAllApplicationsTask will read the apps installed in search paths and
|
||||
// index them, so it depends on this configuration entry. Init this entry
|
||||
// IndexAllApplicationsTask will read the apps installed in search paths and
|
||||
// index them, so it depends on this configuration entry. Init this entry
|
||||
// before indexing apps.
|
||||
if disabled_app_list_and_search_path_store
|
||||
.get(TAURI_STORE_KEY_SEARCH_PATH)
|
||||
@@ -536,7 +543,6 @@ impl ApplicationSearchSource {
|
||||
.set(TAURI_STORE_KEY_SEARCH_PATH, default_search_path);
|
||||
}
|
||||
|
||||
|
||||
let (tx, rx) = tokio::sync::oneshot::channel();
|
||||
let index_applications_task = IndexAllApplicationsTask {
|
||||
tauri_app_handle: app_handle.clone(),
|
||||
@@ -557,7 +563,6 @@ impl ApplicationSearchSource {
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -575,7 +580,11 @@ impl SearchSource for ApplicationSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
_tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
let query_string = query
|
||||
.query_strings
|
||||
.get("query")
|
||||
@@ -833,7 +842,9 @@ pub fn unregister_app_hotkey<R: Runtime>(
|
||||
.global_shortcut()
|
||||
.is_registered(hotkey.as_str())
|
||||
{
|
||||
panic!("inconsistent state, tauri store a hotkey is stored in the tauri store but it is not registered");
|
||||
panic!(
|
||||
"inconsistent state, tauri store a hotkey is stored in the tauri store but it is not registered"
|
||||
);
|
||||
}
|
||||
|
||||
tauri_app_handle
|
||||
|
||||
@@ -32,7 +32,11 @@ impl SearchSource for ApplicationSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, _query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
_tauri_app_handle: AppHandle,
|
||||
_query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
Ok(QueryResponse {
|
||||
source: self.get_type(),
|
||||
hits: Vec::new(),
|
||||
|
||||
@@ -10,6 +10,7 @@ use chinese_number::{ChineseCase, ChineseCountMethod, ChineseVariant, NumberToCh
|
||||
use num2words::Num2Words;
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use tauri::AppHandle;
|
||||
|
||||
pub(crate) const DATA_SOURCE_ID: &str = "Calculator";
|
||||
|
||||
@@ -120,7 +121,11 @@ impl SearchSource for CalculatorSource {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
_tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
let Some(query_string) = query.query_strings.get("query") else {
|
||||
return Ok(QueryResponse {
|
||||
source: self.get_type(),
|
||||
|
||||
@@ -4,6 +4,8 @@ use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use std::sync::LazyLock;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Runtime;
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
// Tauri store keys for file system configuration
|
||||
@@ -52,11 +54,7 @@ impl Default for FileSearchConfig {
|
||||
}
|
||||
|
||||
impl FileSearchConfig {
|
||||
pub(crate) fn get() -> Self {
|
||||
let tauri_app_handle = crate::GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
pub(crate) fn get<R: Runtime>(tauri_app_handle: &AppHandle<R>) -> Self {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_FILE_SYSTEM_CONFIG)
|
||||
.unwrap_or_else(|e| {
|
||||
@@ -187,16 +185,17 @@ impl FileSearchConfig {
|
||||
|
||||
// Tauri commands for managing file system configuration
|
||||
#[tauri::command]
|
||||
pub async fn get_file_system_config() -> FileSearchConfig {
|
||||
FileSearchConfig::get()
|
||||
pub async fn get_file_system_config<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
) -> FileSearchConfig {
|
||||
FileSearchConfig::get(&tauri_app_handle)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn set_file_system_config(config: FileSearchConfig) -> Result<(), String> {
|
||||
let tauri_app_handle = crate::GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
pub async fn set_file_system_config<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
config: FileSearchConfig,
|
||||
) -> Result<(), String> {
|
||||
let store = tauri_app_handle
|
||||
.store(TAURI_STORE_FILE_SYSTEM_CONFIG)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use super::super::EXTENSION_ID;
|
||||
use super::super::config::FileSearchConfig;
|
||||
use super::super::config::SearchBy;
|
||||
use super::super::EXTENSION_ID;
|
||||
use crate::common::{
|
||||
document::{DataSourceReference, Document},
|
||||
};
|
||||
use crate::extension::OnOpened;
|
||||
use crate::common::document::{DataSourceReference, Document};
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::extension::OnOpened;
|
||||
use crate::util::file::get_file_icon;
|
||||
use futures::stream::Stream;
|
||||
use futures::stream::StreamExt;
|
||||
@@ -32,8 +30,7 @@ pub(crate) async fn hits(
|
||||
// Convert results to documents
|
||||
let mut hits: Vec<(Document, f64)> = Vec::new();
|
||||
while let Some(res_file_path) = iter.next().await {
|
||||
let file_path =
|
||||
res_file_path.map_err(|io_err| io_err.to_string())?;
|
||||
let file_path = res_file_path.map_err(|io_err| io_err.to_string())?;
|
||||
|
||||
let icon = get_file_icon(file_path.clone()).await;
|
||||
let file_path_of_type_path = camino::Utf8Path::new(&file_path);
|
||||
@@ -75,7 +72,7 @@ pub(crate) async fn hits(
|
||||
|
||||
hits.push((doc, SCORE));
|
||||
}
|
||||
// Kill the mdfind process once we get the needed results to prevent zombie
|
||||
// Kill the mdfind process once we get the needed results to prevent zombie
|
||||
// processes.
|
||||
mdfind_child_process
|
||||
.kill()
|
||||
|
||||
@@ -7,4 +7,4 @@ mod windows;
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) use macos::hits;
|
||||
#[cfg(target_os = "windows")]
|
||||
pub(crate) use windows::hits;
|
||||
pub(crate) use windows::hits;
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
//! # Credits
|
||||
//!
|
||||
//!
|
||||
//! https://github.com/IRONAGE-Park/rag-sample/blob/3f0ad8c8012026cd3a7e453d08f041609426cb91/src/native/windows.rs
|
||||
//! is the starting point of this implementation.
|
||||
|
||||
use super::super::EXTENSION_ID;
|
||||
use super::super::config::FileSearchConfig;
|
||||
use super::super::config::SearchBy;
|
||||
use super::super::EXTENSION_ID;
|
||||
use crate::common::document::{DataSourceReference, Document};
|
||||
use crate::extension::OnOpened;
|
||||
use crate::extension::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use crate::extension::OnOpened;
|
||||
use crate::util::file::get_file_icon;
|
||||
use windows::{
|
||||
core::{w, IUnknown, Interface, GUID, PWSTR},
|
||||
Win32::System::{
|
||||
Com::{CoCreateInstance, CLSCTX_INPROC_SERVER},
|
||||
Com::{CLSCTX_INPROC_SERVER, CoCreateInstance},
|
||||
Ole::{OleInitialize, OleUninitialize},
|
||||
Search::{
|
||||
IAccessor, ICommand, ICommandText, IDBCreateCommand, IDBCreateSession, IDBInitialize,
|
||||
IDataInitialize, IRowset, DBACCESSOR_ROWDATA, DBBINDING, DBMEMOWNER_CLIENTOWNED,
|
||||
DBPARAMIO_NOTPARAM, DBPART_VALUE, DBTYPE_WSTR, DB_NULL_HCHAPTER, HACCESSOR,
|
||||
MSDAINITIALIZE,
|
||||
DB_NULL_HCHAPTER, DBACCESSOR_ROWDATA, DBBINDING, DBMEMOWNER_CLIENTOWNED,
|
||||
DBPARAMIO_NOTPARAM, DBPART_VALUE, DBTYPE_WSTR, HACCESSOR, IAccessor, ICommand,
|
||||
ICommandText, IDBCreateCommand, IDBCreateSession, IDBInitialize, IDataInitialize,
|
||||
IRowset, MSDAINITIALIZE,
|
||||
},
|
||||
},
|
||||
core::{GUID, IUnknown, Interface, PWSTR, w},
|
||||
};
|
||||
|
||||
/// Owned version of `PWSTR` that holds the heap memory.
|
||||
@@ -389,7 +389,7 @@ pub(crate) async fn hits(
|
||||
let result = execute_windows_search_sql(&sql)?;
|
||||
unsafe { OleUninitialize() };
|
||||
// .take(size) is not needed as `result` will contain `from+size` files at most
|
||||
let result_with_paging = result.into_iter().skip(from);
|
||||
let result_with_paging = result.into_iter().skip(from);
|
||||
// result_with_paging won't contain more than `size` entries
|
||||
let mut hits = Vec::with_capacity(size);
|
||||
|
||||
@@ -449,9 +449,9 @@ pub(crate) async fn hits(
|
||||
Ok(hits)
|
||||
}
|
||||
|
||||
// Skip these tests in our CI, they fail with the following error
|
||||
// Skip these tests in our CI, they fail with the following error
|
||||
// "SQL is invalid: "0x80041820""
|
||||
//
|
||||
//
|
||||
// I have no idea about the underlying root cause
|
||||
#[cfg(all(test, not(ci)))]
|
||||
mod test {
|
||||
@@ -491,7 +491,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("coco", 0, 10, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE ((System.FileName LIKE '%coco%') OR CONTAINS('coco'))");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE ((System.FileName LIKE '%coco%') OR CONTAINS('coco'))"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -505,7 +508,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("coco", 0, 10, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%coco%') AND (SCOPE = 'file:C:/Users/')");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%coco%') AND (SCOPE = 'file:C:/Users/')"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -523,7 +529,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("test", 0, 5, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 5 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%test%') AND (SCOPE = 'file:C:/Users/' OR SCOPE = 'file:D:/Projects/' OR SCOPE = 'file:E:/Documents/')");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 5 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%test%') AND (SCOPE = 'file:C:/Users/' OR SCOPE = 'file:D:/Projects/' OR SCOPE = 'file:E:/Documents/')"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -537,7 +546,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("file", 0, 20, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 20 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%file%') AND ((NOT SCOPE = 'file:C:/Windows/'))");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 20 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%file%') AND ((NOT SCOPE = 'file:C:/Windows/'))"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -551,7 +563,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("data", 5, 15, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 20 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%data%') AND ((NOT SCOPE = 'file:C:/Windows/') AND (NOT SCOPE = 'file:C:/System/') AND (NOT SCOPE = 'file:C:/Temp/'))");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 20 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%data%') AND ((NOT SCOPE = 'file:C:/Windows/') AND (NOT SCOPE = 'file:C:/System/') AND (NOT SCOPE = 'file:C:/Temp/'))"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -565,7 +580,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("readme", 0, 10, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%readme%') AND (System.FileExtension = '.txt')");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%readme%') AND (System.FileExtension = '.txt')"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -579,7 +597,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("config", 0, 50, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 50 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%config%') AND (System.FileExtension = '.rs' OR System.FileExtension = '.toml' OR System.FileExtension = '.md' OR System.FileExtension = '.json')");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 50 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%config%') AND (System.FileExtension = '.rs' OR System.FileExtension = '.toml' OR System.FileExtension = '.md' OR System.FileExtension = '.json')"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -593,7 +614,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("main", 10, 25, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 35 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%main%') AND (SCOPE = 'file:C:/Projects/' OR SCOPE = 'file:D:/Code/') AND ((NOT SCOPE = 'file:C:/Projects/temp/')) AND (System.FileExtension = '.rs' OR System.FileExtension = '.ts')");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 35 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%main%') AND (SCOPE = 'file:C:/Projects/' OR SCOPE = 'file:D:/Code/') AND ((NOT SCOPE = 'file:C:/Projects/temp/')) AND (System.FileExtension = '.rs' OR System.FileExtension = '.ts')"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
@@ -607,7 +631,10 @@ mod test {
|
||||
};
|
||||
let sql = query_sql("hello-world", 0, 10, &config);
|
||||
|
||||
assert_eq!(sql, "SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%hello-world%') AND (SCOPE = 'file:C:/Users/John Doe/') AND (System.FileExtension = '.c++')");
|
||||
assert_eq!(
|
||||
sql,
|
||||
"SELECT TOP 10 System.ItemUrl, System.Search.Rank FROM SystemIndex WHERE (System.FileName LIKE '%hello-world%') AND (SCOPE = 'file:C:/Users/John Doe/') AND (System.FileExtension = '.c++')"
|
||||
);
|
||||
ensure_it_is_valid_sql(&sql);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::common::{
|
||||
use async_trait::async_trait;
|
||||
use config::FileSearchConfig;
|
||||
use hostname;
|
||||
use tauri::AppHandle;
|
||||
|
||||
pub(crate) const EXTENSION_ID: &str = "File Search";
|
||||
|
||||
@@ -40,7 +41,11 @@ impl SearchSource for FileSearchExtensionSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
let Some(query_string) = query.query_strings.get("query") else {
|
||||
return Ok(QueryResponse {
|
||||
source: self.get_type(),
|
||||
@@ -61,7 +66,7 @@ impl SearchSource for FileSearchExtensionSearchSource {
|
||||
}
|
||||
|
||||
// Get configuration from tauri store
|
||||
let config = FileSearchConfig::get();
|
||||
let config = FileSearchConfig::get(&tauri_app_handle);
|
||||
|
||||
// If search paths are empty, then the hit should be empty.
|
||||
//
|
||||
@@ -78,7 +83,9 @@ impl SearchSource for FileSearchExtensionSearchSource {
|
||||
// Execute search in a blocking task
|
||||
let query_source = self.get_type();
|
||||
|
||||
let hits = implementation::hits(&query_string, from, size, &config).await.map_err(SearchError::InternalError)?;
|
||||
let hits = implementation::hits(&query_string, from, size, &config)
|
||||
.await
|
||||
.map_err(SearchError::InternalError)?;
|
||||
|
||||
let total_hits = hits.len();
|
||||
Ok(QueryResponse {
|
||||
|
||||
@@ -9,29 +9,25 @@ pub mod pizza_engine_runtime;
|
||||
pub mod quick_ai_access;
|
||||
|
||||
use super::Extension;
|
||||
use crate::SearchSourceRegistry;
|
||||
use crate::extension::built_in::application::{set_apps_hotkey, unset_apps_hotkey};
|
||||
use crate::extension::{
|
||||
alter_extension_json_file, ExtensionBundleIdBorrowed, PLUGIN_JSON_FILE_NAME,
|
||||
ExtensionBundleIdBorrowed, PLUGIN_JSON_FILE_NAME, alter_extension_json_file,
|
||||
};
|
||||
use crate::{SearchSourceRegistry, GLOBAL_TAURI_APP_HANDLE};
|
||||
use anyhow::Context;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::LazyLock;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
|
||||
pub(crate) static BUILT_IN_EXTENSION_DIRECTORY: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
let mut resource_dir = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set")
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect(
|
||||
"User home directory not found, which should be impossible on desktop environments",
|
||||
);
|
||||
pub(crate) fn get_built_in_extension_directory<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
) -> PathBuf {
|
||||
let mut resource_dir = tauri_app_handle.path().app_data_dir().expect(
|
||||
"User home directory not found, which should be impossible on desktop environments",
|
||||
);
|
||||
resource_dir.push("built_in_extensions");
|
||||
|
||||
resource_dir
|
||||
});
|
||||
}
|
||||
|
||||
/// Helper function to load the built-in extension specified by `extension_id`, used
|
||||
/// in `list_built_in_extensions()`.
|
||||
@@ -86,7 +82,10 @@ async fn load_built_in_extension(
|
||||
.map_err(|e| e.to_string())?;
|
||||
let res_plugin_json = serde_json::from_str::<Extension>(&plugin_json_file_content);
|
||||
let Ok(plugin_json) = res_plugin_json else {
|
||||
log::warn!("user invalidated built-in extension [{}] file, overwriting it with the default template", extension_id);
|
||||
log::warn!(
|
||||
"user invalidated built-in extension [{}] file, overwriting it with the default template",
|
||||
extension_id
|
||||
);
|
||||
|
||||
// If the JSON file cannot be parsed as `struct Extension`, overwrite it with the default template and return.
|
||||
tokio::fs::write(plugin_json_file_path, default_plugin_json_file)
|
||||
@@ -137,13 +136,15 @@ async fn load_built_in_extension(
|
||||
/// We only read alias/hotkey/enabled from the JSON file, we have ensured that if
|
||||
/// alias/hotkey is not supported, then it will be `None`. Besides that, no further
|
||||
/// validation is needed because nothing could go wrong.
|
||||
pub(crate) async fn list_built_in_extensions() -> Result<Vec<Extension>, String> {
|
||||
let dir = BUILT_IN_EXTENSION_DIRECTORY.as_path();
|
||||
pub(crate) async fn list_built_in_extensions<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
) -> Result<Vec<Extension>, String> {
|
||||
let dir = get_built_in_extension_directory(tauri_app_handle);
|
||||
|
||||
let mut built_in_extensions = Vec::new();
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
dir,
|
||||
&dir,
|
||||
application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME,
|
||||
application::PLUGIN_JSON_FILE,
|
||||
)
|
||||
@@ -151,7 +152,7 @@ pub(crate) async fn list_built_in_extensions() -> Result<Vec<Extension>, String>
|
||||
);
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
dir,
|
||||
&dir,
|
||||
calculator::DATA_SOURCE_ID,
|
||||
calculator::PLUGIN_JSON_FILE,
|
||||
)
|
||||
@@ -159,7 +160,7 @@ pub(crate) async fn list_built_in_extensions() -> Result<Vec<Extension>, String>
|
||||
);
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
dir,
|
||||
&dir,
|
||||
ai_overview::EXTENSION_ID,
|
||||
ai_overview::PLUGIN_JSON_FILE,
|
||||
)
|
||||
@@ -167,7 +168,7 @@ pub(crate) async fn list_built_in_extensions() -> Result<Vec<Extension>, String>
|
||||
);
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
dir,
|
||||
&dir,
|
||||
quick_ai_access::EXTENSION_ID,
|
||||
quick_ai_access::PLUGIN_JSON_FILE,
|
||||
)
|
||||
@@ -178,7 +179,7 @@ pub(crate) async fn list_built_in_extensions() -> Result<Vec<Extension>, String>
|
||||
if #[cfg(any(target_os = "macos", target_os = "windows"))] {
|
||||
built_in_extensions.push(
|
||||
load_built_in_extension(
|
||||
dir,
|
||||
&dir,
|
||||
file_search::EXTENSION_ID,
|
||||
file_search::PLUGIN_JSON_FILE,
|
||||
)
|
||||
@@ -232,12 +233,10 @@ pub(crate) fn is_extension_built_in(bundle_id: &ExtensionBundleIdBorrowed<'_>) -
|
||||
bundle_id.developer.is_none()
|
||||
}
|
||||
|
||||
pub(crate) async fn enable_built_in_extension(
|
||||
pub(crate) async fn enable_built_in_extension<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
|
||||
let update_extension = |extension: &mut Extension| -> Result<(), String> {
|
||||
@@ -254,7 +253,7 @@ pub(crate) async fn enable_built_in_extension(
|
||||
set_apps_hotkey(tauri_app_handle)?;
|
||||
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -277,7 +276,7 @@ pub(crate) async fn enable_built_in_extension(
|
||||
.register_source(calculator_search)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -286,7 +285,7 @@ pub(crate) async fn enable_built_in_extension(
|
||||
|
||||
if bundle_id.extension_id == quick_ai_access::EXTENSION_ID {
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -295,7 +294,7 @@ pub(crate) async fn enable_built_in_extension(
|
||||
|
||||
if bundle_id.extension_id == ai_overview::EXTENSION_ID {
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -310,7 +309,7 @@ pub(crate) async fn enable_built_in_extension(
|
||||
.register_source(file_system_search)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -322,12 +321,10 @@ pub(crate) async fn enable_built_in_extension(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn disable_built_in_extension(
|
||||
pub(crate) async fn disable_built_in_extension<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
|
||||
let update_extension = |extension: &mut Extension| -> Result<(), String> {
|
||||
@@ -344,7 +341,7 @@ pub(crate) async fn disable_built_in_extension(
|
||||
unset_apps_hotkey(tauri_app_handle)?;
|
||||
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -365,7 +362,7 @@ pub(crate) async fn disable_built_in_extension(
|
||||
.remove_source(bundle_id.extension_id)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -374,7 +371,7 @@ pub(crate) async fn disable_built_in_extension(
|
||||
|
||||
if bundle_id.extension_id == quick_ai_access::EXTENSION_ID {
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -384,7 +381,7 @@ pub(crate) async fn disable_built_in_extension(
|
||||
|
||||
if bundle_id.extension_id == ai_overview::EXTENSION_ID {
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -399,7 +396,7 @@ pub(crate) async fn disable_built_in_extension(
|
||||
.remove_source(bundle_id.extension_id)
|
||||
.await;
|
||||
alter_extension_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -411,11 +408,11 @@ pub(crate) async fn disable_built_in_extension(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_built_in_extension_alias(bundle_id: &ExtensionBundleIdBorrowed<'_>, alias: &str) {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
pub(crate) fn set_built_in_extension_alias<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
alias: &str,
|
||||
) {
|
||||
if bundle_id.extension_id == application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME {
|
||||
if let Some(app_path) = bundle_id.sub_extension_id {
|
||||
application::set_app_alias(tauri_app_handle, app_path, alias);
|
||||
@@ -423,14 +420,11 @@ pub(crate) fn set_built_in_extension_alias(bundle_id: &ExtensionBundleIdBorrowed
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn register_built_in_extension_hotkey(
|
||||
pub(crate) fn register_built_in_extension_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
if bundle_id.extension_id == application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME {
|
||||
if let Some(app_path) = bundle_id.sub_extension_id {
|
||||
application::register_app_hotkey(&tauri_app_handle, app_path, hotkey)?;
|
||||
@@ -439,13 +433,10 @@ pub(crate) fn register_built_in_extension_hotkey(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn unregister_built_in_extension_hotkey(
|
||||
pub(crate) fn unregister_built_in_extension_hotkey<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
if bundle_id.extension_id == application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME {
|
||||
if let Some(app_path) = bundle_id.sub_extension_id {
|
||||
application::unregister_app_hotkey(&tauri_app_handle, app_path)?;
|
||||
@@ -490,12 +481,10 @@ fn load_extension_from_json_file(
|
||||
Ok(extension)
|
||||
}
|
||||
|
||||
pub(crate) async fn is_built_in_extension_enabled(
|
||||
pub(crate) async fn is_built_in_extension_enabled<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<bool, String> {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
|
||||
if bundle_id.extension_id == application::QUERYSOURCE_ID_DATASOURCE_ID_DATASOURCE_NAME
|
||||
@@ -523,7 +512,7 @@ pub(crate) async fn is_built_in_extension_enabled(
|
||||
|
||||
if bundle_id.extension_id == quick_ai_access::EXTENSION_ID {
|
||||
let extension = load_extension_from_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id.extension_id,
|
||||
)?;
|
||||
return Ok(extension.enabled);
|
||||
@@ -531,7 +520,7 @@ pub(crate) async fn is_built_in_extension_enabled(
|
||||
|
||||
if bundle_id.extension_id == ai_overview::EXTENSION_ID {
|
||||
let extension = load_extension_from_json_file(
|
||||
&BUILT_IN_EXTENSION_DIRECTORY.as_path(),
|
||||
&get_built_in_extension_directory(tauri_app_handle),
|
||||
bundle_id.extension_id,
|
||||
)?;
|
||||
return Ok(extension.enabled);
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
//! which forces us to create a dedicated thread/runtime to execute them.
|
||||
|
||||
use std::any::Any;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub(crate) trait SearchSourceState {
|
||||
|
||||
@@ -2,7 +2,8 @@ pub(crate) mod built_in;
|
||||
pub(crate) mod third_party;
|
||||
|
||||
use crate::common::document::OnOpened;
|
||||
use crate::{common::register::SearchSourceRegistry, GLOBAL_TAURI_APP_HANDLE};
|
||||
use crate::common::register::SearchSourceRegistry;
|
||||
use crate::util::platform::Platform;
|
||||
use anyhow::Context;
|
||||
use borrowme::{Borrow, ToOwned};
|
||||
use derive_more::Display;
|
||||
@@ -11,9 +12,8 @@ use serde::Serialize;
|
||||
use serde_json::Value as Json;
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use tauri::Manager;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE;
|
||||
use crate::util::platform::Platform;
|
||||
|
||||
pub const LOCAL_QUERY_SOURCE_TYPE: &str = "local";
|
||||
const PLUGIN_JSON_FILE_NAME: &str = "plugin.json";
|
||||
@@ -45,8 +45,8 @@ pub struct Extension {
|
||||
platforms: Option<HashSet<Platform>>,
|
||||
/// Extension description.
|
||||
description: String,
|
||||
//// Specify the icon for this extension,
|
||||
///
|
||||
//// Specify the icon for this extension,
|
||||
///
|
||||
/// For the `plugin.json` file, this field can be specified in multi options:
|
||||
///
|
||||
/// 1. It can be a path to the icon file, the path can be
|
||||
@@ -59,9 +59,9 @@ pub struct Extension {
|
||||
/// In cases where your icon file is named similarly to a font class code, Coco
|
||||
/// will treat it as an icon file if it exists, i.e., if file `<extension>/assets/font_coco`
|
||||
/// exists, then Coco will use this file rather than the built-in 'font_coco' icon.
|
||||
///
|
||||
///
|
||||
/// For the `struct Extension` loaded into memory, this field should be:
|
||||
///
|
||||
///
|
||||
/// 1. An absolute path
|
||||
/// 2. A font code
|
||||
icon: String,
|
||||
@@ -413,23 +413,24 @@ fn filter_out_extensions(
|
||||
/// * boolean: indicates if we found any invalid extensions
|
||||
/// * Vec<Extension>: loaded extensions
|
||||
#[tauri::command]
|
||||
pub(crate) async fn list_extensions(
|
||||
pub(crate) async fn list_extensions<R: Runtime>(
|
||||
tauri_app_handle: AppHandle<R>,
|
||||
query: Option<String>,
|
||||
extension_type: Option<ExtensionType>,
|
||||
list_enabled: bool,
|
||||
) -> Result<(bool, Vec<Extension>), String> {
|
||||
log::trace!("loading extensions");
|
||||
|
||||
let third_party_dir = third_party::THIRD_PARTY_EXTENSIONS_DIRECTORY.as_path();
|
||||
let third_party_dir = third_party::get_third_party_extension_directory(&tauri_app_handle);
|
||||
if !third_party_dir.try_exists().map_err(|e| e.to_string())? {
|
||||
tokio::fs::create_dir_all(third_party_dir)
|
||||
tokio::fs::create_dir_all(&third_party_dir)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
let (third_party_found_invalid_extension, mut third_party_extensions) =
|
||||
third_party::list_third_party_extensions(third_party_dir).await?;
|
||||
third_party::list_third_party_extensions(&third_party_dir).await?;
|
||||
|
||||
let built_in_extensions = built_in::list_built_in_extensions().await?;
|
||||
let built_in_extensions = built_in::list_built_in_extensions(&tauri_app_handle).await?;
|
||||
|
||||
let found_invalid_extension = third_party_found_invalid_extension;
|
||||
let mut extensions = {
|
||||
@@ -482,12 +483,12 @@ pub(crate) async fn list_extensions(
|
||||
Ok((found_invalid_extension, extensions))
|
||||
}
|
||||
|
||||
pub(crate) async fn init_extensions(mut extensions: Vec<Extension>) -> Result<(), String> {
|
||||
pub(crate) async fn init_extensions(
|
||||
tauri_app_handle: AppHandle,
|
||||
mut extensions: Vec<Extension>,
|
||||
) -> Result<(), String> {
|
||||
log::trace!("initializing extensions");
|
||||
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
let search_source_registry_tauri_state = tauri_app_handle.state::<SearchSourceRegistry>();
|
||||
|
||||
built_in::application::ApplicationSearchSource::prepare_index_and_store(
|
||||
@@ -508,7 +509,7 @@ pub(crate) async fn init_extensions(mut extensions: Vec<Extension>) -> Result<()
|
||||
.filter(|ext| ext.enabled)
|
||||
{
|
||||
built_in::init_built_in_extension(
|
||||
tauri_app_handle,
|
||||
&tauri_app_handle,
|
||||
&built_in_extension,
|
||||
&search_source_registry_tauri_state,
|
||||
)
|
||||
@@ -517,7 +518,7 @@ pub(crate) async fn init_extensions(mut extensions: Vec<Extension>) -> Result<()
|
||||
|
||||
// Now the third-party extensions
|
||||
let third_party_search_source = third_party::ThirdPartyExtensionsSearchSource::new(extensions);
|
||||
third_party_search_source.init().await?;
|
||||
third_party_search_source.init(&tauri_app_handle).await?;
|
||||
let third_party_search_source_clone = third_party_search_source.clone();
|
||||
// Set the global search source so that we can access it in `#[tauri::command]`s
|
||||
// ignore the result because this function will be invoked twice, which
|
||||
@@ -531,79 +532,96 @@ pub(crate) async fn init_extensions(mut extensions: Vec<Extension>) -> Result<()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn enable_extension(bundle_id: ExtensionBundleId) -> Result<(), String> {
|
||||
pub(crate) async fn enable_extension(
|
||||
tauri_app_handle: AppHandle,
|
||||
bundle_id: ExtensionBundleId,
|
||||
) -> Result<(), String> {
|
||||
let bundle_id_borrowed = bundle_id.borrow();
|
||||
|
||||
if built_in::is_extension_built_in(&bundle_id_borrowed) {
|
||||
built_in::enable_built_in_extension(&bundle_id_borrowed).await?;
|
||||
built_in::enable_built_in_extension(&tauri_app_handle, &bundle_id_borrowed).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").enable_extension(&bundle_id_borrowed).await
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").enable_extension(&tauri_app_handle, &bundle_id_borrowed).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn disable_extension(bundle_id: ExtensionBundleId) -> Result<(), String> {
|
||||
pub(crate) async fn disable_extension(
|
||||
tauri_app_handle: AppHandle,
|
||||
bundle_id: ExtensionBundleId,
|
||||
) -> Result<(), String> {
|
||||
let bundle_id_borrowed = bundle_id.borrow();
|
||||
|
||||
if built_in::is_extension_built_in(&bundle_id_borrowed) {
|
||||
built_in::disable_built_in_extension(&bundle_id_borrowed).await?;
|
||||
built_in::disable_built_in_extension(&tauri_app_handle, &bundle_id_borrowed).await?;
|
||||
return Ok(());
|
||||
}
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").disable_extension(&bundle_id_borrowed).await
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").disable_extension(&tauri_app_handle, &bundle_id_borrowed).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn set_extension_alias(
|
||||
tauri_app_handle: AppHandle,
|
||||
bundle_id: ExtensionBundleId,
|
||||
alias: String,
|
||||
) -> Result<(), String> {
|
||||
let bundle_id_borrowed = bundle_id.borrow();
|
||||
|
||||
if built_in::is_extension_built_in(&bundle_id_borrowed) {
|
||||
built_in::set_built_in_extension_alias(&bundle_id_borrowed, &alias);
|
||||
built_in::set_built_in_extension_alias(&tauri_app_handle, &bundle_id_borrowed, &alias);
|
||||
return Ok(());
|
||||
}
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").set_extension_alias(&bundle_id_borrowed, &alias).await
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").set_extension_alias(&tauri_app_handle, &bundle_id_borrowed, &alias).await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn register_extension_hotkey(
|
||||
tauri_app_handle: AppHandle,
|
||||
bundle_id: ExtensionBundleId,
|
||||
hotkey: String,
|
||||
) -> Result<(), String> {
|
||||
let bundle_id_borrowed = bundle_id.borrow();
|
||||
|
||||
if built_in::is_extension_built_in(&bundle_id_borrowed) {
|
||||
built_in::register_built_in_extension_hotkey(&bundle_id_borrowed, &hotkey)?;
|
||||
built_in::register_built_in_extension_hotkey(
|
||||
&tauri_app_handle,
|
||||
&bundle_id_borrowed,
|
||||
&hotkey,
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").register_extension_hotkey(&bundle_id_borrowed, &hotkey).await
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").register_extension_hotkey(&tauri_app_handle, &bundle_id_borrowed, &hotkey).await
|
||||
}
|
||||
|
||||
/// NOTE: this function won't error out if the extension specified by `extension_id`
|
||||
/// has no hotkey set because we need it to behave like this.
|
||||
#[tauri::command]
|
||||
pub(crate) async fn unregister_extension_hotkey(
|
||||
tauri_app_handle: AppHandle,
|
||||
bundle_id: ExtensionBundleId,
|
||||
) -> Result<(), String> {
|
||||
let bundle_id_borrowed = bundle_id.borrow();
|
||||
|
||||
if built_in::is_extension_built_in(&bundle_id_borrowed) {
|
||||
built_in::unregister_built_in_extension_hotkey(&bundle_id_borrowed)?;
|
||||
built_in::unregister_built_in_extension_hotkey(&tauri_app_handle, &bundle_id_borrowed)?;
|
||||
return Ok(());
|
||||
}
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").unregister_extension_hotkey(&bundle_id_borrowed).await?;
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").unregister_extension_hotkey(&tauri_app_handle, &bundle_id_borrowed).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn is_extension_enabled(bundle_id: ExtensionBundleId) -> Result<bool, String> {
|
||||
pub(crate) async fn is_extension_enabled(
|
||||
tauri_app_handle: AppHandle,
|
||||
bundle_id: ExtensionBundleId,
|
||||
) -> Result<bool, String> {
|
||||
let bundle_id_borrowed = bundle_id.borrow();
|
||||
|
||||
if built_in::is_extension_built_in(&bundle_id_borrowed) {
|
||||
return built_in::is_built_in_extension_enabled(&bundle_id_borrowed).await;
|
||||
return built_in::is_built_in_extension_enabled(&tauri_app_handle, &bundle_id_borrowed)
|
||||
.await;
|
||||
}
|
||||
third_party::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE.get().expect("global third party search source not set, looks like init_extensions() has not been executed").is_extension_enabled(&bundle_id_borrowed).await
|
||||
}
|
||||
@@ -704,10 +722,7 @@ fn alter_extension_json_file(
|
||||
|
||||
// Search in quicklinks
|
||||
if let Some(ref mut quicklinks) = root_extension.quicklinks {
|
||||
if let Some(link) = quicklinks
|
||||
.iter_mut()
|
||||
.find(|lnk| lnk.id == sub_extension_id)
|
||||
{
|
||||
if let Some(link) = quicklinks.iter_mut().find(|lnk| lnk.id == sub_extension_id) {
|
||||
how(link)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
127
src-tauri/src/extension/third_party/mod.rs
vendored
127
src-tauri/src/extension/third_party/mod.rs
vendored
@@ -1,14 +1,14 @@
|
||||
pub(crate) mod store;
|
||||
|
||||
use super::alter_extension_json_file;
|
||||
use super::canonicalize_relative_icon_path;
|
||||
use super::Extension;
|
||||
use super::ExtensionType;
|
||||
use super::LOCAL_QUERY_SOURCE_TYPE;
|
||||
use super::PLUGIN_JSON_FILE_NAME;
|
||||
use crate::common::document::open;
|
||||
use super::alter_extension_json_file;
|
||||
use super::canonicalize_relative_icon_path;
|
||||
use crate::common::document::DataSourceReference;
|
||||
use crate::common::document::Document;
|
||||
use crate::common::document::open;
|
||||
use crate::common::error::SearchError;
|
||||
use crate::common::search::QueryResponse;
|
||||
use crate::common::search::QuerySource;
|
||||
@@ -16,7 +16,6 @@ use crate::common::search::SearchQuery;
|
||||
use crate::common::traits::SearchSource;
|
||||
use crate::extension::ExtensionBundleIdBorrowed;
|
||||
use crate::util::platform::Platform;
|
||||
use crate::GLOBAL_TAURI_APP_HANDLE;
|
||||
use async_trait::async_trait;
|
||||
use borrowme::ToOwned;
|
||||
use function_name::named;
|
||||
@@ -24,29 +23,27 @@ use std::ffi::OsStr;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use std::sync::LazyLock;
|
||||
use std::sync::OnceLock;
|
||||
use tauri::async_runtime;
|
||||
use tauri::AppHandle;
|
||||
use tauri::Manager;
|
||||
use tauri::Runtime;
|
||||
use tauri::async_runtime;
|
||||
use tauri_plugin_global_shortcut::GlobalShortcutExt;
|
||||
use tauri_plugin_global_shortcut::ShortcutState;
|
||||
use tokio::fs::read_dir;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio::sync::RwLockWriteGuard;
|
||||
|
||||
pub(crate) static THIRD_PARTY_EXTENSIONS_DIRECTORY: LazyLock<PathBuf> = LazyLock::new(|| {
|
||||
let mut app_data_dir = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set")
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect(
|
||||
"User home directory not found, which should be impossible on desktop environments",
|
||||
);
|
||||
pub(crate) fn get_third_party_extension_directory<R: Runtime>(
|
||||
tauri_app_handle: &AppHandle<R>,
|
||||
) -> PathBuf {
|
||||
let mut app_data_dir = tauri_app_handle.path().app_data_dir().expect(
|
||||
"User home directory not found, which should be impossible on desktop environments",
|
||||
);
|
||||
app_data_dir.push("third_party_extensions");
|
||||
|
||||
app_data_dir
|
||||
});
|
||||
}
|
||||
|
||||
pub(crate) async fn list_third_party_extensions(
|
||||
directory: &Path,
|
||||
@@ -201,7 +198,15 @@ fn validate_extension(
|
||||
// Extension is incompatible
|
||||
if let Some(ref platforms) = extension.platforms {
|
||||
if !platforms.contains(¤t_platform) {
|
||||
log::warn!("extension [{}] is not compatible with the current platform [{}], it is available to {:?}", extension.id, current_platform, platforms.iter().map(|os|os.to_string()).collect::<Vec<_>>());
|
||||
log::warn!(
|
||||
"extension [{}] is not compatible with the current platform [{}], it is available to {:?}",
|
||||
extension.id,
|
||||
current_platform,
|
||||
platforms
|
||||
.iter()
|
||||
.map(|os| os.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -320,7 +325,8 @@ fn validate_sub_items(extension_id: &str, sub_items: &[Extension]) -> bool {
|
||||
if sub_item.r#type == ExtensionType::Group || sub_item.r#type == ExtensionType::Extension {
|
||||
log::warn!(
|
||||
"invalid extension sub-item [{}-{}]: sub-item should not be of type [Group] or [Extension]",
|
||||
extension_id, sub_item.id
|
||||
extension_id,
|
||||
sub_item.id
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -394,11 +400,11 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
/// Note that when you enable a parent extension, its **enabled** children extensions
|
||||
/// should also be enabled.
|
||||
#[async_recursion::async_recursion]
|
||||
async fn _enable_extension(extension: &Extension) -> Result<(), String> {
|
||||
async fn _enable_extension(
|
||||
tauri_app_handle: &AppHandle,
|
||||
extension: &Extension,
|
||||
) -> Result<(), String> {
|
||||
if extension.supports_alias_hotkey() {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
if let Some(ref hotkey) = extension.hotkey {
|
||||
let on_opened = extension.on_opened().unwrap_or_else(|| panic!( "extension has hotkey, but on_open() returns None, extension ID [{}], extension type [{:?}]", extension.id, extension.r#type));
|
||||
|
||||
@@ -406,12 +412,14 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.on_shortcut(hotkey.as_str(), move |_tauri_app_handle, _hotkey, event| {
|
||||
.on_shortcut(hotkey.as_str(), move |tauri_app_handle, _hotkey, event| {
|
||||
let on_opened_clone = on_opened.clone();
|
||||
let extension_id_clone = extension_id_clone.clone();
|
||||
let app_handle_clone = tauri_app_handle.clone();
|
||||
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
async_runtime::spawn(async move {
|
||||
let result = open(on_opened_clone).await;
|
||||
let result = open(app_handle_clone, on_opened_clone).await;
|
||||
if let Err(msg) = result {
|
||||
log::warn!(
|
||||
"failed to open extension [{}], error [{}]",
|
||||
@@ -430,19 +438,19 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
if extension.r#type.contains_sub_items() {
|
||||
if let Some(commands) = &extension.commands {
|
||||
for command in commands.iter().filter(|ext| ext.enabled) {
|
||||
Self::_enable_extension(command).await?;
|
||||
Self::_enable_extension(&tauri_app_handle, command).await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(scripts) = &extension.scripts {
|
||||
for script in scripts.iter().filter(|ext| ext.enabled) {
|
||||
Self::_enable_extension(script).await?;
|
||||
Self::_enable_extension(&tauri_app_handle, script).await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(quicklinks) = &extension.quicklinks {
|
||||
for quicklink in quicklinks.iter().filter(|ext| ext.enabled) {
|
||||
Self::_enable_extension(quicklink).await?;
|
||||
Self::_enable_extension(&tauri_app_handle, quicklink).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -457,12 +465,11 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
/// Note that when you disable a parent extension, its **enabled** children extensions
|
||||
/// should also be disabled.
|
||||
#[async_recursion::async_recursion]
|
||||
async fn _disable_extension(extension: &Extension) -> Result<(), String> {
|
||||
async fn _disable_extension(
|
||||
tauri_app_handle: &AppHandle,
|
||||
extension: &Extension,
|
||||
) -> Result<(), String> {
|
||||
if let Some(ref hotkey) = extension.hotkey {
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.unregister(hotkey.as_str())
|
||||
@@ -473,19 +480,19 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
if extension.r#type.contains_sub_items() {
|
||||
if let Some(commands) = &extension.commands {
|
||||
for command in commands.iter().filter(|ext| ext.enabled) {
|
||||
Self::_disable_extension(command).await?;
|
||||
Self::_disable_extension(tauri_app_handle, command).await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(scripts) = &extension.scripts {
|
||||
for script in scripts.iter().filter(|ext| ext.enabled) {
|
||||
Self::_disable_extension(script).await?;
|
||||
Self::_disable_extension(tauri_app_handle, script).await?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(quicklinks) = &extension.quicklinks {
|
||||
for quicklink in quicklinks.iter().filter(|ext| ext.enabled) {
|
||||
Self::_disable_extension(quicklink).await?;
|
||||
Self::_disable_extension(tauri_app_handle, quicklink).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -504,6 +511,7 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
#[named]
|
||||
pub(super) async fn enable_extension(
|
||||
&self,
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let mut extensions_write_lock = self.inner.extensions.write().await;
|
||||
@@ -531,11 +539,11 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
|
||||
update_extension(extension)?;
|
||||
alter_extension_json_file(
|
||||
&THIRD_PARTY_EXTENSIONS_DIRECTORY,
|
||||
&get_third_party_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
Self::_enable_extension(extension).await?;
|
||||
Self::_enable_extension(tauri_app_handle, extension).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -543,6 +551,7 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
#[named]
|
||||
pub(super) async fn disable_extension(
|
||||
&self,
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let mut extensions_write_lock = self.inner.extensions.write().await;
|
||||
@@ -570,11 +579,11 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
|
||||
update_extension(extension)?;
|
||||
alter_extension_json_file(
|
||||
&THIRD_PARTY_EXTENSIONS_DIRECTORY,
|
||||
&get_third_party_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
Self::_disable_extension(extension).await?;
|
||||
Self::_disable_extension(tauri_app_handle, extension).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -582,6 +591,7 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
#[named]
|
||||
pub(super) async fn set_extension_alias(
|
||||
&self,
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
alias: &str,
|
||||
) -> Result<(), String> {
|
||||
@@ -602,7 +612,7 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
|
||||
update_extension(extension)?;
|
||||
alter_extension_json_file(
|
||||
&THIRD_PARTY_EXTENSIONS_DIRECTORY,
|
||||
&get_third_party_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
@@ -612,11 +622,11 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
|
||||
/// Initialize the third-party extensions, which literally means
|
||||
/// enabling/activating the enabled extensions.
|
||||
pub(super) async fn init(&self) -> Result<(), String> {
|
||||
pub(super) async fn init(&self, tauri_app_handle: &AppHandle) -> Result<(), String> {
|
||||
let extensions_read_lock = self.inner.extensions.read().await;
|
||||
|
||||
for extension in extensions_read_lock.iter().filter(|ext| ext.enabled) {
|
||||
Self::_enable_extension(extension).await?;
|
||||
Self::_enable_extension(tauri_app_handle, extension).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -625,10 +635,12 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
#[named]
|
||||
pub(super) async fn register_extension_hotkey(
|
||||
&self,
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
hotkey: &str,
|
||||
) -> Result<(), String> {
|
||||
self.unregister_extension_hotkey(bundle_id).await?;
|
||||
self.unregister_extension_hotkey(tauri_app_handle, bundle_id)
|
||||
.await?;
|
||||
|
||||
let mut extensions_write_lock = self.inner.extensions.write().await;
|
||||
let extension =
|
||||
@@ -648,15 +660,12 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
// Update extension (memory and file)
|
||||
update_extension(extension)?;
|
||||
alter_extension_json_file(
|
||||
&THIRD_PARTY_EXTENSIONS_DIRECTORY,
|
||||
&get_third_party_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
|
||||
// Set hotkey
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
let on_opened = extension.on_opened().unwrap_or_else(|| panic!(
|
||||
"setting hotkey for an extension that cannot be opened, extension ID [{:?}], extension type [{:?}]", bundle_id, extension.r#type,
|
||||
));
|
||||
@@ -664,12 +673,14 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
let bundle_id_owned = bundle_id.to_owned();
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.on_shortcut(hotkey, move |_tauri_app_handle, _hotkey, event| {
|
||||
.on_shortcut(hotkey, move |tauri_app_handle, _hotkey, event| {
|
||||
let on_opened_clone = on_opened.clone();
|
||||
let bundle_id_clone = bundle_id_owned.clone();
|
||||
let app_handle_clone = tauri_app_handle.clone();
|
||||
|
||||
if event.state() == ShortcutState::Pressed {
|
||||
async_runtime::spawn(async move {
|
||||
let result = open(on_opened_clone).await;
|
||||
let result = open(app_handle_clone, on_opened_clone).await;
|
||||
if let Err(msg) = result {
|
||||
log::warn!(
|
||||
"failed to open extension [{:?}], error [{}]",
|
||||
@@ -690,6 +701,7 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
#[named]
|
||||
pub(super) async fn unregister_extension_hotkey(
|
||||
&self,
|
||||
tauri_app_handle: &AppHandle,
|
||||
bundle_id: &ExtensionBundleIdBorrowed<'_>,
|
||||
) -> Result<(), String> {
|
||||
let mut extensions_write_lock = self.inner.extensions.write().await;
|
||||
@@ -717,15 +729,12 @@ impl ThirdPartyExtensionsSearchSource {
|
||||
|
||||
update_extension(extension)?;
|
||||
alter_extension_json_file(
|
||||
&THIRD_PARTY_EXTENSIONS_DIRECTORY,
|
||||
&get_third_party_extension_directory(tauri_app_handle),
|
||||
bundle_id,
|
||||
update_extension,
|
||||
)?;
|
||||
|
||||
// Set hotkey
|
||||
let tauri_app_handle = GLOBAL_TAURI_APP_HANDLE
|
||||
.get()
|
||||
.expect("global tauri app handle not set");
|
||||
tauri_app_handle
|
||||
.global_shortcut()
|
||||
.unregister(hotkey.as_str())
|
||||
@@ -846,7 +855,11 @@ impl SearchSource for ThirdPartyExtensionsSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
_tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
let Some(query_string) = query.query_strings.get("query") else {
|
||||
return Ok(QueryResponse {
|
||||
source: self.get_type(),
|
||||
@@ -1045,11 +1058,13 @@ fn calculate_text_similarity(query: &str, text: &str) -> Option<f64> {
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn uninstall_extension(
|
||||
tauri_app_handle: AppHandle,
|
||||
developer: String,
|
||||
extension_id: String,
|
||||
) -> Result<(), String> {
|
||||
let extension_dir = {
|
||||
let mut path = THIRD_PARTY_EXTENSIONS_DIRECTORY.join(developer.as_str());
|
||||
let mut path = get_third_party_extension_directory(&tauri_app_handle);
|
||||
path.push(developer.as_str());
|
||||
path.push(extension_id.as_str());
|
||||
|
||||
path
|
||||
@@ -1075,7 +1090,7 @@ pub(crate) async fn uninstall_extension(
|
||||
// Unregistering hotkey is the only thing that we will do when we disable
|
||||
// an extension, so we directly use this function here even though "disabling"
|
||||
// the extension that one is trying to uninstall does not make too much sense.
|
||||
ThirdPartyExtensionsSearchSource::_disable_extension(&extension).await?;
|
||||
ThirdPartyExtensionsSearchSource::_disable_extension(&tauri_app_handle, &extension).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
55
src-tauri/src/extension/third_party/store.rs
vendored
55
src-tauri/src/extension/third_party/store.rs
vendored
@@ -8,17 +8,18 @@ use crate::common::search::QueryResponse;
|
||||
use crate::common::search::QuerySource;
|
||||
use crate::common::search::SearchQuery;
|
||||
use crate::common::traits::SearchSource;
|
||||
use crate::extension::canonicalize_relative_icon_path;
|
||||
use crate::extension::third_party::THIRD_PARTY_EXTENSIONS_DIRECTORY;
|
||||
use crate::extension::Extension;
|
||||
use crate::extension::PLUGIN_JSON_FILE_NAME;
|
||||
use crate::extension::THIRD_PARTY_EXTENSIONS_SEARCH_SOURCE;
|
||||
use crate::extension::canonicalize_relative_icon_path;
|
||||
use crate::extension::third_party::get_third_party_extension_directory;
|
||||
use crate::server::http_client::HttpClient;
|
||||
use async_trait::async_trait;
|
||||
use reqwest::StatusCode;
|
||||
use serde_json::Map as JsonObject;
|
||||
use serde_json::Value as Json;
|
||||
use std::io::Read;
|
||||
use tauri::AppHandle;
|
||||
|
||||
const DATA_SOURCE_ID: &str = "Extension Store";
|
||||
|
||||
@@ -37,7 +38,11 @@ impl SearchSource for ExtensionStore {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
_tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
const SCORE: f64 = 2000.0;
|
||||
|
||||
let Some(query_string) = query.query_strings.get("query") else {
|
||||
@@ -174,7 +179,10 @@ async fn is_extension_installed(developer: String, extension_id: String) -> bool
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn install_extension_from_store(id: String) -> Result<(), String> {
|
||||
pub(crate) async fn install_extension_from_store(
|
||||
tauri_app_handle: AppHandle,
|
||||
id: String,
|
||||
) -> Result<(), String> {
|
||||
let path = format!("store/extension/{}/_download", id);
|
||||
let response = HttpClient::get("default_coco_server", &path, None)
|
||||
.await
|
||||
@@ -197,9 +205,11 @@ pub(crate) async fn install_extension_from_store(id: String) -> Result<(), Strin
|
||||
//
|
||||
// 1. Its `developer` field is a JSON object, but we need a string
|
||||
// 2. sub-extensions won't have their `id` fields set
|
||||
//
|
||||
//
|
||||
// we need to correct it
|
||||
let mut plugin_json = archive.by_name(PLUGIN_JSON_FILE_NAME).map_err(|e| e.to_string())?;
|
||||
let mut plugin_json = archive
|
||||
.by_name(PLUGIN_JSON_FILE_NAME)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut plugin_json_content = String::new();
|
||||
std::io::Read::read_to_string(&mut plugin_json, &mut plugin_json_content)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -249,12 +259,11 @@ pub(crate) async fn install_extension_from_store(id: String) -> Result<(), Strin
|
||||
|
||||
drop(plugin_json);
|
||||
|
||||
|
||||
// Write extension files to the extension directory
|
||||
let developer = extension.developer.clone().unwrap_or_default();
|
||||
let extension_id = extension.id.clone();
|
||||
let extension_directory = {
|
||||
let mut path = THIRD_PARTY_EXTENSIONS_DIRECTORY.to_path_buf();
|
||||
let mut path = get_third_party_extension_directory(&tauri_app_handle);
|
||||
path.push(developer);
|
||||
path.push(extension_id.as_str());
|
||||
path
|
||||
@@ -266,7 +275,7 @@ pub(crate) async fn install_extension_from_store(id: String) -> Result<(), Strin
|
||||
// Extract all files except plugin.json
|
||||
for i in 0..archive.len() {
|
||||
let mut zip_file = archive.by_index(i).map_err(|e| e.to_string())?;
|
||||
// `.name()` is safe to use in our cases, the cases listed in the below
|
||||
// `.name()` is safe to use in our cases, the cases listed in the below
|
||||
// page won't happen to us.
|
||||
//
|
||||
// https://docs.rs/zip/4.2.0/zip/read/struct.ZipFile.html#method.name
|
||||
@@ -288,14 +297,29 @@ pub(crate) async fn install_extension_from_store(id: String) -> Result<(), Strin
|
||||
let dest_file_path = extension_directory.join(zip_file_name);
|
||||
|
||||
// For cases like `assets/xxx.png`
|
||||
if let Some(parent_dir) = dest_file_path.parent() && !parent_dir.exists() {
|
||||
tokio::fs::create_dir_all(parent_dir).await.map_err(|e| e.to_string())?;
|
||||
if let Some(parent_dir) = dest_file_path.parent()
|
||||
&& !parent_dir.exists()
|
||||
{
|
||||
tokio::fs::create_dir_all(parent_dir)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
let mut dest_file = tokio::fs::File::create(&dest_file_path) .await .map_err(|e| e.to_string())?;
|
||||
let mut src_bytes = Vec::with_capacity(zip_file.size().try_into().expect("we won't have a extension file that is bigger than 4GiB"));
|
||||
zip_file.read_to_end(&mut src_bytes).map_err(|e| e.to_string())?;
|
||||
tokio::io::copy(&mut src_bytes.as_slice(), &mut dest_file).await.map_err(|e| e.to_string())?;
|
||||
let mut dest_file = tokio::fs::File::create(&dest_file_path)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut src_bytes = Vec::with_capacity(
|
||||
zip_file
|
||||
.size()
|
||||
.try_into()
|
||||
.expect("we won't have a extension file that is bigger than 4GiB"),
|
||||
);
|
||||
zip_file
|
||||
.read_to_end(&mut src_bytes)
|
||||
.map_err(|e| e.to_string())?;
|
||||
tokio::io::copy(&mut src_bytes.as_slice(), &mut dest_file)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
// Create plugin.json from the extension variable
|
||||
let plugin_json_path = extension_directory.join(PLUGIN_JSON_FILE_NAME);
|
||||
@@ -304,7 +328,6 @@ pub(crate) async fn install_extension_from_store(id: String) -> Result<(), Strin
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
|
||||
// Turn it into an absolute path if it is a valid relative path because frontend code need this.
|
||||
canonicalize_relative_icon_path(&extension_directory, &mut extension)?;
|
||||
|
||||
|
||||
@@ -28,9 +28,14 @@ pub(crate) const COCO_TAURI_STORE: &str = "coco_tauri_store";
|
||||
lazy_static! {
|
||||
static ref PREVIOUS_MONITOR_NAME: Mutex<Option<String>> = Mutex::new(None);
|
||||
}
|
||||
|
||||
/// To allow us to access tauri's `AppHandle` when its context is inaccessible,
|
||||
/// store it globally. It will be set in `init()`.
|
||||
///
|
||||
/// # WARNING
|
||||
///
|
||||
/// You may find this work, but the usage is discouraged and should be generally
|
||||
/// avoided. If you do need it, always be careful that it may not be set() when
|
||||
/// you access it.
|
||||
pub(crate) static GLOBAL_TAURI_APP_HANDLE: OnceLock<AppHandle> = OnceLock::new();
|
||||
|
||||
#[tauri::command]
|
||||
@@ -173,6 +178,12 @@ pub fn run() {
|
||||
util::app_lang::update_app_lang,
|
||||
])
|
||||
.setup(|app| {
|
||||
let app_handle = app.handle().clone();
|
||||
GLOBAL_TAURI_APP_HANDLE
|
||||
.set(app_handle.clone())
|
||||
.expect("global tauri AppHandle already initialized");
|
||||
log::trace!("global Tauri AppHandle set");
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
log::trace!("hiding Dock icon on macOS");
|
||||
@@ -180,12 +191,6 @@ pub fn run() {
|
||||
log::trace!("Dock icon should be hidden now");
|
||||
}
|
||||
|
||||
let app_handle = app.handle().clone();
|
||||
GLOBAL_TAURI_APP_HANDLE
|
||||
.set(app_handle.clone())
|
||||
.expect("variable already initialized");
|
||||
log::trace!("global Tauri app handle set");
|
||||
|
||||
let registry = SearchSourceRegistry::default();
|
||||
|
||||
app.manage(registry); // Store registry in Tauri's app state
|
||||
@@ -406,12 +411,13 @@ fn move_window_to_active_monitor<R: Runtime>(window: &WebviewWindow<R>) {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn get_app_search_source<R: Runtime>(app_handle: AppHandle<R>) -> Result<(), String> {
|
||||
async fn get_app_search_source(app_handle: AppHandle) -> Result<(), String> {
|
||||
// We want all the extensions here, so no filter condition specified.
|
||||
let (_found_invalid_extensions, extensions) = extension::list_extensions(None, None, false)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
extension::init_extensions(extensions).await?;
|
||||
let (_found_invalid_extensions, extensions) =
|
||||
extension::list_extensions(app_handle.clone(), None, None, false)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
extension::init_extensions(app_handle.clone(), extensions).await?;
|
||||
|
||||
let _ = server::connector::refresh_all_connectors(&app_handle).await;
|
||||
let _ = server::datasource::refresh_all_datasources(&app_handle).await;
|
||||
|
||||
@@ -42,7 +42,11 @@ fn app_log_dir() -> PathBuf {
|
||||
fn setup_panic_hook() {
|
||||
std::panic::set_hook(Box::new(|panic_info| {
|
||||
let timestamp = chrono::Local::now();
|
||||
let datetime_str = timestamp.format("%Y-%m-%d %H:%M:%S%.3f").to_string();
|
||||
// "%Y-%m-%d %H:%M:%S"
|
||||
//
|
||||
// I would like to use the above format, but Windows does not allow that
|
||||
// and complains with OS error 123.
|
||||
let datetime_str = timestamp.format("%Y-%m-%d-%H-%M-%S").to_string();
|
||||
|
||||
let log_dir = app_log_dir();
|
||||
|
||||
@@ -52,7 +56,7 @@ fn setup_panic_hook() {
|
||||
return;
|
||||
}
|
||||
|
||||
let panic_file = log_dir.join(format!("{}.panic", datetime_str));
|
||||
let panic_file = log_dir.join(format!("{}_rust_panic.log", datetime_str));
|
||||
|
||||
// Prepare panic information
|
||||
let panic_message = if let Some(s) = panic_info.payload().downcast_ref::<&str>() {
|
||||
@@ -75,8 +79,8 @@ fn setup_panic_hook() {
|
||||
};
|
||||
|
||||
let panic_log = format!(
|
||||
"[{}] PANIC: {} at {}\n",
|
||||
datetime_str, panic_message, location
|
||||
"Time: [{}]\nLocation: [{}]\nMessage: [{}]",
|
||||
datetime_str, location, panic_message
|
||||
);
|
||||
|
||||
// Write to panic file
|
||||
@@ -98,7 +102,7 @@ fn setup_panic_hook() {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Panic hook should be the first thing to do, everything could panic!
|
||||
// Panic hook setup should be the first thing to do, everything could panic!
|
||||
setup_panic_hook();
|
||||
coco_lib::run();
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::future::Future;
|
||||
use std::sync::Arc;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use tauri::{AppHandle, Manager};
|
||||
use tokio::time::error::Elapsed;
|
||||
use tokio::time::{Duration, timeout};
|
||||
|
||||
@@ -34,6 +34,7 @@ fn same_type_futures(
|
||||
query_source_trait_object: Arc<dyn SearchSource>,
|
||||
timeout_duration: Duration,
|
||||
search_query: SearchQuery,
|
||||
tauri_app_handle: AppHandle,
|
||||
) -> impl Future<
|
||||
Output = (
|
||||
QuerySource,
|
||||
@@ -45,7 +46,9 @@ fn same_type_futures(
|
||||
// Store `query_source` as part of future for debugging purposes.
|
||||
query_source,
|
||||
timeout(timeout_duration, async {
|
||||
query_source_trait_object.search(search_query).await
|
||||
query_source_trait_object
|
||||
.search(tauri_app_handle.clone(), search_query)
|
||||
.await
|
||||
})
|
||||
.await,
|
||||
)
|
||||
@@ -54,8 +57,8 @@ fn same_type_futures(
|
||||
|
||||
#[named]
|
||||
#[tauri::command]
|
||||
pub async fn query_coco_fusion<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
pub async fn query_coco_fusion(
|
||||
app_handle: AppHandle,
|
||||
from: u64,
|
||||
size: u64,
|
||||
query_strings: HashMap<String, String>,
|
||||
@@ -127,6 +130,7 @@ pub async fn query_coco_fusion<R: Runtime>(
|
||||
query_source_trait_object,
|
||||
timeout_duration,
|
||||
search_query,
|
||||
app_handle.clone(),
|
||||
));
|
||||
} else {
|
||||
for query_source_trait_object in sources_list {
|
||||
@@ -137,6 +141,7 @@ pub async fn query_coco_fusion<R: Runtime>(
|
||||
query_source_trait_object,
|
||||
timeout_duration,
|
||||
search_query.clone(),
|
||||
app_handle.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@ use crate::common::server::Server;
|
||||
use crate::common::traits::SearchSource;
|
||||
use crate::server::http_client::HttpClient;
|
||||
use async_trait::async_trait;
|
||||
// use futures::stream::StreamExt;
|
||||
use ordered_float::OrderedFloat;
|
||||
use reqwest::StatusCode;
|
||||
use std::collections::HashMap;
|
||||
use tauri::AppHandle;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub(crate) struct DocumentsSizedCollector {
|
||||
@@ -90,7 +90,11 @@ impl SearchSource for CocoSearchSource {
|
||||
}
|
||||
}
|
||||
|
||||
async fn search(&self, query: SearchQuery) -> Result<QueryResponse, SearchError> {
|
||||
async fn search(
|
||||
&self,
|
||||
_tauri_app_handle: AppHandle,
|
||||
query: SearchQuery,
|
||||
) -> Result<QueryResponse, SearchError> {
|
||||
let url = "/query/_search";
|
||||
let mut total_hits = 0;
|
||||
let mut hits: Vec<(Document, f64)> = Vec::new();
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::server::http_client::HttpClient;
|
||||
use futures_util::StreamExt;
|
||||
use http::Method;
|
||||
use serde_json::json;
|
||||
use tauri::{command, AppHandle, Emitter, Runtime};
|
||||
use tauri::{AppHandle, Emitter, Runtime, command};
|
||||
|
||||
#[command]
|
||||
pub async fn synthesize<R: Runtime>(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::common::http::get_response_body_text;
|
||||
use crate::server::http_client::HttpClient;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{from_str, Value};
|
||||
use serde_json::{Value, from_str};
|
||||
use tauri::command;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//credits to: https://github.com/ayangweb/ayangweb-EcoPaste/blob/169323dbe6365ffe4abb64d867439ed2ea84c6d1/src-tauri/src/core/setup/mac.rs
|
||||
use tauri::{App, Emitter, EventTarget, WebviewWindow};
|
||||
use tauri_nspanel::{cocoa::appkit::NSWindowCollectionBehavior, panel_delegate, WebviewWindowExt};
|
||||
use tauri_nspanel::{WebviewWindowExt, cocoa::appkit::NSWindowCollectionBehavior, panel_delegate};
|
||||
|
||||
use crate::common::MAIN_WINDOW_LABEL;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{hide_coco, show_coco, COCO_TAURI_STORE};
|
||||
use tauri::{async_runtime, App, AppHandle, Manager, Runtime};
|
||||
use crate::{COCO_TAURI_STORE, hide_coco, show_coco};
|
||||
use tauri::{App, AppHandle, Manager, Runtime, async_runtime};
|
||||
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut, ShortcutState};
|
||||
use tauri_plugin_store::{JsonValue, StoreExt};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Configuration entry App language is persisted in the frontend code, but we
|
||||
//! need to access it on the backend.
|
||||
//!
|
||||
//! So we duplicate it here **in the MEMORY** and expose a setter method to the
|
||||
//! So we duplicate it here **in the MEMORY** and expose a setter method to the
|
||||
//! frontend so that the value can be updated and stay update-to-date.
|
||||
|
||||
use function_name::named;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Copy)]
|
||||
pub(crate) enum FileType {
|
||||
Folder,
|
||||
@@ -51,7 +50,6 @@ pub(crate) enum FileType {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
|
||||
async fn get_file_type(path: &str) -> FileType {
|
||||
let path = camino::Utf8Path::new(path);
|
||||
|
||||
@@ -116,7 +114,6 @@ async fn get_file_type(path: &str) -> FileType {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn type_to_icon(ty: FileType) -> &'static str {
|
||||
match ty {
|
||||
FileType::Folder => "font_file_folder",
|
||||
@@ -170,9 +167,8 @@ fn type_to_icon(ty: FileType) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
pub(crate) async fn get_file_icon(path: String) -> &'static str {
|
||||
let ty = get_file_type(path.as_str()).await;
|
||||
type_to_icon(ty)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub(crate) mod app_lang;
|
||||
pub(crate) mod file;
|
||||
pub(crate) mod platform;
|
||||
pub(crate) mod app_lang;
|
||||
|
||||
use std::{path::Path, process::Command};
|
||||
use tauri::{AppHandle, Runtime};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Copy, Clone, Hash, PartialEq, Eq, Display)]
|
||||
@@ -13,12 +13,11 @@ pub(crate) enum Platform {
|
||||
Windows,
|
||||
}
|
||||
|
||||
|
||||
impl Platform {
|
||||
/// Helper function to determine the current platform.
|
||||
pub(crate) fn current() -> Platform {
|
||||
let os_str = std::env::consts::OS;
|
||||
serde_plain::from_str(os_str).unwrap_or_else(|_e| {
|
||||
let os_str = std::env::consts::OS;
|
||||
serde_plain::from_str(os_str).unwrap_or_else(|_e| {
|
||||
panic!("std::env::consts::OS is [{}], which is not a valid value for [enum Platform], valid values: ['macos', 'linux', 'windows']", os_str)
|
||||
})
|
||||
}
|
||||
@@ -26,16 +25,10 @@ impl Platform {
|
||||
/// Return the `X-OS-NAME` HTTP request header.
|
||||
pub(crate) fn to_os_name_http_header_str(&self) -> Cow<'static, str> {
|
||||
match self {
|
||||
Self::Macos => {
|
||||
Cow::Borrowed("macos")
|
||||
}
|
||||
Self::Windows => {
|
||||
Cow::Borrowed("windows")
|
||||
}
|
||||
Self::Macos => Cow::Borrowed("macos"),
|
||||
Self::Windows => Cow::Borrowed("windows"),
|
||||
// For Linux, we need the actual distro `ID`, not just a "linux".
|
||||
Self::Linux => {
|
||||
Cow::Owned(sysinfo::System::distribution_id())
|
||||
}
|
||||
Self::Linux => Cow::Owned(sysinfo::System::distribution_id()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user