fix: open file picker asynchronously

This commit is contained in:
uku 2025-05-12 12:13:55 +02:00
parent 42a6e50349
commit 2f96459f58
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o

View file

@ -8,7 +8,7 @@ use color_eyre::eyre::{OptionExt, Result, bail};
use gobject::GtkZiplineFolder;
use relm::{Dialog, DialogInput};
use relm4::{
Component, ComponentController, ComponentParts, Controller, RelmApp, SimpleComponent,
Component, ComponentController, ComponentParts, Controller, RelmApp,
adw::{self, prelude::*},
gtk::{self, gio, glib::clone},
};
@ -39,12 +39,17 @@ impl Config {
#[derive(Debug)]
enum Message {
SetPath(PathBuf),
OpenFilePicker,
SetFolder(ZiplineFolder),
StartTheProcess,
Nothing,
}
#[derive(Debug)]
enum CommandMessage {
SelectedFile(Option<PathBuf>),
}
struct Widgets {
file_picker_row: adw::ActionRow,
send_button: gtk::Button,
@ -66,9 +71,10 @@ impl Tyrolienne {
}
}
impl SimpleComponent for Tyrolienne {
impl Component for Tyrolienne {
type Input = Message;
type Output = ();
type CommandOutput = CommandMessage;
type Init = (Config, Vec<ZiplineFolder>);
type Root = adw::ApplicationWindow;
type Widgets = Widgets;
@ -103,15 +109,7 @@ impl SimpleComponent for Tyrolienne {
file_picker_row.connect_activated(clone!(
#[strong]
sender,
move |_| {
let file = rfd::FileDialog::new()
.add_filter("Video", &["mp4", "mkv", "webm"])
.pick_file();
if let Some(path) = file {
sender.input(Message::SetPath(path));
}
}
move |_| sender.input(Message::OpenFilePicker)
));
let mut gtk_folders = folders
@ -184,10 +182,14 @@ impl SimpleComponent for Tyrolienne {
ComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, _sender: relm4::ComponentSender<Self>) {
fn update(
&mut self,
message: Self::Input,
sender: relm4::ComponentSender<Self>,
_root: &Self::Root,
) {
match message {
Message::Nothing => {}
Message::SetPath(path) => self.video_path = Some(path),
Message::SetFolder(folder) => {
self.folder = (folder.id != GtkZiplineFolder::NONE_ID).then_some(folder)
}
@ -198,6 +200,27 @@ impl SimpleComponent for Tyrolienne {
body: e.to_string(),
}),
},
Message::OpenFilePicker => sender.oneshot_command(async {
CommandMessage::SelectedFile(
rfd::AsyncFileDialog::new()
.add_filter("Video file", &["mp4", "mkv", "webm"])
.pick_file()
.await
.map(|h| h.path().to_owned()),
)
}),
}
}
fn update_cmd(
&mut self,
message: Self::CommandOutput,
_sender: relm4::ComponentSender<Self>,
_root: &Self::Root,
) {
match message {
CommandMessage::SelectedFile(Some(path)) => self.video_path = Some(path),
CommandMessage::SelectedFile(None) => {}
}
}