Skip to content

Commit 0cb5f20

Browse files
authored
Merge pull request #933 from sdroege/pixbuf-iter-duration
gdk-pixbuf: Fix time related types
2 parents 0b7cc69 + b4ddd83 commit 0cb5f20

File tree

3 files changed

+47
-28
lines changed

3 files changed

+47
-28
lines changed

gdk-pixbuf/src/pixbuf_animation_iter.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

3-
use std::time::SystemTime;
3+
use std::time::{Duration, SystemTime};
44

55
use glib::translate::*;
66

@@ -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

@@ -45,8 +45,16 @@ impl PixbufAnimationIter {
4545

4646
#[doc(alias = "gdk_pixbuf_animation_iter_get_delay_time")]
4747
#[doc(alias = "get_delay_time")]
48-
pub fn delay_time(&self) -> i32 {
49-
unsafe { ffi::gdk_pixbuf_animation_iter_get_delay_time(self.to_glib_none().0) }
48+
pub fn delay_time(&self) -> Option<Duration> {
49+
unsafe {
50+
let res = ffi::gdk_pixbuf_animation_iter_get_delay_time(self.to_glib_none().0);
51+
52+
if res < 0 {
53+
None
54+
} else {
55+
Some(Duration::from_millis(res as u64))
56+
}
57+
}
5058
}
5159

5260
#[doc(alias = "gdk_pixbuf_animation_iter_on_currently_loading_frame")]

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::{prelude::*, subclass::prelude::*, translate::*};
912
use once_cell::sync::Lazy;
@@ -23,7 +26,7 @@ pub trait PixbufAnimationImpl: ObjectImpl {
2326
self.parent_size()
2427
}
2528

26-
fn iter(&self, start_time: Duration) -> PixbufAnimationIter {
29+
fn iter(&self, start_time: SystemTime) -> PixbufAnimationIter {
2730
self.parent_iter(start_time)
2831
}
2932
}
@@ -32,7 +35,7 @@ pub trait PixbufAnimationImplExt: ObjectSubclass {
3235
fn parent_is_static_image(&self) -> bool;
3336
fn parent_static_image(&self) -> Option<Pixbuf>;
3437
fn parent_size(&self) -> (i32, i32);
35-
fn parent_iter(&self, start_time: Duration) -> PixbufAnimationIter;
38+
fn parent_iter(&self, start_time: SystemTime) -> PixbufAnimationIter;
3639
}
3740

3841
impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T {
@@ -89,24 +92,27 @@ impl<T: PixbufAnimationImpl> PixbufAnimationImplExt for T {
8992
}
9093
}
9194

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

103+
let diff = start_time
104+
.duration_since(SystemTime::UNIX_EPOCH)
105+
.expect("failed to convert time");
100106
let time = glib::ffi::GTimeVal {
101-
tv_sec: start_time.as_secs() as _,
102-
tv_usec: start_time.subsec_micros() as _,
107+
tv_sec: diff.as_secs() as _,
108+
tv_usec: diff.subsec_micros() as _,
103109
};
104110
from_glib_full(f(
105111
self.obj()
106112
.unsafe_cast_ref::<PixbufAnimation>()
107113
.to_glib_none()
108114
.0,
109-
&time as *const _,
115+
&time,
110116
))
111117
}
112118
}
@@ -189,8 +195,9 @@ unsafe extern "C" fn animation_get_iter<T: PixbufAnimationImpl>(
189195
let instance = &*(ptr as *mut T::Instance);
190196
let imp = instance.imp();
191197

192-
let total = Duration::from_secs((*start_time_ptr).tv_sec.try_into().unwrap())
198+
let start_time = SystemTime::UNIX_EPOCH
199+
+ Duration::from_secs((*start_time_ptr).tv_sec.try_into().unwrap())
193200
+ Duration::from_micros((*start_time_ptr).tv_usec.try_into().unwrap());
194201

195-
imp.iter(total).into_glib_ptr()
202+
imp.iter(start_time).into_glib_ptr()
196203
}

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::{prelude::*, subclass::prelude::*, translate::*};
99
use once_cell::sync::Lazy;
@@ -25,16 +25,16 @@ pub trait PixbufAnimationIterImpl: ObjectImpl {
2525
self.parent_on_currently_loading_frame()
2626
}
2727

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

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

4040
impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
@@ -52,10 +52,10 @@ impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
5252
.unsafe_cast_ref::<PixbufAnimationIter>()
5353
.to_glib_none()
5454
.0);
55-
if time == -1 {
55+
if time < 0 {
5656
None
5757
} else {
58-
Some(Duration::from_millis(time.try_into().unwrap()))
58+
Some(Duration::from_millis(time as u64))
5959
}
6060
}
6161
}
@@ -94,7 +94,7 @@ impl<T: PixbufAnimationIterImpl> PixbufAnimationIterImplExt for T {
9494
}
9595
}
9696

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

106+
let diff = current_time
107+
.duration_since(SystemTime::UNIX_EPOCH)
108+
.expect("failed to convert time");
106109
let time = glib::ffi::GTimeVal {
107-
tv_sec: time.as_secs() as _,
108-
tv_usec: time.subsec_micros() as _,
110+
tv_sec: diff.as_secs() as _,
111+
tv_usec: diff.subsec_micros() as _,
109112
};
110113
from_glib(f(
111114
self.obj()
112115
.unsafe_cast_ref::<PixbufAnimationIter>()
113116
.to_glib_none()
114117
.0,
115-
&time as *const _,
118+
&time,
116119
))
117120
}
118121
}
@@ -165,13 +168,14 @@ unsafe extern "C" fn animation_iter_on_currently_loading_frame<T: PixbufAnimatio
165168

166169
unsafe extern "C" fn animation_iter_advance<T: PixbufAnimationIterImpl>(
167170
ptr: *mut ffi::GdkPixbufAnimationIter,
168-
time_ptr: *const glib::ffi::GTimeVal,
171+
current_time_ptr: *const glib::ffi::GTimeVal,
169172
) -> glib::ffi::gboolean {
170173
let instance = &*(ptr as *mut T::Instance);
171174
let imp = instance.imp();
172175

173-
let total = Duration::from_secs((*time_ptr).tv_sec.try_into().unwrap())
174-
+ Duration::from_micros((*time_ptr).tv_usec.try_into().unwrap());
176+
let current_time = SystemTime::UNIX_EPOCH
177+
+ Duration::from_secs((*current_time_ptr).tv_sec.try_into().unwrap())
178+
+ Duration::from_micros((*current_time_ptr).tv_usec.try_into().unwrap());
175179

176-
imp.advance(total).into_glib()
180+
imp.advance(current_time).into_glib()
177181
}

0 commit comments

Comments
 (0)