diff --git a/examples/minimal.rs b/examples/minimal.rs index df24192..308ea1f 100644 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -1,8 +1,8 @@ use iced::{ - widget::{Button, Column, Container, Row, Slider, Space, Text}, + widget::{image::Handle, Button, Column, Container, Image, Row, Slider, Space, Text}, Element, }; -use iced_video_player::{Video, VideoPlayer}; +use iced_video_player::{Position, Video, VideoPlayer}; use std::time::Duration; fn main() -> iced::Result { @@ -23,11 +23,12 @@ struct App { video: Video, position: f64, dragging: bool, + thumb: Handle, } impl Default for App { fn default() -> Self { - let video = Video::new( + let mut video = Video::new( &url::Url::from_file_path( std::path::PathBuf::from(file!()) .parent() @@ -39,10 +40,16 @@ impl Default for App { .unwrap(), ) .unwrap(); + let thumb = video + .thumbnails(&[Position::Frame(0)]) + .unwrap() + .pop() + .unwrap(); App { video, position: 0.0, dragging: false, + thumb, } } } @@ -84,15 +91,16 @@ impl App { .push( Container::new( VideoPlayer::new(&self.video) - .width(iced::Length::Shrink) - .height(iced::Length::Shrink) + .width(iced::Length::Fill) + .height(iced::Length::Fill) .content_fit(iced::ContentFit::Contain) .on_end_of_stream(Message::EndOfStream) .on_new_frame(Message::NewFrame), ) + .align_x(iced::Alignment::Center) + .align_y(iced::Alignment::Center) .width(iced::Length::Fill) - .height(iced::Length::Fill) - .center(iced::Length::Fill), + .height(iced::Length::Fill), ) .push( Container::new( diff --git a/src/video_player.rs b/src/video_player.rs index 0a5bbb2..ed7045f 100644 --- a/src/video_player.rs +++ b/src/video_player.rs @@ -61,7 +61,7 @@ where /// Sets the `ContentFit` of the `VideoPlayer`. pub fn content_fit(self, content_fit: iced::ContentFit) -> Self { VideoPlayer { - content_fit: content_fit, + content_fit, ..self } } @@ -145,8 +145,31 @@ where let inner = self.video.0.borrow_mut(); let _ = inner.read_frame(); + // bounds based on `Image::draw` + let image_size = iced::Size::new(inner.width as f32, inner.height as f32); + let bounds = layout.bounds(); + let adjusted_fit = self.content_fit.fit(image_size, bounds.size()); + let scale = iced::Vector::new( + adjusted_fit.width / image_size.width, + adjusted_fit.height / image_size.height, + ); + let final_size = image_size * scale; + + let position = match self.content_fit { + iced::ContentFit::None => iced::Point::new( + bounds.x + (image_size.width - adjusted_fit.width) / 2.0, + bounds.y + (image_size.height - adjusted_fit.height) / 2.0, + ), + _ => iced::Point::new( + bounds.center_x() - final_size.width / 2.0, + bounds.center_y() - final_size.height / 2.0, + ), + }; + + let drawing_bounds = iced::Rectangle::new(position, final_size); + renderer.draw_primitive( - layout.bounds(), + drawing_bounds, VideoPrimitive::new( inner.id, Arc::clone(&inner.frame),