mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-24 16:21:04 +00:00
34 lines
992 B
Rust
34 lines
992 B
Rust
use mlua::{Function, IntoLua, Lua, Value};
|
|
use yazi_adapter::{ADAPTOR, Image};
|
|
use yazi_binding::UrlRef;
|
|
|
|
use super::Utils;
|
|
use crate::{bindings::ImageInfo, elements::Rect};
|
|
|
|
impl Utils {
|
|
pub(super) fn image_info(lua: &Lua) -> mlua::Result<Function> {
|
|
lua.create_async_function(|lua, url: UrlRef| async move {
|
|
if let Ok(info) = yazi_adapter::ImageInfo::new(&url).await {
|
|
ImageInfo::from(info).into_lua(&lua)
|
|
} else {
|
|
Value::Nil.into_lua(&lua)
|
|
}
|
|
})
|
|
}
|
|
|
|
pub(super) fn image_show(lua: &Lua) -> mlua::Result<Function> {
|
|
lua.create_async_function(|lua, (url, rect): (UrlRef, Rect)| async move {
|
|
if let Ok(area) = ADAPTOR.get().image_show(&url, *rect).await {
|
|
Rect::from(area).into_lua(&lua)
|
|
} else {
|
|
Value::Nil.into_lua(&lua)
|
|
}
|
|
})
|
|
}
|
|
|
|
pub(super) fn image_precache(lua: &Lua) -> mlua::Result<Function> {
|
|
lua.create_async_function(|_, (src, dist): (UrlRef, UrlRef)| async move {
|
|
Ok(Image::precache(&src, dist.to_path_buf()).await.is_ok())
|
|
})
|
|
}
|
|
}
|