4 people like it.

Still Mouse Click Event Using Scan

Detects a mouse down then up event without a move.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Input
open System.Windows.Media

[<AutoOpen>]
module Mouse =
    type evt =
        | Down
        | Up
        | Move

    let clickedOn (control:UIElement) =
        let down = control.MouseLeftButtonDown |> Event.map (fun _ -> Down)
        let up = control.MouseLeftButtonUp |> Event.map (fun _ -> Up)
        let move = control.MouseMove |> Event.map (fun _ -> Move)  
        let mouseEvents = Event.merge move (Event.merge up down)
        let click =
            mouseEvents
            |> Event.scan (fun (lastEvt,_) newEvt ->
                match lastEvt, newEvt with
                | Some Down, Up -> Some Up, Some()
                | _, x -> Some x, None    
            ) (None, None)
            |> Event.choose snd
        click
    
type AppControl() as control =
  inherit UserControl(Width = 320.0, Height = 200.0)
  
  let canvas = Canvas(Background = SolidColorBrush Colors.Orange)
  let block = TextBlock(Text="Hit Me2", FontSize = 20.0)

  let mutable clicks = 0
  let clicked = Mouse.clickedOn control
  do  clicked.Add (fun _ -> 
          clicks <- clicks + 1
          block.Text <- "Still Clicks " + clicks.ToString()
      )
    
  do canvas.Children.Add(block)   
     base.Content <- canvas

#if INTERACTIVE
open Microsoft.TryFSharp
App.Dispatch (fun() -> 
    App.Console.ClearCanvas()
    AppControl() |> App.Console.Canvas.Children.Add
    App.Console.CanvasPosition <- CanvasPosition.Right
)
#endif
namespace System
namespace System.Windows
namespace System.Media
Multiple items
type AutoOpenAttribute =
  inherit Attribute
  new : unit -> AutoOpenAttribute
  new : path:string -> AutoOpenAttribute
  member Path : string

Full name: Microsoft.FSharp.Core.AutoOpenAttribute

--------------------
new : unit -> AutoOpenAttribute
new : path:string -> AutoOpenAttribute
type evt =
  | Down
  | Up
  | Move

Full name: Script.Mouse.evt
union case evt.Down: evt
union case evt.Up: evt
union case evt.Move: evt
val clickedOn : control:'a -> IEvent<unit>

Full name: Script.Mouse.clickedOn
val control : 'a
val down : IEvent<evt>
Multiple items
module Event

from Microsoft.FSharp.Control

--------------------
type Event<'T> =
  new : unit -> Event<'T>
  member Trigger : arg:'T -> unit
  member Publish : IEvent<'T>

Full name: Microsoft.FSharp.Control.Event<_>

--------------------
type Event<'Delegate,'Args (requires delegate and 'Delegate :> Delegate)> =
  new : unit -> Event<'Delegate,'Args>
  member Trigger : sender:obj * args:'Args -> unit
  member Publish : IEvent<'Delegate,'Args>

Full name: Microsoft.FSharp.Control.Event<_,_>

--------------------
new : unit -> Event<'T>

--------------------
new : unit -> Event<'Delegate,'Args>
val map : mapping:('T -> 'U) -> sourceEvent:IEvent<'Del,'T> -> IEvent<'U> (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.map
val up : IEvent<evt>
val move : IEvent<evt>
val mouseEvents : IEvent<evt>
val merge : event1:IEvent<'Del1,'T> -> event2:IEvent<'Del2,'T> -> IEvent<'T> (requires delegate and 'Del1 :> Delegate and delegate and 'Del2 :> Delegate)

Full name: Microsoft.FSharp.Control.Event.merge
val click : IEvent<unit>
val scan : collector:('U -> 'T -> 'U) -> state:'U -> sourceEvent:IEvent<'Del,'T> -> IEvent<'U> (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.scan
val lastEvt : evt option
val newEvt : evt
union case Option.Some: Value: 'T -> Option<'T>
val x : evt
union case Option.None: Option<'T>
val choose : chooser:('T -> 'U option) -> sourceEvent:IEvent<'Del,'T> -> IEvent<'U> (requires delegate and 'Del :> Delegate)

Full name: Microsoft.FSharp.Control.Event.choose
val snd : tuple:('T1 * 'T2) -> 'T2

Full name: Microsoft.FSharp.Core.Operators.snd
Multiple items
type AppControl =
  inherit obj
  new : unit -> AppControl

Full name: Script.AppControl

--------------------
new : unit -> AppControl
val control : AppControl
namespace System.Text
module Mouse

from Script
Raw view Test code New version

More information

Link:http://fssnip.net/60
Posted:12 years ago
Author:Zach Bray
Tags: silverlight , events