open_gpui/elements/
surface.rs1#[cfg(target_os = "macos")]
2use crate::PlatformPixelBuffer;
3use crate::{
4 App, Bounds, Element, ElementId, GlobalElementId, InspectorElementId, IntoElement, LayoutId,
5 ObjectFit, Pixels, Style, StyleRefinement, Styled, Window,
6};
7#[cfg(target_os = "macos")]
8use objc2_core_video::{CVPixelBufferGetHeight, CVPixelBufferGetWidth};
9use open_gpui_refineable::Refineable;
10
11#[derive(Clone, Debug, PartialEq, Eq)]
13pub enum SurfaceSource {
14 #[cfg(target_os = "macos")]
16 Surface(PlatformPixelBuffer),
17}
18
19#[cfg(target_os = "macos")]
20impl From<PlatformPixelBuffer> for SurfaceSource {
21 fn from(value: PlatformPixelBuffer) -> Self {
22 SurfaceSource::Surface(value)
23 }
24}
25
26pub struct Surface {
28 source: SurfaceSource,
29 object_fit: ObjectFit,
30 style: StyleRefinement,
31}
32
33#[cfg(target_os = "macos")]
35pub fn surface(source: impl Into<SurfaceSource>) -> Surface {
36 Surface {
37 source: source.into(),
38 object_fit: ObjectFit::Contain,
39 style: Default::default(),
40 }
41}
42
43impl Surface {
44 pub fn object_fit(mut self, object_fit: ObjectFit) -> Self {
46 self.object_fit = object_fit;
47 self
48 }
49}
50
51impl Element for Surface {
52 type RequestLayoutState = ();
53 type PrepaintState = ();
54
55 fn id(&self) -> Option<ElementId> {
56 None
57 }
58
59 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
60 None
61 }
62
63 fn request_layout(
64 &mut self,
65 _global_id: Option<&GlobalElementId>,
66 _inspector_id: Option<&InspectorElementId>,
67 window: &mut Window,
68 cx: &mut App,
69 ) -> (LayoutId, Self::RequestLayoutState) {
70 let mut style = Style::default();
71 style.refine(&self.style);
72 let layout_id = window.request_layout(style, [], cx);
73 (layout_id, ())
74 }
75
76 fn prepaint(
77 &mut self,
78 _global_id: Option<&GlobalElementId>,
79 _inspector_id: Option<&InspectorElementId>,
80 _bounds: Bounds<Pixels>,
81 _request_layout: &mut Self::RequestLayoutState,
82 _window: &mut Window,
83 _cx: &mut App,
84 ) -> Self::PrepaintState {
85 }
86
87 fn paint(
88 &mut self,
89 _global_id: Option<&GlobalElementId>,
90 _inspector_id: Option<&InspectorElementId>,
91 #[cfg_attr(not(target_os = "macos"), allow(unused_variables))] bounds: Bounds<Pixels>,
92 _: &mut Self::RequestLayoutState,
93 _: &mut Self::PrepaintState,
94 #[cfg_attr(not(target_os = "macos"), allow(unused_variables))] window: &mut Window,
95 _: &mut App,
96 ) {
97 match &self.source {
98 #[cfg(target_os = "macos")]
99 SurfaceSource::Surface(surface) => {
100 let size = crate::size(
101 CVPixelBufferGetWidth(surface).into(),
102 CVPixelBufferGetHeight(surface).into(),
103 );
104 let new_bounds = self.object_fit.get_bounds(bounds, size);
105 window.paint_surface(new_bounds, surface.clone());
107 }
108 #[allow(unreachable_patterns)]
109 _ => {}
110 }
111 }
112}
113
114impl IntoElement for Surface {
115 type Element = Self;
116
117 fn into_element(self) -> Self::Element {
118 self
119 }
120}
121
122impl Styled for Surface {
123 fn style(&mut self) -> &mut StyleRefinement {
124 &mut self.style
125 }
126}