Skip to content

Commit

Permalink
add effects and update to latest (2019) touch effects commits
Browse files Browse the repository at this point in the history
  • Loading branch information
roubachof committed Sep 26, 2022
1 parent 2fcbaaf commit d56462a
Show file tree
Hide file tree
Showing 20 changed files with 1,224 additions and 184 deletions.
110 changes: 110 additions & 0 deletions Maui.Tabs/Effects/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Linq;
using System.Windows.Input;
using Xamarin.Forms;

namespace XamEffects {
public static class Commands {
[Obsolete("Not need with usual Linking")]
public static void Init() {
}

public static readonly BindableProperty TapProperty =
BindableProperty.CreateAttached(
"Tap",
typeof(ICommand),
typeof(Commands),
default(ICommand),
propertyChanged: PropertyChanged
);

public static void SetTap(BindableObject view, ICommand value) {
view.SetValue(TapProperty, value);
}

public static ICommand GetTap(BindableObject view) {
return (ICommand)view.GetValue(TapProperty);
}

public static readonly BindableProperty TapParameterProperty =
BindableProperty.CreateAttached(
"TapParameter",
typeof(object),
typeof(Commands),
default(object),
propertyChanged: PropertyChanged
);

public static void SetTapParameter(BindableObject view, object value) {
view.SetValue(TapParameterProperty, value);
}

public static object GetTapParameter(BindableObject view) {
return view.GetValue(TapParameterProperty);
}

public static readonly BindableProperty LongTapProperty =
BindableProperty.CreateAttached(
"LongTap",
typeof(ICommand),
typeof(Commands),
default(ICommand),
propertyChanged: PropertyChanged
);

public static void SetLongTap(BindableObject view, ICommand value) {
view.SetValue(LongTapProperty, value);
}

public static ICommand GetLongTap(BindableObject view) {
return (ICommand)view.GetValue(LongTapProperty);
}

public static readonly BindableProperty LongTapParameterProperty =
BindableProperty.CreateAttached(
"LongTapParameter",
typeof(object),
typeof(Commands),
default(object)
);

public static void SetLongTapParameter(BindableObject view, object value) {
view.SetValue(LongTapParameterProperty, value);
}

public static object GetLongTapParameter(BindableObject view) {
return view.GetValue(LongTapParameterProperty);
}

static void PropertyChanged(BindableObject bindable, object oldValue, object newValue) {
if (!(bindable is View view))
return;

var eff = view.Effects.FirstOrDefault(e => e is CommandsRoutingEffect);

if (GetTap(bindable) != null || GetLongTap(bindable) != null) {
view.InputTransparent = false;

if (eff != null) return;
view.Effects.Add(new CommandsRoutingEffect());
if (EffectsConfig.AutoChildrenInputTransparent && bindable is Layout &&
!EffectsConfig.GetChildrenInputTransparent(view)) {
EffectsConfig.SetChildrenInputTransparent(view, true);
}
}
else {
if (eff == null || view.BindingContext == null) return;
view.Effects.Remove(eff);
if (EffectsConfig.AutoChildrenInputTransparent && bindable is Layout &&
EffectsConfig.GetChildrenInputTransparent(view)) {
EffectsConfig.SetChildrenInputTransparent(view, false);
}
}
}
}

public class CommandsRoutingEffect : RoutingEffect {
public CommandsRoutingEffect() : base("XamEffects." + nameof(Commands)) {
}
}
}
55 changes: 55 additions & 0 deletions Maui.Tabs/Effects/EffectsConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Xamarin.Forms;

namespace XamEffects {
public static class EffectsConfig {
[Obsolete("Not need with usual Linking")]
public static void Init() {
}

public static bool AutoChildrenInputTransparent { get; set; } = true;

public static readonly BindableProperty ChildrenInputTransparentProperty =
BindableProperty.CreateAttached(
"ChildrenInputTransparent",
typeof(bool),
typeof(EffectsConfig),
false,
propertyChanged: (bindable, oldValue, newValue) => {
ConfigureChildrenInputTransparent(bindable);
}
);

public static void SetChildrenInputTransparent(BindableObject view, bool value) {
view.SetValue(ChildrenInputTransparentProperty, value);
}

public static bool GetChildrenInputTransparent(BindableObject view) {
return (bool)view.GetValue(ChildrenInputTransparentProperty);
}

static void ConfigureChildrenInputTransparent(BindableObject bindable) {
if (!(bindable is Layout layout))
return;

if (GetChildrenInputTransparent(bindable)) {
foreach (View layoutChild in layout.Children)
AddInputTransparentToElement(layoutChild);
layout.ChildAdded += Layout_ChildAdded;
}
else {
layout.ChildAdded -= Layout_ChildAdded;
}
}

static void Layout_ChildAdded(object sender, ElementEventArgs e) {
AddInputTransparentToElement(e.Element);
}

static void AddInputTransparentToElement(BindableObject obj) {
if (obj is View view && TouchEffect.GetColor(view) == Colors.Transparent && Commands.GetTap(view) == null && Commands.GetLongTap(view) == null) {
view.InputTransparent = true;
}
}
}
}
55 changes: 55 additions & 0 deletions Maui.Tabs/Effects/TouchEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Linq;
using Xamarin.Forms;

namespace XamEffects {
public static class TouchEffect {

public static readonly BindableProperty ColorProperty =
BindableProperty.CreateAttached(
"Color",
typeof(Color),
typeof(TouchEffect),
Colors.Transparent,
propertyChanged: PropertyChanged
);

public static void SetColor(BindableObject view, Color value) {
view.SetValue(ColorProperty, value);
}

public static Color GetColor(BindableObject view) {
return (Color)view.GetValue(ColorProperty);
}

static void PropertyChanged(BindableObject bindable, object oldValue, object newValue) {
if (!(bindable is View view))
return;

var eff = view.Effects.FirstOrDefault(e => e is TouchRoutingEffect);
if (GetColor(bindable) != Colors.Transparent) {
view.InputTransparent = false;

if (eff != null) return;
view.Effects.Add(new TouchRoutingEffect());
if (EffectsConfig.AutoChildrenInputTransparent && bindable is Layout &&
!EffectsConfig.GetChildrenInputTransparent(view)) {
EffectsConfig.SetChildrenInputTransparent(view, true);
}
}
else {
if (eff == null || view.BindingContext == null) return;
view.Effects.Remove(eff);
if (EffectsConfig.AutoChildrenInputTransparent && bindable is Layout &&
EffectsConfig.GetChildrenInputTransparent(view)) {
EffectsConfig.SetChildrenInputTransparent(view, false);
}
}
}
}

public class TouchRoutingEffect : RoutingEffect {
public TouchRoutingEffect() : base("XamEffects." + nameof(TouchEffect)) {
}
}
}
36 changes: 6 additions & 30 deletions Maui.Tabs/Maui.Tabs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,20 @@
* Independent ViewSwitcher
* Bindable with ItemsSource

--------------
Installation
--------------
## Installation

* In Core project, in `App.xaml.cs`:

```csharp
public App()
public static MauiApp CreateMauiApp()
{
InitializeComponent();

Sharpnado.Tabs.Initializer.Initialize(loggerEnable: false);
...
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.UseSharpnadoTabs();
}
```

* In iOS project:

```csharp
Xamarin.Forms.Forms.Init();
Sharpnado.Tabs.iOS.Preserver.Preserve();
```

* In UWP project:
```csharp
var rendererAssemblies = new[]
{
typeof(UWPShadowsRenderer).GetTypeInfo().Assembly,
typeof(UwpTintableImageEffect).GetTypeInfo().Assembly,
};

Xamarin.Forms.Forms.Init(e, rendererAssemblies);
```
</Description>

<Owners>Jean-Marie Alfonsi</Owners>
Expand Down Expand Up @@ -158,8 +139,6 @@
<Compile Include="..\Tabs\Tabs\BadgeView.cs" Link="BadgeView.cs" />
<Compile Include="..\Tabs\Tabs\BottomTabItem.xaml.cs" Link="BottomTabItem.xaml.cs" />
<Compile Include="..\Tabs\Tabs\Effects\ImageEffect.cs" Link="Effects\ImageEffect.cs" />
<Compile Include="..\Tabs\Tabs\Effects\TapCommandEffect.cs" Link="Effects\TapCommandEffect.cs" />
<Compile Include="..\Tabs\Tabs\Effects\ViewEffect.cs" Link="Effects\ViewEffect.cs" />
<Compile Include="..\Tabs\Tabs\IAnimatableReveal.cs" Link="IAnimatableReveal.cs" />
<Compile Include="..\Tabs\Tabs\IconOptions.cs" Link="IconOptions.cs" />
<Compile Include="..\Tabs\Tabs\Initializer.cs" Link="Initializer.cs" />
Expand All @@ -177,9 +156,6 @@
<Compile Include="..\Tabs\Tabs\ViewSwitcher.cs" Link="ViewSwitcher.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Effects\" />
<Folder Include="Platforms\Android\" />
<Folder Include="Platforms\iOS\" />
<Folder Include="Platforms\MacCatalyst\" />
<Folder Include="Platforms\Tizen\" />
<Folder Include="Platforms\Windows\" />
Expand Down
21 changes: 21 additions & 0 deletions Maui.Tabs/MauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Sharpnado.Tabs;

public static class MauiAppBuilderExtensions
{
public static MauiAppBuilder UseSharpnadoTabs(
this MauiAppBuilder builder,
bool loggerEnable,
bool debugLogEnable = false)
{
InternalLogger.EnableDebug = debugLogEnable;
InternalLogger.EnableLogging = loggerEnable;

#if __IOS__
XamEffects.iOS.CommandsPlatform.Init();
XamEffects.iOS.TouchEffectPlatform.Init();
Sharpnado.Tabs.iOS.iOSTintableImageEffect.Init();
#endif

return builder;
}
}
Loading

0 comments on commit d56462a

Please sign in to comment.