@@ -45,53 +45,62 @@ static ImageDiffView()
45
45
46
46
public override void Render ( DrawingContext context )
47
47
{
48
- var alpha = Alpha ;
49
- var bgMaskBrush = new SolidColorBrush ( ActualThemeVariant == ThemeVariant . Dark ? 0xFF404040 : 0xFFBBBBBB ) ;
50
-
51
- var bg = new DrawingGroup ( )
48
+ if ( _bgBrush == null )
52
49
{
53
- Children =
50
+ var maskBrush = new SolidColorBrush ( ActualThemeVariant == ThemeVariant . Dark ? 0xFF404040 : 0xFFBBBBBB ) ;
51
+ var bg = new DrawingGroup ( )
54
52
{
55
- new GeometryDrawing ( ) { Brush = bgMaskBrush , Geometry = new RectangleGeometry ( new Rect ( 0 , 0 , 12 , 12 ) ) } ,
56
- new GeometryDrawing ( ) { Brush = bgMaskBrush , Geometry = new RectangleGeometry ( new Rect ( 12 , 12 , 12 , 12 ) ) } ,
57
- }
58
- } ;
59
-
60
- var brushBG = new DrawingBrush ( bg )
61
- {
62
- AlignmentX = AlignmentX . Left ,
63
- AlignmentY = AlignmentY . Top ,
64
- DestinationRect = new RelativeRect ( new Size ( 24 , 24 ) , RelativeUnit . Absolute ) ,
65
- Stretch = Stretch . None ,
66
- TileMode = TileMode . Tile ,
67
- } ;
53
+ Children =
54
+ {
55
+ new GeometryDrawing ( ) { Brush = maskBrush , Geometry = new RectangleGeometry ( new Rect ( 0 , 0 , 12 , 12 ) ) } ,
56
+ new GeometryDrawing ( ) { Brush = maskBrush , Geometry = new RectangleGeometry ( new Rect ( 12 , 12 , 12 , 12 ) ) } ,
57
+ }
58
+ } ;
59
+
60
+ _bgBrush = new DrawingBrush ( bg )
61
+ {
62
+ AlignmentX = AlignmentX . Left ,
63
+ AlignmentY = AlignmentY . Top ,
64
+ DestinationRect = new RelativeRect ( new Size ( 24 , 24 ) , RelativeUnit . Absolute ) ,
65
+ Stretch = Stretch . None ,
66
+ TileMode = TileMode . Tile ,
67
+ } ;
68
+ }
68
69
69
- context . FillRectangle ( brushBG , new Rect ( Bounds . Size ) ) ;
70
+ context . FillRectangle ( _bgBrush , new Rect ( Bounds . Size ) ) ;
70
71
72
+ var alpha = Alpha ;
73
+ var w = Bounds . Width - 16 ;
74
+ var h = Bounds . Height - 16 ;
75
+ var x = w * alpha ;
71
76
var left = OldImage ;
72
77
if ( left != null && alpha > 0 )
73
78
{
74
- var src = new Rect ( 0 , 0 , left . Size . Width * Alpha , left . Size . Height ) ;
75
- var dst = new Rect ( 8 , 8 , ( Bounds . Width - 16 ) * Alpha , Bounds . Height - 16 ) ;
79
+ var src = new Rect ( 0 , 0 , left . Size . Width * alpha , left . Size . Height ) ;
80
+ var dst = new Rect ( 8 , 8 , x , h ) ;
76
81
context . DrawImage ( left , src , dst ) ;
77
82
}
78
83
79
84
var right = NewImage ;
80
- if ( right != null )
85
+ if ( right != null && alpha < 1 )
81
86
{
82
- var src = new Rect ( right . Size . Width * Alpha , 0 , right . Size . Width - right . Size . Width * Alpha , right . Size . Height ) ;
83
- var dst = new Rect ( ( Bounds . Width - 16 ) * Alpha + 8 , 8 , ( Bounds . Width - 16 ) * ( 1 - Alpha ) , Bounds . Height - 16 ) ;
87
+ var src = new Rect ( right . Size . Width * alpha , 0 , right . Size . Width * ( 1 - alpha ) , right . Size . Height ) ;
88
+ var dst = new Rect ( x + 8 , 8 , w - x , h ) ;
84
89
context . DrawImage ( right , src , dst ) ;
85
90
}
86
91
87
- var x = ( Bounds . Width - 16 ) * Alpha + 8 ;
88
- context . DrawLine ( new Pen ( Brushes . DarkGreen , 2 ) , new Point ( x , 0 ) , new Point ( x , Bounds . Height ) ) ;
92
+ context . DrawLine ( new Pen ( Brushes . DarkGreen , 2 ) , new Point ( x + 8 , 0 ) , new Point ( x + 8 , Bounds . Height ) ) ;
89
93
}
90
94
91
95
protected override void OnPropertyChanged ( AvaloniaPropertyChangedEventArgs change )
92
96
{
93
97
base . OnPropertyChanged ( change ) ;
94
- if ( change . Property . Name == "ActualThemeVariant" ) InvalidateVisual ( ) ;
98
+
99
+ if ( change . Property . Name == "ActualThemeVariant" )
100
+ {
101
+ _bgBrush = null ;
102
+ InvalidateVisual ( ) ;
103
+ }
95
104
}
96
105
97
106
protected override Size MeasureOverride ( Size availableSize )
@@ -135,6 +144,8 @@ private Size GetDesiredSize(Size img, Size available)
135
144
return new Size ( img . Width / s + 16 , img . Height / s + 16 ) ;
136
145
}
137
146
}
147
+
148
+ private DrawingBrush _bgBrush = null ;
138
149
}
139
150
140
151
public partial class DiffView : UserControl
0 commit comments