markdown_as_child.rs

  1use assets::Assets;
  2use gpui::{Application, Entity, KeyBinding, Length, StyleRefinement, WindowOptions, rgb};
  3use language::LanguageRegistry;
  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        cx.bind_keys([KeyBinding::new("cmd-c", markdown::Copy, None)]);
 26
 27        let node_runtime = NodeRuntime::unavailable();
 28        let language_registry = Arc::new(LanguageRegistry::new(cx.background_executor().clone()));
 29        let fs = fs::FakeFs::new(cx.background_executor().clone());
 30        languages::init(language_registry, fs, node_runtime, cx);
 31        theme::init(LoadThemes::JustBase, cx);
 32        Assets.load_fonts(cx).unwrap();
 33
 34        cx.activate(true);
 35        let _ = cx.open_window(WindowOptions::default(), |_, cx| {
 36            cx.new(|cx| {
 37                let markdown = cx.new(|cx| Markdown::new(MARKDOWN_EXAMPLE.into(), None, None, cx));
 38
 39                HelloWorld { markdown }
 40            })
 41        });
 42    });
 43}
 44struct HelloWorld {
 45    markdown: Entity<Markdown>,
 46}
 47
 48impl Render for HelloWorld {
 49    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 50        let markdown_style = MarkdownStyle {
 51            base_text_style: gpui::TextStyle {
 52                font_family: "Zed Mono".into(),
 53                color: cx.theme().colors().text,
 54                ..Default::default()
 55            },
 56            code_block: StyleRefinement {
 57                text: Some(gpui::TextStyleRefinement {
 58                    font_family: Some("Zed Mono".into()),
 59                    background_color: Some(cx.theme().colors().editor_background),
 60                    ..Default::default()
 61                }),
 62                margin: gpui::EdgesRefinement {
 63                    top: Some(Length::Definite(rems(4.).into())),
 64                    left: Some(Length::Definite(rems(4.).into())),
 65                    right: Some(Length::Definite(rems(4.).into())),
 66                    bottom: Some(Length::Definite(rems(4.).into())),
 67                },
 68                ..Default::default()
 69            },
 70            inline_code: gpui::TextStyleRefinement {
 71                font_family: Some("Zed Mono".into()),
 72                background_color: Some(cx.theme().colors().editor_background),
 73                ..Default::default()
 74            },
 75            rule_color: Color::Muted.color(cx),
 76            block_quote_border_color: Color::Muted.color(cx),
 77            block_quote: gpui::TextStyleRefinement {
 78                color: Some(Color::Muted.color(cx)),
 79                ..Default::default()
 80            },
 81            link: gpui::TextStyleRefinement {
 82                color: Some(Color::Accent.color(cx)),
 83                underline: Some(gpui::UnderlineStyle {
 84                    thickness: px(1.),
 85                    color: Some(Color::Accent.color(cx)),
 86                    wavy: false,
 87                }),
 88                ..Default::default()
 89            },
 90            syntax: cx.theme().syntax().clone(),
 91            selection_background_color: cx.theme().colors().element_selection_background,
 92            heading: Default::default(),
 93            ..Default::default()
 94        };
 95
 96        div()
 97            .flex()
 98            .bg(rgb(0x2e7d32))
 99            .size(Length::Definite(px(700.0).into()))
100            .justify_center()
101            .items_center()
102            .shadow_lg()
103            .border_1()
104            .border_color(rgb(0x0000ff))
105            .text_xl()
106            .text_color(rgb(0xffffff))
107            .child(
108                div()
109                    .child(MarkdownElement::new(self.markdown.clone(), markdown_style))
110                    .p_20(),
111            )
112    }
113}