bevy_iroh 0.4.0

Share Bevy entities between peers over iroh: replication, rooms, presence and typed messages, with optional voice and video
Documentation
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! Ready-made Bevy UI for voice and video. Behind the `ui` feature.
//!
//! Spawn a [`MediaPanel`] and you have a corner of the screen with a mute button, a meter
//! that moves when the microphone hears you, and a "Devices" button that opens pickers for
//! the microphone, the speaker and, if asked, the camera. Put a [`VoiceIndicator`] on any
//! entity that carries a [`VoiceLevel`] and a small green bar floats over it, rising with the
//! voice. Each piece is its own component, so a different layout is a matter of spawning them
//! where you want them.
//!
//! Plain Bevy UI, no theme: a dark panel and a green that is the same green everywhere, so
//! the bar in your corner and the bar over somebody's head read as one instrument.

use bevy::{picking::Pickable, prelude::*};

use super::{
    AudioDevices, CameraChoice, CameraDevices, MediaSettings, MicLevel, MicrophoneChoice,
    Permission, SpeakerChoice, Voice, VoiceLevel,
};
use crate::replicate::Remote;

/// The green every meter here is drawn in.
pub const METER_GREEN: Color = Color::srgb(0.16, 0.95, 0.24);
const PANEL: Color = Color::srgba(0.08, 0.09, 0.11, 0.92);
const TRACK: Color = Color::srgb(0.16, 0.16, 0.18);
const ROW: Color = Color::srgba(1.0, 1.0, 1.0, 0.04);
const ROW_HOVER: Color = Color::srgba(1.0, 1.0, 1.0, 0.10);
const ROW_SELECTED: Color = Color::srgba(0.16, 0.95, 0.24, 0.18);
const TEXT: Color = Color::srgb(0.92, 0.92, 0.94);
const TEXT_DIM: Color = Color::srgb(0.6, 0.6, 0.65);
const MUTED: Color = Color::srgb(0.9, 0.35, 0.3);

/// Registers the systems behind every widget here. Needs `DefaultPlugins` (or the UI and
/// picking plugins) and `IrohPlugin` with the `media` feature.
pub struct MediaUiPlugin;

impl Plugin for MediaUiPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(
            Update,
            (
                build_panels,
                build_meters,
                build_mute_buttons,
                build_pickers,
                toggle_panels,
                mute_clicks,
                picker_clicks,
                row_hover,
                sync_meters,
                sync_mute_buttons,
                build_indicators,
                sync_indicators,
            ),
        );
    }
}

// -- the corner panel ----------------------------------------------------------------------

/// A whole corner: mute button, microphone meter, and a button that opens the device pickers.
/// Spawn it and nothing else; it positions itself top right.
#[derive(Component, Debug, Clone)]
pub struct MediaPanel {
    /// Also offer a camera picker.
    pub video: bool,
}

impl MediaPanel {
    pub fn voice() -> Self {
        Self { video: false }
    }

    pub fn video() -> Self {
        Self { video: true }
    }
}

/// The button on a panel that shows and hides its pickers.
#[derive(Component)]
struct DevicesButton(Entity);

/// The pickers a panel opens.
#[derive(Component)]
struct PickerDrawer;

fn build_panels(mut commands: Commands, panels: Query<(Entity, &MediaPanel), Added<MediaPanel>>) {
    for (entity, panel) in &panels {
        let mut drawer = Entity::PLACEHOLDER;
        commands
            .entity(entity)
            .insert((
                Node {
                    position_type: PositionType::Absolute,
                    top: Val::Px(10.0),
                    right: Val::Px(10.0),
                    flex_direction: FlexDirection::Column,
                    align_items: AlignItems::FlexEnd,
                    row_gap: Val::Px(6.0),
                    ..default()
                },
                Pickable::IGNORE,
            ))
            .with_children(|parent| {
                // The strip: [mute] [meter] [devices]
                let strip = parent
                    .spawn((
                        Node {
                            flex_direction: FlexDirection::Row,
                            align_items: AlignItems::Center,
                            column_gap: Val::Px(8.0),
                            padding: UiRect::axes(Val::Px(10.0), Val::Px(6.0)),
                            border_radius: BorderRadius::all(Val::Px(6.0)),
                            ..default()
                        },
                        BackgroundColor(PANEL),
                    ))
                    .id();
                drawer = parent
                    .spawn((
                        PickerDrawer,
                        Node {
                            display: Display::None,
                            flex_direction: FlexDirection::Column,
                            row_gap: Val::Px(8.0),
                            padding: UiRect::all(Val::Px(10.0)),
                            width: Val::Px(320.0),
                            border_radius: BorderRadius::all(Val::Px(6.0)),
                            ..default()
                        },
                        BackgroundColor(PANEL),
                    ))
                    .with_children(|drawer| {
                        drawer.spawn(DevicePicker::microphone());
                        drawer.spawn(DevicePicker::speaker());
                        if panel.video {
                            drawer.spawn(DevicePicker::camera());
                        }
                    })
                    .id();
                parent.commands().entity(strip).with_children(|strip| {
                    strip.spawn(MuteButton);
                    strip.spawn(MicMeter::default());
                    strip
                        .spawn((
                            DevicesButton(drawer),
                            Button,
                            Node {
                                padding: UiRect::axes(Val::Px(8.0), Val::Px(3.0)),
                                border_radius: BorderRadius::all(Val::Px(4.0)),
                                ..default()
                            },
                            BackgroundColor(ROW),
                        ))
                        .with_child((
                            Text::new("Devices"),
                            TextFont::from_font_size(13.0),
                            TextColor(TEXT),
                        ));
                });
            });
        let _ = drawer;
    }
}

fn toggle_panels(
    buttons: Query<(&Interaction, &DevicesButton), Changed<Interaction>>,
    mut drawers: Query<&mut Node, With<PickerDrawer>>,
) {
    for (interaction, button) in &buttons {
        if *interaction != Interaction::Pressed {
            continue;
        }
        if let Ok(mut node) = drawers.get_mut(button.0) {
            node.display = if node.display == Display::None {
                Display::Flex
            } else {
                Display::None
            };
        }
    }
}

// -- the microphone meter ------------------------------------------------------------------

/// A vertical bar that fills with [`MicLevel`]: green from the bottom, on a dark track.
#[derive(Component, Debug, Clone)]
pub struct MicMeter {
    pub width: f32,
    pub height: f32,
}

impl Default for MicMeter {
    fn default() -> Self {
        Self {
            width: 6.0,
            height: 22.0,
        }
    }
}

#[derive(Component)]
struct MeterFill;

fn build_meters(mut commands: Commands, meters: Query<(Entity, &MicMeter), Added<MicMeter>>) {
    for (entity, meter) in &meters {
        commands
            .entity(entity)
            .insert((
                Node {
                    width: Val::Px(meter.width),
                    height: Val::Px(meter.height),
                    flex_direction: FlexDirection::Column,
                    justify_content: JustifyContent::FlexEnd,
                    flex_shrink: 0.0,
                    border_radius: BorderRadius::all(Val::Px(2.0)),
                    ..default()
                },
                BackgroundColor(TRACK),
            ))
            .with_child((
                MeterFill,
                Node {
                    width: Val::Percent(100.0),
                    height: Val::Percent(0.0),
                    border_radius: BorderRadius::all(Val::Px(2.0)),
                    ..default()
                },
                BackgroundColor(METER_GREEN),
            ));
    }
}

/// Whole percent steps, so a hovering level does not relayout the bar over sub-pixel
/// differences every frame.
fn sync_meters(level: Res<MicLevel>, mut fills: Query<&mut Node, With<MeterFill>>) {
    let wanted = Val::Percent((level.0 * 100.0).round().clamp(0.0, 100.0));
    for mut fill in &mut fills {
        if fill.height != wanted {
            fill.height = wanted;
        }
    }
}

// -- mute ----------------------------------------------------------------------------------

/// A button that mutes and unmutes every local [`Voice`]. Reads "Mic on" or "Muted".
#[derive(Component, Debug, Clone, Default)]
pub struct MuteButton;

#[derive(Component)]
struct MuteLabel;

fn build_mute_buttons(mut commands: Commands, buttons: Query<Entity, Added<MuteButton>>) {
    for entity in &buttons {
        commands
            .entity(entity)
            .insert((
                Button,
                Node {
                    padding: UiRect::axes(Val::Px(8.0), Val::Px(3.0)),
                    min_width: Val::Px(64.0),
                    justify_content: JustifyContent::Center,
                    border_radius: BorderRadius::all(Val::Px(4.0)),
                    ..default()
                },
                BackgroundColor(ROW),
            ))
            .with_child((
                MuteLabel,
                Text::new("Mic on"),
                TextFont::from_font_size(13.0),
                TextColor(TEXT),
            ));
    }
}

fn mute_clicks(
    buttons: Query<&Interaction, (With<MuteButton>, Changed<Interaction>)>,
    mut voices: Query<&mut Voice, Without<Remote>>,
) {
    for interaction in &buttons {
        if *interaction != Interaction::Pressed {
            continue;
        }
        let muted = voices.iter().any(|v| !v.muted);
        for mut voice in &mut voices {
            voice.muted = muted;
        }
    }
}

fn sync_mute_buttons(
    voices: Query<&Voice, Without<Remote>>,
    mut labels: Query<(&mut Text, &mut TextColor), With<MuteLabel>>,
) {
    let muted = !voices.is_empty() && voices.iter().all(|v| v.muted);
    let (text, colour) = if muted {
        ("Muted", MUTED)
    } else {
        ("Mic on", TEXT)
    };
    for (mut label, mut tint) in &mut labels {
        if label.0 != text {
            label.0 = text.into();
            tint.0 = colour;
        }
    }
}

// -- device pickers ------------------------------------------------------------------------

/// Which list a picker shows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeviceKind {
    Microphone,
    Speaker,
    Camera,
}

/// A titled list of devices; the chosen one is highlighted and a click chooses another.
/// In a page the list starts with a button that asks for permission.
#[derive(Component, Debug, Clone)]
pub struct DevicePicker {
    pub kind: DeviceKind,
    pub title: String,
}

impl DevicePicker {
    pub fn microphone() -> Self {
        Self {
            kind: DeviceKind::Microphone,
            title: "Microphone".into(),
        }
    }

    pub fn speaker() -> Self {
        Self {
            kind: DeviceKind::Speaker,
            title: "Speaker".into(),
        }
    }

    pub fn camera() -> Self {
        Self {
            kind: DeviceKind::Camera,
            title: "Camera".into(),
        }
    }
}

/// What a row does when clicked.
#[derive(Component, Clone)]
enum RowAction {
    Choose(DeviceKind, Option<String>),
    Request(DeviceKind),
}

#[derive(Component)]
struct RowSelected(bool);

/// Rows are rebuilt when the lists or the choice change.
fn build_pickers(
    mut commands: Commands,
    audio: Res<AudioDevices>,
    cameras: Res<CameraDevices>,
    settings: Res<MediaSettings>,
    pickers: Query<(Entity, &DevicePicker, Option<&Children>), With<DevicePicker>>,
    added: Query<Entity, Added<DevicePicker>>,
) {
    let changed = audio.is_changed() || cameras.is_changed() || settings.is_changed();
    for (entity, picker, children) in &pickers {
        if !changed && added.get(entity).is_err() {
            continue;
        }
        if let Some(children) = children {
            for child in children.iter() {
                commands.entity(child).despawn();
            }
        }
        let (permission, devices): (Permission, Vec<(String, String, String, bool)>) =
            match picker.kind {
                DeviceKind::Microphone => (
                    audio.permission.clone(),
                    audio
                        .microphones
                        .iter()
                        .map(|d| (d.id.clone(), d.name.clone(), d.detail(), d.is_default))
                        .collect(),
                ),
                DeviceKind::Speaker => (
                    audio.permission.clone(),
                    audio
                        .speakers
                        .iter()
                        .map(|d| (d.id.clone(), d.name.clone(), d.detail(), d.is_default))
                        .collect(),
                ),
                DeviceKind::Camera => (
                    cameras.permission.clone(),
                    cameras
                        .cameras
                        .iter()
                        .map(|d| (d.id.clone(), d.name.clone(), d.detail.clone(), d.is_default))
                        .collect(),
                ),
            };
        let chosen: Option<Option<String>> = match picker.kind {
            DeviceKind::Microphone => match &settings.microphone {
                MicrophoneChoice::Default => Some(None),
                MicrophoneChoice::Device(id) => Some(Some(id.clone())),
                _ => None,
            },
            DeviceKind::Speaker => match &settings.speaker {
                SpeakerChoice::Default => Some(None),
                SpeakerChoice::Device(id) => Some(Some(id.clone())),
                _ => None,
            },
            DeviceKind::Camera => match &settings.camera {
                CameraChoice::Default => Some(None),
                CameraChoice::Device(id) => Some(Some(id.clone())),
                CameraChoice::None => None,
            },
        };
        commands
            .entity(entity)
            .insert(Node {
                flex_direction: FlexDirection::Column,
                row_gap: Val::Px(3.0),
                ..default()
            })
            .with_children(|list| {
                list.spawn((
                    Text::new(picker.title.clone()),
                    TextFont::from_font_size(12.0),
                    TextColor(TEXT_DIM),
                ));
                match permission {
                    Permission::Idle if devices.is_empty() => {
                        row(
                            list,
                            RowAction::Request(picker.kind),
                            false,
                            &format!("Allow {} access", picker.title.to_lowercase()),
                            "the browser will ask",
                        );
                    }
                    Permission::Asking => {
                        list.spawn((
                            Text::new("Asking the browser…"),
                            TextFont::from_font_size(12.0),
                            TextColor(TEXT_DIM),
                        ));
                    }
                    Permission::Denied(ref why) => {
                        list.spawn((
                            Text::new(format!("No access: {why}")),
                            TextFont::from_font_size(12.0),
                            TextColor(MUTED),
                        ));
                    }
                    _ => {}
                }
                if permission == Permission::Ready || !devices.is_empty() {
                    let default_name = devices
                        .iter()
                        .find(|d| d.3)
                        .map(|d| d.1.as_str())
                        .unwrap_or("whatever the system picks");
                    row(
                        list,
                        RowAction::Choose(picker.kind, None),
                        chosen == Some(None),
                        "System default",
                        default_name,
                    );
                    for (id, name, detail, _) in &devices {
                        row(
                            list,
                            RowAction::Choose(picker.kind, Some(id.clone())),
                            chosen.as_ref().is_some_and(|c| c.as_deref() == Some(id)),
                            name,
                            detail,
                        );
                    }
                    if devices.is_empty() && permission == Permission::Ready {
                        list.spawn((
                            Text::new("none found"),
                            TextFont::from_font_size(12.0),
                            TextColor(TEXT_DIM),
                        ));
                    }
                }
            });
    }
}

fn row(
    list: &mut ChildSpawnerCommands,
    action: RowAction,
    selected: bool,
    name: &str,
    detail: &str,
) {
    list.spawn((
        action,
        RowSelected(selected),
        Button,
        Node {
            flex_direction: FlexDirection::Column,
            padding: UiRect::axes(Val::Px(8.0), Val::Px(4.0)),
            border_radius: BorderRadius::all(Val::Px(4.0)),
            ..default()
        },
        BackgroundColor(if selected { ROW_SELECTED } else { ROW }),
    ))
    .with_children(|r| {
        r.spawn((
            Text::new(if selected {
                format!("> {name}")
            } else {
                name.to_string()
            }),
            TextFont::from_font_size(13.0),
            TextColor(TEXT),
        ));
        r.spawn((
            Text::new(detail.to_string()),
            TextFont::from_font_size(10.0),
            TextColor(TEXT_DIM),
        ));
    });
}

fn picker_clicks(
    rows: Query<(&Interaction, &RowAction), Changed<Interaction>>,
    mut settings: ResMut<MediaSettings>,
    mut audio: ResMut<AudioDevices>,
    mut cameras: ResMut<CameraDevices>,
) {
    for (interaction, action) in &rows {
        if *interaction != Interaction::Pressed {
            continue;
        }
        match action {
            RowAction::Request(DeviceKind::Camera) => cameras.request(),
            RowAction::Request(_) => audio.request(),
            RowAction::Choose(DeviceKind::Microphone, id) => {
                settings.microphone = match id {
                    Some(id) => MicrophoneChoice::Device(id.clone()),
                    None => MicrophoneChoice::Default,
                };
            }
            RowAction::Choose(DeviceKind::Speaker, id) => {
                settings.speaker = match id {
                    Some(id) => SpeakerChoice::Device(id.clone()),
                    None => SpeakerChoice::Default,
                };
            }
            RowAction::Choose(DeviceKind::Camera, id) => {
                settings.camera = match id {
                    Some(id) => CameraChoice::Device(id.clone()),
                    None => CameraChoice::Default,
                };
            }
        }
    }
}

fn row_hover(
    mut rows: Query<
        (&Interaction, &mut BackgroundColor, Option<&RowSelected>),
        (With<Button>, Changed<Interaction>),
    >,
) {
    for (interaction, mut colour, selected) in &mut rows {
        let selected = selected.is_some_and(|s| s.0);
        colour.0 = match interaction {
            Interaction::Hovered | Interaction::Pressed => ROW_HOVER,
            Interaction::None if selected => ROW_SELECTED,
            Interaction::None => ROW,
        };
    }
}

// -- the bar over a head -------------------------------------------------------------------

/// A small green bar floating over an entity, rising with its [`VoiceLevel`]. Put it on the
/// entity that carries the voice, or on any entity whose level you copy there. Two unlit
/// cuboids: a dark track, always there so a quiet person still reads as a person with a
/// voice, and the bar, which fades to black at rest.
#[derive(Component, Debug, Clone)]
pub struct VoiceIndicator {
    /// Where the bar's bottom sits, in the entity's space.
    pub offset: Vec3,
    pub height: f32,
    pub width: f32,
}

impl Default for VoiceIndicator {
    fn default() -> Self {
        Self {
            offset: Vec3::new(0.0, 0.85, 0.0),
            height: 0.25,
            width: 0.05,
        }
    }
}

#[derive(Component)]
struct IndicatorBar {
    of: Entity,
    height: f32,
    base: Vec3,
}

fn build_indicators(
    mut commands: Commands,
    added: Query<(Entity, &VoiceIndicator), Added<VoiceIndicator>>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    for (entity, indicator) in &added {
        let cell = meshes.add(Cuboid::from_length(1.0));
        let centre = indicator.offset + Vec3::Y * indicator.height / 2.0;
        commands.entity(entity).with_children(|parent| {
            parent.spawn((
                Mesh3d(cell.clone()),
                MeshMaterial3d(materials.add(StandardMaterial {
                    base_color: Color::srgb(0.05, 0.09, 0.06),
                    unlit: true,
                    ..default()
                })),
                Transform::from_translation(centre).with_scale(Vec3::new(
                    indicator.width,
                    indicator.height,
                    indicator.width * 0.5,
                )),
                Pickable::IGNORE,
            ));
            parent.spawn((
                IndicatorBar {
                    of: entity,
                    height: indicator.height,
                    base: indicator.offset,
                },
                Mesh3d(cell),
                MeshMaterial3d(materials.add(StandardMaterial {
                    base_color: Color::BLACK,
                    unlit: true,
                    ..default()
                })),
                bar_transform(indicator.offset, indicator.height, indicator.width, 0.0),
                Pickable::IGNORE,
            ));
        });
    }
}

/// Grows from the bottom: a cuboid is centred on its origin, so filling upwards scales the
/// height and moves the middle up by half of it. Never exactly zero: a sliver at rest.
fn bar_transform(base: Vec3, height: f32, width: f32, level: f32) -> Transform {
    let h = (height * level.clamp(0.0, 1.0)).max(0.002);
    Transform::from_translation(base + Vec3::new(0.0, h / 2.0, width * 0.55)).with_scale(Vec3::new(
        width,
        h,
        width * 0.5,
    ))
}

fn sync_indicators(
    levels: Query<(&VoiceLevel, Option<&Voice>, &VoiceIndicator)>,
    mut bars: Query<(
        &IndicatorBar,
        &mut Transform,
        &MeshMaterial3d<StandardMaterial>,
    )>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    for (bar, mut transform, material) in &mut bars {
        let (level, indicator) = match levels.get(bar.of) {
            Ok((level, voice, indicator)) => (
                if voice.is_some_and(|v| v.muted) {
                    0.0
                } else {
                    level.0
                },
                indicator,
            ),
            Err(_) => continue,
        };
        let wanted = bar_transform(bar.base, bar.height, indicator.width, level);
        if *transform != wanted {
            *transform = wanted;
        }
        // Over 1.0 so a bloom pass reads it as glow, and fading out with the level so at
        // rest the track is all there is.
        let lit = METER_GREEN.to_linear() * (1.6 * level.clamp(0.0, 1.0));
        let colour = Color::LinearRgba(lit);
        if let Some(mut asset) = materials.get_mut(&material.0)
            && asset.base_color != colour
        {
            asset.base_color = colour;
        }
    }
}