use color_eyre::Result; use ratatui::{ layout::{Alignment, Rect}, widgets::{Block, Padding, Paragraph, Wrap}, Frame, }; use super::Component; use crate::{ config::Config, action::Action, palette::StylePalette, widgets::{PixelSize, BigText}, }; #[derive(Debug, Default)] pub struct LatestBlock { number: u32, palette: StylePalette } impl LatestBlock { fn update_block_number(&mut self, number: u32) -> Result<()> { self.number = number; Ok(()) } } impl Component for LatestBlock { fn register_config_handler(&mut self, config: Config) -> Result<()> { if let Some(style) = config.styles.get(&crate::app::Mode::Explorer) { self.palette.with_normal_style(style.get("normal_style").copied()); self.palette.with_hover_style(style.get("hover_style").copied()); self.palette.with_normal_border_style(style.get("normal_border_style").copied()); self.palette.with_hover_border_style(style.get("hover_border_style").copied()); self.palette.with_normal_title_style(style.get("normal_title_style").copied()); self.palette.with_hover_title_style(style.get("hover_title_style").copied()); } Ok(()) } fn update(&mut self, action: Action) -> Result> { match action { Action::NewBestBlock(number) => self.update_block_number(number)?, _ => {} }; Ok(None) } fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> { let [_, place, _] = super::explorer_block_info_layout(area); let text = self.number.to_string(); let (border_style, border_type) = self.palette.create_border_style(false); let height = place.as_size().height; let width = place.as_size().width; let text_width = text.len() as u16 * 4 + 2; if width < text_width || height < 7 { let paragraph = Paragraph::new(text) .block(Block::bordered() .border_style(border_style) .border_type(border_type) .title_alignment(Alignment::Right) .title_style(self.palette.create_title_style(false)) .padding(Padding::new(0, 0, height.saturating_sub(2) / 2, 0)) .title("Latest")) .alignment(Alignment::Center) .wrap(Wrap { trim: true }); frame.render_widget(paragraph, place); } else { let big_text = BigText::builder() .centered() .pixel_size(PixelSize::Quadrant) .style(self.palette.create_text_style(false)) .lines(vec![ text.into(), ]) .build(); let paragraph = Paragraph::new("") .block(Block::bordered() .border_style(border_style) .border_type(border_type) .title_alignment(Alignment::Right) .title_style(self.palette.create_title_style(false)) .title("Latest")) .alignment(Alignment::Center) .wrap(Wrap { trim: true }); frame.render_widget(paragraph, place); let height_offset = height.saturating_sub(2) / 2; let place = place.offset(ratatui::layout::Offset { x: 1, y: height_offset as i32 }) .intersection(place); frame.render_widget(big_text, place); } Ok(()) } }