markdown_as_child.rs

  1use assets::Assets;
  2use gpui::{Application, Entity, KeyBinding, Length, StyleRefinement, WindowOptions, rgb};
  3use language::{LanguageRegistry, language_settings::AllLanguageSettings};
  4use markdown::{Markdown, MarkdownElement, MarkdownStyle};
  5use node_runtime::NodeRuntime;
  6use settings::SettingsStore;
  7use std::sync::Arc;
  8use theme::LoadThemes;
  9use ui::div;
 10use ui::prelude::*;
 11
 12const MARKDOWN_EXAMPLE: &str = r#"
 13this text should be selectable
 14
 15wow so cool
 16
 17## Heading 2
 18"#;
 19pub fn main() {
 20    env_logger::init();
 21
 22    Application::new().with_assets(Assets).run(|cx| {
 23        let store = SettingsStore::test(cx);
 24        cx.set_global(store);
 25        language::init(cx);
 26        SettingsStore::update(cx, |store, cx| {
 27            store.update_user_settings::<AllLanguageSettings>(cx, |_| {});
 28        });
 29        cx.bind_keys([KeyBinding::new("cmd-c", markdown::Copy, None)]);
 30
 31        let node_runtime = NodeRuntime::unavailable();
 32        let language_registry = Arc::new(LanguageRegistry::new(cx.background_executor().clone()));
 33        languages::init(language_registry, node_runtime, cx);
 34        theme::init(LoadThemes::JustBase, cx);
 35        Assets.load_fonts(cx).unwrap();
 36
 37        cx.activate(true);
 38        let _ = cx.open_window(WindowOptions::default(), |_, cx| {
 39            cx.new(|cx| {
 40                let markdown = cx.new(|cx| Markdown::new(MARKDOWN_EXAMPLE.into(), None, None, cx));
 41
 42                HelloWorld { markdown }
 43            })
 44        });
 45    });
 46}
 47struct HelloWorld {
 48    markdown: Entity<Markdown>,
 49}
 50
 51impl Render for HelloWorld {
 52    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 53        let markdown_style = MarkdownStyle {
 54            base_text_style: gpui::TextStyle {
 55                font_family: "Zed Mono".into(),
 56                color: cx.theme().colors().text,
 57                ..Default::default()
 58            },
 59            code_block: StyleRefinement {
 60                text: Some(gpui::TextStyleRefinement {
 61                    font_family: Some("Zed Mono".into()),
 62                    background_color: Some(cx.theme().colors().editor_background),
 63                    ..Default::default()
 64                }),
 65                margin: gpui::EdgesRefinement {
 66                    top: Some(Length::Definite(rems(4.).into())),
 67                    left: Some(Length::Definite(rems(4.).into())),
 68                    right: Some(Length::Definite(rems(4.).into())),
 69                    bottom: Some(Length::Definite(rems(4.).into())),
 70                },
 71                ..Default::default()
 72            },
 73            inline_code: gpui::TextStyleRefinement {
 74                font_family: Some("Zed Mono".into()),
 75                background_color: Some(cx.theme().colors().editor_background),
 76                ..Default::default()
 77            },
 78            rule_color: Color::Muted.color(cx),
 79            block_quote_border_color: Color::Muted.color(cx),
 80            block_quote: gpui::TextStyleRefinement {
 81                color: Some(Color::Muted.color(cx)),
 82                ..Default::default()
 83            },
 84            link: gpui::TextStyleRefinement {
 85                color: Some(Color::Accent.color(cx)),
 86                underline: Some(gpui::UnderlineStyle {
 87                    thickness: px(1.),
 88                    color: Some(Color::Accent.color(cx)),
 89                    wavy: false,
 90                }),
 91                ..Default::default()
 92            },
 93            syntax: cx.theme().syntax().clone(),
 94            selection_background_color: cx.theme().colors().element_selection_background,
 95            heading: Default::default(),
 96            ..Default::default()
 97        };
 98
 99        div()
100            .flex()
101            .bg(rgb(0x2e7d32))
102            .size(Length::Definite(Pixels(700.0).into()))
103            .justify_center()
104            .items_center()
105            .shadow_lg()
106            .border_1()
107            .border_color(rgb(0x0000ff))
108            .text_xl()
109            .text_color(rgb(0xffffff))
110            .child(
111                div()
112                    .child(MarkdownElement::new(self.markdown.clone(), markdown_style))
113                    .p_20(),
114            )
115    }
116}