search_provider/
result_metadata.rs
#[cfg(feature = "gdk")]
use gdk::prelude::*;
use serde::Serialize;
use zbus::zvariant::{SerializeDict, Type};
use crate::ResultID;
#[derive(SerializeDict, Type, Debug, Default)]
#[zvariant(signature = "dict")]
pub struct ResultMeta {
id: ResultID,
name: String,
description: Option<String>,
#[zvariant(rename = "clipboardText")]
clipboard_text: Option<String>,
icon: Option<zbus::zvariant::OwnedValue>,
gicon: Option<String>,
#[zvariant(rename = "icon-data")]
icon_data: Option<IconData>,
}
impl ResultMeta {
pub fn builder(id: ResultID, name: &str) -> ResultMetaBuilder {
ResultMetaBuilder::new(id, name)
}
}
#[derive(Debug, Type, Serialize)]
pub struct IconData {
pub width: i32,
pub height: i32,
pub rowstride: i32,
pub has_alpha: bool,
pub bits_per_sample: i32,
pub n_channels: i32,
pub data: Vec<u8>,
}
#[cfg(feature = "gdk-pixbuf")]
impl From<&gdk_pixbuf::Pixbuf> for IconData {
fn from(pixbuf: &gdk_pixbuf::Pixbuf) -> Self {
let data = pixbuf.read_pixel_bytes();
Self {
width: pixbuf.width(),
height: pixbuf.height(),
rowstride: pixbuf.rowstride(),
has_alpha: pixbuf.has_alpha(),
bits_per_sample: pixbuf.bits_per_sample(),
n_channels: pixbuf.n_channels(),
data: data.to_vec(),
}
}
}
#[cfg(feature = "gdk")]
impl From<&gdk::Texture> for IconData {
fn from(texture: &gdk::Texture) -> Self {
const BITS_PER_SAMPLE: i32 = 8; const N_CHANNELS: i32 = 4; const HAS_ALPHA: bool = true; let width = texture.width();
let height = texture.height();
let mut downloader = gdk::TextureDownloader::new(texture);
downloader.set_format(gdk::MemoryFormat::R8g8b8a8);
let (data, rowstride) = downloader.download_bytes();
Self {
width,
height,
rowstride: rowstride as i32,
has_alpha: HAS_ALPHA,
bits_per_sample: BITS_PER_SAMPLE,
n_channels: N_CHANNELS,
data: data.to_vec(),
}
}
}
pub struct ResultMetaBuilder {
id: String,
name: String,
description: Option<String>,
clipboard_text: Option<String>,
gicon: Option<String>,
icon: Option<zbus::zvariant::OwnedValue>,
icon_data: Option<IconData>,
}
impl ResultMetaBuilder {
pub fn new(id: ResultID, name: &str) -> Self {
Self {
id,
name: name.to_owned(),
gicon: None,
description: None,
icon: None,
icon_data: None,
clipboard_text: None,
}
}
pub fn description(mut self, description: &str) -> Self {
self.description = Some(description.to_owned());
self
}
pub fn clipboard_text(mut self, clipboard_text: &str) -> Self {
self.clipboard_text = Some(clipboard_text.to_owned());
self
}
pub fn gicon(mut self, gicon: &str) -> Self {
self.gicon = Some(gicon.to_owned());
self
}
pub fn icon(mut self, icon: zbus::zvariant::OwnedValue) -> Self {
self.icon = Some(icon);
self
}
pub fn icon_data(mut self, icon_data: IconData) -> Self {
self.icon_data = Some(icon_data);
self
}
pub fn build(self) -> ResultMeta {
ResultMeta {
id: self.id,
name: self.name,
gicon: self.gicon,
description: self.description,
icon: self.icon,
icon_data: self.icon_data,
clipboard_text: self.clipboard_text,
}
}
}
#[cfg(test)]
mod tests {
use zbus::zvariant::Type;
#[test]
fn icon_data_signature() {
assert_eq!(super::IconData::SIGNATURE, "(iiibiiay)");
}
}