gpui-component/crates/story/examples/large-text.rs
Jason Lee a9953a9349
input: Use zed's Rope (#1214)
Now with 10K lines plain text, in release mode on macOS. The
`large-text` example can work with:

- 120 FPS for display.
- 90 FPS for update text.

```
MTL_HUD_ENABLED=1 cargo run --example large-text --release
```

- Fixed the cursor position at end of line, also support at start of
line.


https://github.com/user-attachments/assets/1de52f6c-e138-4ed3-a600-06372a807ed8

### Check list

- [x] Modifying Text with Rope
- [x] LineColumn, GoToLine
- [x] Code Highlight
- [x] Delete word, select word
- [x] Delete line
- [x] Move left, right, up, down
2025-09-08 16:44:52 +08:00

155 lines
5.5 KiB
Rust

use gpui::*;
use gpui_component::{
button::{Button, ButtonVariants as _},
h_flex,
input::{InputEvent, InputState, TabSize, TextInput},
v_flex, ActiveTheme, ContextModal, Selectable, Sizable,
};
use story::Assets;
pub struct Example {
editor: Entity<InputState>,
go_to_line_state: Entity<InputState>,
soft_wrap: bool,
_subscribes: Vec<Subscription>,
}
impl Example {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
// 10K lines
let text = "这是一个中文演示段落,用于展示更多的 [Markdown GFM] 内容。您可以在此尝试使用使用**粗体**、*斜体*和`代码`等样式。これは日本語のデモ段落です。Markdown の多言語サポートを示すためのテキストが含まれています。例えば、、**ボールド**、_イタリック_、および`コード`のスタイルなどを試すことができます。\n".repeat(10000);
let editor = cx.new(|cx| {
InputState::new(window, cx)
.multi_line()
.line_number(true)
.tab_size(TabSize {
tab_size: 4,
hard_tabs: false,
})
.soft_wrap(false)
.placeholder("Enter your code here...")
.default_value(text)
});
let go_to_line_state = cx.new(|cx| InputState::new(window, cx));
let _subscribes = vec![cx.subscribe(&editor, |_, _, _: &InputEvent, cx| {
cx.notify();
})];
Self {
editor,
go_to_line_state,
soft_wrap: false,
_subscribes,
}
}
fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
cx.new(|cx| Self::new(window, cx))
}
fn go_to_line(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
let editor = self.editor.clone();
let input_state = self.go_to_line_state.clone();
window.open_modal(cx, move |modal, window, cx| {
input_state.update(cx, |state, cx| {
state.set_placeholder(format!("{}", editor.read(cx).line_column()), window, cx);
state.focus(window, cx);
});
modal
.title("Go to line")
.child(TextInput::new(&input_state))
.confirm()
.on_ok({
let editor = editor.clone();
let input_state = input_state.clone();
move |_, window, cx| {
let query = input_state.read(cx).value();
let mut parts = query
.split(':')
.map(|s| s.trim().parse::<usize>().ok())
.collect::<Vec<_>>()
.into_iter();
let Some(line) = parts.next().and_then(|l| l) else {
return false;
};
let column = parts.next().and_then(|c| c);
editor.update(cx, |state, cx| {
state.go_to_line(line, column, window, cx);
});
true
}
})
});
}
fn toggle_soft_wrap(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
self.soft_wrap = !self.soft_wrap;
self.editor.update(cx, |state, cx| {
state.set_soft_wrap(self.soft_wrap, window, cx);
});
cx.notify();
}
}
impl Render for Example {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex().size_full().child(
v_flex()
.id("source")
.w_full()
.flex_1()
.child(
TextInput::new(&self.editor)
.bordered(false)
.h_full()
.focus_bordered(false),
)
.child(
h_flex()
.justify_between()
.text_sm()
.bg(cx.theme().secondary)
.py_1p5()
.px_4()
.border_t_1()
.border_color(cx.theme().border)
.text_color(cx.theme().muted_foreground)
.child(h_flex().gap_3().child({
Button::new("soft-wrap")
.ghost()
.xsmall()
.label("Soft Wrap")
.selected(self.soft_wrap)
.on_click(cx.listener(Self::toggle_soft_wrap))
}))
.child({
let loc = self.editor.read(cx).line_column();
let cursor = self.editor.read(cx).cursor();
Button::new("line-column")
.ghost()
.xsmall()
.label(format!("{} ({} c)", loc, cursor.offset()))
.on_click(cx.listener(Self::go_to_line))
}),
),
)
}
}
fn main() {
let app = Application::new().with_assets(Assets);
app.run(move |cx| {
story::init(cx);
cx.activate(true);
story::create_new_window("Large Text Editor", Example::view, cx);
});
}