gpui-component/crates/story/examples/markdown.rs
Jason Lee 2b796c94e9
highlighter: Improve performance for large file. (#911)
Still need to improve.

This version can work smooth when the file lines are less than 5000
lines.

> cargo run --release on MacBook Pro, Apple M3 CPU

- Editing at 110 FPS+
- Display at 120 FPS.

<img width="977" alt="image"
src="https://github.com/user-attachments/assets/37958bcb-20d8-486b-8c50-2728d16f971b"
/>
2025-06-03 23:20:33 +08:00

101 lines
3 KiB
Rust

use std::rc::Rc;
use gpui::*;
use gpui_component::{
highlighter::{HighlightTheme, Language},
input::{InputEvent, InputState, TabSize, TextInput},
resizable::{h_resizable, resizable_panel, ResizableState},
text::{TextView, TextViewStyle},
ActiveTheme as _,
};
use story::Assets;
pub struct Example {
input_state: Entity<InputState>,
resizable_state: Entity<ResizableState>,
}
const EXAMPLE: &str = include_str!("./markdown.md");
impl Example {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let input_state = cx.new(|cx| {
InputState::new(window, cx)
.code_editor(Language::Markdown)
.line_number(true)
.tab_size(TabSize {
tab_size: 2,
..Default::default()
})
.placeholder("Enter your Markdown here...")
.default_value(EXAMPLE)
});
let resizable_state = ResizableState::new(cx);
let _subscribe = cx.subscribe(&input_state, |_, _, _: &InputEvent, cx| {
cx.notify();
});
Self {
resizable_state,
input_state,
}
}
fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
cx.new(|cx| Self::new(window, cx))
}
}
impl Render for Example {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = if cx.theme().mode.is_dark() {
HighlightTheme::default_dark()
} else {
HighlightTheme::default_light()
};
let is_dark = cx.theme().mode.is_dark();
h_resizable("container", self.resizable_state.clone())
.child(
resizable_panel().child(
div()
.id("source")
.size_full()
.font_family("Monaco")
.text_size(px(12.))
.child(TextInput::new(&self.input_state).h_full().appearance(false)),
),
)
.child(
resizable_panel().child(
div()
.id("preview")
.size_full()
.p_5()
.overflow_y_scroll()
.child(
TextView::markdown("preview", self.input_state.read(cx).value()).style(
TextViewStyle {
highlight_theme: Rc::new(theme.clone()),
is_dark,
..Default::default()
},
),
),
),
)
}
}
fn main() {
let app = Application::new().with_assets(Assets);
app.run(move |cx| {
story::init(cx);
cx.activate(true);
story::create_new_window("Markdown Example", Example::view, cx);
});
}