voelin-ui: keep the stream chat right when its tab is not shown

Since the chat keeps a session's stored messages as the truth, the main
window builds lines only for the tab it shows, and the stream chat shared
that tab's lines: when the stream's channel was not the shown tab its
chat stood still, and a line pushed by hand (the sample data's go-live
message) was gone at the next refresh. The studio now has its own lines,
made from the destination tab's messages the way the chat view makes
them, and the chat tells it when that tab gets messages. The sample data
posts its go-live message as a stored message, as the engine's echo
would arrive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 11:47:27 +03:00
1 parent 055f74059f
commit fe598e67e8
2 files changed
+84 -40

No files matched your search

+10 -2
View File
@@ -170,6 +170,7 @@ impl App {
if !shown { if !shown {
self.refresh_servers(); self.refresh_servers();
} }
self.studio_chat_changed(id, &message.target);
} }
/// Messages the engine stored: added or updated by their local id. /// Messages the engine stored: added or updated by their local id.
@@ -218,6 +219,7 @@ impl App {
if fresh > 0 && !shown { if fresh > 0 && !shown {
self.refresh_servers(); self.refresh_servers();
} }
self.studio_chat_changed(id, target);
} }
/// The page of messages before the oldest one shown. /// The page of messages before the oldest one shown.
@@ -235,8 +237,14 @@ impl App {
self.refresh_chat(); self.refresh_chat();
} }
/// The lines of a tab, grouped and with everything the row shows. /// The lines of a tab, grouped and with everything the row shows (also
fn lines_of(&self, view: &SessionView, tab: &Tab, messages: &[Msg]) -> Vec<ChatLine> { /// the stream chat of the Stream Studio, studio.rs).
pub(crate) fn lines_of(
&self,
view: &SessionView,
tab: &Tab,
messages: &[Msg],
) -> Vec<ChatLine> {
let gateway = view.gateway_has(feature::PINS) let gateway = view.gateway_has(feature::PINS)
|| view.gateway_has(feature::REACTIONS) || view.gateway_has(feature::REACTIONS)
|| view.gateway_has(feature::TOPICS); || view.gateway_has(feature::TOPICS);
+74 -38
View File
@@ -41,8 +41,9 @@ use voelin_core::studio::scene::{
Align, Background, Colour, Fit, Scene, Scenes, Source, SourceKind, Transform, Align, Background, Colour, Fit, Scene, Scenes, Source, SourceKind, Transform,
}; };
use voelin_core::studio::{self, SourceChange, Stats, Status, Studio, camera}; use voelin_core::studio::{self, SourceChange, Stats, Status, Studio, camera};
use voelin_core::{Command, StreamState}; use voelin_core::{Command, HistoryMessage, HistorySource, StreamState};
use voelin_model::{ChatMessage, ChatTarget}; use voelin_model::{ChatMessage, ChatTarget};
use voelin_store::MessageSource;
use crate::app::{ use crate::app::{
App, ChatLine, Nav, Page, StudioAudio, StudioBridge, StudioForm, StudioLayer, StudioNav, App, ChatLine, Nav, Page, StudioAudio, StudioBridge, StudioForm, StudioLayer, StudioNav,
@@ -110,8 +111,8 @@ struct Models {
viewers: Rc<VecModel<StudioViewer>>, viewers: Rc<VecModel<StudioViewer>>,
destinations: Rc<VecModel<StudioPick>>, destinations: Rc<VecModel<StudioPick>>,
picks: Rc<VecModel<StudioPick>>, picks: Rc<VecModel<StudioPick>>,
/// The chat while there is no destination. /// The destination channel's chat (`studio_sync_chat`).
no_chat: Rc<VecModel<ChatLine>>, chat: Rc<VecModel<ChatLine>>,
} }
/// What a picker row adds. /// What a picker row adds.
@@ -467,7 +468,7 @@ impl App {
bridge.set_viewers(ModelRc::from(m.viewers.clone())); bridge.set_viewers(ModelRc::from(m.viewers.clone()));
bridge.set_destinations(ModelRc::from(m.destinations.clone())); bridge.set_destinations(ModelRc::from(m.destinations.clone()));
bridge.set_picks(ModelRc::from(m.picks.clone())); bridge.set_picks(ModelRc::from(m.picks.clone()));
bridge.set_chat(ModelRc::from(m.no_chat.clone())); bridge.set_chat(ModelRc::from(m.chat.clone()));
} }
/// Whether a studio page or window is on screen. /// Whether a studio page or window is on screen.
@@ -843,37 +844,59 @@ impl App {
Some((id, self.sessions.get(&id)?.state.own_channel?)) Some((id, self.sessions.get(&id)?.state.own_channel?))
} }
/// Show the destination channel's chat: its tab's lines (opened if need /// Show the destination channel's chat (its tab opened, with its
/// be, with its history). /// history, if need be).
fn studio_bind_chat(&mut self) { fn studio_bind_chat(&mut self) {
let (model, name) = match self.studio_chat_target() { let name = match self.studio_chat_target() {
Some((id, channel)) => { Some((id, channel)) => {
let target = ChatTarget::Channel(channel); let target = ChatTarget::Channel(channel);
let demo = self.demo_ui; let demo = self.demo_ui;
let view = self.sessions.entry(id).or_default(); let view = self.sessions.entry(id).or_default();
let name = view.presence.channels.get(&channel).map(|c| c.name.clone()); let name = view.presence.channels.get(&channel).map(|c| c.name.clone());
let name = name.unwrap_or_default(); let name = name.unwrap_or_default();
let index = match view.tabs.iter().position(|t| t.target == target) { if !view.tabs.iter().any(|t| t.target == target) {
Some(i) => i, view.tabs.push(Tab::new(target.clone(), format!("#{name}")));
None => { if !demo {
view.tabs.push(Tab::new(target.clone(), format!("#{name}"))); self.engine.send(Command::OpenChat { session: id as u64, target });
if !demo {
self.engine.send(Command::OpenChat { session: id as u64, target });
}
view.tabs.len() - 1
} }
}; }
let lines = ModelRc::from(view.tabs[index].lines.clone()); name
(lines, name)
} }
None => (ModelRc::from(self.studio.models.no_chat.clone()), String::new()), None => String::new(),
}; };
self.studio_each(|b| { self.studio_each(|b| b.set_chat_name(name.clone().into()));
if b.get_chat() != model { self.studio_sync_chat();
b.set_chat(model.clone()); }
}
b.set_chat_name(name.clone().into()); /// The stream chat's lines: the destination tab's, made as the chat
/// view makes them. The main window keeps only its shown tab's lines up
/// to date, and the stream's channel need not be that tab.
fn studio_sync_chat(&self) {
let tab = self.studio_chat_target().and_then(|(id, channel)| {
let view = self.sessions.get(&id)?;
let tab = view.tabs.iter().find(|t| t.target == ChatTarget::Channel(channel))?;
Some((view, tab))
}); });
let lines: Vec<ChatLine> = match tab {
Some((view, tab)) if view.has_history() => self.lines_of(view, tab, &tab.messages),
// Without stored history the tab's lines are pushed as they come.
Some((_, tab)) => tab.lines.iter().collect(),
None => Vec::new(),
};
vm::list::sync(&self.studio.models.chat, &lines);
}
/// A chat of session `id` changed (chat.rs): follow it if it is the
/// stream's.
pub(crate) fn studio_chat_changed(&mut self, id: i64, target: &ChatTarget) {
if self.studio.run.is_some()
&& let Some((session, channel)) = self.studio_chat_target()
&& session == id
&& *target == ChatTarget::Channel(channel)
{
self.studio_sync_chat();
self.studio_overlays(false);
}
} }
fn studio_refresh_form(&self) { fn studio_refresh_form(&self) {
@@ -1071,16 +1094,10 @@ impl App {
/// The last lines of the stream's chat, for the chat overlay. /// The last lines of the stream's chat, for the chat overlay.
fn studio_chat_tail(&self, lines: usize) -> Option<String> { fn studio_chat_tail(&self, lines: usize) -> Option<String> {
let (id, channel) = self.studio_chat_target()?; let chat = &self.studio.models.chat;
let tab = self let count = chat.row_count();
.sessions
.get(&id)?
.tabs
.iter()
.find(|t| t.target == ChatTarget::Channel(channel))?;
let count = tab.lines.row_count();
let text: Vec<String> = (count.saturating_sub(lines)..count) let text: Vec<String> = (count.saturating_sub(lines)..count)
.filter_map(|i| tab.lines.row_data(i)) .filter_map(|i| chat.row_data(i))
.map(|l| { .map(|l| {
let line = format!("{}: {}", l.author, l.text); let line = format!("{}: {}", l.author, l.text);
match line.char_indices().nth(64) { match line.char_indices().nth(64) {
@@ -1863,19 +1880,38 @@ impl App {
pub(crate) fn studio_send_chat(&mut self, text: String) { pub(crate) fn studio_send_chat(&mut self, text: String) {
let Some((id, channel)) = self.studio_chat_target() else { return }; let Some((id, channel)) = self.studio_chat_target() else { return };
let target = ChatTarget::Channel(channel); let target = ChatTarget::Channel(channel);
// Sample data has no engine to echo the message: shown as it would be.
if self.demo_ui { if self.demo_ui {
let view = self.sessions.entry(id).or_default();
let (client, history) = (view.state.own_client, view.has_history());
let author = self.bookmark(id).map(|b| b.nickname.clone()).unwrap_or_default(); let author = self.bookmark(id).map(|b| b.nickname.clone()).unwrap_or_default();
let ts_ms = chrono::Utc::now().timestamp_millis();
let message = ChatMessage { let message = ChatMessage {
target, target: target.clone(),
author_name: author, author_name: author,
author_uid: None, author_uid: client.map(|c| format!("demo-{c}")),
author_id: None, author_id: client,
text, text,
ts_ms: chrono::Utc::now().timestamp_millis(), ts_ms,
via_relay: false, via_relay: false,
blocked: false, blocked: false,
}; };
self.add_message(id, message); if history {
let stored = HistoryMessage {
// Negative: in memory only.
id: -ts_ms,
message,
source: MessageSource::Voice,
remote_id: None,
topic_id: None,
reactions: Vec::new(),
pinned: false,
rev: 0,
};
self.history_batch(id, &target, vec![stored], HistorySource::Live, false);
} else {
self.add_message(id, message);
}
} else { } else {
self.engine.send(Command::SendChat { session: id as u64, target, text }); self.engine.send(Command::SendChat { session: id as u64, target, text });
} }