Skip to content

Commit b4ddd83

Browse files
committed
gdk-pixbuf: Use SystemTime instead of Duration for PixbufAnimation::iter() and PixbufAnimationIter::advance()
They're `GTimeVal` relative to the UNIX epoch in C and not a plain duration. Fixes https://github.com/gtk-rs/gtk-rs-core/918
1 parent 6e7c200 commit b4ddd83

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

gdk-pixbuf/src/pixbuf_animation_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ glib::wrapper! {
1717

1818
impl PixbufAnimationIter {
1919
#[doc(alias = "gdk_pixbuf_animation_iter_advance")]
20-
pub fn advance(&self, start_time: SystemTime) -> bool {
21-
let diff = start_time
20+
pub fn advance(&self, current_time: SystemTime) -> bool {
21+
let diff = current_time
2222
.duration_since(SystemTime::UNIX_EPOCH)
2323
.expect("failed to convert time");
2424

gdk-pixbuf/src/subclass/pixbuf_animation.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// rustdoc-stripper-ignore-next
44
//! Traits intended for subclassing [`PixbufAnimation`](crate::PixbufAnimation).
55
6-
use std::{mem::MaybeUninit, time::Duration};
6+
use std::{
7+
mem::MaybeUninit,
8+
time::{Duration, SystemTime},
9+
};
710

811
use glib::{subclass::prelude::*, translate::*, Cast};
912

@@ -22,7 +25,7 @@ pub trait PixbufAnimationImpl: ObjectImpl {
2225
self.parent_size()
2326
}
2427

25-
fn iter(&self, start_time: Duration) -> PixbufAnimationIter {
28+
fn iter(&self, start_time: SystemTime) -> PixbufAnimationIter {
2629
self.parent_iter(start_time)
2730
}
2831
}
@@ -31,7 +34,7 @@ pub trait PixbufAnimationImplExt: ObjectSubclass {
3134
fn parent_is_static_image(&self) -> bool;
3235
fn parent_static_image(&self) -> Option<Pixbuf>;
3336
fn parent_size(&self) -> (i32, i32);
34-
fn parent_iter(&self, start_time: Duration) -> PixbufAnimationIter;
37+
fn parent_iter(&self, start_time: SystemTime) -> PixbufAnimationIter;
3538
}
3639

3740
impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T {
@@ -88,24 +91,27 @@ impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T {
8891
}
8992
}
9093

91-
fn parent_iter(&self, start_time: Duration) -> PixbufAnimationIter {
94+
fn parent_iter(&self, start_time: SystemTime) -> PixbufAnimationIter {
9295
unsafe {
9396
let data = T::type_data();
9497
let parent_class = data.as_ref().parent_class() as *mut ffi::GdkPixbufAnimationClass;
9598
let f = (*parent_class)
9699
.get_iter
97100
.expect("No parent class implementation for \"get_iter\"");
98101

102+
let diff = start_time
103+
.duration_since(SystemTime::UNIX_EPOCH)
104+
.expect("failed to convert time");
99105
let time = glib::ffi::GTimeVal {
100-
tv_sec: start_time.as_secs() as _,
101-
tv_usec: start_time.subsec_micros() as _,
106+
tv_sec: diff.as_secs() as _,
107+
tv_usec: diff.subsec_micros() as _,
102108
};
103109
from_glib_full(f(
104110
self.obj()
105111
.unsafe_cast_ref::<PixbufAnimation>()
106112
.to_glib_none()
107113
.0,
108-
&time as *const _,
114+
&time,
109115
))
110116
}
111117
}
@@ -169,8 +175,9 @@ unsafe extern "C" fn animation_get_iter<T: PixbufAnimationImpl>(
169175
let instance = &*(ptr as *mut T::Instance);
170176
let imp = instance.imp();
171177

172-
let total = Duration::from_secs((*start_time_ptr).tv_sec.try_into().unwrap())
178+
let start_time = SystemTime::UNIX_EPOCH
179+
+ Duration::from_secs((*start_time_ptr).tv_sec.try_into().unwrap())
173180
+ Duration::from_micros((*start_time_ptr).tv_usec.try_into().unwrap());
174181

175-
imp.iter(total).into_glib_ptr()
182+
imp.iter(start_time).into_glib_ptr()
176183
}

gdk-pixbuf/src/subclass/pixbuf_animation_iter.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// rustdoc-stripper-ignore-next
44
//! Traits intended for subclassing [`PixbufAnimationIter`](crate::PixbufAnimationIter).
55
6-
use std::time::Duration;
6+
use std::time::{Duration, SystemTime};
77

88
use glib::{subclass::prelude::*, translate::*, Cast};
99

@@ -24,16 +24,16 @@ pub trait PixbufAnimationIterImpl: ObjectImpl {
2424
self.parent_on_currently_loading_frame()
2525
}
2626

27-
fn advance(&self, time: Duration) -> bool {
28-
self.parent_advance(time)
27+
fn advance(&self, current_time: SystemTime) -> bool {
28+
self.parent_advance(current_time)
2929
}
3030
}
3131

3232
pub trait PixbufAnimationIterImplExt: ObjectSubclass {
3333
fn parent_delay_time(&self) -> Option<Duration>;
3434
fn parent_pixbuf(&self) -> Pixbuf;
3535
fn parent_on_currently_loading_frame(&self) -> bool;
36-
fn parent_advance(&self, time: Duration) -> bool;
36+
fn parent_advance(&self, current_time: SystemTime) -> bool;
3737
}
3838

3939
impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
@@ -51,10 +51,10 @@ impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
5151
.unsafe_cast_ref::<PixbufAnimationIter>()
5252
.to_glib_none()
5353
.0);
54-
if time == -1 {
54+
if time < 0 {
5555
None
5656
} else {
57-
Some(Duration::from_millis(time.try_into().unwrap()))
57+
Some(Duration::from_millis(time as u64))
5858
}
5959
}
6060
}
@@ -93,7 +93,7 @@ impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
9393
}
9494
}
9595

96-
fn parent_advance(&self, time: Duration) -> bool {
96+
fn parent_advance(&self, current_time: SystemTime) -> bool {
9797
unsafe {
9898
let data = T::type_data();
9999
let parent_class =
@@ -102,16 +102,19 @@ impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
102102
.advance
103103
.expect("No parent class implementation for \"advance\"");
104104

105+
let diff = current_time
106+
.duration_since(SystemTime::UNIX_EPOCH)
107+
.expect("failed to convert time");
105108
let time = glib::ffi::GTimeVal {
106-
tv_sec: time.as_secs() as _,
107-
tv_usec: time.subsec_micros() as _,
109+
tv_sec: diff.as_secs() as _,
110+
tv_usec: diff.subsec_micros() as _,
108111
};
109112
from_glib(f(
110113
self.obj()
111114
.unsafe_cast_ref::<PixbufAnimationIter>()
112115
.to_glib_none()
113116
.0,
114-
&time as *const _,
117+
&time,
115118
))
116119
}
117120
}
@@ -158,13 +161,14 @@ unsafe extern "C" fn animation_iter_on_currently_loading_frame<T: PixbufAnimatio
158161

159162
unsafe extern "C" fn animation_iter_advance<T: PixbufAnimationIterImpl>(
160163
ptr: *mut ffi::GdkPixbufAnimationIter,
161-
time_ptr: *const glib::ffi::GTimeVal,
164+
current_time_ptr: *const glib::ffi::GTimeVal,
162165
) -> glib::ffi::gboolean {
163166
let instance = &*(ptr as *mut T::Instance);
164167
let imp = instance.imp();
165168

166-
let total = Duration::from_secs((*time_ptr).tv_sec.try_into().unwrap())
167-
+ Duration::from_micros((*time_ptr).tv_usec.try_into().unwrap());
169+
let current_time = SystemTime::UNIX_EPOCH
170+
+ Duration::from_secs((*current_time_ptr).tv_sec.try_into().unwrap())
171+
+ Duration::from_micros((*current_time_ptr).tv_usec.try_into().unwrap());
168172

169-
imp.advance(total).into_glib()
173+
imp.advance(current_time).into_glib()
170174
}

0 commit comments

Comments
 (0)