Add platform feedback api for sound and haptic feedback#21189
Add platform feedback api for sound and haptic feedback#21189
Conversation
|
You can test this PR using the following package version. |
|
You can test this PR using the following package version. |
| FeedbackEffect.Click => UIImpactFeedbackStyle.Light, | ||
| FeedbackEffect.LongPress => UIImpactFeedbackStyle.Medium, |
There was a problem hiding this comment.
iOS has five different feedback styles, but this system only permits the use of two.
What if feedback effects worked like theme variants, so that instead of an enum the parameter is a reference type? Cross-platform values can be defined in Avalonia.Controls and platform-specific values can be defined in Avalonia.iOS, Avalonia.Android, etc.
There was a problem hiding this comment.
How would users access those platform specific ones?
There was a problem hiding this comment.
By referencing the platform specific projects in their own platform-specific projects.
There was a problem hiding this comment.
I'm still not quite clear on how that can be done. View and viewmodels can't access platform specific without DI. If they are like theme variants, then they would have to call PerformFeedback multiple times. First for the crossplatform version, then another for whatever platform they want special effects.
Also, how often would devs want to trigger effects outside the defaults for just 1 platform?
There was a problem hiding this comment.
View and viewmodels can't access platform specific without DI.
Huh? People are allowed to make apps which only target one platform. It's quite alright for your views to be defined in a project which references that platform's Avalonia library, e.g. Avalonia.iOS.
But in the project where I used these features, our views were indeed in a platform-agnostic project. So I created a RoutedEventArgs class requesting haptic feedback and defining if it was a "scroll" or "snap". Then in our iOS executable project, I installed a TopLevel handler for that event which translated scroll to Light and snap to Soft and made the appropriate iOS SDK calls.
I think this answers your question about developers triggering effect types other than what you deem the "defaults".
I have no idea what you mean about performing feedback multiple times.
There was a problem hiding this comment.
I think its a bit beyond the scope of this pr. The focus here is to provide basic feedback for the built-in controls, while allowing devs to do the same for their custom controls. This is to ensure behavior is the same or native across all platforms that supports some form of feedback.
Devs can create their own feedback system to provide custom effects for their control, but most devs should be good with just the basic effects.
There was a problem hiding this comment.
Sure, but anyone doing that must implement everything from scratch. The simple change I suggested above would allow them to use this code.
There was a problem hiding this comment.
How would the api look like?
There was a problem hiding this comment.
Remove: public enum FeedbackEffect { ... }.
Add: public abstract class FeedbackEffect;, public class ClickFeedbackEffect : FeedbackEffect;, and so on.
Everything else stays the same. You can define an internal constructor for the abstract class if you don't want third parties deriving their own effects.
There was a problem hiding this comment.
If we go this API route, I think TextTrimming-like API is a better fit.
Avalonia/src/Avalonia.Base/Media/TextTrimming.cs
Lines 9 to 21 in a3c571b
| ? UIImpactFeedbackGenerator.GetFeedbackGenerator(FeedbackToImpactStyle(feedback), _avaloniaView) | ||
| : new UIImpactFeedbackGenerator(FeedbackToImpactStyle(feedback)); | ||
| generator.Prepare(); | ||
| generator.ImpactOccurred(); |
There was a problem hiding this comment.
This method has an overload which accepts a position parameter. It would be trivial to accept an Avalonia position and provide it to iOS here.
|
You can test this PR using the following package version. |
What does the pull request do?
Add audio and haptic feedback for common interact-able controls.
This PR is a rework of #9420 .The api has been stripped down and the core implementation is internal.
By default, custom controls have feedback disabled. They can enable it using
PlatformFeedback.FeedbackTypeattached property and trigger either sound or haptic feedback based on the set value usingPerformFeedbackextension method,e.g.
this.PerformFeedback(FeedbackType.Click).What is the current behavior?
What is the updated/expected behavior with this PR?
How was the solution implemented (if it's not obvious)?
Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues