From d5caf9589a10a979b3241c7b85ca2cd895a533d6 Mon Sep 17 00:00:00 2001 From: Joshua Marner Date: Mon, 22 Apr 2024 17:14:36 -0500 Subject: [PATCH 1/3] Add a typed version of OneWaySeq --- src/Elmish.WPF/Binding.fs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Elmish.WPF/Binding.fs b/src/Elmish.WPF/Binding.fs index d3c07694..f74f4ba6 100644 --- a/src/Elmish.WPF/Binding.fs +++ b/src/Elmish.WPF/Binding.fs @@ -116,6 +116,13 @@ module Binding = OneWayToSource.id |> createBindingT + + module OneWaySeqT = + + let id itemEquals (getId: 'a -> 'id) : string -> Binding<_, 'msg, _> = + OneWaySeq.create itemEquals getId + |> createBindingT + /// /// Strongly-typed bindings that update both ways /// From adf9c37aba67c5e9b678677d154703600b859eff Mon Sep 17 00:00:00 2001 From: Joshua Marner Date: Mon, 22 Apr 2024 17:15:04 -0500 Subject: [PATCH 2/3] Add example of OneWaySeqT --- src/Samples/SubModelStatic.Core/Program.fs | 13 ++++--- src/Samples/SubModelStatic/Counter.xaml | 41 +++++++++++++++++----- src/Samples/SubModelStatic/MainWindow.xaml | 2 +- 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Samples/SubModelStatic.Core/Program.fs b/src/Samples/SubModelStatic.Core/Program.fs index 297e7d08..c983a4eb 100644 --- a/src/Samples/SubModelStatic.Core/Program.fs +++ b/src/Samples/SubModelStatic.Core/Program.fs @@ -11,7 +11,8 @@ module Counter = type Model = { Count: int - StepSize: int } + StepSize: int + History: (int * int) list } type Msg = | Increment @@ -21,14 +22,15 @@ module Counter = let init = { Count = 0 - StepSize = 1 } + StepSize = 1 + History = [] } let canReset = (<>) init let update msg m = match msg with - | Increment -> { m with Count = m.Count + m.StepSize } - | Decrement -> { m with Count = m.Count - m.StepSize } + | Increment -> { m with Count = m.Count + m.StepSize; History = (m.Count, m.History.Length) :: m.History } + | Decrement -> { m with Count = m.Count - m.StepSize; History = (m.Count, m.History.Length) :: m.History } | SetStepSize x -> { m with StepSize = x } | Reset -> init @@ -41,7 +43,7 @@ type [] CounterViewModel (args) = >> Binding.mapModel (fun (m: Counter.Model) -> m.StepSize) >> Binding.mapMsg Counter.Msg.SetStepSize - new() = CounterViewModel(Counter.init |> ViewModelArgs.simple) + new() = CounterViewModel({ Counter.init with History = [ (3,1); (0,0) ] } |> ViewModelArgs.simple) member _.StepSize with get() = base.Get() stepSizeBinding @@ -50,6 +52,7 @@ type [] CounterViewModel (args) = member _.Increment = base.Get() (Binding.CmdT.setAlways Counter.Increment) member _.Decrement = base.Get() (Binding.CmdT.setAlways Counter.Decrement) member _.Reset = base.Get() (Binding.CmdT.set Counter.canReset Counter.Reset) + member _.History = base.Get() (Binding.OneWaySeqT.id (=) snd >> Binding.mapModel (fun m -> m.History)) module Clock = diff --git a/src/Samples/SubModelStatic/Counter.xaml b/src/Samples/SubModelStatic/Counter.xaml index 2fa11674..5551966d 100644 --- a/src/Samples/SubModelStatic/Counter.xaml +++ b/src/Samples/SubModelStatic/Counter.xaml @@ -6,12 +6,37 @@ xmlns:vm="clr-namespace:Elmish.WPF.Samples.SubModelStatic;assembly=SubModelStatic.Core" mc:Ignorable="d" d:DataContext="{d:DesignInstance Type=vm:CounterViewModel, IsDesignTimeCreatable=True}"> - - -