Skip to content

Code Quality: Moved App.Attributes & App.Behaviors to App.Data #12747

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

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions src/Files.App/Attributes/DependencyPropertyAttribute.cs

This file was deleted.

74 changes: 74 additions & 0 deletions src/Files.App/Data/Attributes/DependencyPropertyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2023 Files Community
// Licensed under the MIT License. See the LICENSE.

using Microsoft.UI.Xaml;

namespace Files.App.Attributes
{
/// <summary>
/// Provides an attribute for generation of <see cref="DependencyProperty"/> and its Property.
/// </summary>
/// <remarks>
/// Following code will be generated:
/// <code>
/// <see langword="public static readonly"/> <see cref="DependencyProperty"/> Property =
/// <see cref="DependencyProperty"/>.Register(
/// <see langword="nameof"/>(Field),
/// <see langword="typeof"/>(<typeparamref name="T"/>),
/// <see langword="typeof"/>(TClass),
/// <see langword="new"/> <see cref="PropertyMetadata"/>(DefaultValue, OnPropertyChanged));
/// <br/>
/// <br/>
/// <see langword="public"/> <typeparamref name="T"/> Field
/// {
/// <see langword="get"/> => (<typeparamref name="T"/>)GetValue(Property);
/// <see langword="set"/> => SetValue(Property, <see langword="value"/>);
/// }
/// </code>
/// <typeparam name="T">property type (nullable value type are not allowed)</typeparam>
/// </remarks>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public sealed class DependencyPropertyAttribute<T> : Attribute where T : notnull
{
/// <inheritdoc cref="DependencyPropertyAttribute{T}"/>
/// <param name="name">The name of the property.</param>
/// <param name="propertyChanged">The name of the method, which called when property changed.</param>
public DependencyPropertyAttribute(string name, string propertyChanged = "")
{
Name = name;
PropertyChanged = propertyChanged;
}

/// <summary>
/// Gets the name of the property.
/// </summary>
public string Name { get; }

/// <summary>
/// Gets the name of the method, which called when property changed.
/// </summary>
public string PropertyChanged { get; }

/// <summary>
/// Gets or initializes a value whether property setter is private.
/// </summary>
/// <remarks>
/// Default value is <see langword="false"/>.
/// </remarks>
public bool IsSetterPrivate { get; init; }

/// <summary>
/// Gets or initializes a value whether property type is nullable (nullable value type are not allowed).
/// </summary>
/// <remarks>
/// Default value is <see langword="false"/>.
/// </remarks>
public bool IsNullable { get; init; }

/// <summary>
/// Gets or initializes a default value of property.
/// </summary>
/// <remarks>default: <see cref="DependencyProperty.UnsetValue"/></remarks>
public string DefaultValue { get; init; } = "DependencyProperty.UnsetValue";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,31 @@
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Metadata;

namespace Files.App.Behaviors
namespace Files.App.Data.Behaviors
{
/// <summary>
/// Performs an animation on a ListView or GridView Header to make it sticky using composition.
/// </summary>
/// <seealso>
/// <cref>Microsoft.Xaml.Interactivity.Behavior{Microsoft.UI.Xaml.UIElement}</cref>
/// </seealso>
/// <remarks>
/// See also, <see cref="Microsoft.Xaml.Interactivity.Behavior{Microsoft.UI.Xaml.UIElement}"/>
/// </remarks>
public class StickyHeaderBehavior : BehaviorBase<FrameworkElement>
{
public static bool IsXamlRootAvailable { get; } = ApiInformation.IsPropertyPresent("Microsoft.UI.Xaml.UIElement", "XamlRoot");
private static readonly bool IsXamlRootAvailable =
ApiInformation.IsPropertyPresent("Microsoft.UI.Xaml.UIElement", "XamlRoot");

/// <summary>
/// Attaches the behavior to the associated object.
/// </summary>
/// <returns>
/// <c>true</c> if attaching succeeded; otherwise <c>false</c>.
/// </returns>
protected override bool Initialize()
{
var result = AssignAnimation();
return result;
}
private ScrollViewer? _scrollViewer;

/// <summary>
/// Detaches the behavior from the associated object.
/// </summary>
/// <returns>
/// <c>true</c> if detaching succeeded; otherwise <c>false</c>.
/// </returns>
protected override bool Uninitialize()
{
RemoveAnimation();
return true;
}
private CompositionPropertySet? _scrollProperties;

private CompositionPropertySet? _animationProperties;

private Visual? _headerVisual, _itemsPanelVisual;

private InsetClip? _contentClip;

/// <summary>
/// The UIElement that will be faded.
Expand All @@ -60,12 +46,6 @@ protected override bool Uninitialize()
typeof(StickyHeaderBehavior),
new PropertyMetadata(null, PropertyChangedCallback));

private ScrollViewer _scrollViewer;
private CompositionPropertySet _scrollProperties;
private CompositionPropertySet _animationProperties;
private Visual _headerVisual, _itemsPanelVisual;
private InsetClip _contentClip;

/// <summary>
/// Gets or sets the target element for the ScrollHeader behavior.
/// </summary>
Expand All @@ -78,6 +58,32 @@ public UIElement HeaderElement
set => SetValue(HeaderElementProperty, value);
}

/// <summary>
/// Attaches the behavior to the associated object.
/// </summary>
/// <returns>
/// <c>true</c> if attaching succeeded; otherwise <c>false</c>.
/// </returns>
protected override bool Initialize()
{
var result = AssignAnimation();

return result;
}

/// <summary>
/// Detaches the behavior from the associated object.
/// </summary>
/// <returns>
/// <c>true</c> if detaching succeeded; otherwise <c>false</c>.
/// </returns>
protected override bool Uninitialize()
{
RemoveAnimation();

return true;
}

/// <summary>
/// If any of the properties are changed then the animation is automatically started.
/// </summary>
Expand All @@ -95,7 +101,9 @@ private static void PropertyChangedCallback(DependencyObject d, DependencyProper
/// for the Header as it is scrolling off-screen. The opacity reaches 0 when the Header
/// is entirely scrolled off.
/// </summary>
/// <returns><c>true</c> if the assignment was successful; otherwise, <c>false</c>.</returns>
/// <returns>
/// <c>true</c> if the assignment was successful; otherwise, <c>false</c>.
/// </returns>
private bool AssignAnimation()
{
StopAnimation();
Expand All @@ -122,9 +130,8 @@ private bool AssignAnimation()
if (HeaderElement is null && listView is not null)
HeaderElement = listView.Header as UIElement;

FrameworkElement? headerElement = HeaderElement as FrameworkElement;

if (headerElement is null || headerElement.RenderSize.Height == 0)
if (HeaderElement is not FrameworkElement headerElement || headerElement.RenderSize.Height == 0)
return false;

_headerVisual ??= ElementCompositionPreview.GetElementVisual(headerElement);
Expand Down Expand Up @@ -166,7 +173,7 @@ private bool AssignAnimation()
}

var expressionClipAnimation = ExpressionFunctions.Max(-scrollPropSet.Translation.Y, 0);
_contentClip.TopInset = (float)System.Math.Max(-_scrollViewer.VerticalOffset, 0);
_contentClip.TopInset = (float)Math.Max(-_scrollViewer.VerticalOffset, 0);
_contentClip.StartAnimation("TopInset", expressionClipAnimation);

return true;
Expand Down Expand Up @@ -229,14 +236,14 @@ private void ScrollViewer_GotFocus(object sender, RoutedEventArgs e)
// Popups have no parents, whereas a normal Item would have the ListView as a parent.
if (focusedElement is UIElement element && VisualTreeHelper.GetParent(element) is not null)
{
// Mod: ignore if element is child of header
// NOTE: Ignore if element is child of header
if (!element.FindAscendants().Any(x => x == HeaderElement))
{
FrameworkElement header = (FrameworkElement)HeaderElement;

var point = element.TransformToVisual(scroller).TransformPoint(new Point(0, 0));

// Mod: do not change scroller horizontal offset
// NOTE: Do not change scroller horizontal offset
if (point.Y < header.ActualHeight)
scroller.ChangeView(scroller.HorizontalOffset, scroller.VerticalOffset - (header.ActualHeight - point.Y), 1, false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
x:Class="Files.App.Views.LayoutModes.DetailsLayoutBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:Files.App.Behaviors"
xmlns:behaviors="using:Files.App.Data.Behaviors"
xmlns:converters="using:Files.App.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:filesystem="using:Files.App.Filesystem"
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/Properties/SecurityAdvancedPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
x:Class="Files.App.Views.Properties.SecurityAdvancedPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:behaviors="using:CommunityToolkit.WinUI.UI.Behaviors"
xmlns:behaviors="using:Files.App.Data.Behaviors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="using:Files.App.Helpers"
xmlns:i="using:Microsoft.Xaml.Interactivity"
Expand Down