1
1
use crate :: { parts:: DecorationParts , theme} ;
2
- use std:: collections:: BTreeMap ;
2
+ use std:: collections:: { btree_map , BTreeMap } ;
3
3
use tiny_skia:: { Pixmap , PixmapMut , PixmapRef , Point , PremultipliedColorU8 } ;
4
4
5
5
// These values were generated from a screenshot of an libadwaita window using a script.
@@ -25,38 +25,47 @@ struct RenderedShadow {
25
25
}
26
26
27
27
impl RenderedShadow {
28
- fn new ( scale : u32 , active : bool ) -> RenderedShadow {
28
+ fn new ( scale : u32 , active : bool ) -> Option < RenderedShadow > {
29
29
let shadow_size = SHADOW_SIZE * scale;
30
30
let corner_radius = theme:: CORNER_RADIUS * scale;
31
31
32
- #[ allow( clippy:: unwrap_used) ]
33
- let mut side = Pixmap :: new ( shadow_size, 1 ) . unwrap ( ) ;
32
+ let mut side = Pixmap :: new ( shadow_size, 1 ) ?;
34
33
for x in 0 ..side. width ( ) as usize {
35
- let alpha = ( shadow ( x as f32 + 0.5 , scale, active) * u8:: MAX as f32 ) . round ( ) as u8 ;
34
+ let shadow = shadow ( x as f32 + 0.5 , scale, active) ;
35
+ let alpha = ( shadow * u8:: MAX as f32 ) . round ( ) as u8 ;
36
36
37
- #[ allow( clippy:: unwrap_used) ]
38
- let color = PremultipliedColorU8 :: from_rgba ( 0 , 0 , 0 , alpha) . unwrap ( ) ;
39
- side. pixels_mut ( ) [ x] = color;
37
+ let Some ( color) = PremultipliedColorU8 :: from_rgba ( 0 , 0 , 0 , alpha) else {
38
+ continue ;
39
+ } ;
40
+
41
+ if let Some ( pixel) = side. pixels_mut ( ) . get_mut ( x) {
42
+ * pixel = color;
43
+ }
40
44
}
41
45
42
46
let edges_size = ( corner_radius + shadow_size) * 2 ;
43
- #[ allow( clippy:: unwrap_used) ]
44
- let mut edges = Pixmap :: new ( edges_size, edges_size) . unwrap ( ) ;
47
+ let mut edges = Pixmap :: new ( edges_size, edges_size) ?;
45
48
let edges_middle = Point :: from_xy ( edges_size as f32 / 2.0 , edges_size as f32 / 2.0 ) ;
46
49
for y in 0 ..edges_size as usize {
47
50
let y_pos = y as f32 + 0.5 ;
48
51
for x in 0 ..edges_size as usize {
49
52
let dist = edges_middle. distance ( Point :: from_xy ( x as f32 + 0.5 , y_pos) )
50
53
- corner_radius as f32 ;
51
- let alpha = ( shadow ( dist, scale, active) * u8:: MAX as f32 ) . round ( ) as u8 ;
52
54
53
- #[ allow( clippy:: unwrap_used) ]
54
- let color = PremultipliedColorU8 :: from_rgba ( 0 , 0 , 0 , alpha) . unwrap ( ) ;
55
- edges. pixels_mut ( ) [ y * edges_size as usize + x] = color;
55
+ let shadow = shadow ( dist, scale, active) ;
56
+ let alpha = ( shadow * u8:: MAX as f32 ) . round ( ) as u8 ;
57
+
58
+ let Some ( color) = PremultipliedColorU8 :: from_rgba ( 0 , 0 , 0 , alpha) else {
59
+ continue ;
60
+ } ;
61
+
62
+ if let Some ( pixel) = edges. pixels_mut ( ) . get_mut ( y * edges_size as usize + x) {
63
+ * pixel = color;
64
+ }
56
65
}
57
66
}
58
67
59
- RenderedShadow { side, edges }
68
+ Some ( RenderedShadow { side, edges } )
60
69
}
61
70
62
71
fn side_draw (
@@ -150,7 +159,11 @@ impl RenderedShadow {
150
159
let shadow_size = ( SHADOW_SIZE * scale) as usize ;
151
160
let visible_border_size = ( theme:: VISIBLE_BORDER_SIZE * scale) as usize ;
152
161
let corner_radius = ( theme:: CORNER_RADIUS * scale) as usize ;
153
- assert ! ( corner_radius > visible_border_size) ;
162
+
163
+ debug_assert ! ( corner_radius > visible_border_size) ;
164
+ if corner_radius <= visible_border_size {
165
+ return ;
166
+ }
154
167
155
168
let dst_width = dst_pixmap. width ( ) as usize ;
156
169
let dst_height = dst_pixmap. height ( ) as usize ;
@@ -333,16 +346,15 @@ impl CachedPart {
333
346
scale : u32 ,
334
347
active : bool ,
335
348
part_idx : usize ,
336
- ) -> CachedPart {
337
- #[ allow( clippy:: unwrap_used) ]
338
- let mut pixmap = Pixmap :: new ( dst_pixmap. width ( ) , dst_pixmap. height ( ) ) . unwrap ( ) ;
349
+ ) -> Option < CachedPart > {
350
+ let mut pixmap = Pixmap :: new ( dst_pixmap. width ( ) , dst_pixmap. height ( ) ) ?;
339
351
rendered. draw ( & mut pixmap. as_mut ( ) , scale, part_idx) ;
340
352
341
- CachedPart {
353
+ Some ( CachedPart {
342
354
pixmap,
343
355
scale,
344
356
active,
345
- }
357
+ } )
346
358
}
347
359
348
360
fn matches ( & self , dst_pixmap : & PixmapRef , dst_scale : u32 , dst_active : bool ) -> bool {
@@ -367,7 +379,9 @@ pub struct Shadow {
367
379
368
380
impl Shadow {
369
381
pub fn draw ( & mut self , pixmap : & mut PixmapMut , scale : u32 , active : bool , part_idx : usize ) {
370
- let cache = & mut self . part_cache [ part_idx] ;
382
+ let Some ( cache) = self . part_cache . get_mut ( part_idx) else {
383
+ return ;
384
+ } ;
371
385
372
386
if let Some ( cache_value) = cache {
373
387
if !cache_value. matches ( & pixmap. as_ref ( ) , scale, active) {
@@ -376,22 +390,19 @@ impl Shadow {
376
390
}
377
391
378
392
if cache. is_none ( ) {
379
- let rendered = self
380
- . rendered
381
- . entry ( ( scale, active) )
382
- . or_insert_with ( || RenderedShadow :: new ( scale, active) ) ;
383
-
384
- * cache = Some ( CachedPart :: new (
385
- & pixmap. as_ref ( ) ,
386
- rendered,
387
- scale,
388
- active,
389
- part_idx,
390
- ) ) ;
393
+ let rendered = match self . rendered . entry ( ( scale, active) ) {
394
+ btree_map:: Entry :: Occupied ( entry) => entry. into_mut ( ) ,
395
+ btree_map:: Entry :: Vacant ( entry) => match RenderedShadow :: new ( scale, active) {
396
+ Some ( v) => entry. insert ( v) ,
397
+ None => return ,
398
+ } ,
399
+ } ;
400
+
401
+ * cache = CachedPart :: new ( & pixmap. as_ref ( ) , rendered, scale, active, part_idx) ;
391
402
}
392
403
393
- // We filled the cache above.
394
- # [ allow ( clippy :: unwrap_used ) ]
395
- cache . as_ref ( ) . unwrap ( ) . draw ( pixmap ) ;
404
+ if let Some ( cache ) = cache. as_ref ( ) {
405
+ cache . draw ( pixmap ) ;
406
+ }
396
407
}
397
408
}
0 commit comments