2021-02-05 08:34:12 +11:00
|
|
|
use std::path::Path;
|
|
|
|
|
2022-01-02 21:35:12 +11:00
|
|
|
use objc::runtime::Object;
|
2021-01-17 12:11:04 +11:00
|
|
|
use objc::{class, msg_send, sel, sel_impl};
|
|
|
|
use objc_id::ShareId;
|
|
|
|
|
|
|
|
use block::ConcreteBlock;
|
|
|
|
|
|
|
|
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 {
|
2022-01-02 21:35:12 +11:00
|
|
|
/// Returns the global shared, wrapped, QLThumbnailGenerator.
|
2021-01-17 12:11:04 +11:00
|
|
|
pub fn shared() -> Self {
|
|
|
|
ThumbnailGenerator(unsafe {
|
|
|
|
ShareId::from_ptr(msg_send![class!(QLThumbnailGenerator), sharedGenerator])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-02 21:35:12 +11:00
|
|
|
/// Given a path and config, will generate a preview image, calling back on the provided
|
|
|
|
/// callback closure.
|
|
|
|
///
|
|
|
|
/// Note that this callback can come back on a separate thread, so react accordingly to get to
|
|
|
|
/// the main thread if you need to.
|
|
|
|
pub fn generate_from_path<F>(&self, path: &Path, config: ThumbnailConfig, callback: F)
|
2021-01-17 12:11:04 +11:00
|
|
|
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);
|
2021-02-08 15:25:56 +11:00
|
|
|
callback(Ok((image, quality)));
|
2021-01-19 19:11:52 +11:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let error = Error::new(error);
|
|
|
|
callback(Err(error));
|
2021-01-17 12:11:04 +11:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let block = block.copy();
|
2021-02-05 08:34:12 +11:00
|
|
|
let request = config.to_request(path);
|
2021-01-17 12:11:04 +11:00
|
|
|
|
|
|
|
unsafe {
|
2022-07-11 01:15:29 +10:00
|
|
|
let _: () = msg_send![&*self.0, generateRepresentationsForRequest:request
|
2021-01-17 12:11:04 +11:00
|
|
|
updateHandler:block];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|