Skip to content

Commit 86ce5b6

Browse files
committed
Making images scale accordingly
1 parent d70af24 commit 86ce5b6

File tree

5 files changed

+27
-68
lines changed

5 files changed

+27
-68
lines changed

components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSampleBase.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,6 @@ public abstract partial class MarkdownTextBlockCustomThemeSampleBase : Page
101101
public static readonly DependencyProperty ImageStretchIndexProperty =
102102
DependencyProperty.Register(nameof(ImageStretchIndex), typeof(int), typeof(MarkdownTextBlockCustomThemeSampleBase), new PropertyMetadata(0));
103103

104-
public static readonly DependencyProperty ImageCornerRadiusProperty =
105-
DependencyProperty.Register(nameof(ImageCornerRadius), typeof(double), typeof(MarkdownTextBlockCustomThemeSampleBase), new PropertyMetadata(0d));
106-
107104
public static readonly DependencyProperty BoldFontWeightIndexProperty =
108105
DependencyProperty.Register(nameof(BoldFontWeightIndex), typeof(int), typeof(MarkdownTextBlockCustomThemeSampleBase), new PropertyMetadata(0));
109106

@@ -286,12 +283,6 @@ public int ImageStretchIndex
286283
set => SetValue(ImageStretchIndexProperty, value);
287284
}
288285

289-
public double ImageCornerRadius
290-
{
291-
get => (double)GetValue(ImageCornerRadiusProperty);
292-
set => SetValue(ImageCornerRadiusProperty, value);
293-
}
294-
295286
public int BoldFontWeightIndex
296287
{
297288
get => (int)GetValue(BoldFontWeightIndexProperty);
@@ -435,7 +426,6 @@ public MarkdownThemes CreateThemes()
435426
ImageMaxWidth = ImageMaxWidth,
436427
ImageMaxHeight = ImageMaxHeight,
437428
ImageStretch = ImageStretchOptions[ImageStretchIndex],
438-
ImageCornerRadius = new CornerRadius(ImageCornerRadius),
439429

440430
BoldFontWeight = BoldFontWeights[BoldFontWeightIndex],
441431

@@ -480,7 +470,6 @@ public void ResetToDefaults()
480470
ImageMaxWidth = 0;
481471
ImageMaxHeight = 0;
482472
ImageStretchIndex = 0;
483-
ImageCornerRadius = 0;
484473

485474
BoldFontWeightIndex = 0;
486475

components/MarkdownTextBlock/samples/ThemeOptionsPane.xaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,6 @@
346346
Minimum="0"
347347
SpinButtonPlacementMode="Compact"
348348
ValueChanged="OnValueChanged" />
349-
<muxc:NumberBox
350-
x:Name="ImageCornerRadiusBox"
351-
Header="Corner Radius"
352-
Maximum="50"
353-
Minimum="0"
354-
SpinButtonPlacementMode="Compact"
355-
ValueChanged="OnValueChanged" />
356349
<ComboBox
357350
x:Name="ImageStretchComboBox"
358351
HorizontalAlignment="Stretch"

components/MarkdownTextBlock/samples/ThemeOptionsPane.xaml.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ private void LoadValuesFromSample()
7171
// Images
7272
ImageMaxWidthBox.Value = _sample.ImageMaxWidth;
7373
ImageMaxHeightBox.Value = _sample.ImageMaxHeight;
74-
ImageCornerRadiusBox.Value = _sample.ImageCornerRadius;
7574
ImageStretchComboBox.SelectedIndex = _sample.ImageStretchIndex;
7675
}
7776

@@ -124,7 +123,6 @@ private void SyncValuesToSample()
124123
// Images
125124
_sample.ImageMaxWidth = ImageMaxWidthBox.Value;
126125
_sample.ImageMaxHeight = ImageMaxHeightBox.Value;
127-
_sample.ImageCornerRadius = ImageCornerRadiusBox.Value;
128126
_sample.ImageStretchIndex = ImageStretchComboBox.SelectedIndex;
129127
}
130128

components/MarkdownTextBlock/src/MarkdownThemes.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public sealed class MarkdownThemes : DependencyObject
120120
public double ImageMaxWidth { get; set; } = 0; // 0 = no constraint
121121
public double ImageMaxHeight { get; set; } = 0;
122122
public Stretch ImageStretch { get; set; } = Stretch.Uniform;
123-
public CornerRadius ImageCornerRadius { get; set; } = new CornerRadius(0);
124123

125124
// Table styling
126125
public Brush TableBorderBrush { get; set; } = new SolidColorBrush(Colors.Gray);

components/MarkdownTextBlock/src/TextElements/MyImage.cs

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace CommunityToolkit.WinUI.Controls.TextElements;
1212
internal class MyImage : IAddChild
1313
{
1414
private InlineUIContainer _container = new InlineUIContainer();
15-
private Border _border = new Border();
1615
private LinkInline? _linkInline;
1716
private Image _image = new Image();
1817
private Uri _uri;
@@ -85,8 +84,7 @@ out var height
8584
private void Init()
8685
{
8786
_image.Loaded += LoadImage;
88-
_border.Child = _image;
89-
_container.Child = _border;
87+
_container.Child = _image;
9088
}
9189

9290
private async void LoadImage(object sender, RoutedEventArgs e)
@@ -97,7 +95,7 @@ private async void LoadImage(object sender, RoutedEventArgs e)
9795
if (_imageProvider != null && _imageProvider.ShouldUseThisProvider(_uri.AbsoluteUri))
9896
{
9997
_image = await _imageProvider.GetImage(_uri.AbsoluteUri);
100-
_border.Child = _image;
98+
_container.Child = _image;
10199
}
102100
else
103101
{
@@ -121,7 +119,7 @@ private async void LoadImage(object sender, RoutedEventArgs e)
121119
if (resImage != null)
122120
{
123121
_image = resImage;
124-
_border.Child = _image;
122+
_container.Child = _image;
125123
}
126124
}
127125
else
@@ -139,57 +137,39 @@ private async void LoadImage(object sender, RoutedEventArgs e)
139137
await bitmap.SetSourceAsync(stream);
140138
}
141139
_image.Source = bitmap;
142-
_image.Width = bitmap.PixelWidth == 0 ? bitmap.DecodePixelWidth : bitmap.PixelWidth;
143-
_image.Height = bitmap.PixelHeight == 0 ? bitmap.DecodePixelHeight : bitmap.PixelHeight;
144-
140+
// Don't set fixed Width/Height - let layout system handle it
141+
// Store natural dimensions for MaxWidth/MaxHeight constraints
142+
double naturalWidth = bitmap.PixelWidth == 0 ? bitmap.DecodePixelWidth : bitmap.PixelWidth;
143+
double naturalHeight = bitmap.PixelHeight == 0 ? bitmap.DecodePixelHeight : bitmap.PixelHeight;
144+
145+
// Use natural size as max constraint so image doesn't upscale
146+
_image.MaxWidth = naturalWidth;
147+
_image.MaxHeight = naturalHeight;
145148
}
146149

147150
_loaded = true;
148151
}
149152

150-
// Determine the actual image dimensions
151-
double actualWidth = _precedentWidth != 0 ? _precedentWidth : _image.Width;
152-
double actualHeight = _precedentHeight != 0 ? _precedentHeight : _image.Height;
153-
154-
// Apply max constraints and calculate the final size
155-
// When using Uniform stretch with max constraints, we need to calculate
156-
// the actual rendered size to avoid gaps
157-
double finalWidth = actualWidth;
158-
double finalHeight = actualHeight;
159-
160-
bool hasMaxWidth = _themes.ImageMaxWidth > 0;
161-
bool hasMaxHeight = _themes.ImageMaxHeight > 0;
162-
163-
if (hasMaxWidth || hasMaxHeight)
153+
// Apply precedent (markdown-specified) dimensions if provided
154+
if (_precedentWidth != 0)
164155
{
165-
double scaleX = hasMaxWidth && actualWidth > _themes.ImageMaxWidth
166-
? _themes.ImageMaxWidth / actualWidth
167-
: 1.0;
168-
double scaleY = hasMaxHeight && actualHeight > _themes.ImageMaxHeight
169-
? _themes.ImageMaxHeight / actualHeight
170-
: 1.0;
171-
172-
// For Uniform stretch, use the smaller scale to maintain aspect ratio
173-
if (_themes.ImageStretch == Stretch.Uniform || _themes.ImageStretch == Stretch.UniformToFill)
174-
{
175-
double uniformScale = Math.Min(scaleX, scaleY);
176-
finalWidth = actualWidth * uniformScale;
177-
finalHeight = actualHeight * uniformScale;
178-
}
179-
else
180-
{
181-
// For other stretch modes, apply constraints independently
182-
finalWidth = actualWidth * scaleX;
183-
finalHeight = actualHeight * scaleY;
184-
}
156+
_image.MaxWidth = _precedentWidth;
157+
}
158+
if (_precedentHeight != 0)
159+
{
160+
_image.MaxHeight = _precedentHeight;
185161
}
186162

187-
_image.Width = finalWidth;
188-
_image.Height = finalHeight;
163+
// Apply theme constraints - these override natural/precedent if smaller
164+
if (_themes.ImageMaxWidth > 0 && _themes.ImageMaxWidth < _image.MaxWidth)
165+
{
166+
_image.MaxWidth = _themes.ImageMaxWidth;
167+
}
168+
if (_themes.ImageMaxHeight > 0 && _themes.ImageMaxHeight < _image.MaxHeight)
169+
{
170+
_image.MaxHeight = _themes.ImageMaxHeight;
171+
}
189172
_image.Stretch = _themes.ImageStretch;
190-
191-
// Apply corner radius via the border
192-
_border.CornerRadius = _themes.ImageCornerRadius;
193173
}
194174
catch (Exception) { }
195175
}

0 commit comments

Comments
 (0)