## Break Change - The `Dropdown` has been renamed to `Select`. - The `TextInput` has been renamed to `Input`. | Before | After | | --- | --- | | TextInput | Input | | Dropdown | Select | | DropdownItem | SelectItem | | DropdownItemGroup | SelectGroup | | DropdownState | SelectState | | DropdownEvent | SelectEvent | | DropdownDelegate | SelectDelegate | | dropdown::ListEvent | ~~Removed~~ | - Renamed `PopupMenuExt` to `DropdownMenu`. - Removed pub mod `menu::popup_menu`, `menu::context_menu`, they are moved into `menu` mod. - Renamed `popup_menu` method to `dropdown_menu` for `DropdownMenu`, `Panel`, `PanelView` and `DropdownButton`. | Before | After | | --- | --- | | popup_menu::PopupMenuExt | menu::DropdownMenu | | popup_menu | dropdown_menu | | popup_menu_with_anchor | dropdown_menu_with_anchor |
126 lines
3.9 KiB
Rust
126 lines
3.9 KiB
Rust
use gpui::*;
|
|
use gpui_component::{
|
|
highlighter::Language,
|
|
input::{Input, InputEvent, InputState, TabSize},
|
|
resizable::{ResizableState, h_resizable, resizable_panel},
|
|
text::TextView,
|
|
};
|
|
use story::{Assets, Open};
|
|
|
|
pub struct Example {
|
|
input_state: Entity<InputState>,
|
|
resizable_state: Entity<ResizableState>,
|
|
_subscriptions: Vec<Subscription>,
|
|
}
|
|
|
|
const EXAMPLE: &str = include_str!("./fixtures/test.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()
|
|
})
|
|
.searchable(true)
|
|
.placeholder("Enter your Markdown here...")
|
|
.default_value(EXAMPLE)
|
|
});
|
|
let resizable_state = ResizableState::new(cx);
|
|
|
|
let _subscriptions = vec![cx.subscribe(&input_state, |_, _, _: &InputEvent, _| {})];
|
|
|
|
Self {
|
|
resizable_state,
|
|
input_state,
|
|
_subscriptions,
|
|
}
|
|
}
|
|
|
|
fn on_action_open(&mut self, _: &Open, window: &mut Window, cx: &mut Context<Self>) {
|
|
let path = cx.prompt_for_paths(PathPromptOptions {
|
|
files: true,
|
|
directories: true,
|
|
multiple: false,
|
|
prompt: Some("Select a Markdown file".into()),
|
|
});
|
|
|
|
let input_state = self.input_state.clone();
|
|
cx.spawn_in(window, async move |_, window| {
|
|
let path = path.await.ok()?.ok()??.iter().next()?.clone();
|
|
|
|
let content = std::fs::read_to_string(&path).ok()?;
|
|
|
|
window
|
|
.update(|window, cx| {
|
|
_ = input_state.update(cx, |this, cx| {
|
|
this.set_value(content, window, cx);
|
|
});
|
|
})
|
|
.ok();
|
|
|
|
Some(())
|
|
})
|
|
.detach();
|
|
}
|
|
|
|
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, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
div()
|
|
.id("editor")
|
|
.size_full()
|
|
.on_action(cx.listener(Self::on_action_open))
|
|
.child(
|
|
h_resizable("container", self.resizable_state.clone())
|
|
.child(
|
|
resizable_panel().child(
|
|
div()
|
|
.id("source")
|
|
.size_full()
|
|
.font_family("Monaco")
|
|
.text_size(px(12.))
|
|
.child(
|
|
Input::new(&self.input_state)
|
|
.h_full()
|
|
.p_0()
|
|
.border_0()
|
|
.focus_bordered(false),
|
|
),
|
|
),
|
|
)
|
|
.child(
|
|
resizable_panel().child(
|
|
TextView::markdown(
|
|
"preview",
|
|
self.input_state.read(cx).value().clone(),
|
|
window,
|
|
cx,
|
|
)
|
|
.flex_none()
|
|
.p_5()
|
|
.scrollable()
|
|
.selectable(),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let app = Application::new().with_assets(Assets);
|
|
|
|
app.run(move |cx| {
|
|
story::init(cx);
|
|
cx.activate(true);
|
|
|
|
story::create_new_window("Markdown Editor", Example::view, cx);
|
|
});
|
|
}
|