Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attached Shadows (Composition and Win2D + Animations) #4179

Merged

Conversation

michael-hawker
Copy link
Member

@michael-hawker michael-hawker commented Aug 12, 2021

Fixes #3122 #3607 #3516

Also implements #3693 for the new DropShadow.

FYI @seanocali as this is a different implementation approach (which is simpler to use outside of the DropShadowPanel we've been working on) but should hopefully achieve the same result.

This PR adds attached shadows which can easily be attached to any FrameworkElement without needing to modify the layout like DropShadowPanel does today. They can also be shared using a resource, added to the style of an element, and animated! All the things! 🎉

PR Type

What kind of change does this PR introduce?

What is the current behavior?

DropShadowPanel is clunky and requires modifying how you layout your app.

What is the new behavior?

Just attach a shadow and be done! (DropShadowPanel is deprecated.)

PR Checklist

  • Composition Only Shadow Support? (with Target)
  • Add XML Docs
  • Animation Support to Explicit Animation System?
  • Bug can't use AttachedCardShadow directly with Border?

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • Pull Request has been submitted to the documentation repository instructions. Link:
  • Sample in sample app has been added / updated (for bug fixes / features)
  • New major technical changes in the toolkit have or will be added to the Wiki e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

Other information

@michael-hawker michael-hawker added this to the 7.1 milestone Aug 12, 2021
@ghost
Copy link

ghost commented Aug 12, 2021

Thanks michael-hawker for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost requested review from azchohfi and Rosuavio August 12, 2021 20:34
@ghost ghost added the feature request 📬 A request for new changes to improve functionality label Aug 12, 2021
/// <summary>
/// Helper class for attaching <see cref="AttachedShadowBase"/> shadows to <see cref="FrameworkElement"/>s.
/// </summary>
public static class Shadows
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Community Suggested we call this Effects and have the property be called Shadow so we'd get:
<ui:Effects.Shadow> for the attached property.

// Position our shadow in the correct spot to match the corresponding element.
context.SpriteVisual.Size = context.Element.RenderSize.ToVector2();
context.SpriteVisual.Offset = context.Element.CoordinatesFrom(CastTo).ToVector3();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ryken100 suggested somehow using composition animation to lock the two together? @Sergio0694 thoughts on this too?

Right now I just update the size/offset when the mask is created and in OnSizeChanged below. This seems to work alright for most basic scenarios, but I would imagine would break down with extra translations and if the element casting is animated?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea, but thinking we might be able to use a composition animation to bind the two together like we're doing with BindSize for the brushes? I mean not with exactly the same code, but by reusing the same concept:

public static void BindSize(this CompositionObject source, UIElement target)
{
var visual = ElementCompositionPreview.GetElementVisual(target);
var bindSizeAnimation = source.Compositor.CreateExpressionAnimation($"{nameof(visual)}.Size");
bindSizeAnimation.SetReferenceParameter(nameof(visual), visual);
// Start the animation
source.StartAnimation("Size", bindSizeAnimation);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that could work for Size, but Offset may not work the same as we need to do the absolute offset, eh?

Which means we still need the SizeChanged on the parent at least as that effects the offsets...?

@michael-hawker michael-hawker added the next preview ✈️ Label for marking what we want to include in the next preview release for developers to try. label Aug 24, 2021
@michael-hawker michael-hawker changed the title 🚧 Draft - Ryken100 - Attached Shadows Attached Shadows (Composition and Win2D + Animations) Aug 25, 2021
@michael-hawker michael-hawker marked this pull request as ready for review August 25, 2021 18:12
@michael-hawker
Copy link
Member Author

Ugh, need to update to latest. Will fix that and then also need to double-check if I deprecated DropShadowPanel fully...

@michael-hawker michael-hawker force-pushed the ryken100/feature-AttachedShadows branch from b88e4a2 to b34f441 Compare August 25, 2021 18:53
@michael-hawker
Copy link
Member Author

Need to update the Vector tests to account for the new format options I added for better string parsing.

@michael-hawker michael-hawker mentioned this pull request Aug 25, 2021
27 tasks
@michael-hawker
Copy link
Member Author

Alright, Vector unit tests are passing locally now (and added new ones). So we should be all good with the new behavior now. Hopefully @Sergio0694 won't mind... 🙈

They do map to existing constructors of the underlying types, so feels good. Also re-used the underlying methods to not add extra complexity to implementation even if it's a bit less performant, but we're doing string parsing here after all still... 😋

This was needed to make shadows play nicer as they're technically Vector3 properties but the Z value is ignored, so it's easier in the Sample App to just take a "16,16" value and make the Vector3.

Copy link
Contributor

@XAML-Knight XAML-Knight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved - nice shadows

Microsoft.Toolkit.Uwp.SampleApp/Common/Vector3Converter.cs Outdated Show resolved Hide resolved
Copy link
Contributor

@Rosuavio Rosuavio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the API and it works well from my testing!

@ghost
Copy link

ghost commented Aug 27, 2021

Hello @michael-hawker!

Because this pull request has the auto merge :zap: label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@Sergio0694
Copy link
Member

@michael-hawker Removed the auto-merge one sec, I'm going through the code and had a couple of questions to ask 😄

Copy link
Member

@Sergio0694 Sergio0694 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, left a few notes 😄

Microsoft.Toolkit/TypedResourceKey.cs Outdated Show resolved Hide resolved
Microsoft.Toolkit.Uwp.UI/Shadows/AttachedShadowBase.cs Outdated Show resolved Hide resolved
Microsoft.Toolkit.Uwp.UI/Shadows/AttachedDropShadow.cs Outdated Show resolved Hide resolved
Microsoft.Toolkit.Uwp.UI/Shadows/IAttachedShadow.cs Outdated Show resolved Hide resolved
Co-authored-by: Sergio Pedri <sergio0694@live.com>
@ghost
Copy link

ghost commented Aug 27, 2021

This PR has been marked as "needs attention 👋" and awaiting a response from the team.

Copy link
Member

@Sergio0694 Sergio0694 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! :shipit:

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto merge ⚡ extensions ⚡ feature request 📬 A request for new changes to improve functionality needs attention 👋 next preview ✈️ Label for marking what we want to include in the next preview release for developers to try.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Shadow Receiver XAML Helpers
5 participants