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