use std::cell::RefCell;
use image::imageops::FilterType;
use ratatui::layout::{Alignment, Rect};
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::widgets::{Block, Paragraph};
use ratatui::Frame;
use ratatui_image::picker::Picker;
use ratatui_image::protocol::StatefulProtocol;
use ratatui_image::StatefulImage;
use crate::tui::image_loader::ImageLoader;
const ART_COLS: u16 = 6;
const ART_ROWS: u16 = 3;
#[derive(Default)]
pub struct ArtProtocolState {
uri: Option<String>,
pub protocol: Option<StatefulProtocol>,
}
impl ArtProtocolState {
pub fn ensure_protocol(
&mut self,
art_uri: &Option<String>,
image_loader: &ImageLoader,
picker: &RefCell<Option<Picker>>,
) {
let uri_changed = self.uri.as_deref() != art_uri.as_deref();
if uri_changed {
self.uri = art_uri.clone();
self.protocol = None;
}
if self.protocol.is_none() {
if let Some(ref uri) = art_uri {
if let Some(img) = image_loader.get(uri) {
if let Some(ref mut p) = *picker.borrow_mut() {
let (fw, fh) = p.font_size();
let target_w = (ART_COLS * fw) as u32;
let target_h = (ART_ROWS * fh) as u32;
let resized = img.resize_exact(target_w, target_h, FilterType::Lanczos3);
self.protocol = Some(p.new_resize_protocol(resized));
}
}
}
}
}
}
pub fn render_album_art(
frame: &mut Frame,
area: Rect,
protocol: Option<&mut StatefulProtocol>,
background_style: Style,
music_note: &str,
) {
if area.width == 0 || area.height == 0 {
return;
}
match protocol {
Some(proto) => {
let image_widget = StatefulImage::new(None);
frame.render_stateful_widget(image_widget, area, proto);
}
None => {
render_placeholder(frame, area, background_style, music_note);
}
}
}
fn render_placeholder(frame: &mut Frame, area: Rect, bg_style: Style, note: &str) {
let block = Block::default().style(bg_style);
frame.render_widget(block, area);
let center_y = area.height / 2;
let note_area = Rect::new(area.x, area.y + center_y, area.width, 1);
let paragraph = Paragraph::new(Line::from(note.to_string()))
.alignment(Alignment::Center)
.style(bg_style);
frame.render_widget(paragraph, note_area);
}