-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Attributes to Mvu & Components
- Loading branch information
1 parent
da92948
commit 6eaa99a
Showing
4 changed files
with
120 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace Fabulous | ||
|
||
open System | ||
open Fabulous | ||
open Fabulous.ScalarAttributeDefinitions | ||
|
||
module ComponentAttributes = | ||
/// Define an attribute for EventHandler | ||
let inline defineEventNoArg name ([<InlineIfLambda>] getEvent: obj -> IEvent<EventHandler, EventArgs>) : SimpleScalarAttributeDefinition<unit -> unit> = | ||
let key = | ||
SimpleScalarAttributeDefinition.CreateAttributeData( | ||
ScalarAttributeComparers.noCompare, | ||
(fun _ (newValueOpt: (unit -> unit) voption) node -> | ||
match node.TryGetHandler(name) with | ||
| ValueNone -> () | ||
| ValueSome handler -> handler.Dispose() | ||
|
||
match newValueOpt with | ||
| ValueNone -> node.RemoveHandler(name) | ||
| ValueSome(fn) -> | ||
let event = getEvent node.Target | ||
node.SetHandler(name, event.Subscribe(fun _ -> fn()))) | ||
) | ||
|
||
|> AttributeDefinitionStore.registerScalar | ||
|
||
{ Key = key; Name = name } | ||
|
||
/// Define an attribute for EventHandler<'T> | ||
let inline defineEvent<'args> | ||
name | ||
([<InlineIfLambda>] getEvent: obj -> IEvent<EventHandler<'args>, 'args>) | ||
: SimpleScalarAttributeDefinition<'args -> unit> = | ||
let key = | ||
SimpleScalarAttributeDefinition.CreateAttributeData( | ||
ScalarAttributeComparers.noCompare, | ||
(fun _ (newValueOpt: ('args -> unit) voption) node -> | ||
match node.TryGetHandler(name) with | ||
| ValueNone -> () | ||
| ValueSome handler -> handler.Dispose() | ||
|
||
match newValueOpt with | ||
| ValueNone -> node.RemoveHandler(name) | ||
| ValueSome(fn) -> | ||
let event = getEvent node.Target | ||
node.SetHandler(name, event.Subscribe(fun args -> fn args))) | ||
) | ||
|
||
|> AttributeDefinitionStore.registerScalar | ||
|
||
{ Key = key; Name = name } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
namespace Fabulous | ||
|
||
open System | ||
open System.Runtime.CompilerServices | ||
open Fabulous | ||
open Fabulous.ScalarAttributeDefinitions | ||
|
||
type MsgValue = MsgValue of obj | ||
|
||
[<Extension>] | ||
type SimpleScalarAttributeDefinitionExtensions() = | ||
[<Extension>] | ||
static member inline WithValue(this: SimpleScalarAttributeDefinition<'args -> MsgValue>, value: 'args -> 'msg) = | ||
this.WithValue(value >> box >> MsgValue) | ||
|
||
module MvuAttributes = | ||
|
||
/// Define an attribute for EventHandler | ||
let inline defineEventNoArg name ([<InlineIfLambda>] getEvent: obj -> IEvent<EventHandler, EventArgs>) : SimpleScalarAttributeDefinition<MsgValue> = | ||
let key = | ||
SimpleScalarAttributeDefinition.CreateAttributeData( | ||
ScalarAttributeComparers.noCompare, | ||
(fun _ (newValueOpt: MsgValue voption) node -> | ||
match node.TryGetHandler(name) with | ||
| ValueNone -> () | ||
| ValueSome handler -> handler.Dispose() | ||
|
||
match newValueOpt with | ||
| ValueNone -> node.RemoveHandler(name) | ||
| ValueSome(MsgValue msg) -> | ||
let event = getEvent node.Target | ||
let handler = event.Subscribe(fun _ -> Dispatcher.dispatch node msg) | ||
node.SetHandler(name, handler)) | ||
) | ||
|
||
|> AttributeDefinitionStore.registerScalar | ||
|
||
{ Key = key; Name = name } | ||
|
||
/// Define an attribute for EventHandler<'T> | ||
let inline defineEvent<'args> | ||
name | ||
([<InlineIfLambda>] getEvent: obj -> IEvent<EventHandler<'args>, 'args>) | ||
: SimpleScalarAttributeDefinition<'args -> MsgValue> = | ||
let key = | ||
SimpleScalarAttributeDefinition.CreateAttributeData( | ||
ScalarAttributeComparers.noCompare, | ||
(fun _ (newValueOpt: ('args -> MsgValue) voption) (node: IViewNode) -> | ||
match node.TryGetHandler(name) with | ||
| ValueNone -> () | ||
| ValueSome handler -> handler.Dispose() | ||
|
||
match newValueOpt with | ||
| ValueNone -> node.RemoveHandler(name) | ||
| ValueSome fn -> | ||
let event = getEvent node.Target | ||
|
||
let handler = | ||
event.Subscribe(fun args -> | ||
let (MsgValue r) = fn args | ||
Dispatcher.dispatch node r) | ||
|
||
node.SetHandler(name, handler)) | ||
) | ||
|> AttributeDefinitionStore.registerScalar | ||
|
||
{ Key = key; Name = name } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters