use crate::drawing::{Drawable, Plan};
use crate::layouts::{HasPlanner, Layout};
use skia_safe::{Canvas, Font, Paint, Point};
use std::cell::RefCell;
use std::rc::Rc;
fn format_time(ms: i64) -> String {
if ms < 0 {
return "00:00".to_string();
}
let total_secs = ms / 1000;
format!("{:02}:{:02}", total_secs / 60, total_secs % 60)
}
pub struct TimeText {
pub current_time: RefCell<i64>,
pub video_length: RefCell<i64>,
pub(crate) font: Font,
pub(crate) text_paint: Paint,
pub(crate) shadow_paint: Paint,
pub(crate) planner: RefCell<Option<Rc<dyn Layout>>>,
}
impl HasPlanner for TimeText {
fn planner(&self) -> &RefCell<Option<Rc<dyn Layout>>> {
&self.planner
}
fn set_planner(&self, p: Option<Rc<dyn Layout>>) {
*self.planner.borrow_mut() = p;
}
}
impl Drawable for TimeText {
fn draw(&self, canvas: &Canvas) -> anyhow::Result<()> {
let area = self.get_planned_drawing_area(canvas);
let time_text = format!(
"{} / {}",
format_time(*self.current_time.borrow()),
format_time(*self.video_length.borrow())
);
let (text_width, _) = self.font.measure_str(&time_text, None);
let text_x = area.right - text_width;
let text_y = area.bottom;
canvas.draw_str(
&time_text,
Point::new(text_x + 1.5, text_y + 1.5),
&self.font,
&self.shadow_paint,
);
canvas.draw_str(
&time_text,
Point::new(text_x, text_y),
&self.font,
&self.text_paint,
);
Ok(())
}
fn get_horizontal_plan(&self) -> Plan {
Plan::Fill
}
fn get_vertical_plan(&self) -> Plan {
Plan::Fill
}
}