summaryrefslogtreecommitdiff
path: root/tools/sticker-picker/src/sticker.rs
blob: 309b21fa33f0a034a796d3511555bc319e526d93 (plain)
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
// This file is part of Poezio.
//
// Poezio is free software: you can redistribute it and/or modify
// it under the terms of the GPL-3.0-or-later license. See the COPYING file.

use gtk::prelude::*;
use gtk::subclass::prelude::*;
use std::cell::RefCell;
use std::path::Path;

#[derive(Debug, Default)]
pub struct Sticker {
    filename: RefCell<Option<String>>,
    texture: RefCell<Option<gdk::Texture>>,
}

#[glib::object_subclass]
impl ObjectSubclass for Sticker {
    const NAME: &'static str = "Sticker";
    type Type = StickerType;
}

impl ObjectImpl for Sticker {
    fn properties() -> &'static [glib::ParamSpec] {
        use once_cell::sync::Lazy;
        static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
            vec![
                glib::ParamSpecString::new(
                    "filename",
                    "Filename",
                    "Filename",
                    None,
                    glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT_ONLY,
                ),
                glib::ParamSpecObject::new(
                    "texture",
                    "Texture",
                    "Texture",
                    gdk::Texture::static_type(),
                    glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT_ONLY,
                ),
            ]
        });
        PROPERTIES.as_ref()
    }

    fn set_property(
        &self,
        _obj: &StickerType,
        _id: usize,
        value: &glib::Value,
        pspec: &glib::ParamSpec,
    ) {
        match pspec.name() {
            "filename" => {
                let filename = value.get().unwrap();
                self.filename.replace(filename);
            }
            "texture" => {
                let texture = value.get().unwrap();
                self.texture.replace(texture);
            }
            _ => unimplemented!(),
        }
    }

    fn property(&self, _obj: &StickerType, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
        match pspec.name() {
            "filename" => self.filename.borrow().to_value(),
            "texture" => self.texture.borrow().to_value(),
            _ => unimplemented!(),
        }
    }
}

glib::wrapper! {
    pub struct StickerType(ObjectSubclass<Sticker>);
}

impl StickerType {
    pub fn new(filename: String, path: &Path) -> StickerType {
        let texture = gdk::Texture::from_filename(path).unwrap();
        glib::Object::new(&[("filename", &filename), ("texture", &texture)])
            .expect("Failed to create Sticker")
    }

    pub fn filename(&self) -> Option<String> {
        let imp = self.imp();
        let filename = imp.filename.borrow();
        if let Some(filename) = filename.as_ref() {
            Some(filename.clone())
        } else {
            None
        }
    }

    pub fn texture(&self) -> Option<gdk::Texture> {
        let imp = self.imp();
        let texture = imp.texture.borrow();
        if let Some(texture) = texture.as_ref() {
            Some(texture.clone())
        } else {
            None
        }
    }
}