-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathsimple_image.rs
68 lines (56 loc) · 1.97 KB
/
simple_image.rs
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
// Copyright 2020 the Xilem Authors and the Druid Authors
// SPDX-License-Identifier: Apache-2.0
//! This showcase demonstrates how to use the image widget and its
//! properties. You can change the parameters in the GUI to see how
//! everything behaves.
// On Windows platform, don't show a console when opening the app.
#![windows_subsystem = "windows"]
use masonry::dpi::LogicalSize;
use masonry::widget::{Image, ObjectFit, RootWidget};
use masonry::{Action, AppDriver, DriverCtx, WidgetId};
use vello::peniko::{Image as ImageBuf, ImageFormat};
use winit::window::Window;
struct Driver;
impl AppDriver for Driver {
fn on_action(&mut self, _ctx: &mut DriverCtx<'_>, _widget_id: WidgetId, _action: Action) {}
}
fn make_image() -> Image {
let image_bytes = include_bytes!("./assets/PicWithAlpha.png");
let image_data = image::load_from_memory(image_bytes).unwrap().to_rgba8();
let (width, height) = image_data.dimensions();
let png_data = ImageBuf::new(
image_data.to_vec().into(),
ImageFormat::Rgba8,
width,
height,
);
Image::new(png_data).fit_mode(ObjectFit::Contain)
}
fn main() {
let window_size = LogicalSize::new(650.0, 450.0);
let window_attributes = Window::default_attributes()
.with_title("Simple image example")
.with_min_inner_size(window_size)
.with_max_inner_size(window_size);
masonry::event_loop_runner::run(
masonry::event_loop_runner::EventLoop::with_user_event(),
window_attributes,
RootWidget::new(make_image()),
Driver,
)
.unwrap();
}
// --- MARK: TESTS ---
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;
use masonry::assert_render_snapshot;
use masonry::testing::TestHarness;
use super::*;
#[test]
fn screenshot_test() {
let mut harness = TestHarness::create(make_image());
assert_debug_snapshot!(harness.root_widget());
assert_render_snapshot!(harness, "initial_screenshot");
}
}