Fix context change propagation to webview extensions

- Added emit_to_all_extensions method to ExtensionWebviewManager
- Created webview_extension_emit_to_all Tauri command
- Updated UI store to use new command instead of emit()
- Broadcasts CONTEXT_CHANGED event to all webview windows
- Fixes dynamic context updates not reaching webview extensions
This commit is contained in:
2025-11-14 10:30:56 +01:00
parent c1ee8e6bc0
commit 7487696af4
4 changed files with 53 additions and 4 deletions

View File

@ -293,6 +293,31 @@ impl ExtensionWebviewManager {
}) })
} }
} }
/// Emits an event to all extension webview windows
pub fn emit_to_all_extensions<S: serde::Serialize + Clone>(
&self,
app_handle: &AppHandle,
event: &str,
payload: S,
) -> Result<(), ExtensionError> {
let windows = self.windows.lock().map_err(|e| ExtensionError::MutexPoisoned {
reason: e.to_string(),
})?;
// Iterate over all window IDs
for window_id in windows.keys() {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Some(window) = app_handle.get_webview_window(window_id) {
// Emit event to this specific webview window
if let Err(e) = window.emit(event, payload.clone()) {
eprintln!("Failed to emit event {} to window {}: {}", event, window_id, e);
}
}
}
Ok(())
}
} }
impl Default for ExtensionWebviewManager { impl Default for ExtensionWebviewManager {

View File

@ -244,3 +244,23 @@ pub async fn webview_extension_web_request(
"ok": status >= 200 && status < 300 "ok": status >= 200 && status < 300
})) }))
} }
/// Broadcasts an event to all extension webview windows
#[tauri::command]
pub async fn webview_extension_emit_to_all(
app_handle: tauri::AppHandle,
state: State<'_, AppState>,
event: String,
payload: serde_json::Value,
) -> Result<(), ExtensionError> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
state.extension_webview_manager.emit_to_all_extensions(
&app_handle,
&event,
payload,
)?;
}
Ok(())
}

View File

@ -143,6 +143,8 @@ pub fn run() {
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
extension::webview::web::webview_extension_web_request, extension::webview::web::webview_extension_web_request,
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
extension::webview::web::webview_extension_emit_to_all,
#[cfg(not(any(target_os = "android", target_os = "ios")))]
extension::webview::filesystem::webview_extension_fs_save_file, extension::webview::filesystem::webview_extension_fs_save_file,
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
extension::webview::filesystem::webview_extension_fs_open_file, extension::webview::filesystem::webview_extension_fs_open_file,

View File

@ -1,6 +1,5 @@
import { breakpointsTailwind } from '@vueuse/core' import { breakpointsTailwind } from '@vueuse/core'
import { invoke } from '@tauri-apps/api/core' import { invoke } from '@tauri-apps/api/core'
import { emit } from '@tauri-apps/api/event'
import { HAEXTENSION_EVENTS } from '@haexhub/sdk' import { HAEXTENSION_EVENTS } from '@haexhub/sdk'
import { broadcastContextToAllExtensions } from '~/composables/extensionMessageHandler' import { broadcastContextToAllExtensions } from '~/composables/extensionMessageHandler'
@ -81,9 +80,12 @@ export const useUiStore = defineStore('uiStore', () => {
try { try {
await invoke('webview_extension_context_set', { context }) await invoke('webview_extension_context_set', { context })
console.log('[UI Store] Context set in Tauri state:', context) console.log('[UI Store] Context set in Tauri state:', context)
// Emit Tauri event so webview extensions can listen for changes // Broadcast event to all webview extensions
await emit(HAEXTENSION_EVENTS.CONTEXT_CHANGED, { context }) await invoke('webview_extension_emit_to_all', {
console.log('[UI Store] Emitted context change event:', context) event: HAEXTENSION_EVENTS.CONTEXT_CHANGED,
payload: { context }
})
console.log('[UI Store] Broadcasted context change event to webview extensions:', context)
} catch (error) { } catch (error) {
// Ignore error if not running in Tauri (e.g., browser mode) // Ignore error if not running in Tauri (e.g., browser mode)
console.debug('[UI Store] Failed to update Tauri context:', error) console.debug('[UI Store] Failed to update Tauri context:', error)