fix bounds calculation
This commit is contained in:
parent
5f53b18796
commit
ae461822a8
2 changed files with 40 additions and 9 deletions
|
@ -1,8 +1,8 @@
|
||||||
use iced::{
|
use iced::{
|
||||||
widget::{Button, Column, Container, Row, Slider, Space, Text},
|
widget::{image::Handle, Button, Column, Container, Image, Row, Slider, Space, Text},
|
||||||
Element,
|
Element,
|
||||||
};
|
};
|
||||||
use iced_video_player::{Video, VideoPlayer};
|
use iced_video_player::{Position, Video, VideoPlayer};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
|
@ -23,11 +23,12 @@ struct App {
|
||||||
video: Video,
|
video: Video,
|
||||||
position: f64,
|
position: f64,
|
||||||
dragging: bool,
|
dragging: bool,
|
||||||
|
thumb: Handle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for App {
|
impl Default for App {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let video = Video::new(
|
let mut video = Video::new(
|
||||||
&url::Url::from_file_path(
|
&url::Url::from_file_path(
|
||||||
std::path::PathBuf::from(file!())
|
std::path::PathBuf::from(file!())
|
||||||
.parent()
|
.parent()
|
||||||
|
@ -39,10 +40,16 @@ impl Default for App {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let thumb = video
|
||||||
|
.thumbnails(&[Position::Frame(0)])
|
||||||
|
.unwrap()
|
||||||
|
.pop()
|
||||||
|
.unwrap();
|
||||||
App {
|
App {
|
||||||
video,
|
video,
|
||||||
position: 0.0,
|
position: 0.0,
|
||||||
dragging: false,
|
dragging: false,
|
||||||
|
thumb,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,15 +91,16 @@ impl App {
|
||||||
.push(
|
.push(
|
||||||
Container::new(
|
Container::new(
|
||||||
VideoPlayer::new(&self.video)
|
VideoPlayer::new(&self.video)
|
||||||
.width(iced::Length::Shrink)
|
.width(iced::Length::Fill)
|
||||||
.height(iced::Length::Shrink)
|
.height(iced::Length::Fill)
|
||||||
.content_fit(iced::ContentFit::Contain)
|
.content_fit(iced::ContentFit::Contain)
|
||||||
.on_end_of_stream(Message::EndOfStream)
|
.on_end_of_stream(Message::EndOfStream)
|
||||||
.on_new_frame(Message::NewFrame),
|
.on_new_frame(Message::NewFrame),
|
||||||
)
|
)
|
||||||
|
.align_x(iced::Alignment::Center)
|
||||||
|
.align_y(iced::Alignment::Center)
|
||||||
.width(iced::Length::Fill)
|
.width(iced::Length::Fill)
|
||||||
.height(iced::Length::Fill)
|
.height(iced::Length::Fill),
|
||||||
.center(iced::Length::Fill),
|
|
||||||
)
|
)
|
||||||
.push(
|
.push(
|
||||||
Container::new(
|
Container::new(
|
||||||
|
|
|
@ -61,7 +61,7 @@ where
|
||||||
/// Sets the `ContentFit` of the `VideoPlayer`.
|
/// Sets the `ContentFit` of the `VideoPlayer`.
|
||||||
pub fn content_fit(self, content_fit: iced::ContentFit) -> Self {
|
pub fn content_fit(self, content_fit: iced::ContentFit) -> Self {
|
||||||
VideoPlayer {
|
VideoPlayer {
|
||||||
content_fit: content_fit,
|
content_fit,
|
||||||
..self
|
..self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,8 +145,31 @@ where
|
||||||
let inner = self.video.0.borrow_mut();
|
let inner = self.video.0.borrow_mut();
|
||||||
let _ = inner.read_frame();
|
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(
|
renderer.draw_primitive(
|
||||||
layout.bounds(),
|
drawing_bounds,
|
||||||
VideoPrimitive::new(
|
VideoPrimitive::new(
|
||||||
inner.id,
|
inner.id,
|
||||||
Arc::clone(&inner.frame),
|
Arc::clone(&inner.frame),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue