diff --git a/src/Elmish.WPF.sln b/src/Elmish.WPF.sln index 3efb3461..a8c9ea10 100644 --- a/src/Elmish.WPF.sln +++ b/src/Elmish.WPF.sln @@ -64,6 +64,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution ..\TUTORIAL.md = ..\TUTORIAL.md EndProjectSection EndProject +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Sticky.Core", "Samples\Sticky.Core\Sticky.Core.fsproj", "{F251BEA3-6AD1-4C14-9F3D-87FCFA4292EB}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sticky", "Samples\Sticky\Sticky.csproj", "{668E3AAD-B2AA-4934-804E-234C4F502038}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -174,6 +178,14 @@ Global {F095E7E4-28ED-4223-A92F-4E86922E34EF}.Debug|Any CPU.Build.0 = Debug|Any CPU {F095E7E4-28ED-4223-A92F-4E86922E34EF}.Release|Any CPU.ActiveCfg = Release|Any CPU {F095E7E4-28ED-4223-A92F-4E86922E34EF}.Release|Any CPU.Build.0 = Release|Any CPU + {F251BEA3-6AD1-4C14-9F3D-87FCFA4292EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F251BEA3-6AD1-4C14-9F3D-87FCFA4292EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F251BEA3-6AD1-4C14-9F3D-87FCFA4292EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F251BEA3-6AD1-4C14-9F3D-87FCFA4292EB}.Release|Any CPU.Build.0 = Release|Any CPU + {668E3AAD-B2AA-4934-804E-234C4F502038}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {668E3AAD-B2AA-4934-804E-234C4F502038}.Debug|Any CPU.Build.0 = Debug|Any CPU + {668E3AAD-B2AA-4934-804E-234C4F502038}.Release|Any CPU.ActiveCfg = Release|Any CPU + {668E3AAD-B2AA-4934-804E-234C4F502038}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -203,6 +215,8 @@ Global {36A4AEAF-8282-47EC-B751-BB3D16AB1A20} = {BBAFEB1E-93C0-4C7E-8E0A-026BB05C88EC} {F66F6E21-357E-4CE8-9807-042C5171AB06} = {BBAFEB1E-93C0-4C7E-8E0A-026BB05C88EC} {F095E7E4-28ED-4223-A92F-4E86922E34EF} = {BBAFEB1E-93C0-4C7E-8E0A-026BB05C88EC} + {F251BEA3-6AD1-4C14-9F3D-87FCFA4292EB} = {BBAFEB1E-93C0-4C7E-8E0A-026BB05C88EC} + {668E3AAD-B2AA-4934-804E-234C4F502038} = {BBAFEB1E-93C0-4C7E-8E0A-026BB05C88EC} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3125D461-08F4-4071-AAE5-1038EF84A360} diff --git a/src/Elmish.WPF/Binding.fs b/src/Elmish.WPF/Binding.fs index 6f4567e1..d473b5dc 100644 --- a/src/Elmish.WPF/Binding.fs +++ b/src/Elmish.WPF/Binding.fs @@ -866,7 +866,7 @@ module Binding = BindingData.subModelSelectedItemLast a.Data b.Data ///Creates a one-way binding with the model as the value. - let internal id<'model, 'msg when 'model : equality> : string -> Binding<'model, 'msg> = + let id<'model, 'msg when 'model : equality> : string -> Binding<'model, 'msg> = { Get = id } |> BindingData.OneWay.box |> OneWayData diff --git a/src/Samples/Sticky.Core/Program.fs b/src/Samples/Sticky.Core/Program.fs new file mode 100644 index 00000000..2771f491 --- /dev/null +++ b/src/Samples/Sticky.Core/Program.fs @@ -0,0 +1,57 @@ +module Elmish.WPF.Samples.Sticky.Program + +open Serilog +open Serilog.Extensions.Logging +open Elmish.WPF + +type Model = + { Count: int + StepSize: int } + +type Msg = + | Increment + | Decrement + | SetStepSize of int + | Reset + +let init = + { Count = 0 + StepSize = 1 } + +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 } + | SetStepSize x -> { m with StepSize = x } + | Reset -> init + +let bindings () : Binding list = [ + "CounterValue" + |> Binding.id + |> Binding.sticky (fun v -> v % 2 = 0) + |> Binding.mapModel (fun m -> m.Count) + "Increment" |> Binding.cmd Increment + "Decrement" |> Binding.cmd Decrement + "StepSize" |> Binding.twoWay( + (fun m -> float m.StepSize), + int >> SetStepSize) + "Reset" |> Binding.cmdIf(Reset, canReset) +] + +let designVm = ViewModel.designInstance init (bindings ()) + +let main window = + + let logger = + LoggerConfiguration() + .MinimumLevel.Override("Elmish.WPF.Update", Events.LogEventLevel.Verbose) + .MinimumLevel.Override("Elmish.WPF.Bindings", Events.LogEventLevel.Verbose) + .MinimumLevel.Override("Elmish.WPF.Performance", Events.LogEventLevel.Verbose) + .WriteTo.Console() + .CreateLogger() + + WpfProgram.mkSimple (fun () -> init) update bindings + |> WpfProgram.withLogger (new SerilogLoggerFactory(logger)) + |> WpfProgram.startElmishLoop window diff --git a/src/Samples/Sticky.Core/Sticky.Core.fsproj b/src/Samples/Sticky.Core/Sticky.Core.fsproj new file mode 100644 index 00000000..18146be4 --- /dev/null +++ b/src/Samples/Sticky.Core/Sticky.Core.fsproj @@ -0,0 +1,23 @@ + + + + net5.0-windows + true + true + + + + + + + + + + + + + + + + + diff --git a/src/Samples/Sticky/App.xaml b/src/Samples/Sticky/App.xaml new file mode 100644 index 00000000..c0946fb7 --- /dev/null +++ b/src/Samples/Sticky/App.xaml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/src/Samples/Sticky/App.xaml.cs b/src/Samples/Sticky/App.xaml.cs new file mode 100644 index 00000000..8743cc7d --- /dev/null +++ b/src/Samples/Sticky/App.xaml.cs @@ -0,0 +1,20 @@ +using System; +using System.Windows; + +namespace Elmish.WPF.Samples.Sticky +{ + public partial class App : Application + { + public App() + { + this.Activated += StartElmish; + } + + private void StartElmish(object sender, EventArgs e) + { + this.Activated -= StartElmish; + Program.main(MainWindow); + } + + } +} diff --git a/src/Samples/Sticky/MainWindow.xaml b/src/Samples/Sticky/MainWindow.xaml new file mode 100644 index 00000000..95ce2a9c --- /dev/null +++ b/src/Samples/Sticky/MainWindow.xaml @@ -0,0 +1,24 @@ + + + + + +