use crate::icons::icons as embedded_icons;
use crate::styles::typography;
use crate::theming;
use crate::tokens::DESIGN_TOKENS;
use egui::{Pos2, Rect, Sense, Ui, Vec2};
#[derive(Debug, Clone, PartialEq)]
pub enum TradingPanelRowAction {
None,
ToggleExpanded,
Fullscreen,
}
#[derive(Debug, Clone)]
pub struct TradingPanelRowConfig {
pub height: f32,
pub left_padding: f32,
pub icon_size: f32,
pub icon_btn_size: f32,
pub right_padding: f32,
}
impl Default for TradingPanelRowConfig {
fn default() -> Self {
Self {
height: DESIGN_TOKENS.sizing.panel.trading_panel_row_height,
left_padding: DESIGN_TOKENS.spacing.lg,
icon_size: DESIGN_TOKENS.sizing.panel.trading_panel_row_icon_size,
icon_btn_size: DESIGN_TOKENS.sizing.panel.trading_panel_row_btn_size,
right_padding: DESIGN_TOKENS.spacing.md,
}
}
}
pub struct TradingPanelRow {
pub config: TradingPanelRowConfig,
pub is_expanded: bool,
}
impl Default for TradingPanelRow {
fn default() -> Self {
Self::new()
}
}
impl TradingPanelRow {
pub fn new() -> Self {
Self {
config: TradingPanelRowConfig::default(),
is_expanded: false,
}
}
pub fn with_config(config: TradingPanelRowConfig) -> Self {
Self {
config,
is_expanded: false,
}
}
pub fn set_expanded(&mut self, expanded: bool) {
self.is_expanded = expanded;
}
pub fn toggle_expanded(&mut self) {
self.is_expanded = !self.is_expanded;
}
pub fn show(&mut self, ui: &mut Ui) -> TradingPanelRowAction {
let mut action = TradingPanelRowAction::None;
let text_color = theming::text_color(ui);
let text_muted = theming::muted_color(ui);
let hover_bg = theming::btn_bg_hover(ui);
let separator_color = theming::separator_color(ui);
let available_width = ui.available_width();
let (response, painter) = ui.allocate_painter(
Vec2::new(available_width, self.config.height),
Sense::hover(),
);
let rect = response.rect;
if ui.is_rect_visible(rect) {
painter.hline(
rect.x_range(),
rect.min.y,
egui::Stroke::new(1.0, separator_color),
);
let text_pos = Pos2::new(rect.min.x + self.config.left_padding, rect.center().y);
painter.text(
text_pos,
egui::Align2::LEFT_CENTER,
"Trading Panel",
egui::FontId::proportional(typography::SM),
text_muted,
);
}
let btn_size = self.config.icon_btn_size;
let icon_size = self.config.icon_size;
let chevron_x = rect.max.x - self.config.right_padding - btn_size;
let chevron_rect = Rect::from_center_size(
Pos2::new(chevron_x + btn_size / 2.0, rect.center().y),
Vec2::splat(btn_size),
);
let chevron_response = ui.allocate_rect(chevron_rect, Sense::click());
if ui.is_rect_visible(chevron_rect) {
if chevron_response.hovered() {
painter.rect_filled(chevron_rect, DESIGN_TOKENS.rounding.sm, hover_bg);
}
let chevron_icon_rect =
Rect::from_center_size(chevron_rect.center(), Vec2::splat(icon_size));
let chevron_color = if chevron_response.hovered() {
text_color
} else {
text_muted
};
let chevron_icon = &embedded_icons::CHEVRON_DOWN;
chevron_icon
.as_image_tinted(Vec2::splat(icon_size), chevron_color)
.paint_at(ui, chevron_icon_rect);
}
if chevron_response.clicked() {
action = TradingPanelRowAction::ToggleExpanded;
}
chevron_response.on_hover_text(if self.is_expanded {
"Collapse"
} else {
"Expand"
});
let fullscreen_x = chevron_x - btn_size - DESIGN_TOKENS.spacing.xs;
let fullscreen_rect = Rect::from_center_size(
Pos2::new(fullscreen_x + btn_size / 2.0, rect.center().y),
Vec2::splat(btn_size),
);
let fullscreen_response = ui.allocate_rect(fullscreen_rect, Sense::click());
if ui.is_rect_visible(fullscreen_rect) {
if fullscreen_response.hovered() {
painter.rect_filled(fullscreen_rect, DESIGN_TOKENS.rounding.sm, hover_bg);
}
let fullscreen_icon_rect =
Rect::from_center_size(fullscreen_rect.center(), Vec2::splat(icon_size));
let fullscreen_color = if fullscreen_response.hovered() {
text_color
} else {
text_muted
};
embedded_icons::FULLSCREEN
.as_image_tinted(Vec2::splat(icon_size), fullscreen_color)
.paint_at(ui, fullscreen_icon_rect);
}
if fullscreen_response.clicked() {
action = TradingPanelRowAction::Fullscreen;
}
fullscreen_response.on_hover_text("Fullscreen");
action
}
}