2021-01-17 12:11:04 +11:00
|
|
|
use objc::runtime::{Object};
|
|
|
|
use objc::{class, msg_send, sel, sel_impl};
|
|
|
|
use objc_id::ShareId;
|
|
|
|
|
|
|
|
use block::ConcreteBlock;
|
|
|
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
use crate::error::Error;
|
2021-01-19 19:11:52 +11:00
|
|
|
use crate::foundation::{id, nil, NSUInteger};
|
2021-01-17 12:11:04 +11:00
|
|
|
use crate::image::Image;
|
|
|
|
|
|
|
|
mod config;
|
|
|
|
pub use config::{ThumbnailConfig, ThumbnailQuality};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ThumbnailGenerator(pub ShareId<Object>);
|
|
|
|
|
|
|
|
impl ThumbnailGenerator {
|
|
|
|
pub fn shared() -> Self {
|
|
|
|
ThumbnailGenerator(unsafe {
|
|
|
|
ShareId::from_ptr(msg_send![class!(QLThumbnailGenerator), sharedGenerator])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate<F>(&self, url: &Url, config: ThumbnailConfig, callback: F)
|
|
|
|
where
|
|
|
|
F: Fn(Result<(Image, ThumbnailQuality), Error>) + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
let block = ConcreteBlock::new(move |thumbnail: id, thumbnail_type: NSUInteger, error: id| {
|
2021-01-19 19:11:52 +11:00
|
|
|
if error == nil {
|
|
|
|
unsafe {
|
|
|
|
let image = Image::with(msg_send![thumbnail, NSImage]);
|
|
|
|
let quality = ThumbnailQuality::from(thumbnail_type);
|
|
|
|
callback(Ok((image, ThumbnailQuality::Low)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let error = Error::new(error);
|
|
|
|
callback(Err(error));
|
2021-01-17 12:11:04 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let block = block.copy();
|
|
|
|
let request = config.to_request(url);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![&*self.0, generateRepresentationsForRequest:request
|
|
|
|
updateHandler:block];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|