forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

render.rs287 lines, 14.2 KiBHistoryRawDownload
1//! What the window looks like this frame: the one `render` the whole
2//! interface hangs off, root to overlays.
3
4use crate::*;
5
6/// The frame `image`, letterboxed/pillarboxed into whatever box it is given,
7/// filling that box on its own axis and leaving the other bare -- the bare
8/// strip is the box's own background (`BG_CANVAS`), never painted here, which
9/// is why every caller sits this on a div already carrying that colour.
10///
11/// `Img::object_fit(Contain)` reads right (verified by hand against
12/// `ObjectFit::Contain::get_bounds`) but only when taffy has already given the
13/// element a real size to fit *into* -- and for a plain `img()` with both axes
14/// `Auto` (`size_full()`/`max_w_full().max_h_full()` both resolve that way in
15/// this pin), it resolves to the image's own native size first and object-fit
16/// never gets a box smaller than the frame to shrink into, so the frame reads
17/// through un-fitted. A `canvas()` element carries no such precompute -- its
18/// `request_layout` is a plain `Style` read, so it is sized by its parent
19/// exactly as a `div()` would be -- and its `paint` callback is handed the
20/// real resolved `bounds`, which is all `Contain::get_bounds` needs.
21///
22/// `.absolute()` with all four insets zero, not `.size_full()` in flex flow:
23/// a flex item under `justify_center()`/`items_center()` (see `picture_area`
24/// below) never resolves a percentage cross-axis size for a plain-`Style`
25/// element like `canvas()`, which is a taffy circularity and not this
26/// element's own bug (confirmed live: bounds handed to `paint` came back
27/// `944px x 0px`, main axis right, cross axis collapsed). An absolutely
28/// positioned box is sized against its containing block directly instead --
29/// the nearest `.relative()` ancestor, which every caller here already sits
30/// this on -- so it never asks taffy's flex algorithm for a cross-axis size
31/// at all.
32///
33/// That containing block still has to have a real size of its own, though:
34/// gpui's default `display` is `Block` (`Style::default()`), not `Flex`, and
35/// a `Block` box with height `auto` is sized by its *normal-flow* children --
36/// an `.absolute()` one, this, takes no part in that (confirmed live: with
37/// the box lacking its own `.flex()`, this canvas's own bounds came back
38/// `944px x 0px` even though its containing block, measured independently,
39/// was a real `944px x 335px`). Every `.relative()` ancestor this sits on
40/// must also be `.flex()` (see `ui::stance::screen`'s `stance-picture`) for
41/// exactly this reason.
42pub(crate) fn letterboxed_image(image: Arc<RenderImage>) -> impl IntoElement + Styled {
43 canvas(
44 |_, _, _| (),
45 move |bounds, _, window, _| {
46 let fitted = gpui::ObjectFit::Contain.get_bounds(bounds, image.size(0));
47 let _ = window.paint_image(fitted, Corners::default(), image, 0, false);
48 },
49 )
50 .absolute()
51 .top_0()
52 .bottom_0()
53 .left_0()
54 .right_0()
55 .size_full()
56}
57
58impl Player {
59 /// The picture region alone: the image, the subtitle cue plate, the
60 /// preview badge and the three transient bars over its bottom edge.
61 /// Its own method so player fullscreen ([`Player::act`]) can stand it up
62 /// as the window's only child without a second copy of what it draws --
63 /// and so the darkroom's own screen region (DESIGN.md §5,
64 /// `ui::stance::screen`) draws the same picture rather than a second one.
65 pub(crate) fn picture_area(
66 &mut self,
67 position: f64,
68 window: &mut Window,
69 cx: &mut Context<Self>,
70 ) -> impl IntoElement {
71 div()
72 .flex_1()
73 .min_h(px(0.))
74 .overflow_hidden()
75 // The bed the cue plate is placed against: it hangs off the
76 // bottom of the picture region, which is the one box that is the
77 // picture and nothing else.
78 .relative()
79 // ...and the box the frame is *watched* in, which is what the
80 // session paints at ([`Player::push_view_size`]).
81 .child(bounds_probe(self.picture_box.clone()))
82 .flex()
83 .justify_center()
84 .items_center()
85 .bg(rgb(BG_CANVAS()))
86 .children(
87 self.image
88 .clone()
89 .map(|i| letterboxed_image(i).into_any_element())
90 // With no file open the letterbox is the whole region,
91 // and a black rectangle says only that something is
92 // broken -- so it says what it wants instead. The window
93 // is already the drop target.
94 .or_else(|| {
95 (self.active_session().is_none()).then(|| empty_hint().into_any_element())
96 }),
97 )
98 // After the picture, so the plate is drawn over it rather than
99 // under.
100 .children(self.subtitle_overlay(position, window))
101 // The three transient lines hang off the bottom of the picture
102 // rather than taking a row of the column: a notice that arrives
103 // must not push the transport, the toolbar and the timeline down
104 // by its own height -- which is a control moving with state, on
105 // every control below it at once.
106 .child(
107 div()
108 .absolute()
109 .bottom_0()
110 .left_0()
111 .right_0()
112 .flex()
113 .flex_col()
114 .gap(px(2.))
115 .children(self.import_bar(cx))
116 .children(self.seek_bar())
117 // How tall the lot came out, for the cue plate above to
118 // step over ([`sub_bottom`]) -- the bars are over the
119 // picture, and a message drawn across the line being
120 // read loses both of them. Zero with no bar up, since
121 // this box is then empty.
122 .child(height_probe(self.notice_h.clone())),
123 )
124 // The preview's scrub bar, drawn LAST: the notice box above shares
125 // its bottom edge, and a bar under the "PREVIEWING ..." plate was
126 // invisible for exactly the moment it matters.
127 .children(self.preview_scrub_bar(cx))
128 }
129}
130
131impl Player {
132 /// Tells the active session where the picture actually lands, in device
133 /// pixels, so its seats can paint at that size instead of the project's
134 /// ([`engine::PlaybackSession::set_view_size`]). Read off the picture
135 /// area's own bounds -- the box `letterboxed_image` fits the frame into --
136 /// and the project's aspect, which is the aspect of every frame it hands
137 /// over.
138 ///
139 /// A size that moved enough for the eye to see gets a fresh picture: the
140 /// canvas a span paints at is decided when it opens, so a resized window
141 /// would otherwise sharpen on its next seek and not before.
142 fn push_view_size(&mut self, window: &mut Window) {
143 if self.shot_pending.is_some() {
144 // A screenshot in flight asked for the project's own size and is
145 // waiting on that frame: pushing the preview's size back now would
146 // cancel the very picture it is waiting for.
147 return;
148 }
149 let region = self.picture_box.get();
150 if region.size.width <= px(1.) || region.size.height <= px(1.) {
151 // Before the first paint there is no box to read, and a floor is
152 // not a measurement.
153 return;
154 }
155 let scale = window.scale_factor();
156 let Some(session) = self.active_session_mut() else {
157 return;
158 };
159 // Device pixels, the unit `Contain` measures its fitting size in.
160 let meta = session.meta();
161 let fitted = gpui::ObjectFit::Contain.get_bounds(
162 region,
163 size(
164 gpui::DevicePixels(meta.width as i32),
165 gpui::DevicePixels(meta.height as i32),
166 ),
167 );
168 let w = (f32::from(fitted.size.width) * scale).round() as u32;
169 let h = (f32::from(fitted.size.height) * scale).round() as u32;
170 if session.set_view_size(w, h) {
171 session.resync_picture();
172 }
173 }
174}
175
176impl Render for Player {
177 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
178 // A file that has just been opened sits on its first frame with the
179 // clock stopped: opening is not playing, whichever way the file
180 // arrived. The play binding and the transport button start it.
181 if let Some(session) = self.active_session_mut() {
182 session.tick();
183 }
184 self.pump(window);
185 // A screenshot waiting for its project-sized frame is written on the
186 // repaint that lands it -- and before the size push below, which is
187 // what gives the preview its own size back.
188 self.flush_shot(cx);
189 // ...at the size it is watched at, not the project's: a visualizer
190 // seat redrawing 1920x1080 for a 600 px preview is nine times the
191 // pixels for nothing the eye can see. An export has a seat of its own
192 // and never comes through here.
193 self.push_view_size(window);
194 // What every hover label asks before it paints: a card or a menu is
195 // drawn over whatever the pointer is resting on.
196 OVERLAID.store(self.overlaid(), Ordering::Relaxed);
197 // A cleared seek is a frame delivered, which is the one readiness signal
198 // there is: whatever a slider drag held back is written here.
199 if self.seek_since.is_none() {
200 self.flush_drag(cx);
201 }
202 self.poll_export();
203 self.poll_import(cx);
204 self.poll_silence();
205 self.poll_proxies(cx);
206 // Every way a source can arrive -- argv, an import, a project load --
207 // has been through a repaint by the time its clips are drawn, so this
208 // is the one place that has to notice a new one.
209 self.cache_media(cx);
210 self.cache_export_seat(cx);
211 self.cache_hw_caps(cx);
212 // What the compositor calls this window. Pushed only when it changes:
213 // it is a protocol round trip and this runs at vsync.
214 let title = window_title(&self.name);
215 if title != self.titled {
216 window.set_window_title(&title);
217 self.titled = title;
218 }
219 // A compositor keybind (the window manager's own fullscreen, not
220 // ours) can drop `window.is_fullscreen()` without ever going through
221 // [`Player::act`] -- the picture-only layout below must not survive
222 // that on its own, or the chrome never comes back. Only the *leaving*
223 // direction is reconciled here: the platform going fullscreen behind
224 // our back is not this editor asking for the picture-only layout, so
225 // it is left alone rather than guessed at.
226 if self.player_fullscreen && !window.is_fullscreen() {
227 self.player_fullscreen = false;
228 }
229 // No shadow flag: the session is the only truth about play state, and
230 // [`Player::transport`] is the one place it is read.
231 let state = self.transport();
232 // A paused timeline has nothing to animate; the toggle handlers notify,
233 // which is what starts the loop again. A paused seek keeps the loop
234 // running by itself until `pump` has the frame it asked for. An export
235 // pauses playback and still needs the loop: its progress only reaches
236 // the screen on a repaint. A notice does not: it waits to be dismissed
237 // rather than for a clock, so keeping the loop alive for it would spin
238 // the GPU until someone answered it.
239 // An import does too, and for the same reason: its clock and its sweep
240 // only reach the screen on a repaint, and a still line is the very
241 // thing it exists to disprove.
242 if state.is_playing()
243 || self.seek_since.is_some()
244 || self.export.is_some()
245 || self.importing.is_some()
246 // A silence scan too: its progress and its two clocks only reach
247 // the screen on a repaint, and a still line is the very thing this
248 // card was rewritten to disprove.
249 || self.silence_scan.is_some()
250 // ...and a stand-in being made: its percentage on the library row
251 // only reaches the screen on a repaint, and a bar that never moves
252 // is exactly what a person reads as a hung encode.
253 || self.making_proxies()
254 {
255 window.request_animation_frame();
256 }
257
258 // Read per render, never cached: a delete shortens the timeline and the
259 // timecode, the ruler and the clamp below all have to follow it -- and
260 // so does the room a tail being dragged needs to grow into.
261 let duration = self.drawn_duration();
262 let position = self.playhead(duration);
263 // Re-settled every frame against the duration this one is drawing: an
264 // edit that shortens the timeline moves the far end of the view, and a
265 // playhead that has run off the bed pulls the view after it -- which is
266 // what makes a zoomed-in timeline scroll while it plays.
267 // ...but only a playhead that is *going* somewhere pulls it: following
268 // is what a moving one does, during playback and through a seek. A view
269 // yanked back to a playhead nobody moved is a hand's own scroll undone
270 // by the very next frame, which is what made the wheel look dead.
271 // ...and a hand that scrolled the view away from the head keeps it
272 // ([`Player::panned`]): a follow that centres the head again would undo
273 // the notch before it was seen, which is what made the wheel look dead
274 // while playing. It is given straight back below, the moment the head
275 // is on the bed a person chose to look at -- so the scroll wins now and
276 // the follow resumes by itself, with nothing to press.
277 self.scale = match (state.is_playing() || self.seek_since.is_some()) && !self.panned {
278 true => self.view().following(position),
279 false => self.view().settled(),
280 };
281 if self.panned && self.view().shows(position) {
282 self.panned = false;
283 }
284
285 ui::stance::render(self, window, cx)
286 }
287}