use nami::Computed;
use nami::Signal;
use nami::signal::IntoComputed;
use waterui_controls::{
IntoLabel,
button::{ButtonStyle, button},
};
use waterui_core::{Environment, Str, View};
#[cfg(not(target_os = "espidf"))]
fn open_url(url: &str) {
if let Err(e) = robius_open::Uri::new(url).open() {
tracing::error!("Failed to open URL '{}': {:?}", url, e);
}
}
#[cfg(target_os = "espidf")]
fn open_url(_url: &str) {}
#[derive(Debug)]
pub struct Link<Label> {
label: Label,
url: Computed<Str>,
}
impl<Label> Link<Label>
where
Label: IntoLabel + 'static,
{
pub fn new(label: Label, url: impl IntoComputed<Str>) -> Self {
Self {
label,
url: url.into_computed(),
}
}
}
impl<Label> View for Link<Label>
where
Label: IntoLabel + 'static,
{
fn body(self, _env: &Environment) -> impl View {
let url = self.url;
button(self.label).style(ButtonStyle::Link).action(move || {
let url_str = url.get();
open_url(&url_str);
})
}
}
pub fn link<Label>(label: Label, url: impl IntoComputed<Str>) -> Link<Label>
where
Label: IntoLabel + 'static,
{
Link::new(label, url)
}