use crate::drawing::{Drawable, Plan};
use crate::layouts::{HasPlanner, Layout};
use skia_safe::{Canvas, Paint, Point, RRect, Rect};
use std::cell::RefCell;
use std::rc::Rc;
pub struct VideoProgressBar {
pub current_time: RefCell<i64>,
pub video_length: RefCell<i64>,
pub(crate) bg_paint: Paint,
pub(crate) progress_paint: Paint,
pub(crate) thumb_paint: Paint,
pub(crate) planner: RefCell<Option<Rc<dyn Layout>>>,
}
impl HasPlanner for VideoProgressBar {
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 VideoProgressBar {
fn draw(&self, canvas: &Canvas) -> anyhow::Result<()> {
let area = self.get_planned_drawing_area(canvas);
let bar_height = area.height();
canvas.draw_rrect(
RRect::new_rect_xy(area, bar_height / 2.0, bar_height / 2.0),
&self.bg_paint,
);
let ct = *self.current_time.borrow();
let vl = *self.video_length.borrow();
if vl > 0 {
let progress = (ct as f64 / vl as f64).clamp(0.0, 1.0) as f32;
let progress_width = area.width() * progress;
if progress_width > 0.0 {
let fill_rect = Rect::from_xywh(area.left, area.top, progress_width, bar_height);
canvas.draw_rrect(
RRect::new_rect_xy(fill_rect, bar_height / 2.0, bar_height / 2.0),
&self.progress_paint,
);
canvas.draw_circle(
Point::new(area.left + progress_width, area.top + bar_height / 2.0),
5.0,
&self.thumb_paint,
);
}
}
Ok(())
}
fn get_horizontal_plan(&self) -> Plan {
Plan::Fill
}
fn get_vertical_plan(&self) -> Plan {
Plan::Fill
}
}