story: Add open file to Markdown and Editor example. (#1363)
Close #1352
This commit is contained in:
parent
97df4efd46
commit
2574e7f812
4 changed files with 200 additions and 110 deletions
|
|
@ -24,7 +24,7 @@ use lsp_types::{
|
||||||
CodeAction, CodeActionKind, CompletionContext, CompletionItem, CompletionResponse,
|
CodeAction, CodeActionKind, CompletionContext, CompletionItem, CompletionResponse,
|
||||||
CompletionTextEdit, InsertReplaceEdit, TextEdit, WorkspaceEdit,
|
CompletionTextEdit, InsertReplaceEdit, TextEdit, WorkspaceEdit,
|
||||||
};
|
};
|
||||||
use story::Assets;
|
use story::{Assets, Open};
|
||||||
|
|
||||||
fn init() {
|
fn init() {
|
||||||
LanguageRegistry::singleton().register(
|
LanguageRegistry::singleton().register(
|
||||||
|
|
@ -865,6 +865,45 @@ impl Example {
|
||||||
lsp_store.update_diagnostics(diagnostics.clone());
|
lsp_store.update_diagnostics(diagnostics.clone());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 source file".into()),
|
||||||
|
});
|
||||||
|
|
||||||
|
let view = cx.entity();
|
||||||
|
let editor = self.editor.clone();
|
||||||
|
cx.spawn_in(window, async move |_, window| {
|
||||||
|
let path = path.await.ok()?.ok()??.iter().next()?.clone();
|
||||||
|
|
||||||
|
let language = path
|
||||||
|
.extension()
|
||||||
|
.and_then(|ext| ext.to_str())
|
||||||
|
.unwrap_or_default();
|
||||||
|
let language = Language::from_str(&language);
|
||||||
|
let lang = Lang::BuiltIn(language.clone());
|
||||||
|
let content = std::fs::read_to_string(&path).ok()?;
|
||||||
|
|
||||||
|
window
|
||||||
|
.update(|window, cx| {
|
||||||
|
_ = editor.update(cx, |this, cx| {
|
||||||
|
this.set_highlighter(language.name(), cx);
|
||||||
|
this.set_value(content, window, cx);
|
||||||
|
});
|
||||||
|
view.update(cx, |this, cx| {
|
||||||
|
this.language = lang;
|
||||||
|
cx.notify();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
|
||||||
|
Some(())
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Render for Example {
|
impl Render for Example {
|
||||||
|
|
@ -883,7 +922,11 @@ impl Render for Example {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
v_flex().size_full().child(
|
v_flex()
|
||||||
|
.id("app")
|
||||||
|
.size_full()
|
||||||
|
.on_action(cx.listener(Self::on_action_open))
|
||||||
|
.child(
|
||||||
v_flex()
|
v_flex()
|
||||||
.id("source")
|
.id("source")
|
||||||
.w_full()
|
.w_full()
|
||||||
|
|
@ -918,13 +961,19 @@ impl Render for Example {
|
||||||
.child(
|
.child(
|
||||||
Button::new("line-number")
|
Button::new("line-number")
|
||||||
.ghost()
|
.ghost()
|
||||||
.when(self.line_number, |this| this.icon(IconName::Check))
|
.when(self.line_number, |this| {
|
||||||
|
this.icon(IconName::Check)
|
||||||
|
})
|
||||||
.label("Line Number")
|
.label("Line Number")
|
||||||
.xsmall()
|
.xsmall()
|
||||||
.on_click(cx.listener(|this, _, window, cx| {
|
.on_click(cx.listener(|this, _, window, cx| {
|
||||||
this.line_number = !this.line_number;
|
this.line_number = !this.line_number;
|
||||||
this.editor.update(cx, |state, cx| {
|
this.editor.update(cx, |state, cx| {
|
||||||
state.set_line_number(this.line_number, window, cx);
|
state.set_line_number(
|
||||||
|
this.line_number,
|
||||||
|
window,
|
||||||
|
cx,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
cx.notify();
|
cx.notify();
|
||||||
})),
|
})),
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ use gpui::*;
|
||||||
use gpui_component::{
|
use gpui_component::{
|
||||||
highlighter::Language,
|
highlighter::Language,
|
||||||
input::{InputEvent, InputState, TabSize, TextInput},
|
input::{InputEvent, InputState, TabSize, TextInput},
|
||||||
resizable::{h_resizable, resizable_panel, ResizableState},
|
resizable::{ResizableState, h_resizable, resizable_panel},
|
||||||
text::TextView,
|
text::TextView,
|
||||||
};
|
};
|
||||||
use story::Assets;
|
use story::{Assets, Open};
|
||||||
|
|
||||||
pub struct Example {
|
pub struct Example {
|
||||||
input_state: Entity<InputState>,
|
input_state: Entity<InputState>,
|
||||||
|
|
@ -40,6 +40,33 @@ impl Example {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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> {
|
fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
|
||||||
cx.new(|cx| Self::new(window, cx))
|
cx.new(|cx| Self::new(window, cx))
|
||||||
}
|
}
|
||||||
|
|
@ -47,6 +74,11 @@ impl Example {
|
||||||
|
|
||||||
impl Render for Example {
|
impl Render for Example {
|
||||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
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())
|
h_resizable("container", self.resizable_state.clone())
|
||||||
.child(
|
.child(
|
||||||
resizable_panel().child(
|
resizable_panel().child(
|
||||||
|
|
@ -81,6 +113,7 @@ impl Render for Example {
|
||||||
.selectable(),
|
.selectable(),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
use gpui::{App, Menu, MenuItem, SharedString, actions};
|
use gpui::{App, Menu, MenuItem, SharedString};
|
||||||
use gpui_component::{ThemeMode, ThemeRegistry};
|
use gpui_component::{ThemeMode, ThemeRegistry};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
CloseWindow, Open, Quit, SelectLocale, ToggleSearch,
|
About, CloseWindow, Open, Quit, SelectLocale, ToggleSearch,
|
||||||
themes::{SwitchTheme, SwitchThemeMode},
|
themes::{SwitchTheme, SwitchThemeMode},
|
||||||
};
|
};
|
||||||
|
|
||||||
actions!(story, [About]);
|
|
||||||
|
|
||||||
pub fn init(title: impl Into<SharedString>, cx: &mut App) {
|
pub fn init(title: impl Into<SharedString>, cx: &mut App) {
|
||||||
cx.set_menus(vec![
|
cx.set_menus(vec![
|
||||||
Menu {
|
Menu {
|
||||||
|
|
@ -15,6 +13,8 @@ pub fn init(title: impl Into<SharedString>, cx: &mut App) {
|
||||||
items: vec![
|
items: vec![
|
||||||
MenuItem::action("About", About),
|
MenuItem::action("About", About),
|
||||||
MenuItem::Separator,
|
MenuItem::Separator,
|
||||||
|
MenuItem::action("Open...", Open),
|
||||||
|
MenuItem::Separator,
|
||||||
MenuItem::Submenu(Menu {
|
MenuItem::Submenu(Menu {
|
||||||
name: "Appearance".into(),
|
name: "Appearance".into(),
|
||||||
items: vec![
|
items: vec![
|
||||||
|
|
|
||||||
|
|
@ -140,8 +140,9 @@ pub struct SelectRadius(usize);
|
||||||
actions!(
|
actions!(
|
||||||
story,
|
story,
|
||||||
[
|
[
|
||||||
Quit,
|
About,
|
||||||
Open,
|
Open,
|
||||||
|
Quit,
|
||||||
CloseWindow,
|
CloseWindow,
|
||||||
ToggleSearch,
|
ToggleSearch,
|
||||||
TestAction,
|
TestAction,
|
||||||
|
|
@ -307,7 +308,14 @@ pub fn init(cx: &mut App) {
|
||||||
|
|
||||||
cx.bind_keys([
|
cx.bind_keys([
|
||||||
KeyBinding::new("/", ToggleSearch, None),
|
KeyBinding::new("/", ToggleSearch, None),
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
KeyBinding::new("cmd-o", Open, None),
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
KeyBinding::new("ctrl-o", Open, None),
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
KeyBinding::new("cmd-q", Quit, None),
|
KeyBinding::new("cmd-q", Quit, None),
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
KeyBinding::new("alt-f4", Quit, None),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
cx.on_action(|_: &Quit, cx: &mut App| {
|
cx.on_action(|_: &Quit, cx: &mut App| {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue