Skip to main content

wgpui_base/
element_ext.rs

1use wgpui::{App, Bounds, IntoElement, ParentElement, Pixels, Styled as _, Window, canvas};
2
3use crate::TextSelectionScopeId;
4
5/// Marks any element subtree as belonging to a text-selection scope.
6pub trait TextSelectionScopeExt: Sized {
7    /// Marks this element subtree as belonging to a text-selection scope.
8    fn text_selection_scope(self, scope: TextSelectionScopeId) -> impl IntoElement
9    where
10        Self: IntoElement,
11    {
12        crate::text_selection::text_selection_scope(scope, self)
13    }
14}
15
16impl<T: Sized> TextSelectionScopeExt for T {}
17
18/// Extends a GPUI parent element with post-layout prepaint observation.
19pub trait ElementExt: ParentElement + Sized {
20    /// Invokes `callback` during prepaint with this element's resolved bounds.
21    fn on_prepaint<F>(self, callback: F) -> Self
22    where
23        F: FnOnce(Bounds<Pixels>, &mut Window, &mut App) + 'static,
24    {
25        self.child(
26            canvas(
27                move |bounds, window, cx| callback(bounds, window, cx),
28                |_, _, _, _| {},
29            )
30            // Without an inset, an absolute child starts after the padding but
31            // is still sized to the padding box, overhanging the far edges.
32            .absolute()
33            .top_0()
34            .left_0()
35            .size_full(),
36        )
37    }
38}
39
40impl<T: ParentElement> ElementExt for T {}