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