use egui::{Color32, RichText, Ui};
use egui_cha::ViewCtx;
pub struct Link<'a> {
text: &'a str,
url: Option<&'a str>,
}
impl<'a> Link<'a> {
pub fn new(text: &'a str, url: &'a str) -> Self {
Self {
text,
url: Some(url),
}
}
pub fn clickable(text: &'a str) -> Self {
Self { text, url: None }
}
pub fn show(self, ui: &mut Ui) -> bool {
let is_dark = ui.ctx().style().visuals.dark_mode;
let color = if is_dark {
Color32::from_rgb(96, 165, 250)
} else {
Color32::from_rgb(59, 130, 246)
};
if let Some(url) = self.url {
ui.hyperlink_to(RichText::new(self.text).color(color), url)
.clicked()
} else {
let response = ui.link(RichText::new(self.text).color(color));
response.clicked()
}
}
pub fn on_click<Msg>(self, ctx: &mut ViewCtx<'_, Msg>, msg: Msg) -> bool {
let is_dark = ctx.ui.ctx().style().visuals.dark_mode;
let color = if is_dark {
Color32::from_rgb(96, 165, 250)
} else {
Color32::from_rgb(59, 130, 246)
};
let response = ctx.ui.link(RichText::new(self.text).color(color));
if response.clicked() {
ctx.emit(msg);
true
} else {
false
}
}
}