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

Patch Component render function #416

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions src/Avalonia.FuncUI.sln
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Elmish Examples", "Elmish E
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Examples.Elmish.Tetris", "Examples\Elmish Examples\Examples.Elmish.Tetris\Examples.Elmish.Tetris.fsproj", "{EC63B886-E809-4B74-B533-BFF3D60017C9}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Examples.ComponentPlayground", "Examples\Examples.ComponentPlayground\Examples.ComponentPlayground.fsproj", "{FA4E81D2-1083-43E1-99E3-0F4EA31B3701}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -206,6 +208,10 @@ Global
{EC63B886-E809-4B74-B533-BFF3D60017C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EC63B886-E809-4B74-B533-BFF3D60017C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EC63B886-E809-4B74-B533-BFF3D60017C9}.Release|Any CPU.Build.0 = Release|Any CPU
{FA4E81D2-1083-43E1-99E3-0F4EA31B3701}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA4E81D2-1083-43E1-99E3-0F4EA31B3701}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA4E81D2-1083-43E1-99E3-0F4EA31B3701}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA4E81D2-1083-43E1-99E3-0F4EA31B3701}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -242,6 +248,7 @@ Global
{70BDCE72-149A-435A-9910-E79DE329F978} = {F50826CE-D9BC-45CF-A110-C42225B75AD3}
{6D2C62FC-5634-4997-AF1F-2E8A5D27E117} = {84811DB3-C276-4F0D-B3BA-78B88E2C6EF0}
{EC63B886-E809-4B74-B533-BFF3D60017C9} = {6D2C62FC-5634-4997-AF1F-2E8A5D27E117}
{FA4E81D2-1083-43E1-99E3-0F4EA31B3701} = {F50826CE-D9BC-45CF-A110-C42225B75AD3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4630E817-6780-4C98-9379-EA3B45224339}
Expand Down
2 changes: 2 additions & 0 deletions src/Avalonia.FuncUI/Avalonia.FuncUI.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Helpers.fs" />
<Compile Include="DSL\ViewBuilder.fs" />
<Compile Include="DSL\AttrBuilder.fs" />
<Compile Include="DSL\Component.fs" />
<Compile Include="DSL\View.fs" />
<Compile Include="DSL\Base\AvaloniaObject.fs" />

Expand Down Expand Up @@ -169,6 +170,7 @@
<Compile Include="Experimental\Experimental.EnvironmentState.fs" />
<Compile Include="Experimental\Experimental.Operators.fs" />
<Compile Include="Experimental\Experimental.Hooks.fs" />
<Compile Include="Experimental\Experimental.ClosureComponent.fs" />
</ItemGroup>

</Project>
16 changes: 1 addition & 15 deletions src/Avalonia.FuncUI/Components/Component.fs
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
namespace Avalonia.FuncUI

open System
open System.Diagnostics.CodeAnalysis
open Avalonia.Controls
open Avalonia.FuncUI
open Avalonia.FuncUI.Types
open Avalonia.FuncUI.VirtualDom
open Avalonia.Threading

[<AllowNullLiteral>]
[<DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)>]
type Component (render: IComponentContext -> IView) as this =

Check warning on line 9 in src/Avalonia.FuncUI/Components/Component.fs

View workflow job for this annotation

GitHub Actions / build

The recursive object reference 'this' is unused. The presence of a recursive object reference adds runtime initialization checks to members in this and derived types. Consider removing this recursive object reference.
inherit ComponentBase ()

override this.Render ctx =
render ctx

type Component with

static member create(key: string, render: IComponentContext -> IView) : IView<Component> =
{ View.ViewType = typeof<Component>
View.ViewKey = ValueSome key
View.Attrs = list.Empty
View.Outlet = ValueNone
View.ConstructorArgs = [| render :> obj |] }
:> IView<Component>
render ctx
30 changes: 30 additions & 0 deletions src/Avalonia.FuncUI/Components/Lib/Lib.Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Avalonia.FuncUI

open System
open System.Collections.Generic
open Microsoft.FSharp.Core

[<RequireQualifiedAccess>]
module internal ComponentHelpers =
Expand Down Expand Up @@ -38,3 +39,32 @@ module internal CommonExtensions =
type Guid with
member this.StringValue with get () = this.ToString()
static member Unique with get () = Guid.NewGuid()


[<RequireQualifiedAccess>]
module internal RenderFunctionAnalysis =
open System
open System.Reflection
open System.Collections.Concurrent

let internal cache = ConcurrentDictionary<Type, bool>()

let private flags =
BindingFlags.Instance |||
BindingFlags.NonPublic |||
BindingFlags.Public

let capturesState (func : obj) : bool =
let type' = func.GetType()

let hasValue, value = cache.TryGetValue type'

match hasValue with
| true -> value
| false ->
let capturesState =
type'.GetConstructors(flags)
|> Array.map (fun info -> info.GetParameters().Length)
|> Array.exists (fun parameterLength -> parameterLength > 0)

cache.AddOrUpdate(type', capturesState, (fun identifier lastValue -> capturesState))
18 changes: 18 additions & 0 deletions src/Avalonia.FuncUI/DSL/Component.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[<AutoOpen>]
module Avalonia.FuncUI.DSL.__ComponentExtensions

open Avalonia.FuncUI
open Avalonia.FuncUI.Builder
open Avalonia.FuncUI.Types

type Component with

static member create(key: string, render: IComponentContext -> IView) : IView<ComponentBase> =
{ View.ViewType = typeof<Component>
View.ViewKey = ValueSome key
View.Attrs = [
//Component.renderFunction render
]
View.Outlet = ValueNone
View.ConstructorArgs = [| render :> obj |] }
:> IView<ComponentBase>
79 changes: 79 additions & 0 deletions src/Avalonia.FuncUI/Experimental/Experimental.ClosureComponent.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace Avalonia.FuncUI.Experimental

open System
open System.Diagnostics.CodeAnalysis
open Avalonia
open Avalonia.FuncUI
open Avalonia.FuncUI.Types
open Avalonia.FuncUI.Builder

/// Component that works well with a render function that captures state.
[<AllowNullLiteral>]
[<DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)>]
type ClosureComponent (render: IComponentContext -> IView) as this =
inherit ComponentBase ()

static let _RenderFunctionProperty =
AvaloniaProperty.RegisterDirect<ClosureComponent, IComponentContext -> IView>(
name = "RenderFunction",
getter = Func<ClosureComponent, IComponentContext -> IView>(_.RenderFunction),
setter = (fun this value -> this.RenderFunction <- value)
)

static do
let _ = _RenderFunctionProperty.Changed.AddClassHandler<ClosureComponent, IComponentContext -> IView>(fun this e ->
let capturesState = RenderFunctionAnalysis.capturesState(e.NewValue.Value)

if capturesState then
this.ForceRender()
)
()

let mutable _renderFunction = render

static member RenderFunctionProperty = _RenderFunctionProperty

member this.RenderFunction
with get() = _renderFunction
and set(value) =
let oldValue = _renderFunction
_renderFunction <- value
let _ = this.RaisePropertyChanged(ClosureComponent.RenderFunctionProperty, oldValue, value)
()

override this.Render ctx =
_renderFunction ctx



[<AutoOpen>]
module __ClosureComponentExtensions =

type ClosureComponent with

static member internal renderFunction<'t when 't :> ClosureComponent>(value: IComponentContext -> IView) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<IComponentContext -> IView>(ClosureComponent.RenderFunctionProperty, value, ValueNone)

static member create(key: string, render: IComponentContext -> IView) : IView<ClosureComponent> =
let view: View<ClosureComponent> =
{ View.ViewType = typeof<ClosureComponent>
View.ViewKey = ValueSome key
View.Attrs = [
ClosureComponent.renderFunction render
]
View.Outlet = ValueNone
View.ConstructorArgs = [| render :> obj |] }

view :> IView<ClosureComponent>

static member create(render: IComponentContext -> IView) : IView<ClosureComponent> =
let view: View<ClosureComponent> =
{ View.ViewType = typeof<ClosureComponent>
View.ViewKey = ValueNone
View.Attrs = [
ClosureComponent.renderFunction render
]
View.Outlet = ValueNone
View.ConstructorArgs = [| render :> obj |] }

view :> IView<ClosureComponent>
16 changes: 13 additions & 3 deletions src/Avalonia.FuncUI/Experimental/Experimental.EnvironmentState.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,27 @@ module private EnvironmentStateConsumer =
else
tryFindNext (ancestor, state)


[<AutoOpen>]
module __ContextExtensions_useEnvHook =
module __Control_useEnvValue =

type IComponentContext with
type Control with

member this.readEnvValue(state: EnvironmentState<'value>) : 'value =
match EnvironmentStateConsumer.tryFind (this.control, state), state.DefaultValue with
match EnvironmentStateConsumer.tryFind (this, state), state.DefaultValue with
| ValueSome value, _ -> value
| ValueNone, Some defaultValue -> defaultValue
| ValueNone, None -> failwithf "No value provided for environment value '%s'" state.Name


[<AutoOpen>]
module __ContextExtensions_useEnvHook =

type IComponentContext with

member this.readEnvValue(state: EnvironmentState<'value>) : 'value =
this.control.readEnvValue(state)

member this.useEnvState(state: EnvironmentState<IWritable<'value>>, ?renderOnChange: bool) : IWritable<'value> =
this.usePassedLazy (
obtainValue = (fun () -> this.readEnvValue(state)),
Expand Down
6 changes: 3 additions & 3 deletions src/Avalonia.FuncUI/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ module internal Extensions =
let handler = EventHandler<'args>(fun _ e ->
observer.OnNext e
)

// subscribe to event changes so they can be pushed to subscribers
this.AddDisposableHandler(routedEvent, handler, routedEvent.RoutingStrategies)
)

{ new IObservable<'args>
with member this.Subscribe(observer: IObserver<'args>) = sub.Invoke(observer) }
with member this.Subscribe(observer: IObserver<'args>) = sub.Invoke(observer) }
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,4 @@ module __ContextExtensions_useModal =
type IComponentContext with

member this.useModalState() : ModalHostState =
this.readEnvValue ModalHost.State



this.readEnvValue ModalHost.State
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<AvaloniaResource Include="**\*.xaml" />
<AvaloniaResource Include="Assets\*" />
</ItemGroup>

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

<ItemGroup>
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="$(AvaloniaVersion)" />
<ProjectReference Include="..\..\Avalonia.FuncUI.Elmish\Avalonia.FuncUI.Elmish.fsproj" />
<ProjectReference Include="..\..\Avalonia.FuncUI\Avalonia.FuncUI.fsproj" />
</ItemGroup>

</Project>
Loading
Loading