Skip to main content

lgui_core/application/
view.rs

1use std::sync::Arc;
2
3use crate::core::{component, context_provider, Element, RenderCx, RootComponent};
4
5use super::ApplicationContext;
6
7pub type AppView = Arc<
8    dyn for<'scope, 'context> Fn(&mut RenderCx<'scope, 'context>) -> Element
9        + Send
10        + Sync
11        + 'static,
12>;
13
14struct ApplicationRoot {
15    context: ApplicationContext,
16    view: AppView,
17}
18
19impl RootComponent for ApplicationRoot {
20    fn render_root(self, cx: &mut RenderCx<'_, '_>) -> Element {
21        let viewport = cx.viewport();
22        let view = self.view;
23        let content = component(viewport, move |cx, _| view(cx)).key("lgui.application.root");
24        context_provider(self.context, content)
25    }
26}
27
28pub(crate) fn application_root_view(context: ApplicationContext, view: AppView) -> AppView {
29    Arc::new(move |cx| {
30        ApplicationRoot {
31            context: context.clone(),
32            view: Arc::clone(&view),
33        }
34        .render_root(cx)
35    })
36}
37
38impl RootComponent for AppView {
39    fn render_root(self, cx: &mut RenderCx<'_, '_>) -> Element {
40        self(cx)
41    }
42}