1 module nodes.image_view;
2 
3 import fluid;
4 
5 @safe:
6 
7 @("ImageView can load and display images")
8 unittest {
9 
10     auto image = generateColorImage(10, 10, color("#f00"));
11     auto view = imageView(image);
12     auto root = testSpace(view);
13 
14     root.drawAndAssert(
15         view.drawsImage(image),
16     );
17 
18 }
19 
20 @("ImageView uses specified size as minSize")
21 unittest {
22 
23     auto image = generateColorImage(10, 10, color("#f00"));
24     auto view = imageView(image, Vector2(50, 50));
25     auto root = testSpace(view);
26 
27     root.drawAndAssert(
28         view.drawsImage(image).at(0, 0, 50, 50)
29     );
30 
31 }
32 
33 @("ImageView can guess the size from Image")
34 unittest {
35 
36     auto image = generateColorImage(10, 10, color("#f00"));
37     auto view = imageView(image);
38     auto root = testSpace(view);
39 
40     root.drawAndAssert(
41         view.drawsImage(image).at(0, 0, 10, 10)
42     );
43 
44 }
45 
46 @("ImageView tries to fit the image into given area")
47 unittest {
48 
49     auto image = generateColorImage(10, 20, color("#f00"));
50     auto view = imageView(
51         .layout!(1, "fill"),
52         image
53     );
54     auto root = sizeLock!testSpace(
55         .sizeLimit(20, 20),
56         view
57     );
58 
59     // Fit into a square
60     root.drawAndAssert(
61         view.drawsImage(image).at(5, 0, 10, 20)
62     );
63 
64     // Smaller square
65     root.limit = sizeLimit(10, 10);
66     root.updateSize();
67     root.drawAndAssert(
68         view.drawsImage(image).at(2.5, 0, 5, 10)
69     );
70 
71     // Larger square
72     root.limit = sizeLimit(40, 40);
73     root.updateSize();
74     root.drawAndAssert(
75         view.drawsImage(image).at(10, 0, 20, 40)
76     );
77 
78     // Wide rectangle
79     root.limit = sizeLimit(40, 20);
80     root.updateSize();
81     root.drawAndAssert(
82         view.drawsImage(image).at(15, 0, 10, 20)
83     );
84 
85     // Tall rectangle
86     root.limit = sizeLimit(20, 40);
87     root.updateSize();
88     root.drawAndAssert(
89         view.drawsImage(image).at(0, 0, 20, 40)
90     );
91 
92 }
93 
94 @("ImageView can load and draw images from files")
95 unittest {
96 
97     auto view = imageView("logo.png");
98     auto stack = chain(
99         fileChain(),
100         arsdImageChain(),
101         view,
102     );
103     auto root = testSpace(stack);
104 
105     root.draw();
106     root.drawAndAssert(
107         view.drawsImage(view.image),
108     );
109 
110     // logo.png parameters
111     assert(view.image.width == 998);
112     assert(view.image.height == 480);
113 
114 }
115 
116 @("ImageView displays correctly in HiDPI")
117 unittest {
118 
119     auto view = imageView("icon.png");
120     auto stack = chain(
121         fileChain(),
122         arsdImageChain(),
123         view,
124     );
125     auto root = testSpace(stack);
126 
127     // 100% UI scale; image drawn at 96 DPI
128     root.drawAndAssert(
129         view.drawsImage().at(0, 0, 304, 303),
130     );
131     assert(view.image.dpiX == 96);
132     assert(view.image.dpiY == 96);
133 
134     // 125% UI scale; image drawn at 96 DPI
135     // Warning: Affected by https://git.samerion.com/Samerion/Fluid/issues/330
136     root.setScale(1.25);
137     root.drawAndAssert(
138         view.drawsImage().at(0.09, 0, 243.80, 243),
139     );
140 
141     // 125% UI scale; image drawn at 120 DPI
142     view.image.dpiX = 120;
143     view.image.dpiY = 120;
144     view.updateSize();
145     root.drawAndAssert(
146         view.drawsImage().at(0, 0, 304, 303),
147     );
148 
149 }