add rfd for file dialog

This commit is contained in:
Fabien Freling 2024-04-22 14:09:31 +02:00
parent 67400dd16b
commit a1cfa56b5d
4 changed files with 270 additions and 39 deletions

View file

@ -1,5 +1,9 @@
use iced::{alignment, widget::{button, column, container, row, scrollable, text, text_input, Column}, Element, Length, Padding, Sandbox, Settings};
use iced::{
alignment,
widget::{button, column, container, row, scrollable, text, text_input, Column},
Element, Length, Padding, Sandbox, Settings,
};
use rfd::FileDialog;
#[derive(Debug, Clone)]
enum Message {
@ -7,15 +11,14 @@ enum Message {
Submitted,
DeleteItem(usize),
}
/**
* This is your model. It contains all the data needed for your application to work properly.
* The model can only be updated with the `update` function.
* This is your model. It contains all the data needed for your application to work properly.
* The model can only be updated with the `update` function.
*/
struct GroceryList {
grocery_items: Vec<String>,
input_value: String,
}
impl Sandbox for GroceryList {
@ -24,12 +27,8 @@ impl Sandbox for GroceryList {
/* Initialize your app */
fn new() -> GroceryList {
Self {
grocery_items: vec![
"Eggs".to_owned(),
"Milk".to_owned(),
"Flour".to_owned()
],
input_value: String::default()
grocery_items: vec!["Eggs".to_owned(), "Milk".to_owned(), "Flour".to_owned()],
input_value: String::default(),
}
}
@ -46,11 +45,23 @@ impl Sandbox for GroceryList {
Message::Submitted => {
let input_value = self.input_value.clone();
self.input_value = String::default(); // Clear the input value
self.grocery_items.push(input_value);
let _file = FileDialog::new()
.set_directory("/home/ffreling/Sync")
.pick_file();
let file_str = _file.and_then(|p| Some(String::from(p.to_str().unwrap())));
println!("{:?}", file_str);
match file_str {
None => (),
Some(path_str) => self.grocery_items.push(path_str),
}
// self.grocery_items.push(input_value);
}
Message::DeleteItem(item) => {
let _files = FileDialog::new()
.set_directory("/home/ffreling/Sync")
.pick_file();
self.grocery_items.remove(item);
},
}
}
}
@ -60,16 +71,14 @@ impl Sandbox for GroceryList {
items_list_view(&self.grocery_items),
row!(
text_input("Input grocery item", &self.input_value)
.on_input(|value| Message::InputValue(value))
.on_submit(Message::Submitted),
button("Submit")
.on_press(Message::Submitted)
.on_input(|value| Message::InputValue(value))
.on_submit(Message::Submitted),
button("Submit").on_press(Message::Submitted)
)
.spacing(30)
.padding(Padding::from(30))
)
.align_items(iced::Alignment::Center)
.align_items(iced::Alignment::Center),
)
.height(Length::Fill)
.width(Length::Fill)
@ -81,43 +90,34 @@ impl Sandbox for GroceryList {
fn theme(&self) -> iced::Theme {
iced::Theme::Dark
}
}
fn items_list_view(items: &Vec<String>) -> Element<'static, Message> {
let mut column = Column::new()
.spacing(20)
.align_items(iced::Alignment::Center)
.width(Length::Fill);
.spacing(20)
.align_items(iced::Alignment::Center)
.width(Length::Fill);
for (index, value) in items.into_iter().enumerate() {
column = column.push(grocery_item(index, value));
}
scrollable(
container(
column
)
)
.height(250.0)
.width(300)
.into()
scrollable(container(column))
.height(250.0)
.width(300)
.into()
}
fn grocery_item(index: usize, value: &str) -> Element<'static, Message> {
row!(
text(value),
button("Delete")
.on_press(Message::DeleteItem(index))
button("Delete").on_press(Message::DeleteItem(index))
)
.align_items(iced::Alignment::Center)
.spacing(30)
.into()
}
pub fn main() -> iced::Result {
GroceryList::run(Settings::default())
}