Skip to content

Commit

Permalink
Change IconSource.CreateIconElement to a "dynamic" factory. (#4563)
Browse files Browse the repository at this point in the history
* Change IconSource's CreateIconElement to make the IconSource behave as a DynamicFactory.  IconSource now records the icons created by it and pushes properties changed on the icon source to those icon elements. To minimize Compat issues, I've reverted all of the usages of the createIconElement except animated icon's back to the shared helper.

* Update Iconsource tests to exercise this new functionality, Fix a bug where the foreground property wasn't getting applied.  Fixed a bug where the collection iterator could fall off the end of the vector.

* Update control templates that use animated icon to utilize the fact that icon source properties can be updated.
  • Loading branch information
StephenLPeters authored Mar 24, 2021
1 parent 4e774f7 commit 083796a
Show file tree
Hide file tree
Showing 45 changed files with 621 additions and 115 deletions.
3 changes: 2 additions & 1 deletion dev/AnimatedIcon/AnimatedIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ void AnimatedIcon::OnSourcePropertyChanged(const winrt::DependencyPropertyChange
progressAnimation.SetReferenceParameter(L"_", m_progressPropertySet);
visual.Properties().StartAnimation(s_progressPropertyName, progressAnimation);
}
else
// If we were previously able to display primary content and now cannot, use the fallback icon.
else if (m_canDisplayPrimaryContent)
{
m_canDisplayPrimaryContent = false;
SetRootPanelChildToFallbackIcon();
Expand Down
13 changes: 13 additions & 0 deletions dev/AnimatedIcon/TestUI/AnimatedIconHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ public String Title
null
);

public String FallbackGlyph
{
get { return (String)GetValue(FallbackGlyphProperty); }
set { SetValue(FallbackGlyphProperty, value); }
}

public static readonly DependencyProperty FallbackGlyphProperty = DependencyProperty.Register(
"FallbackGlyph",
typeof(String),
typeof(AnimatedIconHost),
null
);

public AnimatedIconHost()
{
this.DefaultStyleKey = typeof(AnimatedIconHost);
Expand Down
12 changes: 12 additions & 0 deletions dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,18 @@
</controls:AnimatedIcon>
</Border>
</Button>
<StackPanel>
<StackPanel.Resources>
<controls:FontIconSource x:Name="MyFontIconSource" Glyph="{Binding ElementName=boundFallback, Path=FallbackGlyph}"/>
</StackPanel.Resources>
<local:AnimatedIconHost Foreground="{StaticResource TestIconForeground}" x:Name="boundFallback" Title="AnimatedFindVisualSource" Background="DarkSlateGray" FallbackGlyph="&#xE001;">
<local:AnimatedIconHost.IconSource>
<controls:AnimatedIconSource FallbackIconSource="{StaticResource MyFontIconSource}"/>
</local:AnimatedIconHost.IconSource>
</local:AnimatedIconHost>
<controls:AnimatedIcon FallbackIconSource="{StaticResource MyFontIconSource}"/>
<Button Content="Change FallbackGlyph" Click="ChangeFallbackGlyphButton_Click"/>
</StackPanel>
<StackPanel Background="Maroon">
<controls:AnimatedIcon Height="100" Width="100" Foreground="Pink">
<controls:AnimatedIcon.FallbackIconSource>
Expand Down
5 changes: 5 additions & 0 deletions dev/AnimatedIcon/TestUI/AnimatedIconPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,10 @@ private void SpeedUpSlider_ValueChanged(object sender, RangeBaseValueChangedEven
AnimatedIconTestHooks.SetSpeedUpMultiplier(CheckIcon, (float)e.NewValue);
}
}

private void ChangeFallbackGlyphButton_Click(object sender, RoutedEventArgs e)
{
boundFallback.FallbackGlyph = "\uE9AE";
}
}
}
134 changes: 49 additions & 85 deletions dev/CommonStyles/CheckBox_themeresources.xaml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dev/DropDownButton/DropDownButton.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
<local:FontIconSource
FontSize="8"
FontFamily="{ThemeResource SymbolThemeFontFamily}"
Foreground="{ThemeResource ButtonForegroundPressed}"
Glyph="&#xE96E;"
IsTextScaleFactorEnabled="False"/>
</local:AnimatedIcon.FallbackIconSource>
Expand Down
20 changes: 18 additions & 2 deletions dev/Generated/AnimatedIconSource.properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void AnimatedIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::AnimatedIconSource>(),
false /* isAttached */,
ValueHelper<winrt::IconSource>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnFallbackIconSourcePropertyChanged));
}
if (!s_SourceProperty)
{
Expand All @@ -44,7 +44,7 @@ void AnimatedIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::AnimatedIconSource>(),
false /* isAttached */,
ValueHelper<winrt::IAnimatedVisualSource2>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnSourcePropertyChanged));
}
}

Expand All @@ -55,6 +55,22 @@ void AnimatedIconSourceProperties::ClearProperties()
IconSource::ClearProperties();
}

void AnimatedIconSourceProperties::OnFallbackIconSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::AnimatedIconSource>();
winrt::get_self<AnimatedIconSource>(owner)->OnPropertyChanged(args);
}

void AnimatedIconSourceProperties::OnSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::AnimatedIconSource>();
winrt::get_self<AnimatedIconSource>(owner)->OnPropertyChanged(args);
}

void AnimatedIconSourceProperties::FallbackIconSource(winrt::IconSource const& value)
{
[[gsl::suppress(con)]]
Expand Down
8 changes: 8 additions & 0 deletions dev/Generated/AnimatedIconSource.properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ class AnimatedIconSourceProperties

static void EnsureProperties();
static void ClearProperties();

static void OnFallbackIconSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);
};
20 changes: 18 additions & 2 deletions dev/Generated/BitmapIconSource.properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void BitmapIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::BitmapIconSource>(),
false /* isAttached */,
ValueHelper<bool>::BoxValueIfNecessary(true),
nullptr);
winrt::PropertyChangedCallback(&OnShowAsMonochromePropertyChanged));
}
if (!s_UriSourceProperty)
{
Expand All @@ -44,7 +44,7 @@ void BitmapIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::BitmapIconSource>(),
false /* isAttached */,
ValueHelper<winrt::Uri>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnUriSourcePropertyChanged));
}
}

Expand All @@ -55,6 +55,22 @@ void BitmapIconSourceProperties::ClearProperties()
IconSource::ClearProperties();
}

void BitmapIconSourceProperties::OnShowAsMonochromePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::BitmapIconSource>();
winrt::get_self<BitmapIconSource>(owner)->OnPropertyChanged(args);
}

void BitmapIconSourceProperties::OnUriSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::BitmapIconSource>();
winrt::get_self<BitmapIconSource>(owner)->OnPropertyChanged(args);
}

void BitmapIconSourceProperties::ShowAsMonochrome(bool value)
{
[[gsl::suppress(con)]]
Expand Down
8 changes: 8 additions & 0 deletions dev/Generated/BitmapIconSource.properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ class BitmapIconSourceProperties

static void EnsureProperties();
static void ClearProperties();

static void OnShowAsMonochromePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnUriSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);
};
70 changes: 63 additions & 7 deletions dev/Generated/FontIconSource.properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<winrt::FontFamily>::BoxValueIfNecessary(winrt::FontFamily{ c_fontIconSourceDefaultFontFamily }),
nullptr);
winrt::PropertyChangedCallback(&OnFontFamilyPropertyChanged));
}
if (!s_FontSizeProperty)
{
Expand All @@ -49,7 +49,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<double>::BoxValueIfNecessary(20.0),
nullptr);
winrt::PropertyChangedCallback(&OnFontSizePropertyChanged));
}
if (!s_FontStyleProperty)
{
Expand All @@ -60,7 +60,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<winrt::FontStyle>::BoxValueIfNecessary(winrt::FontStyle::Normal),
nullptr);
winrt::PropertyChangedCallback(&OnFontStylePropertyChanged));
}
if (!s_FontWeightProperty)
{
Expand All @@ -71,7 +71,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<winrt::FontWeight>::BoxValueIfNecessary({ 400 }),
nullptr);
winrt::PropertyChangedCallback(&OnFontWeightPropertyChanged));
}
if (!s_GlyphProperty)
{
Expand All @@ -82,7 +82,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<winrt::hstring>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnGlyphPropertyChanged));
}
if (!s_IsTextScaleFactorEnabledProperty)
{
Expand All @@ -93,7 +93,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<bool>::BoxValueIfNecessary(true),
nullptr);
winrt::PropertyChangedCallback(&OnIsTextScaleFactorEnabledPropertyChanged));
}
if (!s_MirroredWhenRightToLeftProperty)
{
Expand All @@ -104,7 +104,7 @@ void FontIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::FontIconSource>(),
false /* isAttached */,
ValueHelper<bool>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnMirroredWhenRightToLeftPropertyChanged));
}
}

Expand All @@ -120,6 +120,62 @@ void FontIconSourceProperties::ClearProperties()
IconSource::ClearProperties();
}

void FontIconSourceProperties::OnFontFamilyPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::OnFontSizePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::OnFontStylePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::OnFontWeightPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::OnGlyphPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::OnIsTextScaleFactorEnabledPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::OnMirroredWhenRightToLeftPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::FontIconSource>();
winrt::get_self<FontIconSource>(owner)->OnPropertyChanged(args);
}

void FontIconSourceProperties::FontFamily(winrt::FontFamily const& value)
{
[[gsl::suppress(con)]]
Expand Down
28 changes: 28 additions & 0 deletions dev/Generated/FontIconSource.properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,32 @@ class FontIconSourceProperties

static void EnsureProperties();
static void ClearProperties();

static void OnFontFamilyPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnFontSizePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnFontStylePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnFontWeightPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnGlyphPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnIsTextScaleFactorEnabledPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);

static void OnMirroredWhenRightToLeftPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);
};
10 changes: 9 additions & 1 deletion dev/Generated/IconSource.properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void IconSourceProperties::EnsureProperties()
winrt::name_of<winrt::IconSource>(),
false /* isAttached */,
ValueHelper<winrt::Brush>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnForegroundPropertyChanged));
}
}

Expand All @@ -40,6 +40,14 @@ void IconSourceProperties::ClearProperties()
s_ForegroundProperty = nullptr;
}

void IconSourceProperties::OnForegroundPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::IconSource>();
winrt::get_self<IconSource>(owner)->OnPropertyChanged(args);
}

void IconSourceProperties::Foreground(winrt::Brush const& value)
{
[[gsl::suppress(con)]]
Expand Down
4 changes: 4 additions & 0 deletions dev/Generated/IconSource.properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ class IconSourceProperties

static void EnsureProperties();
static void ClearProperties();

static void OnForegroundPropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args);
};
10 changes: 9 additions & 1 deletion dev/Generated/ImageIconSource.properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void ImageIconSourceProperties::EnsureProperties()
winrt::name_of<winrt::ImageIconSource>(),
false /* isAttached */,
ValueHelper<winrt::ImageSource>::BoxedDefaultValue(),
nullptr);
winrt::PropertyChangedCallback(&OnImageSourcePropertyChanged));
}
}

Expand All @@ -42,6 +42,14 @@ void ImageIconSourceProperties::ClearProperties()
IconSource::ClearProperties();
}

void ImageIconSourceProperties::OnImageSourcePropertyChanged(
winrt::DependencyObject const& sender,
winrt::DependencyPropertyChangedEventArgs const& args)
{
auto owner = sender.as<winrt::ImageIconSource>();
winrt::get_self<ImageIconSource>(owner)->OnPropertyChanged(args);
}

void ImageIconSourceProperties::ImageSource(winrt::ImageSource const& value)
{
[[gsl::suppress(con)]]
Expand Down
Loading

0 comments on commit 083796a

Please sign in to comment.