use egui::{Response, RichText, Ui};
use crate::ext::UiExt;
use crate::tokens::DESIGN_TOKENS;
pub struct PnlLabel {
value: f64,
text: String,
strong: bool,
size: Option<f32>,
}
impl PnlLabel {
pub fn currency(value: f64) -> Self {
let sign = if value >= 0.0 { "+" } else { "" };
Self {
value,
text: format!("{sign}${value:.2}"),
strong: false,
size: None,
}
}
pub fn percent(value: f64) -> Self {
let sign = if value >= 0.0 { "+" } else { "" };
Self {
value,
text: format!("{sign}{value:.2}%"),
strong: false,
size: None,
}
}
pub fn value(value: f64, text: impl Into<String>) -> Self {
Self {
value,
text: text.into(),
strong: false,
size: None,
}
}
#[must_use]
pub fn strong(mut self) -> Self {
self.strong = true;
self
}
#[must_use]
pub fn size(mut self, size: f32) -> Self {
self.size = Some(size);
self
}
pub fn show(self, ui: &mut Ui) -> Response {
let color = ui.pnl_color(self.value);
let mut rt = RichText::new(&self.text).color(color);
if self.strong {
rt = rt.strong();
}
if let Some(s) = self.size {
rt = rt.size(s);
}
ui.label(rt)
}
}
pub struct SideLabel {
is_buy: bool,
text: String,
size: Option<f32>,
}
impl SideLabel {
pub fn buy() -> Self {
Self {
is_buy: true,
text: "BUY".to_string(),
size: None,
}
}
pub fn sell() -> Self {
Self {
is_buy: false,
text: "SELL".to_string(),
size: None,
}
}
pub fn long() -> Self {
Self {
is_buy: true,
text: "LONG".to_string(),
size: None,
}
}
pub fn short() -> Self {
Self {
is_buy: false,
text: "SHORT".to_string(),
size: None,
}
}
pub fn new(is_buy: bool, text: impl Into<String>) -> Self {
Self {
is_buy,
text: text.into(),
size: None,
}
}
#[must_use]
pub fn size(mut self, size: f32) -> Self {
self.size = Some(size);
self
}
pub fn show(self, ui: &mut Ui) -> Response {
let color = if self.is_buy {
DESIGN_TOKENS.semantic.extended.bullish
} else {
DESIGN_TOKENS.semantic.extended.bearish
};
let mut rt = RichText::new(&self.text).color(color);
if let Some(s) = self.size {
rt = rt.size(s);
}
ui.label(rt)
}
}