use std::fmt::Debug;
use super::widgets::ErrorPopup;
use crate::bind::{Bind, MaybeSend, State};
impl<T: 'static, E: Debug + 'static> Bind<T, E> {
pub fn read_or_error<Fut>(&mut self, f: impl FnOnce() -> Fut, ui: &mut egui::Ui) -> Option<&T>
where
Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,
T: MaybeSend,
E: MaybeSend,
{
self.poll();
if let Some(Err(e)) = &self.data {
if ErrorPopup::new(&format!("{e:?}")).show(ui.ctx()) {
self.request(f());
}
None
} else if let Some(Ok(data)) = self.data.as_ref() {
Some(data)
} else {
None
}
}
pub fn read_mut_or_error<Fut>(
&mut self,
f: impl FnOnce() -> Fut,
ui: &mut egui::Ui,
) -> Option<&mut T>
where
Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,
T: MaybeSend,
E: MaybeSend,
{
self.poll();
if let Some(Err(e)) = &self.data {
if ErrorPopup::new(&format!("{e:?}")).show(ui.ctx()) {
self.request(f());
}
None
} else if let Some(Ok(data)) = self.data.as_mut() {
Some(data)
} else {
None
}
}
pub fn read_or_request_or_error<Fut>(
&mut self,
f: impl FnOnce() -> Fut,
ui: &mut egui::Ui,
) -> Option<&T>
where
Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,
T: MaybeSend,
E: MaybeSend,
{
self.poll();
if matches!(self.state, State::Idle) {
self.request(f());
None
} else if let Some(Err(e)) = &self.data {
if ErrorPopup::new(&format!("{e:?}")).show(ui.ctx()) {
self.request(f());
}
None
} else if let Some(Ok(data)) = self.data.as_ref() {
Some(data)
} else {
None
}
}
pub fn read_mut_or_request_or_error<Fut>(
&mut self,
f: impl FnOnce() -> Fut,
ui: &mut egui::Ui,
) -> Option<&mut T>
where
Fut: Future<Output = Result<T, E>> + MaybeSend + 'static,
T: MaybeSend,
E: MaybeSend,
{
self.poll();
if matches!(self.state, State::Idle) {
self.request(f());
None
} else if let Some(Err(e)) = &self.data {
if ErrorPopup::new(&format!("{e:?}")).show(ui.ctx()) {
self.request(f());
}
None
} else if let Some(Ok(data)) = self.data.as_mut() {
Some(data)
} else {
None
}
}
}