zed/crates/workspace/src/shared_screen.rs

129 lines
3.5 KiB
Rust
Raw Normal View History

2022-11-15 01:31:12 +00:00
use crate::{
2023-02-22 01:42:19 +00:00
item::{Item, ItemEvent},
ItemNavHistory, WorkspaceId,
2022-11-15 01:31:12 +00:00
};
use anyhow::Result;
2022-10-24 08:04:08 +00:00
use call::participant::{Frame, RemoteVideoTrack};
use client::{proto::PeerId, User};
2022-10-24 08:04:08 +00:00
use futures::StreamExt;
use gpui::{
div, img, AppContext, Element, EventEmitter, FocusHandle, FocusableView, InteractiveElement,
ParentElement, Render, SharedString, Styled, Task, View, ViewContext, VisualContext,
WindowContext,
2023-04-14 21:46:53 +00:00
};
use std::sync::{Arc, Weak};
use ui::{h_stack, prelude::*, Icon, IconElement, Label};
2022-10-24 08:04:08 +00:00
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<()>>,
focus: FocusHandle,
2022-10-24 08:04:08 +00:00
}
impl SharedScreen {
pub fn new(
track: &Arc<RemoteVideoTrack>,
peer_id: PeerId,
user: Arc<User>,
cx: &mut ViewContext<Self>,
) -> Self {
cx.focus_handle();
2022-10-24 08:04:08 +00:00
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();
})?;
2022-10-24 08:04:08 +00:00
}
this.update(&mut cx, |_, cx| cx.emit(Event::Close))?;
Ok(())
2022-10-24 08:04:08 +00:00
}),
focus: cx.focus_handle(),
2022-10-24 08:04:08 +00:00
}
}
}
impl EventEmitter<Event> for SharedScreen {}
2022-10-24 08:04:08 +00:00
impl FocusableView for SharedScreen {
fn focus_handle(&self, _: &AppContext) -> FocusHandle {
self.focus.clone()
2022-10-24 08:04:08 +00:00
}
}
impl Render for SharedScreen {
fn render(&mut self, _: &mut ViewContext<Self>) -> impl IntoElement {
div().track_focus(&self.focus).size_full().children(
self.frame
.as_ref()
.map(|frame| img(frame.image()).size_full()),
)
2022-10-24 08:04:08 +00:00
}
}
impl Item for SharedScreen {
type Event = Event;
fn tab_tooltip_text(&self, _: &AppContext) -> Option<SharedString> {
2023-04-14 21:46:53 +00:00
Some(format!("{}'s screen", self.user.github_login).into())
}
2022-10-24 08:04:08 +00:00
fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
if let Some(nav_history) = self.nav_history.as_mut() {
2022-10-24 08:04:08 +00:00
nav_history.push::<()>(None, cx);
}
}
fn tab_content(
2022-10-24 08:04:08 +00:00
&self,
_: Option<usize>,
selected: bool,
_: &WindowContext<'_>,
) -> gpui::AnyElement {
h_stack()
.gap_1()
.child(IconElement::new(Icon::Screen))
.child(
Label::new(format!("{}'s screen", self.user.github_login)).color(if selected {
Color::Default
} else {
Color::Muted
}),
2022-10-24 08:04:08 +00:00
)
.into_any()
2022-10-24 08:04:08 +00:00
}
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<View<Self>> {
2022-10-24 08:04:08 +00:00
let track = self.track.upgrade()?;
Some(cx.new_view(|cx| Self::new(&track, self.peer_id, self.user.clone(), cx)))
2022-10-24 08:04:08 +00:00
}
fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {
2022-10-24 08:04:08 +00:00
match event {
Event::Close => f(ItemEvent::CloseItem),
2022-10-24 08:04:08 +00:00
}
}
}