Skip to content

Commit

Permalink
Implement Opacity property in ViewHandlers (#706)
Browse files Browse the repository at this point in the history
* Implement Opacity property in ViewHandler

* Fix build error

* Fix build error

* must have been added before

* Removed duplicated Opacity property in StubBase

* Windows

Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
jsuarezruiz and mattleibow authored Jun 2, 2021
1 parent a6d35a1 commit 896538d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/Android/VisualElementTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ void UpdateNativeView(object sender, EventArgs e)
Performance.Stop(reference);
}

[PortHandler]
void UpdateOpacity()
{
Performance.Start(out string reference);
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/WinUI/VisualElementTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ static bool ShouldUpdateClip(VisualElement view, FrameworkElement frameworkEleme
return false;
}

[PortHandler]
static void UpdateOpacity(VisualElement view, FrameworkElement frameworkElement)
{
frameworkElement.Opacity = view.Opacity;
Expand Down
6 changes: 6 additions & 0 deletions src/Core/src/Handlers/View/ViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public abstract partial class ViewHandler : IViewHandler
[nameof(IView.Width)] = MapWidth,
[nameof(IView.Height)] = MapHeight,
[nameof(IView.IsEnabled)] = MapIsEnabled,
[nameof(IView.Opacity)] = MapOpacity,
[nameof(IView.Semantics)] = MapSemantics,
[nameof(IView.TranslationX)] = MapTranslationX,
[nameof(IView.TranslationY)] = MapTranslationY,
Expand Down Expand Up @@ -139,6 +140,11 @@ public static void MapBackground(ViewHandler handler, IView view)
((NativeView?)handler.NativeView)?.UpdateBackground(view);
}

public static void MapOpacity(IViewHandler handler, IView view)
{
((NativeView?)handler.NativeView)?.UpdateOpacity(view);
}

public static void MapAutomationId(ViewHandler handler, IView view)
{
((NativeView?)handler.NativeView)?.UpdateAutomationId(view);
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public static void UpdateBackground(this AView nativeView, IView view, Drawable?
nativeView.Background = paint!.ToDrawable();
}

public static void UpdateOpacity(this AView nativeView, IView view)
{
nativeView.Alpha = (float)view.Opacity;
}

public static bool GetClipToOutline(this AView view)
{
return view.ClipToOutline;
Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/Platform/Standard/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static void UpdateBackground(this object nativeView, IView view) { }

public static void UpdateAutomationId(this object nativeView, IView view) { }

public static void UpdateOpacity(this object nativeView, IView view) { }

public static void UpdateSemantics(this object nativeView, IView view) { }

public static void UpdateTranslationX(this object nativeView, IView view) { }
Expand Down
11 changes: 8 additions & 3 deletions src/Core/src/Platform/Windows/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class ViewExtensions
{
public static void UpdateIsEnabled(this FrameworkElement nativeView, IView view) =>
(nativeView as Control)?.UpdateIsEnabled(view.IsEnabled);

public static void UpdateVisibility(this FrameworkElement nativeView, IView view)
{
double opacity = view.Opacity;
Expand All @@ -32,6 +32,11 @@ public static void UpdateVisibility(this FrameworkElement nativeView, IView view
}
}

public static void UpdateOpacity(this FrameworkElement nativeView, IView view)
{
nativeView.Opacity = view.Visibility == Visibility.Hidden ? 0 : view.Opacity;
}

public static void UpdateBackground(this FrameworkElement nativeView, IView view)
{
if (nativeView is Control control)
Expand Down Expand Up @@ -72,7 +77,7 @@ internal static void UpdateProperty(this FrameworkElement nativeControl, Depende
nativeControl.SetValue(property, value);
}

public static void InvalidateMeasure(this FrameworkElement nativeView, IView view)
public static void InvalidateMeasure(this FrameworkElement nativeView, IView view)
{
nativeView.InvalidateMeasure();
}
Expand All @@ -89,4 +94,4 @@ public static void UpdateHeight(this FrameworkElement nativeView, IView view)
nativeView.Height = view.Height >= 0 ? view.Height : double.NaN;
}
}
}
}
5 changes: 5 additions & 0 deletions src/Core/src/Platform/iOS/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public static void UpdateBackground(this UIView nativeView, IView view)
}
}

public static void UpdateOpacity(this UIView nativeView, IView view)
{
nativeView.Alpha = (float)view.Opacity;
}

public static void UpdateAutomationId(this UIView nativeView, IView view) =>
nativeView.AccessibilityIdentifier = view.AutomationId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ protected SemanticHeadingLevel GetSemanticHeading(IViewHandler viewHandler)

return viewHandler.VirtualView.Semantics.HeadingLevel;
}

protected float GetOpacity(IViewHandler viewHandler) =>
((View)viewHandler.NativeView).Alpha;

double GetTranslationX(IViewHandler viewHandler)
{
Expand Down
16 changes: 16 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/HandlerTestBase.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ public async Task SetAutomationId()
Assert.Equal(view.AutomationId, id);
}

[Theory(DisplayName = "Opacity is set correctly")]
[InlineData(0)]
[InlineData(0.25)]
[InlineData(0.5)]
[InlineData(0.75)]
[InlineData(1)]
public async Task SetOpacity(double opacity)
{
var view = new TStub
{
Opacity = opacity
};
var id = await GetValueAsync(view, handler => GetOpacity(handler));
Assert.Equal(view.Opacity, id);
}

[Theory(DisplayName = "Visibility is set correctly")]
[InlineData(Visibility.Collapsed)]
[InlineData(Visibility.Hidden)]
Expand Down
3 changes: 3 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/HandlerTestBase.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ protected SemanticHeadingLevel GetSemanticHeading(IViewHandler viewHandler) =>
((UIView)viewHandler.NativeView).AccessibilityTraits.HasFlag(UIAccessibilityTrait.Header)
? SemanticHeadingLevel.Level1 : SemanticHeadingLevel.None;

protected nfloat GetOpacity(IViewHandler viewHandler) =>
((UIView)viewHandler.NativeView).Alpha;

protected Visibility GetVisibility(IViewHandler viewHandler)
{
var nativeView = (UIView)viewHandler.NativeView;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/tests/UnitTests/TestClasses/ViewStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ public void InvalidateMeasure() { }
public Size Measure(double widthConstraint, double heightConstraint) =>
Size.Zero;
}
}
}

0 comments on commit 896538d

Please sign in to comment.