Skip to content

Instantly share code, notes, and snippets.

@cannorin
Last active June 26, 2019 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cannorin/f1c8fe9032c86283443649fad7be470a to your computer and use it in GitHub Desktop.
Save cannorin/f1c8fe9032c86283443649fad7be470a to your computer and use it in GitHub Desktop.
an example of using Effects with Fabulous to add a long pressed gesture
ref: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/effects/
getting started:
1. put LongPressedEffect.fs in the netstandard/shared project
2. put LongPressedEffect.{iOS,Android}.fs in the corresponding platform project
3. use the `create` callback to add an effect to Fabulous components (cf. Usage.fs)
license: MIT
(*
The MIT License
LongPressedEffect - an example of using Effects with Fabulous to add a long pressed gesture
Copyright(c) 2019 cannorin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)
namespace ViewExtensions.Android
open ViewExtensions
open Xamarin.Forms
open Xamarin.Forms.Platform.Android
open System
type AndroidLongPressedEffect() =
inherit PlatformEffect()
let mutable attached = false
let mutable effects : LongPressedEffect[] = [||]
let handleLongPress (sender: obj) (e: Android.Views.View.LongClickEventArgs) =
for effect in effects do
effect.OnLongPressed()
let handler = new EventHandler<_>(handleLongPress)
override this.OnAttached() =
if not attached then
effects <-
this.Element.Effects
|> Seq.map box
|> Seq.choose (function :? LongPressedEffect as e -> Some e | _ -> None)
|> Seq.toArray
if not (isNull this.Control) then
this.Control.LongClickable <- true
this.Control.LongClick.AddHandler handler
else
this.Container.LongClickable <- true
this.Container.LongClick.AddHandler handler
attached <- true
override this.OnDetached() =
if attached then
if not (isNull this.Control) then
this.Control.LongClick.RemoveHandler handler
else
this.Container.LongClick.RemoveHandler handler
attached <- false
[<assembly: ResolutionGroupName("My")>]
[<assembly: ExportEffect(typeof<AndroidLongPressedEffect>, "LongPressedEffect")>]
do()
(*
The MIT License
LongPressedEffect - an example of using Effects with Fabulous to add a long pressed gesture
Copyright(c) 2019 cannorin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)
namespace ViewExtensions
open System.Windows.Input
open Xamarin.Forms
type LongPressedEventArgs = unit
type LongPressedEffect() =
inherit RoutingEffect("My.LongPressedEffect")
let mutable _handler : (LongPressedEventArgs -> unit) voption = ValueNone
member this.Handler
with get() : LongPressedEventArgs -> unit =
match _handler with ValueSome f -> f | ValueNone -> ignore
and set(f) = _handler <- ValueSome f
member this.OnLongPressed(e: LongPressedEventArgs) =
this.Handler e
module Effect =
let LongPressed(handler) = LongPressedEffect(Handler = handler)
(*
The MIT License
LongPressedEffect - an example of using Effects with Fabulous to add a long pressed gesture
Copyright(c) 2019 cannorin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)
namespace ViewExtensions.iOS
open ViewExtensions
open Xamarin.Forms
open Xamarin.Forms.Platform.iOS
open UIKit
type iOSLongPressedEffect() =
inherit PlatformEffect()
let mutable attached = false
let mutable effects : LongPressedEffect[] = [||]
let handleLongPress (sender: UILongPressGestureRecognizer) =
if sender.State = UIGestureRecognizerState.Began then
for effect in effects do
effect.OnLongPressed()
let lpgr =
new UILongPressGestureRecognizer(fun e -> handleLongPress e)
override this.OnAttached() =
if not attached then
effects <-
this.Element.Effects
|> Seq.map box
|> Seq.choose (function :? LongPressedEffect as e -> Some e | _ -> None)
|> Seq.toArray
this.Container.AddGestureRecognizer(lpgr)
attached <- true
override this.OnDetached() =
if attached then
this.Container.RemoveGestureRecognizer(lpgr)
attached <- false
[<assembly: ResolutionGroupName("My")>]
[<assembly: ExportEffect(typeof<iOSLongPressedEffect>, "LongPressedEffect")>]
do()
View.Button(
created = fun button ->
button.Effects.Add(Effect.LongPressed(fun e -> dispatch Something))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment