-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows controls to pass state to descendants (similar to swift UI env…
…ironment objects & react contexts) (#278)
- Loading branch information
Showing
7 changed files
with
376 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
namespace Avalonia.FuncUI | ||
|
||
open System | ||
open Avalonia.Controls | ||
open Avalonia.FuncUI.Types | ||
open Avalonia.FuncUI.DSL | ||
open Avalonia.LogicalTree | ||
open Avalonia.Styling | ||
#nowarn "57" | ||
|
||
type EnvironmentState<'value> = | ||
internal { | ||
Name: string | ||
DefaultValue: IWritable<'value> option | ||
} | ||
|
||
[<Experimental "this feature is experimental. The API might change.">] | ||
static member Create (name: string, ?defaultValue: IWritable<'value>) : EnvironmentState<'value> = | ||
{ Name = name | ||
DefaultValue = defaultValue } | ||
|
||
[<Experimental "this feature is experimental. The API might change.">] | ||
[<AllowNullLiteral>] | ||
type EnvironmentStateProvider<'value> | ||
( state: EnvironmentState<'value>, | ||
providedState: IWritable<'value>) as this = | ||
|
||
inherit ContentControl () | ||
|
||
member this.EnvironmentState with get () = state | ||
|
||
member this.ProvidedState with get () = providedState | ||
|
||
interface IStyleable with | ||
member this.StyleKey = typeof<ContentControl> | ||
|
||
type EnvironmentStateProvider<'value> with | ||
|
||
[<Experimental "this feature is experimental. The API might change.">] | ||
static member create (state: EnvironmentState<'value>, providedValue: IWritable<'value>, content: IView) = | ||
{ View.ViewType = typeof<EnvironmentStateProvider<'value>> | ||
View.ViewKey = ValueNone | ||
View.Attrs = [ ContentControl.content content ] | ||
View.Outlet = ValueNone | ||
View.ConstructorArgs = [| state :> obj; providedValue :> obj |] } | ||
:> IView<EnvironmentStateProvider<'value>> | ||
|
||
type EnvironmentState<'value> with | ||
|
||
[<Experimental "this feature is experimental. The API might change.">] | ||
member this.provide (providedValue: IWritable<'value>, content: IView) = | ||
EnvironmentStateProvider<'value>.create(this, providedValue, content) | ||
|
||
[<RequireQualifiedAccess>] | ||
module private EnvironmentStateConsumer = | ||
|
||
let rec private tryFindNext (control: EnvironmentStateProvider<'value>, state: EnvironmentState<'value>) = | ||
match control.FindLogicalAncestorOfType<EnvironmentStateProvider<'value>>(includeSelf = false) with | ||
| null -> ValueNone | ||
| ancestor -> | ||
if ancestor.EnvironmentState.Name.Equals(state.Name, StringComparison.Ordinal) then | ||
ValueSome ancestor.ProvidedState | ||
else | ||
tryFindNext (ancestor, state) | ||
|
||
let tryFind (control: Control, state: EnvironmentState<'value>) = | ||
match control.FindLogicalAncestorOfType<EnvironmentStateProvider<'value>>(includeSelf = false) with | ||
| null -> ValueNone | ||
| ancestor -> | ||
if ancestor.EnvironmentState.Name.Equals(state.Name, StringComparison.Ordinal) then | ||
ValueSome ancestor.ProvidedState | ||
else | ||
tryFindNext (ancestor, state) | ||
|
||
[<AutoOpen>] | ||
module __ContextExtensions_useEnvHook = | ||
|
||
type IComponentContext with | ||
|
||
[<Experimental "this feature is experimental. The API might change.">] | ||
member this.useEnvState (state: EnvironmentState<'value>, ?renderOnChange: bool) = | ||
let obtainValue () = | ||
match EnvironmentStateConsumer.tryFind (this.control, state), state.DefaultValue with | ||
| ValueSome value, _ -> value | ||
| ValueNone, Some defaultValue -> defaultValue | ||
| ValueNone, None -> failwithf "No value provided for environment state '%s'" state.Name | ||
|
||
this.usePassedLazy ( | ||
obtainValue = obtainValue, | ||
?renderOnChange = renderOnChange | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<AvaloniaVersion>11.0.0-preview5</AvaloniaVersion> | ||
<FuncUIVersion>0.6.0-preview8</FuncUIVersion> | ||
<FuncUIVersion>0.6.0-preview9</FuncUIVersion> | ||
</PropertyGroup> | ||
</Project> |
19 changes: 19 additions & 0 deletions
19
src/Examples/Component Examples/Examples.EnvApp/Examples.EnvApp.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<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> |
Oops, something went wrong.