-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(AcrylicBrush): First Android version
- Loading branch information
1 parent
89fa0b2
commit 4a291e7
Showing
10 changed files
with
310 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 116 additions & 18 deletions
134
src/Uno.UI/UI/Xaml/Media/AcrylicBrush/Android/AcrylicBrush.Android.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,138 @@ | ||
using System; | ||
#pragma warning disable CS0618 | ||
using System; | ||
using Android.Graphics; | ||
using Android.Graphics.Drawables; | ||
using Android.Views; | ||
using Java.Lang; | ||
using Uno.Disposables; | ||
using Uno.UI.Controls; | ||
using Uno.UI.Xaml.Media; | ||
using Windows.UI.Xaml.Controls; | ||
|
||
namespace Windows.UI.Xaml.Media | ||
{ | ||
public partial class AcrylicBrush | ||
{ | ||
protected override Paint GetPaintInner(Foundation.Rect destinationRect) | ||
{ | ||
/// <summary> | ||
/// Returns the fallback solid color brush. | ||
/// </summary> | ||
/// <param name="destinationRect">Destination rect.</param> | ||
/// <returns></returns> | ||
protected override Paint GetPaintInner(Foundation.Rect destinationRect) => | ||
new Paint() | ||
{ | ||
Color = FallbackColorWithOpacity, | ||
AntiAlias = true | ||
}; | ||
|
||
internal IDisposable Subscribe(BindableView owner, Foundation.Rect drawArea, Path maskingPath) | ||
{ | ||
return new Paint() { Color = FallbackColorWithOpacity, AntiAlias = true }; | ||
var state = new AcrylicState(owner, drawArea, maskingPath); | ||
|
||
var compositeDisposable = new CompositeDisposable(6); | ||
|
||
this.RegisterDisposablePropertyChangedCallback( | ||
AlwaysUseFallbackProperty, | ||
(_, __) => Apply(state)) | ||
.DisposeWith(compositeDisposable); | ||
|
||
this.RegisterDisposablePropertyChangedCallback( | ||
FallbackColorProperty, | ||
(_, __) => Apply(state)) | ||
.DisposeWith(compositeDisposable); | ||
|
||
this.RegisterDisposablePropertyChangedCallback( | ||
TintColorProperty, | ||
(_, __) => Apply(state)) | ||
.DisposeWith(compositeDisposable); | ||
|
||
this.RegisterDisposablePropertyChangedCallback( | ||
TintOpacityProperty, | ||
(_, __) => Apply(state)) | ||
.DisposeWith(compositeDisposable); | ||
|
||
this.RegisterDisposablePropertyChangedCallback( | ||
OpacityProperty, | ||
(_, __) => Apply(state)) | ||
.DisposeWith(compositeDisposable); | ||
|
||
Apply(state); | ||
|
||
Disposable.Create(() => | ||
{ | ||
state.FallbackDisposable.Disposable = null; | ||
state.BlurDisposable.Disposable = null; | ||
}).DisposeWith(compositeDisposable); | ||
|
||
return compositeDisposable; | ||
} | ||
|
||
bool ready = false; | ||
internal IDisposable Apply(BindableView owner) | ||
private void Apply(AcrylicState state) | ||
{ | ||
if(!(owner is Border)) | ||
if (AlwaysUseFallback || !SupportsBlur()) | ||
{ | ||
throw new InvalidOperationException( | ||
"AcrylicBrush can currently be applied " + | ||
"to empty border only on Android"); | ||
state.BlurDisposable.Disposable = null; | ||
|
||
// Fall back to solid color | ||
var fillPaint = GetFillPaint(Foundation.Rect.Empty); | ||
ExecuteWithNoRelayout(state.Owner, v => v.SetBackgroundDrawable(Brush.GetBackgroundDrawable(this, state.DrawArea, fillPaint, state.MaskingPath))); | ||
|
||
if (state.FallbackDisposable.Disposable == null) | ||
{ | ||
state.FallbackDisposable.Disposable = Disposable.Create( | ||
() => ExecuteWithNoRelayout(state.Owner, v => v.SetBackgroundDrawable(null))); | ||
} | ||
} | ||
View v = owner; | ||
|
||
if (!ready) | ||
else | ||
{ | ||
Cleanup(owner); | ||
SetAcrylicBlur(owner); | ||
UpdateProperties(); | ||
ready = true; | ||
state.FallbackDisposable.Disposable = null; | ||
|
||
ApplyAcrylicBlur(state); | ||
|
||
if (state.BlurDisposable.Disposable == null) | ||
{ | ||
state.BlurDisposable.Disposable = Disposable.Create( | ||
() => RemoveAcrylicBlur(state)); | ||
} | ||
} | ||
return new CompositeDisposable(); | ||
} | ||
|
||
private void ExecuteWithNoRelayout(BindableView view, Action<BindableView> action) | ||
{ | ||
using (view.PreventRequestLayout()) | ||
{ | ||
action(view); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Wraps the acrylic brush metadata for a single view. | ||
/// </summary> | ||
private class AcrylicState | ||
{ | ||
public AcrylicState(BindableView owner, Foundation.Rect drawArea, Path maskingPath) | ||
{ | ||
Owner = owner; | ||
DrawArea = drawArea; | ||
MaskingPath = maskingPath; | ||
} | ||
|
||
public SerialDisposable FallbackDisposable { get; } = new SerialDisposable(); | ||
|
||
public SerialDisposable BlurDisposable { get; } = new SerialDisposable(); | ||
|
||
public GradientDrawable BackgroundDrawable { get; set; } | ||
|
||
public View BlurViewWrapper { get; set; } | ||
|
||
public RealtimeBlurView BlurView { get; set; } | ||
|
||
public BindableView Owner { get; } | ||
|
||
public Foundation.Rect DrawArea { get; } | ||
|
||
public Path MaskingPath { get; } | ||
} | ||
} | ||
} | ||
#pragma warning restore CS6018 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.