improv(file_chooser): make subscription handler a generic closure
This commit is contained in:
parent
405aaf134d
commit
9f27d2b7f5
1 changed files with 9 additions and 5 deletions
|
|
@ -44,8 +44,12 @@ pub fn save_file() -> Option<save::Dialog> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a subscription for file chooser events.
|
/// Creates a subscription for file chooser events.
|
||||||
pub fn subscription<M: Send + 'static>(handle: fn(Message) -> M) -> Subscription<M> {
|
pub fn subscription<M, H>(handle: H) -> Subscription<M>
|
||||||
let type_id = std::any::TypeId::of::<Handler<M>>();
|
where
|
||||||
|
M: Send + 'static,
|
||||||
|
H: Fn(Message) -> M + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
let type_id = std::any::TypeId::of::<Handler<M, H>>();
|
||||||
|
|
||||||
iced::subscription::channel(type_id, 1, move |output| async move {
|
iced::subscription::channel(type_id, 1, move |output| async move {
|
||||||
let mut state = Handler {
|
let mut state = Handler {
|
||||||
|
|
@ -156,13 +160,13 @@ impl Sender {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Handler<M> {
|
struct Handler<M, Handle: Fn(Message) -> M> {
|
||||||
active: Option<ashpd::desktop::Request<SelectedFiles>>,
|
active: Option<ashpd::desktop::Request<SelectedFiles>>,
|
||||||
handle: fn(Message) -> M,
|
handle: Handle,
|
||||||
output: channel::mpsc::Sender<M>,
|
output: channel::mpsc::Sender<M>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M> Handler<M> {
|
impl<M, Handle: Fn(Message) -> M> Handler<M, Handle> {
|
||||||
/// Emits close request if there is an active dialog request.
|
/// Emits close request if there is an active dialog request.
|
||||||
async fn close(&mut self) {
|
async fn close(&mut self) {
|
||||||
if let Some(request) = self.active.take() {
|
if let Some(request) = self.active.take() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue