Skip to content

Commit

Permalink
feat: WinUI ImageIcon
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jun 23, 2021
1 parent d5f71ee commit 547020f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

namespace Microsoft.UI.Xaml.Controls
{
public partial class ImageIcon
{
public ImageSource Source
{
get => (ImageSource)GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}

public static DependencyProperty SourceProperty { get; } =
DependencyProperty.Register(nameof(Source), typeof(ImageSource), typeof(ImageIcon), new PropertyMetadata(null, OnSourcePropertyChanged));


private static void OnSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
var owner = sender as ImageIcon;
owner?.OnSourcePropertyChanged(args);
}
}
}
26 changes: 26 additions & 0 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/ImageIcon/ImageIcon.Uno.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Microsoft.UI.Xaml.Controls
{
public partial class ImageIcon
{
private bool _initialized = false;

private void InitializeVisualTree()
{
if (!_initialized)
{
var image = new Image
{
Stretch = Stretch.Uniform
};

var grid = new Grid();
grid.Children.Add(image);

AddIconElementView(grid);
}
}
}
}
44 changes: 44 additions & 0 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/ImageIcon/ImageIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#nullable enable

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Microsoft.UI.Xaml.Controls
{
public partial class ImageIcon : IconElement
{
private Image? m_rootImage = null;

public ImageIcon()
{
}

protected override void OnApplyTemplate()
{
#if HAS_UNO
// Uno specific: WinUI shares the visual tree initialziation with BitmapIcon
InitializeVisualTree();
#endif

if (VisualTreeHelper.GetChild(this, 0) is Grid grid)
{
var image = (Image)VisualTreeHelper.GetChild(grid, 0);
image.Source = Source;
m_rootImage = image;
}
else
{
m_rootImage = null;
}
}

private void OnSourcePropertyChanged(DependencyPropertyChangedEventArgs agrs)
{
if (m_rootImage is { } image)
{
image.Source = Source;
}
}
}
}
10 changes: 10 additions & 0 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/ImageIcon/ImageIcon.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. -->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft.UI.Xaml.Controls">

<Style TargetType="local:ImageIcon">
</Style>

</ResourceDictionary>

0 comments on commit 547020f

Please sign in to comment.