forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

output_window.rs163 lines, 6.4 KiBHistoryRawDownload
1//! The video output window (View > Video output window): a window of its own
2//! that shows the program picture and nothing else -- full-bleed on black,
3//! letterboxed -- for a client monitor or a second screen, the way an NLE
4//! feeds its full-screen video output.
5//!
6//! No second decode. The picture is the very frame the viewer holds in
7//! [`Player::image`], which this window reads at each paint. The main window
8//! repaints this one whenever it renders ([`Player::render`]), so the output
9//! follows the playhead and a scrub. The window is session state only
10//! ([`Player::output`]): it is not in the project file and is not reopened.
11
12use crate::*;
13use gpui::Entity;
14
15/// The picture's surround: true black whatever the UI theme, since a client display is not chrome and should not go pale under a light theme.
16const VIDEO_BLACK: u32 = 0x000000;
17
18/// The output window's root view: the picture, read from the player on each paint.
19pub(crate) struct OutputView {
20 player: Entity<Player>,
21 focus: FocusHandle,
22}
23
24impl OutputView {
25 /// Closes the window and clears the player's record of it. Esc and a double-click both end here.
26 fn close(&self, window: &mut Window, cx: &mut Context<Self>) {
27 self.player.update(cx, |player, cx| {
28 player.output = None;
29 cx.notify();
30 });
31 window.remove_window();
32 }
33}
34
35impl Render for OutputView {
36 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
37 let image = self.player.read(cx).image.clone();
38 div()
39 .id("video-output")
40 .relative()
41 .flex()
42 .size_full()
43 .bg(rgb(VIDEO_BLACK))
44 .track_focus(&self.focus)
45 .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| {
46 if event.keystroke.key == "escape" {
47 this.close(window, cx);
48 }
49 }))
50 .on_click(cx.listener(|this, event: &ClickEvent, window, cx| {
51 if event.click_count() == 2 {
52 this.close(window, cx);
53 }
54 }))
55 .when_some(image, |d, image| d.child(letterboxed_image(image)))
56 }
57}
58
59/// Opens the output window on `display` (the primary one when `None`), fullscreen there,
60/// and hands the keyboard to it so Esc works at once. `None` when the window would not open.
61pub(crate) fn open(player: Entity<Player>, display: Option<DisplayId>, cx: &mut App) -> Option<WindowHandle<OutputView>> {
62 // Always a named display: an unnamed window lands wherever the system puts it, not on the
63 // screen the menu ticked.
64 let screen = display.and_then(|id| cx.find_display(id)).or_else(|| cx.displays().into_iter().next());
65 let bounds = match &screen {
66 Some(screen) => screen.bounds(),
67 None => Bounds::centered(None, size(px(1280.), px(720.)), cx),
68 };
69 let display = screen.map(|screen| screen.id());
70 let handle = cx
71 .open_window(
72 WindowOptions {
73 window_bounds: Some(WindowBounds::Fullscreen(bounds)),
74 display_id: display,
75 titlebar: None,
76 app_id: Some("edith".to_string()),
77 ..Default::default()
78 },
79 move |window, cx| {
80 let view = cx.new(|cx| OutputView { player, focus: cx.focus_handle() });
81 let focus = view.read(cx).focus.clone();
82 window.focus(&focus);
83 view
84 },
85 )
86 .ok()?;
87 Some(handle)
88}
89
90impl Player {
91 /// The display the output window goes to: the one picked, else the first the system lists.
92 pub(crate) fn output_target(&self) -> Option<DisplayId> {
93 self.output_display.or(self.output_displays.first().map(|(id, _)| *id))
94 }
95
96 /// Each frame, before the picture is painted: the display list the View menu reads, the
97 /// output window's record (cleared if the window was closed by the system), and a repaint
98 /// of that window, which shows the same picture as this one.
99 pub(crate) fn sync_output(&mut self, cx: &mut Context<Self>) {
100 // Numbered left to right, top to bottom: the system lists screens in no fixed order.
101 let mut screens = cx.displays();
102 screens.sort_by_key(|screen| {
103 let origin = screen.bounds().origin;
104 (f32::from(origin.x) as i64, f32::from(origin.y) as i64)
105 });
106 self.output_displays = screens
107 .iter()
108 .enumerate()
109 .map(|(i, screen)| {
110 let bounds = screen.bounds();
111 let (w, h) = (f32::from(bounds.size.width), f32::from(bounds.size.height));
112 (screen.id(), format!("Display {} ({} × {})", i + 1, w, h).into())
113 })
114 .collect();
115 if let Some(open) = self.output {
116 match open.update(cx, |_, _, cx| cx.notify()) {
117 Ok(()) => {}
118 Err(_) => self.output = None,
119 }
120 }
121 }
122
123 /// View > Video output window: opens the window on the target display, or closes it.
124 pub(crate) fn toggle_output_window(&mut self, cx: &mut Context<Self>) {
125 if self.output.is_some() {
126 self.close_output(cx);
127 } else {
128 self.open_output(cx);
129 }
130 }
131
132 /// View > Output display: the window moves to the display picked, if it is open now.
133 pub(crate) fn set_output_display(&mut self, index: usize, cx: &mut Context<Self>) {
134 let Some((id, _)) = self.output_displays.get(index).cloned() else {
135 return;
136 };
137 self.output_display = Some(id);
138 if self.output.is_some() {
139 self.close_output(cx);
140 self.open_output(cx);
141 }
142 }
143
144 /// Opens the window once the current update has finished: the window paints as it opens,
145 /// and that paint reads this player, which the update is still holding.
146 fn open_output(&mut self, cx: &mut Context<Self>) {
147 let player = cx.entity();
148 let display = self.output_display;
149 cx.defer(move |cx| {
150 let handle = output_window::open(player.clone(), display, cx);
151 player.update(cx, |player, cx| {
152 player.output = handle;
153 cx.notify();
154 });
155 });
156 }
157
158 fn close_output(&mut self, cx: &mut Context<Self>) {
159 if let Some(open) = self.output.take() {
160 let _ = open.update(cx, |_, window, _| window.remove_window());
161 }
162 }
163}