Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controlled Components #148

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Avalonia.FuncUI.DSL/Avalonia.FuncUI.DSL.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
<Compile Include="TabControl.fs" />
<Compile Include="TabItem.fs" />
<Compile Include="ViewBox.fs" />
<Compile Include="ControlledTextBox.fs" />
<Compile Include="ControlledCheckBox.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/Avalonia.FuncUI.DSL/ComboBox.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ module ComboBox =
AttrBuilder<'t>.CreateProperty<VerticalAlignment>(ComboBox.VerticalContentAlignmentProperty, alignment, ValueNone)

static member virtualizationMode<'t when 't :> ComboBox>(mode: ItemVirtualizationMode) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<ItemVirtualizationMode>(ComboBox.VirtualizationModeProperty, mode, ValueNone)
AttrBuilder<'t>.CreateProperty<ItemVirtualizationMode>(ComboBox.VirtualizationModeProperty, mode, ValueNone)

static member controlledSelectedIndex<'t when 't :> ComboBox>(value: int, onChange: int -> unit) =
AttrBuilder<'t>.CreateControlledProperty<int>(ComboBox.SelectedIndexProperty, value, onChange, ValueNone)

23 changes: 23 additions & 0 deletions src/Avalonia.FuncUI.DSL/ControlledCheckBox.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Avalonia.FuncUI.DSL

open Avalonia.FuncUI.Controls

[<AutoOpen>]
module ControlledCheckBox =
open Avalonia.FuncUI.Builder
open Avalonia.FuncUI.Types

let create (attrs: IAttr<ControlledCheckBox> list): IView<ControlledCheckBox> =
ViewBuilder.Create<ControlledCheckBox>(attrs)

type ControlledCheckBox with
static member value<'t when 't :> ControlledCheckBox> state =
let getter : 't -> CheckBoxState = fun c -> c.State()
let setter : ('t * CheckBoxState) -> unit = (fun (c, v) -> v |> c.MutateControlledValue)
AttrBuilder<'t>.CreateProperty<CheckBoxState>("Value", state, ValueSome getter, ValueSome setter, ValueNone)

static member onChange<'t when 't :> ControlledCheckBox> fn =
let getter : 't -> (CheckBoxEventArgs -> unit) = fun c -> c.OnChangeCallback
let setter : ('t * (CheckBoxEventArgs -> unit)) -> unit =
(fun (c, f) -> c.OnChangeCallback <- f)
AttrBuilder.CreateProperty<CheckBoxEventArgs -> unit>("OnChange", fn, ValueSome getter, ValueSome setter, ValueNone)
28 changes: 28 additions & 0 deletions src/Avalonia.FuncUI.DSL/ControlledTextBox.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Avalonia.FuncUI.DSL

open Avalonia.FuncUI.Controls
open Avalonia.Input

[<AutoOpen>]
module ControlledTextBox =
open Avalonia.FuncUI.Builder
open Avalonia.FuncUI.Types

let create (attrs: IAttr<ControlledTextBox> list): IView<ControlledTextBox> =
ViewBuilder.Create<ControlledTextBox>(attrs)

type ControlledTextBox with

static member value<'t when 't :> ControlledTextBox> str =
let getter : 't -> string = fun c -> c.Text
let setter : ('t * string) -> unit = (fun (c, v) -> v |> c.MutateControlledValue)

AttrBuilder<'t>.CreateProperty<string>("Value", str, ValueSome getter, ValueSome setter, ValueNone)

static member onChange<'t when 't :> ControlledTextBox> fn =
let getter : 't -> (TextInputEventArgs -> unit) = fun c -> c.OnChangeCallback
let setter : ('t * (TextInputEventArgs -> unit)) -> unit =
(fun (c, f) -> c.OnChangeCallback <- f)

AttrBuilder<'t>.CreateProperty<TextInputEventArgs -> unit>("OnChange", fn, ValueSome getter, ValueSome setter, ValueNone)

4 changes: 4 additions & 0 deletions src/Avalonia.FuncUI.DSL/Expander.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ module Expander =

static member onIsExpandedChanged<'t when 't :> Expander>(func: bool -> unit, ?subPatchOptions: SubPatchOptions) : IAttr<'t> =
AttrBuilder<'t>.CreateSubscription(Expander.IsExpandedProperty, func, ?subPatchOptions = subPatchOptions)

static member controlledIsExpanded<'t when 't :> Expander>(value: bool, fn: bool -> unit, ?subPatchOptions: SubPatchOptions) : IAttr<'t> =
AttrBuilder<'t>.CreateControlledProperty<bool>(Expander.IsExpandedProperty, value, fn, ValueNone, ?subPatchOptions = subPatchOptions)

7 changes: 7 additions & 0 deletions src/Avalonia.FuncUI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Examples.Presso", "Examples
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Examples.MusicPlayer", "Examples\Examples.MusicPlayer\Examples.MusicPlayer.fsproj", "{C9BF89F2-6DE8-4F37-A6AD-6D40E982F265}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Examples.ControlledComponents", "Examples\Examples.ControlledComponents\Examples.ControlledComponents.fsproj", "{F23A44E1-3D11-4DF9-AC4B-2CDA42EF9369}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -75,6 +77,10 @@ Global
{C9BF89F2-6DE8-4F37-A6AD-6D40E982F265}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9BF89F2-6DE8-4F37-A6AD-6D40E982F265}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9BF89F2-6DE8-4F37-A6AD-6D40E982F265}.Release|Any CPU.Build.0 = Release|Any CPU
{F23A44E1-3D11-4DF9-AC4B-2CDA42EF9369}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F23A44E1-3D11-4DF9-AC4B-2CDA42EF9369}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F23A44E1-3D11-4DF9-AC4B-2CDA42EF9369}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F23A44E1-3D11-4DF9-AC4B-2CDA42EF9369}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -87,6 +93,7 @@ Global
{1401B46D-1F14-4D29-8E45-96E62D46405F} = {F6F4AAF7-2BDA-4D2F-B78D-F6D8A03F660E}
{7A0CA9E2-AFB8-4BA0-A725-F80EB98717C4} = {F6F4AAF7-2BDA-4D2F-B78D-F6D8A03F660E}
{C9BF89F2-6DE8-4F37-A6AD-6D40E982F265} = {F6F4AAF7-2BDA-4D2F-B78D-F6D8A03F660E}
{F23A44E1-3D11-4DF9-AC4B-2CDA42EF9369} = {F6F4AAF7-2BDA-4D2F-B78D-F6D8A03F660E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4630E817-6780-4C98-9379-EA3B45224339}
Expand Down
4 changes: 4 additions & 0 deletions src/Avalonia.FuncUI/Avalonia.FuncUI.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ItemGroup>
<PackageReference Include="Avalonia.Desktop" Version="0.10.0-rc1" />
<PackageReference Update="FSharp.Core" Version="4.7.2" />
<PackageReference Include="FSharp.Control.Reactive" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -33,6 +34,9 @@
<Compile Include="Components\DataTemplateView.fs" />
<Compile Include="Builder.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="Controls\Helpers.fs" />
<Compile Include="Controls\ControlledCheckBox.fs" />
<Compile Include="Controls\ControlledTextBox.fs" />
</ItemGroup>

</Project>
Loading