Android: notifications that read like the app's voice bar
The voice service's notification said "In voice on <server>" with Mute
and Disconnect, and the screen-sharing one only "Sharing your screen".
The voice notification now shows what the app's voice card shows: the
channel as its title, the server and how many are there ("Home · 3 in
voice", "just you"), the time in voice as a chronometer, and Mute,
Deafen and Leave, which act on every voice session as the old Mute did.
Several servers at once read "In voice on 2 servers" with their names.
Tapping it opens the voice channel screen (the activity forwards the
action to Rust, which queues it until the window runs). While our
stream is live the screen-sharing notification says where it goes and
how many watch ("Live in Chill Zone", "Home · 2 watching"). Both use
the app's accent colour.
The window takes such platform requests through a small inbox in
voelin-ui (`voelin_ui::request`), handled on the window's thread and
outside the app's borrow, since navigating calls back into Rust.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e0cc4fe76d
commit
f3c41264bb
15 files changed
+529
-98
No files matched your search
@@ -18,6 +18,8 @@ crate-type = ["cdylib", "rlib"]
|
||||
tokio = { version = "1.53.1", features = ["rt-multi-thread", "macros", "sync", "time"] }
|
||||
tracing = "0.1.44"
|
||||
voelin-core = { path = "../voelin-core" }
|
||||
# Channels and people for the voice notification.
|
||||
voelin-model = { path = "../voelin-model" }
|
||||
|
||||
[target.'cfg(target_os = "android")'.dependencies]
|
||||
anyhow = "1.0.104"
|
||||
@@ -31,7 +33,8 @@ voelin-store = { path = "../voelin-store" }
|
||||
voelin-ui = { path = "../voelin-ui" }
|
||||
|
||||
[dev-dependencies]
|
||||
voelin-model = { path = "../voelin-model" }
|
||||
# Client ids of stream viewers in the tests.
|
||||
tsclientlib = { path = "../proto/tsclientlib" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -108,18 +108,25 @@ fn install_crash_reports(data_dir: &Path) {
|
||||
voelin_platform::crash::install(data_dir.join(voelin_platform::crash::DIR_NAME), enabled);
|
||||
}
|
||||
|
||||
/// Keep the voice foreground service in step with the voice connections.
|
||||
/// Keep the voice foreground service, and the screen-sharing service's
|
||||
/// notification, in step with the voice connections and our stream.
|
||||
fn watch_voice(host: &'static EngineHost) {
|
||||
let mut events = host.engine().subscribe();
|
||||
host.runtime().spawn(async move {
|
||||
loop {
|
||||
match events.recv().await {
|
||||
Ok(event) => {
|
||||
if let Some(notice) = with_foreground(|f| f.update(&event))
|
||||
let changes = with_foreground(|f| f.update(&event));
|
||||
if let Some(notice) = changes.voice
|
||||
&& let Err(e) = bridge::set_voice_notification(notice.as_ref())
|
||||
{
|
||||
warn!("voice service: {e}");
|
||||
}
|
||||
if let Some(notice) = changes.screen
|
||||
&& let Err(e) = bridge::set_screen_notification(notice.as_ref())
|
||||
{
|
||||
warn!("screen sharing notification: {e}");
|
||||
}
|
||||
}
|
||||
Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
|
||||
Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
|
||||
@@ -150,3 +157,15 @@ pub fn toggle_mute() {
|
||||
host.engine().send(Command::SetInputMuted { session, muted: !muted });
|
||||
}
|
||||
}
|
||||
|
||||
/// "Deafen" / "Undeafen" in the voice notification, as Mute does.
|
||||
pub fn toggle_deafen() {
|
||||
let Some(host) = HOST.get() else {
|
||||
return;
|
||||
};
|
||||
let (sessions, deafened) =
|
||||
with_foreground(|f| (f.voice_sessions(), f.notice().is_some_and(|n| n.deafened)));
|
||||
for session in sessions {
|
||||
host.engine().send(Command::SetOutputMuted { session, muted: !deafened });
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ use jni::refs::{Global, Reference as _};
|
||||
use jni::sys::{jboolean, jint, jlong};
|
||||
use jni::{Env, JavaVM, jni_sig, jni_str, native_method};
|
||||
|
||||
use crate::foreground::VoiceNotice;
|
||||
use crate::foreground::{ScreenNotice, VoiceNotice};
|
||||
|
||||
static BRIDGE: OnceLock<Global<JClass<'static>>> = OnceLock::new();
|
||||
|
||||
@@ -58,18 +58,56 @@ pub fn request_permissions() -> Result<()> {
|
||||
})
|
||||
}
|
||||
|
||||
/// A Java string, or `null` for `None`.
|
||||
fn string_or_null<'local>(env: &mut Env<'local>, text: Option<&str>) -> Result<JObject<'local>> {
|
||||
Ok(match text {
|
||||
Some(text) => JObject::from(env.new_string(text)?),
|
||||
None => JObject::null(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Show or update the voice service's notification; `None` stops the service.
|
||||
pub fn set_voice_notification(notice: Option<&VoiceNotice>) -> Result<()> {
|
||||
with_bridge(|env, class| {
|
||||
let (text, muted) = match notice {
|
||||
Some(n) => (JObject::from(env.new_string(&n.text)?), n.muted),
|
||||
None => (JObject::null(), false),
|
||||
};
|
||||
let title = string_or_null(env, notice.map(|n| n.title.as_str()))?;
|
||||
let text = string_or_null(env, notice.map(|n| n.text.as_str()))?;
|
||||
let (muted, deafened, since) =
|
||||
notice.map_or((false, false, 0), |n| (n.muted, n.deafened, n.since_ms));
|
||||
env.call_static_method(
|
||||
class,
|
||||
jni_str!("setVoiceNotification"),
|
||||
jni_sig!((text: java.lang.String, muted: jboolean) -> void),
|
||||
&[JValue::Object(&text), JValue::Bool(muted)],
|
||||
jni_sig!(
|
||||
(
|
||||
title: java.lang.String,
|
||||
text: java.lang.String,
|
||||
muted: jboolean,
|
||||
deafened: jboolean,
|
||||
since_ms: jlong,
|
||||
) -> void
|
||||
),
|
||||
&[
|
||||
JValue::Object(&title),
|
||||
JValue::Object(&text),
|
||||
JValue::Bool(muted),
|
||||
JValue::Bool(deafened),
|
||||
JValue::Long(since as jlong),
|
||||
],
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// What the screen-sharing notification says while our stream is live;
|
||||
/// `None` puts back the plain "sharing your screen".
|
||||
pub fn set_screen_notification(notice: Option<&ScreenNotice>) -> Result<()> {
|
||||
with_bridge(|env, class| {
|
||||
let title = string_or_null(env, notice.map(|n| n.title.as_str()))?;
|
||||
let text = string_or_null(env, notice.map(|n| n.text.as_str()))?;
|
||||
env.call_static_method(
|
||||
class,
|
||||
jni_str!("setScreenNotification"),
|
||||
jni_sig!((title: java.lang.String, text: java.lang.String) -> void),
|
||||
&[JValue::Object(&title), JValue::Object(&text)],
|
||||
)?;
|
||||
Ok(())
|
||||
})
|
||||
@@ -368,3 +406,25 @@ fn on_toggle_mute<'local>(_env: &mut Env<'local>, _class: JClass<'local>) -> Res
|
||||
crate::app::toggle_mute();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const _: jni::NativeMethod = native_method! {
|
||||
java_type = "io.github.faumaray.voelin.Native",
|
||||
static extern fn on_toggle_deafen(),
|
||||
};
|
||||
|
||||
/// "Deafen" / "Undeafen" in the voice notification.
|
||||
fn on_toggle_deafen<'local>(_env: &mut Env<'local>, _class: JClass<'local>) -> Result<()> {
|
||||
crate::app::toggle_deafen();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const _: jni::NativeMethod = native_method! {
|
||||
java_type = "io.github.faumaray.voelin.Native",
|
||||
static extern fn on_open_voice(),
|
||||
};
|
||||
|
||||
/// The voice notification was tapped: show the voice channel.
|
||||
fn on_open_voice<'local>(_env: &mut Env<'local>, _class: JClass<'local>) -> Result<()> {
|
||||
voelin_ui::request(voelin_ui::Request::ShowVoice);
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,20 +1,49 @@
|
||||
//! When the app needs its voice foreground service.
|
||||
//! What the app's foreground services show.
|
||||
//!
|
||||
//! Android keeps the microphone and the process of a backgrounded app only
|
||||
//! while a foreground service with a visible notification runs. The service
|
||||
//! runs while any session has a voice connection (connecting or connected);
|
||||
//! its notification names the servers and offers Mute and Disconnect.
|
||||
//! while a foreground service with a visible notification runs. The voice
|
||||
//! service runs while any session has a voice connection (connecting or
|
||||
//! connected); its notification names the channel and the server, counts
|
||||
//! the people there and offers Mute, Deafen and Leave. While our stream is
|
||||
//! live, the screen-sharing service's notification says where it goes and
|
||||
//! how many watch.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use voelin_core::stream::{StreamState, ViewerInfo, ViewerState};
|
||||
use voelin_core::{Event, SessionId, VoiceState};
|
||||
use voelin_model::Presence;
|
||||
|
||||
/// What the voice notification shows.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct VoiceNotice {
|
||||
/// The channel we are in ("Chill Zone"), or what is going on.
|
||||
pub title: String,
|
||||
/// The server and how many are in the channel.
|
||||
pub text: String,
|
||||
/// The microphone is muted in every voice session (the action unmutes).
|
||||
pub muted: bool,
|
||||
/// The sound is off in every voice session (the action turns it on).
|
||||
pub deafened: bool,
|
||||
/// When the first voice session connected (Unix ms), for the elapsed
|
||||
/// time; 0 while connecting.
|
||||
pub since_ms: u64,
|
||||
}
|
||||
|
||||
/// What the screen-sharing notification shows while our stream is live.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct ScreenNotice {
|
||||
pub title: String,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
/// The notifications that changed with an event: `Some(None)` stops the
|
||||
/// voice service, or puts the screen notification back to its default.
|
||||
#[derive(Debug, Default, PartialEq, Eq)]
|
||||
pub struct Changes {
|
||||
pub voice: Option<Option<VoiceNotice>>,
|
||||
pub screen: Option<Option<ScreenNotice>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -27,26 +56,81 @@ struct Session {
|
||||
name: Option<String>,
|
||||
voice: VoiceState,
|
||||
muted: bool,
|
||||
deafened: bool,
|
||||
own_channel: Option<u64>,
|
||||
presence: Arc<Presence>,
|
||||
/// When voice connected (Unix ms).
|
||||
since_ms: Option<u64>,
|
||||
/// Our stream is live.
|
||||
live: bool,
|
||||
/// Viewers connected to our stream.
|
||||
watching: usize,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
fn name(&self) -> &str {
|
||||
self.name.as_deref().unwrap_or("a server")
|
||||
}
|
||||
|
||||
/// Our channel's name and how many are in it.
|
||||
fn channel(&self) -> Option<(&str, usize)> {
|
||||
let id = self.own_channel?;
|
||||
let channel = self.presence.channels.get(&id)?;
|
||||
let people =
|
||||
self.presence.clients.values().filter(|c| c.channel == id && !c.is_query).count();
|
||||
Some((channel.name.as_str(), people))
|
||||
}
|
||||
}
|
||||
|
||||
fn now_ms() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_millis() as u64)
|
||||
}
|
||||
|
||||
impl Foreground {
|
||||
/// Apply `event`. Returns the new notification when it changed:
|
||||
/// `Some(None)` means the service should stop.
|
||||
pub fn update(&mut self, event: &Event) -> Option<Option<VoiceNotice>> {
|
||||
let before = self.notice();
|
||||
/// Apply `event`; the notifications it changed.
|
||||
pub fn update(&mut self, event: &Event) -> Changes {
|
||||
self.update_at(event, now_ms())
|
||||
}
|
||||
|
||||
fn update_at(&mut self, event: &Event, now_ms: u64) -> Changes {
|
||||
let (voice, screen) = (self.notice(), self.screen());
|
||||
match event {
|
||||
Event::State { session, state } => {
|
||||
let s = self.sessions.entry(*session).or_default();
|
||||
s.since_ms = match state.voice {
|
||||
VoiceState::Connected => s.since_ms.or(Some(now_ms)),
|
||||
_ => None,
|
||||
};
|
||||
s.voice = state.voice;
|
||||
s.muted = state.input_muted;
|
||||
s.deafened = state.output_muted;
|
||||
s.own_channel = state.own_channel;
|
||||
}
|
||||
Event::ServerInfo { session, name, .. } => {
|
||||
self.sessions.entry(*session).or_default().name = Some(name.clone());
|
||||
}
|
||||
_ => return None,
|
||||
Event::Presence { session, presence } => {
|
||||
self.sessions.entry(*session).or_default().presence = presence.clone();
|
||||
}
|
||||
Event::StreamState { session, state } => {
|
||||
let s = self.sessions.entry(*session).or_default();
|
||||
s.live = matches!(state, StreamState::Live { .. });
|
||||
if !s.live {
|
||||
s.watching = 0;
|
||||
}
|
||||
}
|
||||
Event::StreamViewers { session, viewers } => {
|
||||
self.sessions.entry(*session).or_default().watching = connected(viewers);
|
||||
}
|
||||
_ => return Changes::default(),
|
||||
}
|
||||
let (voice_after, screen_after) = (self.notice(), self.screen());
|
||||
Changes {
|
||||
voice: (voice_after != voice).then_some(voice_after),
|
||||
screen: (screen_after != screen).then_some(screen_after),
|
||||
}
|
||||
let after = self.notice();
|
||||
(after != before).then_some(after)
|
||||
}
|
||||
|
||||
fn active(&self) -> impl Iterator<Item = (&SessionId, &Session)> {
|
||||
@@ -58,49 +142,101 @@ impl Foreground {
|
||||
self.active().map(|(id, _)| *id).collect()
|
||||
}
|
||||
|
||||
/// The notification, `None` without voice.
|
||||
/// The voice notification, `None` without voice.
|
||||
pub fn notice(&self) -> Option<VoiceNotice> {
|
||||
let active: Vec<&Session> = self.active().map(|(_, s)| s).collect();
|
||||
if active.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let names: Vec<&str> =
|
||||
active.iter().map(|s| s.name.as_deref().unwrap_or("a server")).collect();
|
||||
let names = names.join(", ");
|
||||
let text = if active.iter().all(|s| s.voice == VoiceState::Connecting) {
|
||||
format!("Connecting to {names}")
|
||||
} else {
|
||||
format!("In voice on {names}")
|
||||
let connecting = active.iter().all(|s| s.voice == VoiceState::Connecting);
|
||||
let (title, text) = match active.as_slice() {
|
||||
[] => return None,
|
||||
[one] if connecting => ("Connecting…".to_owned(), one.name().to_owned()),
|
||||
[one] => match one.channel() {
|
||||
Some((channel, 1)) => (channel.to_owned(), format!("{} · just you", one.name())),
|
||||
Some((channel, n)) => {
|
||||
(channel.to_owned(), format!("{} · {n} in voice", one.name()))
|
||||
}
|
||||
None => ("In voice".to_owned(), one.name().to_owned()),
|
||||
},
|
||||
several => (
|
||||
format!("In voice on {} servers", several.len()),
|
||||
several.iter().map(|s| s.name()).collect::<Vec<_>>().join(", "),
|
||||
),
|
||||
};
|
||||
Some(VoiceNotice { text, muted: active.iter().all(|s| s.muted) })
|
||||
Some(VoiceNotice {
|
||||
title,
|
||||
text,
|
||||
muted: active.iter().all(|s| s.muted),
|
||||
deafened: active.iter().all(|s| s.deafened),
|
||||
since_ms: active.iter().filter_map(|s| s.since_ms).min().unwrap_or(0),
|
||||
})
|
||||
}
|
||||
|
||||
/// The screen-sharing notification while our stream is live.
|
||||
pub fn screen(&self) -> Option<ScreenNotice> {
|
||||
let s = self.sessions.values().find(|s| s.live)?;
|
||||
let title = match s.channel() {
|
||||
Some((channel, _)) => format!("Live in {channel}"),
|
||||
None => "Live".to_owned(),
|
||||
};
|
||||
let text = match s.watching {
|
||||
0 => s.name().to_owned(),
|
||||
1 => format!("{} · 1 watching", s.name()),
|
||||
n => format!("{} · {n} watching", s.name()),
|
||||
};
|
||||
Some(ScreenNotice { title, text })
|
||||
}
|
||||
}
|
||||
|
||||
fn connected(viewers: &[ViewerInfo]) -> usize {
|
||||
viewers.iter().filter(|v| v.state == ViewerState::Connected).count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use voelin_core::SessionState;
|
||||
use voelin_model::{Capabilities, ServerFlavor};
|
||||
use voelin_core::stream::EndReason;
|
||||
use voelin_model::{Capabilities, ChannelInfo, ClientInfo, ServerFlavor};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn voice(session: SessionId, voice: VoiceState, input_muted: bool) -> Event {
|
||||
Event::State {
|
||||
session,
|
||||
state: SessionState { voice, input_muted, ..SessionState::default() },
|
||||
state: SessionState {
|
||||
voice,
|
||||
input_muted,
|
||||
own_channel: Some(2),
|
||||
..SessionState::default()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn notice(text: &str, muted: bool) -> Option<Option<VoiceNotice>> {
|
||||
Some(Some(VoiceNotice { text: text.into(), muted }))
|
||||
fn notice(title: &str, text: &str, muted: bool, since_ms: u64) -> Option<Option<VoiceNotice>> {
|
||||
Some(Some(VoiceNotice {
|
||||
title: title.into(),
|
||||
text: text.into(),
|
||||
muted,
|
||||
deafened: false,
|
||||
since_ms,
|
||||
}))
|
||||
}
|
||||
|
||||
fn presence(people: u16) -> Event {
|
||||
let mut p = Presence::default();
|
||||
p.channels
|
||||
.insert(2, ChannelInfo { id: 2, name: "Chill Zone".into(), ..Default::default() });
|
||||
for id in 1..=people {
|
||||
p.clients.insert(id, ClientInfo { id, channel: 2, ..Default::default() });
|
||||
}
|
||||
Event::Presence { session: 1, presence: Arc::new(p) }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn service_follows_voice_connections() {
|
||||
fn the_voice_service_follows_voice_connections() {
|
||||
let mut fg = Foreground::default();
|
||||
assert_eq!(fg.update(&voice(1, VoiceState::Disconnected, false)), None);
|
||||
assert_eq!(fg.update_at(&voice(1, VoiceState::Disconnected, false), 5).voice, None);
|
||||
assert_eq!(
|
||||
fg.update(&voice(1, VoiceState::Connecting, false)),
|
||||
notice("Connecting to a server", false)
|
||||
fg.update_at(&voice(1, VoiceState::Connecting, false), 5).voice,
|
||||
notice("Connecting…", "a server", false, 0)
|
||||
);
|
||||
let info = Event::ServerInfo {
|
||||
session: 1,
|
||||
@@ -108,25 +244,93 @@ mod tests {
|
||||
flavor: ServerFlavor::Unknown(String::new()),
|
||||
capabilities: Capabilities::default(),
|
||||
};
|
||||
assert_eq!(fg.update(&info), notice("Connecting to Home", false));
|
||||
assert_eq!(fg.update_at(&info, 6).voice, notice("Connecting…", "Home", false, 0));
|
||||
assert_eq!(
|
||||
fg.update(&voice(1, VoiceState::Connected, false)),
|
||||
notice("In voice on Home", false)
|
||||
fg.update_at(&voice(1, VoiceState::Connected, false), 7).voice,
|
||||
notice("In voice", "Home", false, 7)
|
||||
);
|
||||
assert_eq!(fg.update(&voice(1, VoiceState::Connected, false)), None, "no change");
|
||||
// The channel and its people, once presence knows them.
|
||||
assert_eq!(
|
||||
fg.update(&voice(1, VoiceState::Connected, true)),
|
||||
notice("In voice on Home", true)
|
||||
fg.update_at(&presence(3), 8).voice,
|
||||
notice("Chill Zone", "Home · 3 in voice", false, 7)
|
||||
);
|
||||
// A second session that is not muted: the action mutes both.
|
||||
assert_eq!(
|
||||
fg.update(&voice(2, VoiceState::Connecting, false)),
|
||||
notice("In voice on Home, a server", false)
|
||||
fg.update_at(&presence(1), 8).voice,
|
||||
notice("Chill Zone", "Home · just you", false, 7)
|
||||
);
|
||||
assert_eq!(
|
||||
fg.update_at(&voice(1, VoiceState::Connected, false), 9).voice,
|
||||
None,
|
||||
"no change"
|
||||
);
|
||||
assert_eq!(
|
||||
fg.update_at(&voice(1, VoiceState::Connected, true), 9).voice,
|
||||
notice("Chill Zone", "Home · just you", true, 7)
|
||||
);
|
||||
// A second session that is not muted: the action mutes both, and
|
||||
// the time counts from the first connection.
|
||||
assert_eq!(
|
||||
fg.update_at(&voice(2, VoiceState::Connected, false), 20).voice,
|
||||
notice("In voice on 2 servers", "Home, a server", false, 7)
|
||||
);
|
||||
assert_eq!(fg.voice_sessions(), [1, 2]);
|
||||
assert_eq!(fg.update(&Event::Error { session: 1, message: "x".into() }), None);
|
||||
fg.update(&voice(2, VoiceState::Disconnected, false));
|
||||
assert_eq!(fg.update(&voice(1, VoiceState::Disconnected, true)), Some(None));
|
||||
assert_eq!(
|
||||
fg.update(&Event::Error { session: 1, message: "x".into() }),
|
||||
Changes::default()
|
||||
);
|
||||
fg.update_at(&voice(2, VoiceState::Disconnected, false), 30);
|
||||
assert_eq!(fg.update_at(&voice(1, VoiceState::Disconnected, true), 30).voice, Some(None));
|
||||
assert!(fg.voice_sessions().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deafen_needs_every_session_deafened() {
|
||||
let mut fg = Foreground::default();
|
||||
let deaf = |session, output_muted| Event::State {
|
||||
session,
|
||||
state: SessionState {
|
||||
voice: VoiceState::Connected,
|
||||
output_muted,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
fg.update_at(&deaf(1, true), 1);
|
||||
assert!(fg.notice().unwrap().deafened);
|
||||
fg.update_at(&deaf(2, false), 1);
|
||||
assert!(!fg.notice().unwrap().deafened);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_screen_notice_says_where_we_are_live() {
|
||||
let mut fg = Foreground::default();
|
||||
fg.update_at(&voice(1, VoiceState::Connected, false), 1);
|
||||
fg.update_at(&presence(2), 1);
|
||||
assert_eq!(fg.screen(), None);
|
||||
// `StreamState::Live` carries a sink only the engine makes.
|
||||
fg.sessions.get_mut(&1).unwrap().live = true;
|
||||
let screen = |title: &str, text: &str| {
|
||||
Some(Some(ScreenNotice { title: title.into(), text: text.into() }))
|
||||
};
|
||||
assert_eq!(fg.screen(), screen("Live in Chill Zone", "a server").flatten());
|
||||
let viewer = |client, state| ViewerInfo {
|
||||
client: tsclientlib::ClientId(client),
|
||||
state,
|
||||
message: String::new(),
|
||||
layer: None,
|
||||
estimate: None,
|
||||
srtp_profile: None,
|
||||
codec: None,
|
||||
};
|
||||
let viewers = Event::StreamViewers {
|
||||
session: 1,
|
||||
viewers: vec![viewer(2, ViewerState::Connected), viewer(3, ViewerState::Requested)],
|
||||
};
|
||||
assert_eq!(
|
||||
fg.update_at(&viewers, 3).screen,
|
||||
screen("Live in Chill Zone", "a server · 1 watching")
|
||||
);
|
||||
let ended =
|
||||
Event::StreamState { session: 1, state: StreamState::Ended(EndReason::Stopped) };
|
||||
assert_eq!(fg.update_at(&ended, 4).screen, Some(None));
|
||||
}
|
||||
}
|
||||
@@ -621,6 +621,8 @@ pub fn run(options: RunOptions) -> Result<()> {
|
||||
});
|
||||
|
||||
let _dev = crate::dev::start(&ui, &switches);
|
||||
// What the platform asked for before the window ran.
|
||||
crate::inbox::take();
|
||||
|
||||
ui.run()?;
|
||||
// With our own engine, leave servers properly, so no ghost client stays
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
//! Requests from the platform around the window: on Android a tapped
|
||||
//! notification. They can come from any thread and before the window runs;
|
||||
//! [`request`] keeps them until the window takes them on its thread.
|
||||
|
||||
use std::sync::{Mutex, PoisonError};
|
||||
|
||||
use slint::ComponentHandle;
|
||||
|
||||
use crate::app::{Nav, Page, with_app};
|
||||
|
||||
/// What the platform asks the window to do.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Request {
|
||||
/// Show the voice channel we are in (the phone's voice screen).
|
||||
ShowVoice,
|
||||
}
|
||||
|
||||
static PENDING: Mutex<Vec<Request>> = Mutex::new(Vec::new());
|
||||
|
||||
/// Hand `request` to the window: at once if it runs, else when it starts.
|
||||
pub fn request(request: Request) {
|
||||
PENDING.lock().unwrap_or_else(PoisonError::into_inner).push(request);
|
||||
// Fails without an event loop yet; `run` takes them then.
|
||||
let _ = slint::invoke_from_event_loop(take);
|
||||
}
|
||||
|
||||
/// Handle what is pending, on the window's thread; kept while there is no
|
||||
/// window.
|
||||
pub(crate) fn take() {
|
||||
let Some(ui) = with_app(|app| app.ui.upgrade()).flatten() else { return };
|
||||
let requests = std::mem::take(&mut *PENDING.lock().unwrap_or_else(PoisonError::into_inner));
|
||||
// Not inside `with_app`: navigating calls back into Rust, which borrows
|
||||
// the app again.
|
||||
let nav = ui.global::<Nav>();
|
||||
for request in requests {
|
||||
match request {
|
||||
Request::ShowVoice => {
|
||||
nav.invoke_show(Page::Server);
|
||||
nav.invoke_show_voice(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ mod dev;
|
||||
mod emoji;
|
||||
mod hotkey;
|
||||
mod images;
|
||||
mod inbox;
|
||||
mod members;
|
||||
mod servers;
|
||||
mod settings;
|
||||
@@ -18,3 +19,4 @@ mod video;
|
||||
mod vm;
|
||||
|
||||
pub use app::{HostedEngine, RunOptions, run};
|
||||
pub use inbox::{Request, request};
|
||||
Reference in new issue
Block a user