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

Add remapping extensions for core #17918

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
76 changes: 76 additions & 0 deletions src/Core/src/CommandMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,81 @@ public static void PrependToMapping<TVirtualView, TViewHandler>(this ICommandMap
action?.Invoke(handler!, view, args);
});
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Replace a command mapping in place but call the previous mapping if the types do not match.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The modified method to call when the command is updated.</param>
/// <returns>Target Command Mapper</returns>
internal static ICommandMapper<TVirtualView, TViewHandler> Replace<TVirtualView, TViewHandler>(this ICommandMapper<TVirtualView, TViewHandler> commandMapper,
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
string key, Action<TViewHandler, TVirtualView, object?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
commandMapper.ModifyMapping<TVirtualView, TViewHandler>(key, (h, v, a, p) => method.Invoke(h, v, a));
return commandMapper;
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Specify a method to be run before an existing command mapping.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The method to call before the existing mapping begins.</param>
/// <returns>Target Command Mapper</returns>
internal static ICommandMapper<TVirtualView, TViewHandler> Prepend<TVirtualView, TViewHandler>(this ICommandMapper<TVirtualView, TViewHandler> commandMapper,
string key, Action<TViewHandler, TVirtualView, object?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
commandMapper.PrependToMapping(key, method);
return commandMapper;
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Specify a method to be run after an existing command mapping.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The method to call after the existing mapping is finished.</param>
/// <returns>Target Command Mapper</returns>
internal static ICommandMapper<TVirtualView, TViewHandler> Append<TVirtualView, TViewHandler>(this ICommandMapper<TVirtualView, TViewHandler> commandMapper,
string key, Action<TViewHandler, TVirtualView, object?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
commandMapper.AppendToMapping(key, method);
return commandMapper;
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Modify a command mapping in place.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The modified method to call when the command is updated.</param>
/// <returns>Target Command Mapper</returns>
internal static ICommandMapper<TVirtualView, TViewHandler> Modify<TVirtualView, TViewHandler>(this ICommandMapper<TVirtualView, TViewHandler> commandMapper,
string key, Action<TViewHandler, TVirtualView, object?, Action<IElementHandler, IElement, object?>?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
commandMapper.ModifyMapping(key, method);
return commandMapper;
}
}
}
9 changes: 6 additions & 3 deletions src/Core/src/Handlers/SwipeView/SwipeViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,17 @@ public static void MapIsEnabled(ISwipeViewHandler handler, ISwipeView swipeView)
ViewHandler.MapIsEnabled(handler, swipeView);
}

public static void MapBackground(ISwipeViewHandler handler, ISwipeView swipeView)
static void MapBackground(ISwipeViewHandler handler, ISwipeView view, System.Action<IElementHandler, IElement>? action)
{
if (swipeView.Background == null)
if (view.Background == null)
handler.PlatformView.Control?.SetWindowBackground();
else
ViewHandler.MapBackground(handler, swipeView);
action?.Invoke(handler, view);
}

public static void MapBackground(ISwipeViewHandler handler, ISwipeView swipeView) =>
MapBackground(handler, swipeView, (_, _) => ViewHandler.MapBackground(handler, swipeView));

public static void MapSwipeTransitionMode(ISwipeViewHandler handler, ISwipeView swipeView)
{
handler.PlatformView.UpdateSwipeTransitionMode(swipeView.SwipeTransitionMode);
Expand Down
8 changes: 5 additions & 3 deletions src/Core/src/Handlers/SwipeView/SwipeViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public partial class SwipeViewHandler : ISwipeViewHandler
{
public static IPropertyMapper<ISwipeView, ISwipeViewHandler> Mapper = new PropertyMapper<ISwipeView, ISwipeViewHandler>(ViewHandler.ViewMapper)
{
[nameof(IContentView.Content)] = MapContent,
[nameof(ISwipeView.SwipeTransitionMode)] = MapSwipeTransitionMode,
[nameof(ISwipeView.LeftItems)] = MapLeftItems,
[nameof(ISwipeView.TopItems)] = MapTopItems,
Expand All @@ -25,10 +24,13 @@ public partial class SwipeViewHandler : ISwipeViewHandler
#if ANDROID || IOS || TIZEN
[nameof(IView.IsEnabled)] = MapIsEnabled,
#endif
}
.Replace(nameof(IContentView.Content), MapContent)
#if ANDROID
[nameof(IView.Background)] = MapBackground,
.Modify(nameof(IView.Background), MapBackground)
#endif
};
;


public static CommandMapper<ISwipeView, ISwipeViewHandler> CommandMapper = new(ViewCommandMapper)
{
Expand Down
75 changes: 75 additions & 0 deletions src/Core/src/PropertyMapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,80 @@ public static void PrependToMapping<TVirtualView, TViewHandler>(this IPropertyMa
action?.Invoke(handler!, view);
});
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Specify a method to be run before an existing property mapping.
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call before the existing mapping begins.</param>
/// <returns>Target Property Mapper</returns>
internal static IPropertyMapper<TVirtualView, TViewHandler> Modify<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView, Action<IElementHandler, IElement>?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.ModifyMapping(key, method);
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
return propertyMapper;
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Replace a property mapping in place but call the previous mapping if the types do not match.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The modified method to call when the property is updated.</param>
/// <returns>Target Property Mapper</returns>
internal static IPropertyMapper<TVirtualView, TViewHandler> Replace<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
return propertyMapper.Modify(key, (h, v, p) => method.Invoke(h, v));
}
mattleibow marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Specify a method to be run after an existing property mapping but skip if the types do not match.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call after the existing mapping is finished.</param>
/// <returns>Target Property Mapper</returns>
internal static IPropertyMapper<TVirtualView, TViewHandler> Append<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.AppendToMapping(key, method);
return propertyMapper;
}

/// <summary>
/// Used internally to provide a fluent API for easily modifying
/// mappers at the point of instantiation.
/// Specify a method to be run before an existing property mapping.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="propertyMapper">The property mapper in which to change the mapping.</param>
/// <param name="key">The name of the property.</param>
/// <param name="method">The method to call before the existing mapping begins.</param>
/// <returns>Target Property Mapper</returns>
internal static IPropertyMapper<TVirtualView, TViewHandler> Prepend<TVirtualView, TViewHandler>(this IPropertyMapper<TVirtualView, TViewHandler> propertyMapper,
string key, Action<TViewHandler, TVirtualView> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
propertyMapper.PrependToMapping(key, method);
return propertyMapper;
}
}
}
Loading