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