Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions apps/desktop/src/contexts/listener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ const useHandleDetectEvents = (store: ListenerStore) => {
}
} else if (payload.type === "micMuted") {
setMuted(payload.value);
} else if (payload.type === "micActiveWithoutHyprnote") {
if (!notificationDetectEnabledRef.current) {
return;
}

const minutes = Math.round(payload.duration_secs / 60);
const appName = payload.app.name;

void notificationCommands.showNotification({
key: payload.key,
title: "Meeting in progress?",
message: `${appName} has been using the mic for ${minutes} min. Start listening?`,
timeout: { secs: 15, nanos: 0 },
event_id: null,
start_time: null,
participants: null,
event_details: null,
action_label: null,
});
}
})
.then((fn) => {
Expand Down
2 changes: 1 addition & 1 deletion plugins/detect/js/bindings.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ detectEvent: "plugin:detect:detect-event"

/** user-defined types **/

export type DetectEvent = { type: "micStarted"; key: string; apps: InstalledApp[] } | { type: "micStopped"; apps: InstalledApp[] } | { type: "micMuted"; value: boolean } | { type: "sleepStateChanged"; value: boolean }
export type DetectEvent = { type: "micStarted"; key: string; apps: InstalledApp[] } | { type: "micStopped"; apps: InstalledApp[] } | { type: "micMuted"; value: boolean } | { type: "sleepStateChanged"; value: boolean } | { type: "micActiveWithoutHyprnote"; key: string; app: InstalledApp; duration_secs: number }
export type InstalledApp = { id: string; name: string }

/** tauri-specta globals **/
Expand Down
6 changes: 6 additions & 0 deletions plugins/detect/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ common_event_derives! {
MicMuteStateChanged { value: bool },
#[serde(rename = "sleepStateChanged")]
SleepStateChanged { value: bool },
#[serde(rename = "micActiveWithoutHyprnote")]
MicActiveWithoutHyprnote {
key: String,
app: hypr_detect::InstalledApp,
duration_secs: u64,
},
}
}

Expand Down
81 changes: 77 additions & 4 deletions plugins/detect/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use tauri::{AppHandle, EventTarget, Manager, Runtime};
use tauri_plugin_listener::ListenerPluginExt;
use tauri_plugin_windows::WindowImpl;
Expand All @@ -8,6 +10,8 @@ use crate::{
policy::{MicEventType, PolicyContext},
};

const MIC_ACTIVE_THRESHOLD: Duration = Duration::from_secs(3 * 60);

pub async fn setup<R: Runtime>(app: &AppHandle<R>) -> Result<(), Box<dyn std::error::Error>> {
let app_handle = app.app_handle().clone();
let callback = hypr_detect::new_callback(move |event| {
Expand Down Expand Up @@ -56,7 +60,7 @@ async fn handle_mic_started<R: Runtime>(
};

let state = app_handle.state::<SharedState>();
let state_guard = state.lock().await;
let mut state_guard = state.lock().await;

let is_dnd = dnd::is_do_not_disturb();

Expand All @@ -69,12 +73,28 @@ async fn handle_mic_started<R: Runtime>(

match state_guard.policy.evaluate(&ctx) {
Ok(result) => {
for app in &result.filtered_apps {
if !state_guard.mic_usage_timers.contains_key(&app.id) {
let timer_handle = spawn_mic_active_timer(
app_handle.clone(),
app.clone(),
MIC_ACTIVE_THRESHOLD,
);
state_guard
.mic_usage_timers
.insert(app.id.clone(), timer_handle);
}
}

let dedup_key = result.dedup_key;
let filtered_apps = result.filtered_apps;
drop(state_guard);

emit_to_main(
app_handle,
DetectEvent::MicStarted {
key: result.dedup_key,
apps: result.filtered_apps,
key: dedup_key,
apps: filtered_apps,
},
);
}
Expand All @@ -97,7 +117,14 @@ async fn handle_mic_stopped<R: Runtime>(
};

let state = app_handle.state::<SharedState>();
let state_guard = state.lock().await;
let mut state_guard = state.lock().await;

for app in &apps {
if let Some(handle) = state_guard.mic_usage_timers.remove(&app.id) {
handle.abort();
tracing::info!(app_id = %app.id, "cancelled_mic_active_timer");
}
}

let is_dnd = dnd::is_do_not_disturb();

Expand All @@ -124,6 +151,52 @@ async fn handle_mic_stopped<R: Runtime>(
}
}

fn spawn_mic_active_timer<R: Runtime>(
app_handle: AppHandle<R>,
app: hypr_detect::InstalledApp,
threshold: Duration,
) -> tokio::task::JoinHandle<()> {
let duration_secs = threshold.as_secs();
let app_id = app.id.clone();
tokio::spawn(async move {
tokio::time::sleep(threshold).await;

let is_listening = {
let listener_state = app_handle.listener().get_state().await;
matches!(
listener_state,
tauri_plugin_listener::State::Active | tauri_plugin_listener::State::Finalizing
)
};

if is_listening {
tracing::info!(
app_id = %app_id,
"skip_mic_active_without_hyprnote: hyprnote_is_listening"
);
} else {
tracing::info!(
app_id = %app_id,
duration_secs,
"mic_active_without_hyprnote"
);
let key = uuid::Uuid::new_v4().to_string();
emit_to_main(
&app_handle,
DetectEvent::MicActiveWithoutHyprnote {
key,
app,
duration_secs,
},
);
}

let state = app_handle.state::<SharedState>();
let mut state_guard = state.lock().await;
state_guard.mic_usage_timers.remove(&app_id);
})
}

fn emit_to_main<R: Runtime>(app_handle: &AppHandle<R>, event: DetectEvent) {
let _ = event.emit_to(
app_handle,
Expand Down
3 changes: 3 additions & 0 deletions plugins/detect/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use tauri::Manager;
use tokio::sync::Mutex;

Expand All @@ -24,6 +26,7 @@ pub struct State {
#[allow(dead_code)]
pub(crate) detector: hypr_detect::Detector,
pub(crate) policy: policy::MicNotificationPolicy,
pub(crate) mic_usage_timers: HashMap<String, tokio::task::JoinHandle<()>>,
}

fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
Expand Down
Loading