feat(friends): friend store, mutual handshake, and Friends UI (phase 3)
Build the friends feature on top of the phase-2 control plane: you can now befriend someone you've connected with and manage a contacts list. - common/friends.rs: a persisted FriendStore in its own friends.toml (kept out of config.toml so a headless --reconfigure can't clobber it, same as identity.key). Friends are keyed by stable control EndpointId; state is PendingOutgoing / PendingIncoming / Accepted. The handshake transitions (on_friend_request → mutual-match detection, on_friend_ accept) are pure and unit-tested. - gui/code.rs: the bootstrap. The GUI host wraps its share code as `pixelpassF1:<control-id>.<ticket>` so a viewer learns the host's stable id; unwrap is lenient, so a bare/CLI ticket still works (no friend offer). The video/streaming path is untouched. - presence service gains an outbound path (unbounded channel → per-msg send tasks) and exposes our control id for wrapping codes. - gui wiring: on connect, the viewer announces itself to the host with a Hello (carrying our display name); the host replies once, so both ends learn each other and an "Add friend" offer appears on the running host/view screens. Incoming requests/accepts/declines fold into the store with desktop notifications. New Friends screen (accept/decline/ remove, edit your display name, see your id) reachable from the menu, which shows a pending-request count. New [gui] display_name setting, seeded from $USER. Verified: friends store + handshake transitions covered by unit tests (7); code wrap/unwrap round-trips (4); the control loopback still passes; the live GUI starts clean with the presence endpoint online. fmt + clippy clean on both features; 41 gui + 8 headless tests pass. The full two-party UX (connect → mutual add → persisted) wants a cross-machine manual check, as usual. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+401
-21
@@ -37,6 +37,7 @@
|
||||
//! dropped and no egui frame is running.
|
||||
|
||||
mod child;
|
||||
mod code;
|
||||
mod presence;
|
||||
mod theme;
|
||||
mod tray;
|
||||
@@ -68,6 +69,8 @@ use winit::window::{Window, WindowAttributes, WindowId};
|
||||
use self::child::{ChildEvent, ChildProc};
|
||||
use self::presence::PresenceHandle;
|
||||
use self::tray::{TrayAction, TrayHandle, TrayStatus};
|
||||
use crate::common::control::ControlMsg;
|
||||
use crate::common::friends::FriendState;
|
||||
|
||||
/// Initial / minimum window size, in logical points. Initial height fits the
|
||||
/// host screen (ticket + Copy + QR + Stop) without needing to scroll on a 1080p
|
||||
@@ -614,6 +617,11 @@ pub fn run(relay: Option<String>) -> anyhow::Result<()> {
|
||||
let names = theme::all_themes().into_iter().map(|t| t.name).collect();
|
||||
let draft = active.clone();
|
||||
|
||||
let friends = crate::common::friends::load().unwrap_or_else(|e| {
|
||||
tracing::warn!("failed to load friends list: {e:#}");
|
||||
Default::default()
|
||||
});
|
||||
|
||||
let state = PixelPassApp {
|
||||
screen: Screen::default(),
|
||||
host: HostState::default(),
|
||||
@@ -632,6 +640,9 @@ pub fn run(relay: Option<String>) -> anyhow::Result<()> {
|
||||
},
|
||||
waker,
|
||||
presence,
|
||||
friends,
|
||||
display_name: gui_settings.display_name,
|
||||
met: Vec::new(),
|
||||
};
|
||||
let mut app = App {
|
||||
state,
|
||||
@@ -688,6 +699,14 @@ fn short_id(id: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// A `Hello` control message carrying our display name — the self-introduction
|
||||
/// a viewer sends the host on connect, and the host's reply.
|
||||
fn control_hello(name: &str) -> ControlMsg {
|
||||
ControlMsg::Hello {
|
||||
name: name.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Fire a desktop notification, on a detached thread so the D-Bus round-trip
|
||||
/// can't stall the egui frame. Best-effort: with no notification daemon it
|
||||
/// just does nothing. (notify-rust talks D-Bus via pure-Rust zbus, so this
|
||||
@@ -724,6 +743,14 @@ fn persist_show_qr(value: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
fn persist_display_name(value: &str) {
|
||||
let mut cfg = crate::common::config::load().unwrap_or_default();
|
||||
cfg.gui.display_name = value.to_string();
|
||||
if let Err(e) = crate::common::config::save(&cfg) {
|
||||
tracing::warn!("failed to save settings: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
fn persist_theme(name: &str) {
|
||||
let mut cfg = crate::common::config::load().unwrap_or_default();
|
||||
cfg.gui.theme = name.to_string();
|
||||
@@ -739,6 +766,7 @@ enum Screen {
|
||||
Menu,
|
||||
Host,
|
||||
Viewer,
|
||||
Friends,
|
||||
Settings,
|
||||
Shortcuts,
|
||||
}
|
||||
@@ -813,7 +841,13 @@ struct HostState {
|
||||
window: bool,
|
||||
// running session + accumulated live state
|
||||
proc: Option<ChildProc>,
|
||||
/// The bare video ticket from the child (used for the host-id fingerprint
|
||||
/// line). The copy/QR/display use [`HostState::share_code`] instead.
|
||||
ticket: Option<String>,
|
||||
/// The share code shown/copied/QR'd: the ticket wrapped with our control id
|
||||
/// (see [`code::wrap`]) when the presence service is up, else the bare
|
||||
/// ticket. Wrapping is what lets a viewer offer to befriend the host.
|
||||
share_code: Option<String>,
|
||||
info: Option<HostInfo>,
|
||||
active: u32,
|
||||
max: u32,
|
||||
@@ -855,6 +889,10 @@ struct ViewerState {
|
||||
/// Short endpoint id we're dialing, decoded from the ticket at Connect.
|
||||
/// Shown in the "Connecting to …" line so a dead host is identifiable.
|
||||
connecting_to: Option<String>,
|
||||
/// The host's stable control id, if the pasted code was a wrapped friend
|
||||
/// code. Lets us announce ourselves to the host (so both ends can befriend)
|
||||
/// once connected. `None` for a bare/CLI ticket.
|
||||
host_control_id: Option<iroh::EndpointId>,
|
||||
/// Set when the View screen opens so the code field grabs focus once
|
||||
/// (cleared on use, so it doesn't steal focus every frame).
|
||||
focus_ticket: bool,
|
||||
@@ -890,6 +928,20 @@ struct PixelPassApp {
|
||||
/// if it couldn't start (no identity), in which case friends features are
|
||||
/// simply absent.
|
||||
presence: Option<PresenceHandle>,
|
||||
/// The persisted friends list (mutual-consent contacts).
|
||||
friends: crate::common::friends::FriendStore,
|
||||
/// Our display name, shown to friends. Persisted in `[gui] display_name`.
|
||||
display_name: String,
|
||||
/// Peers met this session (over a connection) who aren't yet in the friends
|
||||
/// list — drives the "add friend" offer. Session-scoped, not persisted.
|
||||
met: Vec<MetPeer>,
|
||||
}
|
||||
|
||||
/// A peer encountered this session but not yet befriended.
|
||||
struct MetPeer {
|
||||
id: iroh::EndpointId,
|
||||
/// Their reported display name (a short id placeholder until a name arrives).
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// The active theme plus the Settings picker/editor working state.
|
||||
@@ -970,16 +1022,290 @@ impl PixelPassApp {
|
||||
self.sync_tray_status();
|
||||
}
|
||||
|
||||
/// Drain inbound control-plane messages. Phase 3 turns these into friend-list
|
||||
/// state, in-app notifications, and the bell badge; for now they're logged so
|
||||
/// the control plane is observable end-to-end.
|
||||
/// Drain inbound control-plane messages and fold them into the friends list
|
||||
/// and the session's met-peers, queueing any replies. Collected up front so
|
||||
/// the presence borrow is released before we mutate `self` / re-borrow it to
|
||||
/// send. (Phase 4 adds the bell badge + ShareCode handling.)
|
||||
fn pump_presence_events(&mut self) {
|
||||
let Some(presence) = &self.presence else {
|
||||
let Some(inbound) = self.presence.as_ref().map(|p| p.drain()) else {
|
||||
return;
|
||||
};
|
||||
for inbound in presence.drain() {
|
||||
tracing::info!(from = %inbound.from, msg = ?inbound.msg, "presence: control message");
|
||||
if inbound.is_empty() {
|
||||
return;
|
||||
}
|
||||
let my_name = self.display_name.clone();
|
||||
let mut outbox: Vec<(iroh::EndpointId, ControlMsg)> = Vec::new();
|
||||
let mut store_changed = false;
|
||||
|
||||
for inb in inbound {
|
||||
let from = inb.from;
|
||||
match inb.msg {
|
||||
ControlMsg::Hello { name } => {
|
||||
// A peer announcing themselves (the viewer→host intro, or the
|
||||
// host's reply). Keep a known friend's name fresh; otherwise
|
||||
// record them as a met peer and reply once on first contact.
|
||||
if let Some(f) = self.friends.find_mut(&from) {
|
||||
f.name = name;
|
||||
store_changed = true;
|
||||
} else if self.note_met(from, name) {
|
||||
outbox.push((from, control_hello(&my_name)));
|
||||
}
|
||||
}
|
||||
ControlMsg::FriendRequest { name } => {
|
||||
self.note_met(from, name.clone());
|
||||
if self.friends.on_friend_request(from, name.clone()) {
|
||||
// We'd already requested them — mutual, so it's settled.
|
||||
outbox.push((
|
||||
from,
|
||||
ControlMsg::FriendAccept {
|
||||
name: my_name.clone(),
|
||||
},
|
||||
));
|
||||
notify(
|
||||
"PixelPass — now friends",
|
||||
format!("You and {name} are now friends."),
|
||||
);
|
||||
} else {
|
||||
notify(
|
||||
"PixelPass — friend request",
|
||||
format!("{name} wants to be friends."),
|
||||
);
|
||||
}
|
||||
store_changed = true;
|
||||
}
|
||||
ControlMsg::FriendAccept { name } => {
|
||||
if self.friends.on_friend_accept(from, name.clone()) {
|
||||
store_changed = true;
|
||||
notify(
|
||||
"PixelPass — request accepted",
|
||||
format!("{name} accepted your friend request."),
|
||||
);
|
||||
}
|
||||
}
|
||||
ControlMsg::FriendDecline => {
|
||||
if self.friends.remove(&from) {
|
||||
store_changed = true;
|
||||
}
|
||||
}
|
||||
ControlMsg::ShareCode { name, ticket } => {
|
||||
// Phase 4 turns this into the bell badge + an in-app notice.
|
||||
// For now, only honour codes from accepted friends and log.
|
||||
if self.friends.is_accepted(&from) {
|
||||
tracing::info!(from = %from, %name, "presence: friend shared a code: {ticket}");
|
||||
} else {
|
||||
tracing::warn!(from = %from, "presence: ignoring ShareCode from a non-friend");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if store_changed {
|
||||
self.save_friends();
|
||||
}
|
||||
if let Some(p) = &self.presence {
|
||||
for (peer, msg) in outbox {
|
||||
p.send(peer, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Record a peer met this session. Returns true if they were newly added
|
||||
/// (false if we already knew them, in which case the name is refreshed).
|
||||
fn note_met(&mut self, id: iroh::EndpointId, name: String) -> bool {
|
||||
if let Some(m) = self.met.iter_mut().find(|m| m.id == id) {
|
||||
// Don't overwrite a real name with a short-id placeholder.
|
||||
if !name.is_empty() {
|
||||
m.name = name;
|
||||
}
|
||||
false
|
||||
} else {
|
||||
self.met.push(MetPeer { id, name });
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn pending_incoming_count(&self) -> usize {
|
||||
self.friends
|
||||
.friends
|
||||
.iter()
|
||||
.filter(|f| f.state == FriendState::PendingIncoming)
|
||||
.count()
|
||||
}
|
||||
|
||||
fn save_friends(&self) {
|
||||
if let Err(e) = self.friends.save() {
|
||||
tracing::warn!("failed to save friends list: {e:#}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a friend request to a peer (or accept theirs if they already asked),
|
||||
/// persisting the new state and notifying the peer over the control plane.
|
||||
fn request_friend(&mut self, id: iroh::EndpointId, name: String) {
|
||||
let my_name = self.display_name.clone();
|
||||
let msg = if matches!(
|
||||
self.friends.find(&id).map(|f| f.state),
|
||||
Some(FriendState::PendingIncoming)
|
||||
) {
|
||||
self.friends.upsert(id, name, FriendState::Accepted);
|
||||
ControlMsg::FriendAccept { name: my_name }
|
||||
} else {
|
||||
self.friends.upsert(id, name, FriendState::PendingOutgoing);
|
||||
ControlMsg::FriendRequest { name: my_name }
|
||||
};
|
||||
self.save_friends();
|
||||
if let Some(p) = &self.presence {
|
||||
p.send(id, msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark an incoming request accepted and tell the peer.
|
||||
fn accept_friend(&mut self, id: iroh::EndpointId) {
|
||||
let my_name = self.display_name.clone();
|
||||
if let Some(f) = self.friends.find_mut(&id) {
|
||||
f.state = FriendState::Accepted;
|
||||
self.save_friends();
|
||||
if let Some(p) = &self.presence {
|
||||
p.send(id, ControlMsg::FriendAccept { name: my_name });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a friend / decline a request / cancel an outgoing one, telling the
|
||||
/// peer so their side drops us too.
|
||||
fn remove_friend(&mut self, id: iroh::EndpointId) {
|
||||
if self.friends.remove(&id) {
|
||||
self.save_friends();
|
||||
if let Some(p) = &self.presence {
|
||||
p.send(id, ControlMsg::FriendDecline);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The "people you just connected with" offer shown on the running host /
|
||||
/// viewer screens: met peers not yet in the friends list, each with an Add
|
||||
/// button.
|
||||
fn friend_offers(&mut self, ui: &mut egui::Ui) {
|
||||
let offers: Vec<(iroh::EndpointId, String)> = self
|
||||
.met
|
||||
.iter()
|
||||
.filter(|m| self.friends.find(&m.id).is_none())
|
||||
.map(|m| (m.id, m.name.clone()))
|
||||
.collect();
|
||||
if offers.is_empty() {
|
||||
return;
|
||||
}
|
||||
ui.add_space(12.0);
|
||||
ui.separator();
|
||||
ui.label("People you just connected with:");
|
||||
let mut add: Option<(iroh::EndpointId, String)> = None;
|
||||
for (id, name) in &offers {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(name.as_str());
|
||||
if ui.small_button("➕ Add friend").clicked() {
|
||||
add = Some((*id, name.clone()));
|
||||
}
|
||||
});
|
||||
}
|
||||
if let Some((id, name)) = add {
|
||||
self.request_friend(id, name);
|
||||
}
|
||||
}
|
||||
|
||||
fn friends_screen(&mut self, ui: &mut egui::Ui) {
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("← Menu").clicked() {
|
||||
self.screen = Screen::Menu;
|
||||
}
|
||||
ui.heading("Friends");
|
||||
});
|
||||
ui.separator();
|
||||
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
// Your identity: editable display name + your stable id.
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Your name");
|
||||
if ui
|
||||
.text_edit_singleline(&mut self.display_name)
|
||||
.on_hover_text("Shown to friends in requests and shared codes.")
|
||||
.changed()
|
||||
{
|
||||
persist_display_name(&self.display_name);
|
||||
}
|
||||
});
|
||||
if let Some(p) = &self.presence {
|
||||
ui.label(
|
||||
egui::RichText::new(format!("Your ID: {}", short_id(&p.id().to_string())))
|
||||
.small()
|
||||
.weak(),
|
||||
);
|
||||
} else {
|
||||
ui.colored_label(
|
||||
self.theme.active.warning,
|
||||
"⚠ Friends service unavailable (no identity).",
|
||||
);
|
||||
}
|
||||
|
||||
ui.add_space(8.0);
|
||||
ui.separator();
|
||||
ui.add_space(4.0);
|
||||
|
||||
if self.friends.friends.is_empty() {
|
||||
ui.label(
|
||||
egui::RichText::new(
|
||||
"No friends yet. Connect with someone, then use \"Add friend\" \
|
||||
on the host/view screen.",
|
||||
)
|
||||
.weak(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Collect the chosen action first so we don't mutate the store while
|
||||
// iterating it.
|
||||
enum Action {
|
||||
Accept(iroh::EndpointId),
|
||||
Remove(iroh::EndpointId),
|
||||
}
|
||||
let mut action: Option<Action> = None;
|
||||
for f in &self.friends.friends {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label(egui::RichText::new(&f.name).strong());
|
||||
ui.label(
|
||||
egui::RichText::new(format!("· {}", short_id(&f.id.to_string())))
|
||||
.small()
|
||||
.weak(),
|
||||
);
|
||||
match f.state {
|
||||
FriendState::Accepted => {
|
||||
ui.label(egui::RichText::new("· friend").small().weak());
|
||||
if ui.small_button("Remove").clicked() {
|
||||
action = Some(Action::Remove(f.id));
|
||||
}
|
||||
}
|
||||
FriendState::PendingIncoming => {
|
||||
ui.label(egui::RichText::new("· wants to be friends").small().weak());
|
||||
if ui.small_button("Accept").clicked() {
|
||||
action = Some(Action::Accept(f.id));
|
||||
}
|
||||
if ui.small_button("Decline").clicked() {
|
||||
action = Some(Action::Remove(f.id));
|
||||
}
|
||||
}
|
||||
FriendState::PendingOutgoing => {
|
||||
ui.label(egui::RichText::new("· request sent").small().weak());
|
||||
if ui.small_button("Cancel").clicked() {
|
||||
action = Some(Action::Remove(f.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
match action {
|
||||
Some(Action::Accept(id)) => self.accept_friend(id),
|
||||
Some(Action::Remove(id)) => self.remove_friend(id),
|
||||
None => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Render the current screen. Called from inside the egui frame.
|
||||
@@ -1018,6 +1344,7 @@ impl PixelPassApp {
|
||||
Screen::Menu => self.menu(ui),
|
||||
Screen::Host => self.host(ui),
|
||||
Screen::Viewer => self.viewer(ui),
|
||||
Screen::Friends => self.friends_screen(ui),
|
||||
Screen::Settings => self.settings(ui),
|
||||
Screen::Shortcuts => self.shortcuts(ui),
|
||||
}
|
||||
@@ -1051,7 +1378,9 @@ impl PixelPassApp {
|
||||
self.screen = Screen::Menu;
|
||||
}
|
||||
Screen::Settings if self.theme.editing => self.cancel_theme_edit(),
|
||||
Screen::Settings | Screen::Shortcuts => self.screen = Screen::Menu,
|
||||
Screen::Settings | Screen::Shortcuts | Screen::Friends => {
|
||||
self.screen = Screen::Menu
|
||||
}
|
||||
Screen::Menu => {}
|
||||
}
|
||||
return;
|
||||
@@ -1076,16 +1405,16 @@ impl PixelPassApp {
|
||||
Screen::Host => {
|
||||
if self.host.proc.is_some() {
|
||||
if key(Key::C)
|
||||
&& let Some(ticket) = self.host.ticket.clone()
|
||||
&& let Some(code) = self.host.share_code.clone()
|
||||
{
|
||||
self.copy_to_clipboard(&ticket);
|
||||
self.copy_to_clipboard(&code);
|
||||
}
|
||||
} else if key(Key::Space) || key(Key::Enter) {
|
||||
self.start_host();
|
||||
}
|
||||
}
|
||||
// View's Enter (Connect) is handled in viewer_form.
|
||||
Screen::Viewer | Screen::Settings | Screen::Shortcuts => {}
|
||||
Screen::Viewer | Screen::Friends | Screen::Settings | Screen::Shortcuts => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1179,6 +1508,16 @@ impl PixelPassApp {
|
||||
self.prefill_viewer_ticket();
|
||||
}
|
||||
ui.add_space(20.0);
|
||||
let pending = self.pending_incoming_count();
|
||||
let friends_label = if pending > 0 {
|
||||
format!("👥 Friends ({pending})")
|
||||
} else {
|
||||
"👥 Friends".to_string()
|
||||
};
|
||||
if ui.button(friends_label).clicked() {
|
||||
self.screen = Screen::Friends;
|
||||
}
|
||||
ui.add_space(8.0);
|
||||
if ui.button("⚙ Settings").clicked() {
|
||||
// Refresh the picker so themes added to the folder since launch
|
||||
// (or last visit) show up without a restart.
|
||||
@@ -1554,9 +1893,9 @@ impl PixelPassApp {
|
||||
|
||||
ui.add_space(12.0);
|
||||
|
||||
if let Some(ticket) = self.host.ticket.clone() {
|
||||
if let Some(share_code) = self.host.share_code.clone() {
|
||||
ui.label("Share this code with your viewer(s):");
|
||||
if let Some(id) = ticket_endpoint_id(&ticket) {
|
||||
if let Some(id) = self.host.ticket.as_deref().and_then(ticket_endpoint_id) {
|
||||
// The viewer shows "Connecting to <id>…" with this same
|
||||
// truncation, so the two ends can be eyeballed for a match.
|
||||
ui.label(
|
||||
@@ -1568,7 +1907,7 @@ impl PixelPassApp {
|
||||
ui.add_space(4.0);
|
||||
egui::Frame::group(ui.style()).show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::Label::new(egui::RichText::new(&ticket).monospace().small())
|
||||
egui::Label::new(egui::RichText::new(&share_code).monospace().small())
|
||||
.wrap()
|
||||
.selectable(true),
|
||||
);
|
||||
@@ -1576,7 +1915,7 @@ impl PixelPassApp {
|
||||
ui.add_space(4.0);
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("📋 Copy code").clicked() {
|
||||
self.copy_to_clipboard(&ticket);
|
||||
self.copy_to_clipboard(&share_code);
|
||||
}
|
||||
if self.host.copied {
|
||||
ui.colored_label(self.theme.active.success, "✓ Copied to clipboard");
|
||||
@@ -1593,13 +1932,13 @@ impl PixelPassApp {
|
||||
}
|
||||
|
||||
// Lazy QR build: first draw after a Ticket event has `qr_texture =
|
||||
// None`, so we encode the ticket and load the texture once. The
|
||||
// None`, so we encode the share code and load the texture once. The
|
||||
// 4-module quiet zone (white border) matters — phone scanners reject
|
||||
// QR codes flush against a non-white edge. Skipped when the user
|
||||
// disabled the QR panel in Settings.
|
||||
if self.show_qr
|
||||
&& self.host.qr_texture.is_none()
|
||||
&& let Ok(code) = qrcode::QrCode::new(ticket.as_bytes())
|
||||
&& let Ok(code) = qrcode::QrCode::new(share_code.as_bytes())
|
||||
{
|
||||
let w = code.width();
|
||||
let quiet = 4;
|
||||
@@ -1633,6 +1972,8 @@ impl PixelPassApp {
|
||||
ui.colored_label(self.theme.active.warning, format!("⚠ {reason}"));
|
||||
}
|
||||
|
||||
self.friend_offers(ui);
|
||||
|
||||
ui.add_space(16.0);
|
||||
if ui
|
||||
.add_sized([140.0, 36.0], egui::Button::new("Stop hosting"))
|
||||
@@ -1646,6 +1987,7 @@ impl PixelPassApp {
|
||||
self.host.error = None;
|
||||
self.host.last_refusal = None;
|
||||
self.host.ticket = None;
|
||||
self.host.share_code = None;
|
||||
self.host.info = None;
|
||||
self.host.active = 0;
|
||||
self.host.max = 0;
|
||||
@@ -1653,6 +1995,7 @@ impl PixelPassApp {
|
||||
self.host.copied = false;
|
||||
self.host.viewers.clear();
|
||||
self.host.qr_texture = None;
|
||||
self.met.clear();
|
||||
|
||||
let mut args = vec![
|
||||
"--host".to_string(),
|
||||
@@ -1687,9 +2030,11 @@ impl PixelPassApp {
|
||||
self.host.proc = None;
|
||||
self.host.capturing = false;
|
||||
self.host.ticket = None;
|
||||
self.host.share_code = None;
|
||||
self.host.copied = false;
|
||||
self.host.viewers.clear();
|
||||
self.host.qr_texture = None;
|
||||
self.met.clear();
|
||||
}
|
||||
|
||||
/// Drain the host child's event channel into state, and detect an
|
||||
@@ -1723,13 +2068,21 @@ impl PixelPassApp {
|
||||
fn apply_host_event(&mut self, ev: ChildEvent) {
|
||||
match ev {
|
||||
ChildEvent::Ticket { value } => {
|
||||
// Wrap the bare ticket with our stable control id so a viewer
|
||||
// learns who to befriend (see code::wrap). Falls back to the
|
||||
// bare ticket if the presence service isn't up.
|
||||
let share_code = match &self.presence {
|
||||
Some(p) => code::wrap(p.id(), &value),
|
||||
None => value.clone(),
|
||||
};
|
||||
// Auto-copy on arrival, mirroring the CLI/interactive host
|
||||
// (which copies the ticket and prints "copied to your
|
||||
// clipboard"). A failure here is non-fatal: the ticket stays
|
||||
// clipboard"). A failure here is non-fatal: the code stays
|
||||
// visible for manual copy, and `copied` stays false so the UI
|
||||
// doesn't falsely claim success.
|
||||
self.host.copied = set_clipboard(&value);
|
||||
self.host.copied = set_clipboard(&share_code);
|
||||
self.host.ticket = Some(value);
|
||||
self.host.share_code = Some(share_code);
|
||||
}
|
||||
ChildEvent::HostInfo {
|
||||
display_server,
|
||||
@@ -1855,9 +2208,11 @@ impl PixelPassApp {
|
||||
|
||||
// Decode the pasted code live: confirms it's a real ticket and shows
|
||||
// which host it points at, so a stale clipboard paste is caught here
|
||||
// instead of after the 15s connect timeout.
|
||||
// instead of after the 15s connect timeout. Unwrap first so a wrapped
|
||||
// friend code decodes to its underlying ticket.
|
||||
let trimmed = self.viewer.ticket_input.trim().to_string();
|
||||
let decoded_id = ticket_endpoint_id(&trimmed);
|
||||
let (host_ctrl, bare_ticket) = code::unwrap(&trimmed);
|
||||
let decoded_id = ticket_endpoint_id(&bare_ticket);
|
||||
if !trimmed.is_empty() {
|
||||
ui.add_space(4.0);
|
||||
match &decoded_id {
|
||||
@@ -1870,6 +2225,13 @@ impl PixelPassApp {
|
||||
"⚠ This doesn't look like a share code.",
|
||||
),
|
||||
};
|
||||
if host_ctrl.is_some() {
|
||||
ui.label(
|
||||
egui::RichText::new("This host can be added as a friend after you connect.")
|
||||
.small()
|
||||
.weak(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
ui.add_space(10.0);
|
||||
@@ -1915,6 +2277,8 @@ impl PixelPassApp {
|
||||
ui.colored_label(self.theme.active.waiting, msg);
|
||||
}
|
||||
|
||||
self.friend_offers(ui);
|
||||
|
||||
ui.add_space(16.0);
|
||||
if ui
|
||||
.add_sized([140.0, 36.0], egui::Button::new("Disconnect"))
|
||||
@@ -1946,7 +2310,11 @@ impl PixelPassApp {
|
||||
self.viewer.url = None;
|
||||
self.viewer.launched = false;
|
||||
|
||||
let ticket = self.viewer.ticket_input.trim().to_string();
|
||||
// Unwrap a wrapped friend code into (host control id, bare ticket). The
|
||||
// child only ever sees the bare ticket; the control id is kept so we can
|
||||
// announce ourselves to the host once connected.
|
||||
let (host_ctrl, ticket) = code::unwrap(&self.viewer.ticket_input);
|
||||
self.viewer.host_control_id = host_ctrl;
|
||||
self.viewer.connecting_to = ticket_endpoint_id(&ticket).map(|id| short_id(&id));
|
||||
let mut args = vec![ticket, "--output".to_string(), "json".to_string()];
|
||||
if let Some(relay) = &self.relay {
|
||||
@@ -1964,6 +2332,8 @@ impl PixelPassApp {
|
||||
self.viewer.url = None;
|
||||
self.viewer.launched = false;
|
||||
self.viewer.connecting_to = None;
|
||||
self.viewer.host_control_id = None;
|
||||
self.met.clear();
|
||||
}
|
||||
|
||||
fn pump_viewer_events(&mut self) {
|
||||
@@ -1980,6 +2350,16 @@ impl PixelPassApp {
|
||||
Err(e) => self.viewer.error = Some(format!("Couldn't launch player: {e}")),
|
||||
}
|
||||
}
|
||||
// If this was a wrapped friend code, announce ourselves to the
|
||||
// host over the control plane so both ends can offer to befriend
|
||||
// each other. We record the host as a met peer up front (name
|
||||
// filled in when the host's Hello reply arrives).
|
||||
if let Some(host_id) = self.viewer.host_control_id {
|
||||
self.note_met(host_id, short_id(&host_id.to_string()));
|
||||
if let Some(p) = &self.presence {
|
||||
p.send(host_id, control_hello(&self.display_name));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user