Skip to content

Commit

Permalink
added a sticky sample
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonMN committed Feb 17, 2021
1 parent 9287f82 commit 52d1363
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/Elmish.WPF.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/Elmish.WPF/Binding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/Samples/Sticky.Core/Program.fs
Original file line number Diff line number Diff line change
@@ -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<Model, Msg> 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
23 changes: 23 additions & 0 deletions src/Samples/Sticky.Core/Sticky.Core.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWpf>true</UseWpf>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Elmish.WPF\Elmish.WPF.fsproj" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/Samples/Sticky/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application x:Class="Elmish.WPF.Samples.Sticky.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
20 changes: 20 additions & 0 deletions src/Samples/Sticky/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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);
}

}
}
24 changes: 24 additions & 0 deletions src/Samples/Sticky/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Window x:Class="Elmish.WPF.Samples.Sticky.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Elmish.WPF.Samples.Sticky;assembly=Sticky.Core"
Title="Sticky"
Height="120"
Width="500"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d"
d:DataContext="{x:Static vm:Program.designVm}">
<StackPanel Orientation="Vertical">
<TextBlock Text="The counter value binding is sticky to even numbers"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,25,0,0">
<TextBlock Text="{Binding CounterValue, StringFormat='Counter value: {0}'}" Width="110" Margin="0,5,10,5" />
<Button Command="{Binding Decrement}" Content="-" Margin="0,5,10,5" Width="30" />
<Button Command="{Binding Increment}" Content="+" Margin="0,5,10,5" Width="30" />
<TextBlock Text="{Binding StepSize, StringFormat='Step size: {0}'}" Width="70" Margin="0,5,10,5" />
<Slider Value="{Binding StepSize}" TickFrequency="1" Maximum="10" Minimum="1" IsSnapToTickEnabled="True" Width="100" Margin="0,5,10,5" />
<Button Command="{Binding Reset}" Content="Reset" Margin="0,5,10,5" Width="50" />
</StackPanel>
</StackPanel>
</Window>
11 changes: 11 additions & 0 deletions src/Samples/Sticky/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Windows;

namespace Elmish.WPF.Samples.Sticky {
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
14 changes: 14 additions & 0 deletions src/Samples/Sticky/Sticky.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWpf>true</UseWpf>
<OutputType>Exe</OutputType>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Sticky.Core\Sticky.Core.fsproj" />
</ItemGroup>

</Project>

0 comments on commit 52d1363

Please sign in to comment.