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
use crate::{
    item::{Item, ItemEvent},
    ItemNavHistory, WorkspaceId,
};
use anyhow::Result;
use call::participant::{Frame, RemoteVideoTrack};
use client::{proto::PeerId, User};
use futures::StreamExt;
use gpui::{
    elements::*,
    geometry::{rect::RectF, vector::vec2f},
    platform::MouseButton,
    AppContext, Entity, Task, View, ViewContext,
};
use smallvec::SmallVec;
use std::{
    borrow::Cow,
    sync::{Arc, Weak},
};

pub enum Event {
    Close,
}

pub struct SharedScreen {
    track: Weak<RemoteVideoTrack>,
    frame: Option<Frame>,
    pub peer_id: PeerId,
    user: Arc<User>,
    nav_history: Option<ItemNavHistory>,
    _maintain_frame: Task<Result<()>>,
}

impl SharedScreen {
    pub fn new(
        track: &Arc<RemoteVideoTrack>,
        peer_id: PeerId,
        user: Arc<User>,
        cx: &mut ViewContext<Self>,
    ) -> Self {
        let mut frames = track.frames();
        Self {
            track: Arc::downgrade(track),
            frame: None,
            peer_id,
            user,
            nav_history: Default::default(),
            _maintain_frame: cx.spawn(|this, mut cx| async move {
                while let Some(frame) = frames.next().await {
                    this.update(&mut cx, |this, cx| {
                        this.frame = Some(frame);
                        cx.notify();
                    })?;
                }
                this.update(&mut cx, |_, cx| cx.emit(Event::Close))?;
                Ok(())
            }),
        }
    }
}

impl Entity for SharedScreen {
    type Event = Event;
}

impl View for SharedScreen {
    fn ui_name() -> &'static str {
        "SharedScreen"
    }

    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
        enum Focus {}

        let frame = self.frame.clone();
        MouseEventHandler::new::<Focus, _>(0, cx, |_, cx| {
            Canvas::new(move |bounds, _, _, cx| {
                if let Some(frame) = frame.clone() {
                    let size = constrain_size_preserving_aspect_ratio(
                        bounds.size(),
                        vec2f(frame.width() as f32, frame.height() as f32),
                    );
                    let origin = bounds.origin() + (bounds.size() / 2.) - size / 2.;
                    cx.scene().push_surface(gpui::platform::mac::Surface {
                        bounds: RectF::new(origin, size),
                        image_buffer: frame.image(),
                    });
                }
            })
            .contained()
            .with_style(theme::current(cx).shared_screen)
        })
        .on_down(MouseButton::Left, |_, _, cx| cx.focus_parent())
        .into_any()
    }
}

impl Item for SharedScreen {
    fn tab_tooltip_text(&self, _: &AppContext) -> Option<Cow<str>> {
        Some(format!("{}'s screen", self.user.github_login).into())
    }
    fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
        if let Some(nav_history) = self.nav_history.as_mut() {
            nav_history.push::<()>(None, cx);
        }
    }

    fn tab_content<V: 'static>(
        &self,
        _: Option<usize>,
        style: &theme::Tab,
        _: &AppContext,
    ) -> gpui::AnyElement<V> {
        Flex::row()
            .with_child(
                Svg::new("icons/desktop.svg")
                    .with_color(style.label.text.color)
                    .constrained()
                    .with_width(style.type_icon_width)
                    .aligned()
                    .contained()
                    .with_margin_right(style.spacing),
            )
            .with_child(
                Label::new(
                    format!("{}'s screen", self.user.github_login),
                    style.label.clone(),
                )
                .aligned(),
            )
            .into_any()
    }

    fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
        self.nav_history = Some(history);
    }

    fn clone_on_split(
        &self,
        _workspace_id: WorkspaceId,
        cx: &mut ViewContext<Self>,
    ) -> Option<Self> {
        let track = self.track.upgrade()?;
        Some(Self::new(&track, self.peer_id, self.user.clone(), cx))
    }

    fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
        match event {
            Event::Close => smallvec::smallvec!(ItemEvent::CloseItem),
        }
    }
}