1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
#![deny(missing_docs)]
#![allow(unused_doc_comments)]

//! This library contains some widgets for relm.
//!
//! These widgets got extracted from OMMUI-related projects so they can
//! be used in several of them.

#[macro_use]
extern crate relm_derive;
#[macro_use]
extern crate relm;

extern crate failure;
extern crate gdk_pixbuf;
extern crate glib;
extern crate gtk;
extern crate indexmap;
extern crate ommui_data;
extern crate pango;
extern crate uuid;

pub mod boolean_parameter_entry;
pub mod description_item;
pub mod description_listbox;
pub mod description_listview;
pub mod enumeration_parameter_entry;
pub mod error;
pub mod message;
pub mod numeric_parameter_entry;
pub mod parameter_editor;
pub mod parameter_query;
pub mod profile_list;
pub mod progress;
pub mod text_entry;

pub use error::{Error, ErrorKind};

/// The result returned by functions in this crate.
pub type Result<T> = std::result::Result<T, Error>;

/// A translation trait.
pub trait XTr: std::fmt::Debug {
    /// Translate a string.
    fn xtr(&self, s: &str) -> String;
}

impl XTr for indexmap::IndexMap<String, String> {
    fn xtr(&self, s: &str) -> String {
        if let Some(t) = self.get(&s.to_string()) {
            t.to_string()
        } else {
            s.to_string()
        }
    }
}

impl<T: XTr> XTr for std::sync::RwLock<T> {
    fn xtr(&self, s: &str) -> String {
        self.read().unwrap().xtr(s)
    }
}

impl<T: XTr> XTr for std::sync::Arc<T> {
    fn xtr(&self, s: &str) -> String {
        (**self).xtr(s)
    }
}

fn tr<S: AsRef<str>>(s: S) -> String {
    s.as_ref().to_string()
}

/// A trait for providing pixbufs.
pub trait Pixbufs {
    /// Get a pixbuf.
    fn get_pixbuf(&self, k: &str) -> Option<gdk_pixbuf::Pixbuf>;
}

impl Pixbufs for indexmap::IndexMap<String, gdk_pixbuf::Pixbuf> {
    fn get_pixbuf(&self, k: &str) -> Option<gdk_pixbuf::Pixbuf> {
        self.get(&k.to_string()).cloned()
    }
}

/// A trait for items loadable from in-memory byte arrays.
pub trait LoadFromBytes: Sized {
    /// Load the item with a defined pixel size.
    fn load_from_bytes_with_size(
        data: &'static [u8],
        pixelsize: u16,
    ) -> Result<Self>;
}

impl LoadFromBytes for gdk_pixbuf::Pixbuf {
    fn load_from_bytes_with_size(
        data: &'static [u8],
        pixelsize: u16,
    ) -> Result<Self> {
        let b = glib::Bytes::from_static(data);
        let stream = gio::MemoryInputStream::new_from_bytes(&b);
        Ok(gdk_pixbuf::Pixbuf::new_from_stream_at_scale(
            &stream,
            pixelsize as i32,
            pixelsize as i32,
            true,
            None,
        )?)
    }
}