Repository navigation
Expand file tree
/
Copy pathruntime.rs
More file actions
194 lines (182 loc) · 7.38 KB
/
Copy pathruntime.rs
File metadata and controls
194 lines (182 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Meetings in the Linux and Windows apps: the controller thread that
//! detects calls and records them (`meeting_watcher`), the capsules its
//! events drive, and the recording state the trays read. The macOS app runs
//! the same controller and capsules from `macos_rune`.
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::{self, SyncSender};
use std::sync::{Mutex, OnceLock};
use std::thread::{self, JoinHandle};
use std::time::Instant;
use color_eyre::Result;
use crate::desktop::tray_menu::MeetingTray;
use crate::meeting::MeetingRequest;
use crate::meeting::watcher::{ControllerEvent, MeetingMenuState};
use crate::rune_ui::{Destination, Ui};
static REQUESTS: OnceLock<SyncSender<MeetingRequest>> = OnceLock::new();
/// The app, whose clipboard the tray's copy writes.
static APP: OnceLock<rune::AppHandle<Ui>> = OnceLock::new();
static STATE: Mutex<MeetingMenuState> = Mutex::new(MeetingMenuState::Idle);
/// The meeting controller and the thread that shows its events. Detection
/// stays idle until the user turns Meetings on.
pub(crate) struct Meetings {
controller: JoinHandle<()>,
events: JoinHandle<()>,
}
impl Meetings {
/// Starts the controller and installs its capsules on `app`.
pub(crate) fn install(app: &mut rune::App<Ui>, shutdown: &'static AtomicBool) -> Result<Self> {
let (event_sender, events) = mpsc::sync_channel(32);
let (commands, command_receiver) = mpsc::sync_channel(8);
let (requests, request_receiver) = mpsc::sync_channel(8);
let _ = REQUESTS.set(requests);
let controller = thread::Builder::new()
.name("hex-meetings".into())
.spawn(move || {
if let Err(error) = crate::meeting::watcher::controller_loop(
shutdown,
event_sender.clone(),
command_receiver,
request_receiver,
) {
tracing::error!(%error, "meeting controller stopped");
let _ = event_sender.send(ControllerEvent::Failed {
title: "Meeting detection stopped".into(),
error: error.to_string(),
});
}
})?;
let window = app.handle();
let _ = APP.set(window.clone());
let mut capsules = {
let chosen = window.clone();
let moved = window.clone();
crate::rune_ui::meeting_capsules::MeetingCapsules::install(
app,
commands,
move |choice| {
crate::app_settings::save_meeting_provider(choice.clone());
chosen.update(move |ui, _| crate::rune_ui::meeting_provider_chosen(ui, choice));
},
move |corner| {
crate::app_settings::save_meeting_indicator_corner(corner);
moved.update(move |ui, _| crate::rune_ui::meeting_corner_moved(ui, corner));
},
)
};
let events = thread::Builder::new()
.name("hex-meeting-events".into())
.spawn(move || {
for event in events {
capsules.event(&event);
follow(&event, &window);
}
})?;
Ok(Self { controller, events })
}
/// Waits for the controller, which stops once `shutdown` is set: a
/// recording keeps its audio and finishes its transcript at the next
/// launch.
pub(crate) fn join(self) {
if self.controller.join().is_err() {
tracing::error!("meeting controller panicked");
}
let _ = self.events.join();
}
}
/// Asks the controller to start, stop, or summarize a meeting.
pub(crate) fn request(request: MeetingRequest) -> Result<(), String> {
if let Some(lock) = crate::labs::Feature::Meetings.managed() {
return Err(lock.refusal().into());
}
REQUESTS
.get()
.ok_or("meetings are not running")?
.try_send(request)
.map_err(|error| format!("could not reach the meeting recorder: {error}"))
}
/// The recording state the tray shows.
pub(crate) fn state() -> MeetingMenuState {
STATE
.lock()
.unwrap_or_else(|error| error.into_inner())
.clone()
}
/// The recorder as the tray shows it; `None` while Meetings is off.
pub(crate) fn tray_state() -> Option<MeetingTray> {
if !crate::app_settings::meetings_enabled() || REQUESTS.get().is_none() {
return None;
}
Some(match state() {
MeetingMenuState::Idle => MeetingTray::Idle,
MeetingMenuState::Starting => MeetingTray::Starting,
MeetingMenuState::Recording { .. } => MeetingTray::Recording,
MeetingMenuState::Transcribing => MeetingTray::Transcribing,
})
}
/// The tray's Copy last meeting transcript.
pub(crate) fn copy_last_transcript() -> Result<(), String> {
let markdown =
crate::meeting::latest_transcript_markdown().map_err(|error| format!("{error:#}"))?;
APP.get()
.ok_or("the window is not running")?
.update(move |_, tx| tx.copy(markdown));
Ok(())
}
/// Mirrors an event into the recording state and the window.
fn follow(event: &ControllerEvent, window: &rune::AppHandle<Ui>) {
let mut state = STATE.lock().unwrap_or_else(|error| error.into_inner());
match event {
ControllerEvent::Starting => *state = MeetingMenuState::Starting,
ControllerEvent::RecordingStarted { id, title, .. } => {
*state = MeetingMenuState::Recording {
started: Instant::now(),
id: id.clone(),
title: title.clone(),
chapters: Vec::new(),
};
}
ControllerEvent::Retitled { title } => {
if let MeetingMenuState::Recording { title: shown, .. } = &mut *state {
shown.clone_from(title);
}
}
ControllerEvent::Chapters(chapters) => {
if let MeetingMenuState::Recording {
chapters: shown, ..
} = &mut *state
{
shown.clone_from(chapters);
}
}
ControllerEvent::Transcribing => *state = MeetingMenuState::Transcribing,
ControllerEvent::Finished(manifest) => {
tracing::info!(id = %manifest.id, "meeting transcript ready");
*state = MeetingMenuState::Idle;
}
ControllerEvent::OpenMeeting(id) => {
let id = id.clone();
window.update(move |ui, tx| {
crate::rune_ui::show(ui, Destination::Meetings(Some(id)), tx)
});
window.open();
}
ControllerEvent::Failed { title, error } => {
tracing::error!(%title, %error, "meeting failed");
*state = MeetingMenuState::Idle;
let message = format!("{title}: {error}");
window.update(move |ui, tx| {
crate::rune_ui::meeting_failed(ui, message);
crate::rune_ui::show(ui, Destination::Meetings(None), tx);
});
window.open();
}
ControllerEvent::CallEnded { app, .. } => {
tracing::info!(%app, "the call ended; the recording continues until Stop");
}
ControllerEvent::Offer(_)
| ControllerEvent::Reminder { .. }
| ControllerEvent::ReminderWithdrawn { .. }
| ControllerEvent::NotesStarted
| ControllerEvent::NotesFinished(_) => {}
}
}