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
use kas::draw::{DrawShared, ImageHandle};
use kas::layout::PixmapScaling;
use kas::prelude::*;
#[cfg(feature = "image")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "image")))]
#[derive(thiserror::Error, Debug)]
pub enum ImageError {
#[error("IO error")]
IOError(#[from] std::io::Error),
#[error(transparent)]
Image(#[from] image::ImageError),
#[error("failed to allocate texture space for image")]
Allocation,
}
#[cfg(feature = "image")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "image")))]
impl From<kas::draw::AllocError> for ImageError {
fn from(_: kas::draw::AllocError) -> ImageError {
ImageError::Allocation
}
}
#[cfg(feature = "image")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "image")))]
pub type Result<T> = std::result::Result<T, ImageError>;
impl_scope! {
#[derive(Clone, Debug, Default)]
#[widget]
pub struct Image {
core: widget_core!(),
scaling: PixmapScaling,
handle: Option<ImageHandle>,
}
impl Self {
#[inline]
pub fn new(handle: ImageHandle, draw: &mut dyn DrawShared) -> Option<Self> {
let mut sprite = Self::default();
sprite.set(handle, draw).map(|_| sprite)
}
#[cfg(feature = "image")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "image")))]
#[inline]
pub fn new_path<P: AsRef<std::path::Path>>(
path: P,
draw: &mut dyn DrawShared,
) -> Result<Self> {
let mut sprite = Self::default();
let _ = sprite.load_path(path, draw)?;
Ok(sprite)
}
pub fn set(&mut self, handle: ImageHandle, draw: &mut dyn DrawShared) -> Option<TkAction> {
if let Some(size) = draw.image_size(&handle) {
self.scaling.size = size.cast();
self.handle = Some(handle);
Some(TkAction::RESIZE)
} else {
None
}
}
#[cfg(feature = "image")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "image")))]
pub fn load_path<P: AsRef<std::path::Path>>(
&mut self,
path: P,
draw: &mut dyn DrawShared,
) -> Result<TkAction> {
let image = image::io::Reader::open(path)?
.with_guessed_format()?
.decode()?;
let image = image.into_rgba8();
let size = image.dimensions();
let handle = draw.image_alloc(size)?;
draw.image_upload(&handle, &image, kas::draw::ImageFormat::Rgba8);
if let Some(old_handle) = self.handle.take() {
draw.image_free(old_handle);
}
self.scaling.size = size.cast();
self.handle = Some(handle);
Ok(TkAction::RESIZE)
}
pub fn clear(&mut self, draw: &mut dyn DrawShared) -> TkAction {
if let Some(handle) = self.handle.take() {
draw.image_free(handle);
TkAction::RESIZE
} else {
TkAction::empty()
}
}
#[inline]
#[must_use]
pub fn with_scaling(mut self, f: impl FnOnce(&mut PixmapScaling)) -> Self {
f(&mut self.scaling);
self
}
#[inline]
pub fn set_scaling(&mut self, f: impl FnOnce(&mut PixmapScaling)) -> TkAction {
f(&mut self.scaling);
TkAction::RESIZE
}
}
impl Layout for Image {
fn size_rules(&mut self, size_mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
self.scaling.size_rules(size_mgr, axis)
}
fn set_rect(&mut self, mgr: &mut ConfigMgr, rect: Rect) {
let scale_factor = mgr.size_mgr().scale_factor();
self.core.rect = self.scaling.align_rect(rect, scale_factor);
}
fn draw(&mut self, mut draw: DrawMgr) {
if let Some(id) = self.handle.as_ref().map(|h| h.id()) {
draw.image(self.rect(), id);
}
}
}
}