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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::prelude::*;
use crate::ui::imageviewer::initialization_flags::Flags;
use self::components::ScrollBars;
#[CustomControl(overwrite=OnPaint+OnKeyPressed+OnMouseEvent+OnResize, internal=true)]
pub struct ImageViewer {
surface: Surface,
image: Image,
render_options: image::RenderOptions,
x: i32,
y: i32,
background: Option<Character>,
flags: Flags,
drag_point: Option<Point>,
scrollbars: ScrollBars,
}
impl ImageViewer {
/// Creates a new image viewer with the specified image, layout and flags.
/// The flags can be a combination of the following values:
/// * `imageviewer::Flags::ScrollBars` - if set, the image viewer will have horizontal and vertical scrollbars
///
/// the render type method can be one of the following:
/// * `image::RenderMethod::SmallBlocks` - if set, the image will be rendered with small blocks
/// * `image::RenderMethod::LargeBlocks64Colors` - if set, the image will be rendered with large blocks
/// * `image::RenderMethod::GrayScale` - if set, the image will be rendered with gray scale
/// * `image::RenderMethod::AsciiArt` - if set, the image will be rendered as ascii art
///
/// the scale can be one of the following:
/// * `image::Scale::None` - if set, the image will be rendered with no scaling (as it is)
/// * `image::Scale::Scale5` - 5% scale
/// * `image::Scale::Scale10` - 10% scale
/// * `image::Scale::Scale20` - 20% scale
/// * `image::Scale::Scale25` - 25% scale
/// * `image::Scale::Scale33` - 33% scale
/// * `image::Scale::Scale50` - 50% scale
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
/// use std::str::FromStr;
///
/// let heart = Image::from_str(r#"
/// |.............|
/// |...rr...rr...|
/// |..rrrr.rrrr..|
/// |.rrrrrrrrrrr.|
/// |.raaaaaaaaar.|
/// |..ryyyyyyyr..|
/// | rwwwwwr |
/// |....rwwwr....|
/// |.....rwr.....|
/// |......r......|
/// "#).unwrap();
/// let iv = ImageViewer::new(heart, layout!("d:f"),
/// image::RenderOptionsBuilder::new()
/// .scale(image::Scale::Scale50)
/// .character_set(image::CharacterSet::SmallBlocks)
/// .color_schema(image::ColorSchema::Color16)
/// .luminance_threshold(0.5)
/// .build(),
/// imageviewer::Flags::None);
/// ```
pub fn new(image: Image, layout: Layout, render_options: image::RenderOptions, flags: Flags) -> Self {
let mut obj = Self {
base: ControlBase::with_status_flags(
layout,
(StatusFlags::Visible | StatusFlags::Enabled | StatusFlags::AcceptInput)
| if flags == Flags::ScrollBars {
StatusFlags::IncreaseBottomMarginOnFocus | StatusFlags::IncreaseRightMarginOnFocus
} else {
StatusFlags::None
},
),
surface: Surface::new(image.width(), image.height()),
x: 0,
y: 0,
flags,
image,
render_options,
background: None,
drag_point: None,
scrollbars: ScrollBars::new(flags == Flags::ScrollBars),
};
obj.update_surface();
if flags == Flags::ScrollBars {
let sz = obj.surface.size();
obj.scrollbars.update(0, 0, sz);
}
obj
}
/// Sets a new image to the image viewer.
pub fn set_image(&mut self, image: Image) {
self.image = image;
self.update_surface();
}
/// Returns the render options of the image viewer.
#[inline(always)]
pub fn render_options(&self) -> &image::RenderOptions {
&self.render_options
}
/// Sets the render options of the image viewer.
#[inline(always)]
pub fn set_render_options(&mut self, render_options: image::RenderOptions) {
self.render_options = render_options;
self.update_surface();
}
fn update_surface(&mut self) {
let sz = self.image.render_size(&self.render_options);
self.surface.resize(sz);
self.surface.draw_image(0, 0, &self.image, &self.render_options);
let sz = self.surface.size();
let control_size = self.size();
self.scrollbars.update(sz.width as u64, sz.height as u64, control_size);
self.move_scroll_to(self.x, self.y);
}
/// Sets the image viewer background character.
/// The background character is used to fill the empty space in the image viewer (e.g. if the image is smaller than the drawing area).
pub fn set_backgound(&mut self, backgroud_char: Character) {
self.background = Some(backgroud_char);
}
/// Clears the image viewer background character.
pub fn clear_background(&mut self) {
self.background = None;
}
fn move_scroll_to(&mut self, x: i32, y: i32) {
let sz = self.size();
let surface_size = self.surface.size();
self.x = if surface_size.width <= sz.width {
0
} else {
x.max((sz.width as i32) - (surface_size.width as i32))
};
self.y = if surface_size.height <= sz.height {
0
} else {
y.max((sz.height as i32) - (surface_size.height as i32))
};
self.x = self.x.min(0);
self.y = self.y.min(0);
self.scrollbars.set_indexes((-self.x) as u64, (-self.y) as u64);
}
fn update_scroll_pos_from_scrollbars(&mut self) {
let h = -(self.scrollbars.horizontal_index() as i32);
let v = -(self.scrollbars.vertical_index() as i32);
self.move_scroll_to(h, v);
}
}
impl OnResize for ImageViewer {
fn on_resize(&mut self, _old_size: Size, _new_size: Size) {
let paint_sz = self.surface.size();
self.scrollbars.resize(paint_sz.width as u64, paint_sz.height as u64,&self.base);
self.move_scroll_to(self.x, self.y);
}
}
impl OnPaint for ImageViewer {
fn on_paint(&self, surface: &mut Surface, theme: &Theme) {
if (self.has_focus()) && (self.flags == Flags::ScrollBars) {
self.scrollbars.paint(surface, theme, self);
surface.reduce_clip_by(0, 0, 1, 1);
}
if let Some(back) = self.background {
surface.clear(back);
}
surface.draw_surface(self.x, self.y, &self.surface);
}
}
impl OnKeyPressed for ImageViewer {
fn on_key_pressed(&mut self, key: Key, _character: char) -> EventProcessStatus {
match key.value() {
key!("Left") => {
self.move_scroll_to(self.x + 1, self.y);
EventProcessStatus::Processed
}
key!("Right") => {
self.move_scroll_to(self.x - 1, self.y);
EventProcessStatus::Processed
}
key!("Up") => {
self.move_scroll_to(self.x, self.y + 1);
EventProcessStatus::Processed
}
key!("Down") => {
self.move_scroll_to(self.x, self.y - 1);
EventProcessStatus::Processed
}
key!("Shift+Left") => {
self.move_scroll_to(0, self.y);
EventProcessStatus::Processed
}
key!("Shift+Right") => {
self.move_scroll_to(i32::MIN, self.y);
EventProcessStatus::Processed
}
key!("Shift+Up") => {
self.move_scroll_to(self.x, 0);
EventProcessStatus::Processed
}
key!("Shift+Down") => {
self.move_scroll_to(self.x, i32::MIN);
EventProcessStatus::Processed
}
key!("Ctrl+Left") => {
self.move_scroll_to(self.x + self.size().width as i32, self.y);
EventProcessStatus::Processed
}
key!("Ctrl+Right") => {
self.move_scroll_to(self.x - self.size().width as i32, self.y);
EventProcessStatus::Processed
}
key!("Ctrl+Up") | key!("PageUp") => {
self.move_scroll_to(self.x, self.y + self.size().height as i32);
EventProcessStatus::Processed
}
key!("Ctrl+Down") | key!("PageDown") => {
self.move_scroll_to(self.x, self.y - self.size().height as i32);
EventProcessStatus::Processed
}
key!("Home") => {
self.move_scroll_to(0, 0);
EventProcessStatus::Processed
}
key!("End") => {
self.move_scroll_to(i32::MIN, i32::MIN);
EventProcessStatus::Processed
}
_ => EventProcessStatus::Ignored,
}
}
}
impl OnMouseEvent for ImageViewer {
fn on_mouse_event(&mut self, event: &MouseEvent) -> EventProcessStatus {
if self.scrollbars.process_mouse_event(event) {
self.update_scroll_pos_from_scrollbars();
return EventProcessStatus::Processed;
}
let response = match event {
MouseEvent::Enter => EventProcessStatus::Ignored,
MouseEvent::Leave => EventProcessStatus::Ignored,
MouseEvent::Over(_) => EventProcessStatus::Ignored,
MouseEvent::Pressed(data) => {
if (self.flags == Flags::ScrollBars) && (self.has_focus()) {
let sz = self.size();
if (data.x == sz.width as i32) || (data.y == sz.height as i32) {
return EventProcessStatus::Ignored;
}
}
self.drag_point = Some(Point::new(data.x, data.y));
EventProcessStatus::Processed
}
MouseEvent::Released(data) => {
if let Some(p) = self.drag_point {
self.move_scroll_to(self.x + data.x - p.x, self.y + data.y - p.y);
}
self.drag_point = None;
EventProcessStatus::Processed
}
MouseEvent::DoubleClick(_) => EventProcessStatus::Ignored,
MouseEvent::Drag(data) => {
if let Some(p) = self.drag_point {
self.move_scroll_to(self.x + data.x - p.x, self.y + data.y - p.y);
}
self.drag_point = Some(Point::new(data.x, data.y));
EventProcessStatus::Processed
}
MouseEvent::Wheel(dir) => {
match dir {
MouseWheelDirection::Left => self.move_scroll_to(self.x + 1, self.y),
MouseWheelDirection::Right => self.move_scroll_to(self.x - 1, self.y),
MouseWheelDirection::Up => self.move_scroll_to(self.x, self.y + 1),
MouseWheelDirection::Down => self.move_scroll_to(self.x, self.y - 1),
};
EventProcessStatus::Processed
}
};
// if one of the components require a repaint, than we should repaint even if the canvas required us to ignore the event
if self.scrollbars.should_repaint() {
EventProcessStatus::Processed
} else {
response
}
}
}