Replies: 1 comment
-
use gpui::*;
use gpui_component::input::{Input, InputState};
use gpui_component::label::Label;
use gpui_component::{button::*, *};
use std::path::PathBuf;
pub struct HelloWorld {
file_path: Option<PathBuf>,
input_state: Entity<InputState>,
}
impl HelloWorld {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let input = cx.new(|cx| InputState::new(window, cx));
HelloWorld {
file_path: None,
input_state: input,
}
}
fn select_file(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
let file_options = PathPromptOptions {
files: true, // Enable file selection
directories: false, // Disable directory selection
multiple: false, // Disable multi-selection
prompt: Some(SharedString::from("Please select a file")), // Custom prompt
};
let receiver = cx.prompt_for_paths(file_options);
cx.spawn_in(window, async move |state, cx| match receiver.await {
Ok(file_result) => match file_result {
Ok(Some(paths)) => {
if let Some(selected_file) = paths.into_iter().next() {
println!("Selected single file: {}", selected_file.display());
let value = selected_file.display().to_string();
let _ = cx.update(|window, cx| {
let _ = state.update(cx, |state, cx| {
state.file_path = Some(selected_file);
state
.input_state
.update(cx, |input, cx| input.set_value(value, window, cx))
});
});
}
}
Ok(None) => println!("User canceled file selection"),
Err(e) => eprintln!("File selection failed: {}", e),
},
Err(canceled) => eprintln!("File selection was canceled: {}", canceled),
})
.detach();
}
}
impl Render for HelloWorld {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.v_flex()
.gap_2() // Vertical Flex layout
.size_full() // Parent container takes full height
.items_center()
.justify_center()
.child("Hello, World!")
.child(
div()
.h_full()
.h_flex()
.justify_between()
.child(Label::new("File:"))
.w_1_6()
.child(
Input::new(&self.input_state)
.focus_bordered(false)
.disabled(false)
.prefix(Icon::new(IconName::Search).small())
.suffix(
Button::new("btn")
.primary()
.label("Please select!")
.on_click(cx.listener(Self::select_file)),
),
)
.w_5_6(),
)
}
}
fn main() {
let app = Application::new();
app.run(move |cx| {
// This must be called before using any GPUI Component features.
gpui_component::init(cx);
cx.spawn(async move |cx| {
cx.open_window(WindowOptions::default(), |window, cx| {
let view = cx.new(|cx| HelloWorld::new(window, cx));
// This first level on the window, should be a Root.
cx.new(|cx| Root::new(view.into(), window, cx))
})?;
Ok::<_, anyhow::Error>(())
})
.detach();
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want to click a button in the app to select a file and display the file path string in the input component. This requires using asynchronous operations, but I don’t know how to implement it.
error[E0501]: cannot borrow
*cxas mutable because previous closure requires unique access--> src\main.rs:41:33
|
41 | let _ = state.update(cx, |state, _cx| {
| ^ ------ ------------ closure construction occurs here
| | |
| _______| first borrow later used by call
| |
42 | | state.input_state.update(cx, |input, cx| {
| | -- first borrow occurs due to use of
*cxin closure43 | | input.set_value(selected_file.display().to_string(), window, cx);
44 | | });
45 | |
46 | | state.file_path = Some(selected_file);
47 | | });
| |^ second borrow occurs here
error[E0500]: closure requires unique access to
*cxbut it is already borrowed--> src\main.rs:41:50
|
41 | let _ = state.update(cx, |state, _cx| {
| ------ -- ^^^^^^^^^^^^ closure construction occurs here
| | |
| | borrow occurs here
| first borrow later used by call
42 | state.input_state.update(cx, |input, cx| {
| -- second borrow occurs due to use of
*cxin closureerror[E0521]: borrowed data escapes outside of method
--> src\main.rs:36:9
|
23 | fn select_file(&mut self, : &ClickEvent, window: &mut Window, cx: &mut Context) {
| ------ - let's call the lifetime of this reference
'1| |
|
windowis a reference that is only valid in the method body...
36 | / cx.spawn(async move |state, cx| match receiver.await {
37 | | Ok(file_result) => match file_result {
38 | | Ok(Some(paths)) => {
39 | | if let Some(selected_file) = paths.into_iter().next() {
... |
53 | | Err(canceled) => eprintln!("File selection was canceled: {}", canceled),
54 | | })
| | ^
| | |
| |_________
windowescapes the method body here| argument requires that
'1must outlive'staticSome errors have detailed explanations: E0500, E0501, E0521.
For more information about an error, try
rustc --explain E0500.error: could not compile
gpui_demo(bin "gpui_demo") due to 3 previous errorsBeta Was this translation helpful? Give feedback.
All reactions