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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// DO NOT EDIT

use glib::object::Cast;
use glib::object::IsA;
use glib::translate::*;
use glib::StaticType;
use glib::ToValue;
use std::fmt;

glib::wrapper! {
    /// Contains information found when looking up an icon in [`IconTheme`][crate::IconTheme].
    ///
    /// [`IconPaintable`][crate::IconPaintable] implements [`gdk::Paintable`][crate::gdk::Paintable].
    ///
    /// # Implements
    ///
    /// [`trait@glib::ObjectExt`], [`trait@gdk::prelude::PaintableExt`]
    #[doc(alias = "GtkIconPaintable")]
    pub struct IconPaintable(Object<ffi::GtkIconPaintable>) @implements gdk::Paintable;

    match fn {
        type_ => || ffi::gtk_icon_paintable_get_type(),
    }
}

impl IconPaintable {
    /// Creates a [`IconPaintable`][crate::IconPaintable] for a file with a given size and scale.
    ///
    /// The icon can then be rendered by using it as a [`gdk::Paintable`][crate::gdk::Paintable].
    /// ## `file`
    /// a `GFile`
    /// ## `size`
    /// desired icon size
    /// ## `scale`
    /// the desired scale
    ///
    /// # Returns
    ///
    /// a [`IconPaintable`][crate::IconPaintable] containing
    ///  for the icon. Unref with `g_object_unref()`
    #[doc(alias = "gtk_icon_paintable_new_for_file")]
    #[doc(alias = "new_for_file")]
    pub fn for_file<P: IsA<gio::File>>(file: &P, size: i32, scale: i32) -> IconPaintable {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gtk_icon_paintable_new_for_file(
                file.as_ref().to_glib_none().0,
                size,
                scale,
            ))
        }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`IconPaintable`] objects.
    ///
    /// This method returns an instance of [`IconPaintableBuilder`] which can be used to create [`IconPaintable`] objects.
    pub fn builder() -> IconPaintableBuilder {
        IconPaintableBuilder::default()
    }

    /// Gets the `GFile` that was used to load the icon.
    ///
    /// Returns [`None`] if the icon was not loaded from a file.
    ///
    /// # Returns
    ///
    /// the `GFile` for the icon
    #[doc(alias = "gtk_icon_paintable_get_file")]
    #[doc(alias = "get_file")]
    pub fn file(&self) -> Option<gio::File> {
        unsafe { from_glib_full(ffi::gtk_icon_paintable_get_file(self.to_glib_none().0)) }
    }

    /// Get the icon name being used for this icon.
    ///
    /// When an icon looked up in the icon theme was not available, the
    /// icon theme may use fallback icons - either those specified to
    /// [`IconTheme::lookup_icon()`][crate::IconTheme::lookup_icon()] or the always-available
    /// "image-missing". The icon chosen is returned by this function.
    ///
    /// If the icon was created without an icon theme, this function
    /// returns [`None`].
    ///
    /// # Returns
    ///
    /// the themed icon-name for the
    ///  icon, or [`None`] if its not a themed icon.
    #[doc(alias = "gtk_icon_paintable_get_icon_name")]
    #[doc(alias = "get_icon_name")]
    pub fn icon_name(&self) -> Option<std::path::PathBuf> {
        unsafe { from_glib_none(ffi::gtk_icon_paintable_get_icon_name(self.to_glib_none().0)) }
    }

    /// Checks if the icon is symbolic or not.
    ///
    /// This currently uses only the file name and not the file contents
    /// for determining this. This behaviour may change in the future.
    ///
    /// Note that to render a symbolic [`IconPaintable`][crate::IconPaintable] properly (with
    /// recoloring), you have to set its icon name on a [`Image`][crate::Image].
    ///
    /// # Returns
    ///
    /// [`true`] if the icon is symbolic, [`false`] otherwise
    #[doc(alias = "gtk_icon_paintable_is_symbolic")]
    pub fn is_symbolic(&self) -> bool {
        unsafe { from_glib(ffi::gtk_icon_paintable_is_symbolic(self.to_glib_none().0)) }
    }
}

#[derive(Clone, Default)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`IconPaintable`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
pub struct IconPaintableBuilder {
    file: Option<gio::File>,
    icon_name: Option<String>,
    is_symbolic: Option<bool>,
}

impl IconPaintableBuilder {
    // rustdoc-stripper-ignore-next
    /// Create a new [`IconPaintableBuilder`].
    pub fn new() -> Self {
        Self::default()
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`IconPaintable`].
    pub fn build(self) -> IconPaintable {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        if let Some(ref file) = self.file {
            properties.push(("file", file));
        }
        if let Some(ref icon_name) = self.icon_name {
            properties.push(("icon-name", icon_name));
        }
        if let Some(ref is_symbolic) = self.is_symbolic {
            properties.push(("is-symbolic", is_symbolic));
        }
        glib::Object::new::<IconPaintable>(&properties)
            .expect("Failed to create an instance of IconPaintable")
    }

    /// The file representing the icon, if any.
    pub fn file<P: IsA<gio::File>>(mut self, file: &P) -> Self {
        self.file = Some(file.clone().upcast());
        self
    }

    /// The icon name that was chosen during lookup.
    pub fn icon_name(mut self, icon_name: &str) -> Self {
        self.icon_name = Some(icon_name.to_string());
        self
    }

    /// Whether the icon is symbolic or not.
    pub fn is_symbolic(mut self, is_symbolic: bool) -> Self {
        self.is_symbolic = Some(is_symbolic);
        self
    }
}

impl fmt::Display for IconPaintable {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("IconPaintable")
    }
}