Skip to content

Commit

Permalink
Merge branch 'main' into niels9001/tabbedcommandbar
Browse files Browse the repository at this point in the history
  • Loading branch information
niels9001 authored Jan 9, 2024
2 parents 98facbd + eb98b48 commit 9b7b312
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 20 deletions.
11 changes: 9 additions & 2 deletions components/Media/src/Brushes/BackdropGammaTransferBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,14 @@ protected override void OnConnected()
return;
}

var backdrop = Window.Current.Compositor.CreateBackdropBrush();

#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

var backdrop = compositor.CreateBackdropBrush();

// Use a Win2D blur affect applied to a CompositionBackdropBrush.
var graphicsEffect = new GammaTransferEffect
Expand All @@ -370,7 +377,7 @@ protected override void OnConnected()
Source = new CompositionEffectSourceParameter("backdrop")
};

var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[]
var effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[]
{
"GammaTransfer.AlphaAmplitude",
"GammaTransfer.AlphaExponent",
Expand Down
10 changes: 8 additions & 2 deletions components/Media/src/Brushes/CanvasBrushBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ protected override void OnConnected()
_graphics.RenderingDeviceReplaced -= CanvasDevice_RenderingDeviceReplaced;
}

_graphics = CanvasComposition.CreateCompositionGraphicsDevice(Window.Current.Compositor, _device);
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

_graphics = CanvasComposition.CreateCompositionGraphicsDevice(compositor, _device);
_graphics.RenderingDeviceReplaced += CanvasDevice_RenderingDeviceReplaced;

// Delay creating composition resources until they're required.
Expand Down Expand Up @@ -95,7 +101,7 @@ protected override void OnConnected()
}
}

_surfaceBrush = Window.Current.Compositor.CreateSurfaceBrush(surface);
_surfaceBrush = compositor.CreateSurfaceBrush(surface);
_surfaceBrush.Stretch = CompositionStretch.Fill;

CompositionBrush = _surfaceBrush;
Expand Down
13 changes: 10 additions & 3 deletions components/Media/src/Brushes/ImageBlendBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,22 @@ private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedE
/// <inheritdoc/>
protected override void OnConnected()
{
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

// Delay creating composition resources until they're required.
if (CompositionBrush == null && Source != null && Source is BitmapImage bitmap)
{
// Use LoadedImageSurface API to get ICompositionSurface from image uri provided
// If UriSource is invalid, StartLoadFromUri will return a blank texture.
_surface = LoadedImageSurface.StartLoadFromUri(bitmap.UriSource);


// Load Surface onto SurfaceBrush
_surfaceBrush = Window.Current.Compositor.CreateSurfaceBrush(_surface);
_surfaceBrush = compositor.CreateSurfaceBrush(_surface);
_surfaceBrush.Stretch = CompositionStretchFromStretch(Stretch);

#if WINUI2
Expand All @@ -150,7 +157,7 @@ protected override void OnConnected()
return;
}

var backdrop = Window.Current.Compositor.CreateBackdropBrush();
var backdrop = compositor.CreateBackdropBrush();

// Use a Win2D invert affect applied to a CompositionBackdropBrush.
var graphicsEffect = new CanvasBlendEffect
Expand All @@ -161,7 +168,7 @@ protected override void OnConnected()
Foreground = new CompositionEffectSourceParameter("image")
};

var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect);
var effectFactory = compositor.CreateEffectFactory(graphicsEffect);
var effectBrush = effectFactory.CreateBrush();

effectBrush.SetSourceParameter("backdrop", backdrop);
Expand Down
7 changes: 6 additions & 1 deletion components/Media/src/Helpers/SurfaceLoader.Instance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public sealed partial class SurfaceLoader : IDisposable
/// <returns>A <see cref="SurfaceLoader"/> instance to use in the current window</returns>
public static SurfaceLoader GetInstance()
{
return GetInstance(Window.Current.Compositor);
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif
return GetInstance(compositor);
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions components/Media/src/Helpers/SurfaceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ public sealed partial class SurfaceLoader
/// <returns>A <see cref="Task{T}"/> that returns the loaded <see cref="CompositionBrush"/> instance</returns>
public static async Task<CompositionBrush?> LoadImageAsync(Uri uri, DpiMode dpiMode, CacheMode cacheMode = CacheMode.Default)
{
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

// Lock and check the cache first
using (await Win2DMutex.LockAsync())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public static PipelineBuilder FromBackdrop()
{
ValueTask<CompositionBrush> Factory()
{
var brush = BackdropBrushCache.GetValue(Window.Current.Compositor, c => c.CreateBackdropBrush());
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif
var brush = BackdropBrushCache.GetValue(compositor, c => c.CreateBackdropBrush());

return new ValueTask<CompositionBrush>(brush);
}
Expand Down
18 changes: 15 additions & 3 deletions components/Media/src/Pipelines/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ private PipelineBuilder(
[Pure]
public async Task<CompositionBrush> BuildAsync()
{
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

var effect = await this.sourceProducer() as IGraphicsEffect;

// Validate the pipeline
Expand All @@ -170,8 +176,8 @@ public async Task<CompositionBrush> BuildAsync()

// Build the effects factory
var factory = this.animationProperties.Count > 0
? Window.Current.Compositor.CreateEffectFactory(effect, this.animationProperties)
: Window.Current.Compositor.CreateEffectFactory(effect);
? compositor.CreateEffectFactory(effect, this.animationProperties)
: compositor.CreateEffectFactory(effect);

// Create the effect factory and apply the final effect
var effectBrush = factory.CreateBrush();
Expand All @@ -191,7 +197,13 @@ public async Task<CompositionBrush> BuildAsync()
/// <returns>A <see cref="Task{T}"/> that returns the final <see cref="SpriteVisual"/> instance to use</returns>
public async Task<SpriteVisual> AttachAsync(UIElement target, UIElement? reference = null)
{
SpriteVisual visual = Window.Current.Compositor.CreateSpriteVisual();
#if WINUI2
var compositor = Window.Current.Compositor;
#elif WINUI3
var compositor = CompositionTarget.GetCompositorForCurrentThread();
#endif

SpriteVisual visual = compositor.CreateSpriteVisual();

visual.Brush = await BuildAsync();

Expand Down
31 changes: 23 additions & 8 deletions components/SettingsControls/src/SettingsCard/SettingsCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,20 @@ private void OnDescriptionChanged()
{
if (GetTemplateChild(DescriptionPresenter) is FrameworkElement descriptionPresenter)
{
descriptionPresenter.Visibility = Description != null
? Visibility.Visible
: Visibility.Collapsed;
descriptionPresenter.Visibility = IsNullOrEmptyString(Description)
? Visibility.Collapsed
: Visibility.Visible;
}

}

private void OnHeaderChanged()
{
if (GetTemplateChild(HeaderPresenter) is FrameworkElement headerPresenter)
{
headerPresenter.Visibility = Header != null
? Visibility.Visible
: Visibility.Collapsed;
headerPresenter.Visibility = IsNullOrEmptyString(Header)
? Visibility.Collapsed
: Visibility.Visible;
}

}
Expand All @@ -274,7 +274,7 @@ private void CheckVerticalSpacingState(VisualState s)
{
// On state change, checking if the Content should be wrapped (e.g. when the card is made smaller or the ContentAlignment is set to Vertical). If the Content and the Header or Description are not null, we add spacing between the Content and the Header/Description.

if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (Header != null || Description != null))
if (s != null && (s.Name == RightWrappedState || s.Name == RightWrappedNoIconState || s.Name == VerticalState) && (Content != null) && (!IsNullOrEmptyString(Header) || !IsNullOrEmptyString(Description)))
{
VisualStateManager.GoToState(this, ContentSpacingState, true);
}
Expand All @@ -295,4 +295,19 @@ private void CheckVerticalSpacingState(VisualState s)
return FocusManager.GetFocusedElement() as FrameworkElement;
}
}

private static bool IsNullOrEmptyString(object obj)
{
if (obj == null)
{
return true;
}

if (obj is string objString && objString == string.Empty)
{
return true;
}

return false;
}
}
14 changes: 14 additions & 0 deletions components/SettingsControls/tests/Test_SettingsCard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ namespace SettingsControlsExperiment.Tests;
[TestClass]
public partial class SettingsCardTestClass : VisualUITestBase
{
[UIThreadTestMethod]
public void EmptyNameTest(SettingsCard card)
{
// See https://github.com/CommunityToolkit/Windows/issues/310#issue-2066181868
card.Name = string.Empty;
}

[UIThreadTestMethod]
public void EmptyDescriptionTest(SettingsCard card)
{
// See https://github.com/CommunityToolkit/Windows/issues/310#issue-2066181868
card.Description = string.Empty;
}

// If you don't need access to UI objects directly or async code, use this pattern.
[TestMethod]
public void SimpleSynchronousExampleTest()
Expand Down

0 comments on commit 9b7b312

Please sign in to comment.