Skip to content

Commit

Permalink
Merge branch 'Numpsy-users/rw/avalon_nights'
Browse files Browse the repository at this point in the history
  • Loading branch information
JaggerJo committed Feb 15, 2023
2 parents f428256 + 8377e04 commit 4dee185
Show file tree
Hide file tree
Showing 41 changed files with 141 additions and 144 deletions.
7 changes: 4 additions & 3 deletions src/Avalonia.FuncUI.ControlCatalog/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ type MainWindow() as this =
base.Title <- "Control Catalog"
base.Height <- 600.0
base.Width <- 800.0
this.VisualRoot.VisualRoot.Renderer.DrawFps <- true

this.VisualRoot.Renderer.Diagnostics.DebugOverlays <- Avalonia.Rendering.RendererDebugOverlays.Fps
//this.VisualRoot.VisualRoot.Renderer.DrawDirtyRects <- true
()

type App() =
inherit Application()

override this.Initialize() =
this.Styles.Add (FluentTheme(baseUri = null, Mode = FluentThemeMode.Dark))
this.Styles.Add (FluentTheme())
this.Styles.Load "avares://Avalonia.FuncUI.ControlCatalog/Styles/TabControl.xaml"
this.RequestedThemeVariant <- Styling.ThemeVariant.Dark

override this.OnFrameworkInitializationCompleted() =
match this.ApplicationLifetime with
Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.FuncUI.ControlCatalog/Styles/TabControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}">
<ItemsPresenter
Name="PART_ItemsPresenter"
ItemTemplate="{TemplateBinding ItemTemplate}"
Items="{TemplateBinding Items}"
ItemsPanel="{TemplateBinding ItemsPanel}" />
</ScrollViewer>
<ContentControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ type internal ComponentHighlightAdorner (adornedElement: Component) =
|> Seq.filter (fun c -> c.GetType() = typeof<ComponentHighlightAdorner>)
|> Seq.cast<ComponentHighlightAdorner>
|> Seq.filter (fun c -> c.Component.ComponentId = adornedElement.ComponentId)
|> Seq.cast<IControl>
|> Seq.cast<Control>
|> layer.Children.RemoveAll
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ module PatcherTests =

[<Fact>]
let ``Patch Styles, Classes or Resources`` () =
let stylesGetter: IAvaloniaObject -> obj = (fun c -> (c :?> StyledElement).Styles :> obj)
let stylesSetter: IAvaloniaObject * obj -> unit =
let stylesGetter: AvaloniaObject -> obj = (fun c -> (c :?> StyledElement).Styles :> obj)
let stylesSetter: AvaloniaObject * obj -> unit =
(fun (c, v) ->
let se = (c :?> StyledElement)
let s = v :?> Styles
se.Styles.Clear()
se.Styles.AddRange(s))

let classesGetter: IAvaloniaObject -> obj = (fun c -> (c :?> StyledElement).Classes :> obj)
let classesSetter: IAvaloniaObject * obj -> unit = (fun (c, v) -> (c :?> StyledElement).Classes <- v :?> Classes)
let classesGetter: AvaloniaObject -> obj = (fun c -> (c :?> StyledElement).Classes :> obj)
let classesSetter: AvaloniaObject * obj -> unit = (fun (c, v) -> (c :?> StyledElement).Classes <- v :?> Classes)

let resourcesGetter: IAvaloniaObject -> obj = (fun c -> (c :?> StyledElement).Resources :> obj)
let resourcesSetter: IAvaloniaObject * obj -> unit = (fun (c, v) -> (c :?> StyledElement).Resources <- v :?> IResourceDictionary)
let resourcesGetter: AvaloniaObject -> obj = (fun c -> (c :?> StyledElement).Resources :> obj)
let resourcesSetter: AvaloniaObject * obj -> unit = (fun (c, v) -> (c :?> StyledElement).Resources <- v :?> IResourceDictionary)

let delta : Delta.ViewDelta =
{
Expand Down Expand Up @@ -240,8 +240,8 @@ module PatcherTests =

Assert.IsType(typeof<Button>, stackpanel.Children.[1])
let button = stackpanel.Children.[1] :?> Button
Assert.Equal(SolidColorBrush.Parse("red").ToImmutable(), button.Background)
Assert.Equal(SolidColorBrush.Parse("red").ToImmutable() :> IBrush, button.Background)

Assert.IsType(typeof<Button>, stackpanel.Children.[2])
let button = stackpanel.Children.[2] :?> Button
Assert.Equal(SolidColorBrush.Parse("green").ToImmutable(), button.Background)
Assert.Equal(SolidColorBrush.Parse("green").ToImmutable() :> IBrush, button.Background)
1 change: 0 additions & 1 deletion src/Avalonia.FuncUI/Avalonia.FuncUI.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
<Compile Include="DSL\Panels\Grid.fs" />
<Compile Include="DSL\Panels\StackPanel.fs" />
<Compile Include="DSL\Panels\WrapPanel.fs" />
<Compile Include="DSL\Panels\ItemsRepeater.fs" />
<Compile Include="DSL\Shapes\Shape.fs" />
<Compile Include="DSL\Shapes\Rectangle.fs" />
<Compile Include="DSL\Shapes\Ellipse.fs" />
Expand Down
18 changes: 9 additions & 9 deletions src/Avalonia.FuncUI/Builder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ open Avalonia.FuncUI.Types
open Avalonia.FuncUI.Library

module private Helpers =
let wrappedGetter<'view, 'value>(func: 'view -> 'value) : IAvaloniaObject -> obj =
let wrapper (control: IAvaloniaObject) : obj =
let wrappedGetter<'view, 'value>(func: 'view -> 'value) : AvaloniaObject -> obj =
let wrapper (control: AvaloniaObject) : obj =
let view = control :> obj :?> 'view
let value = func view
value :> obj
wrapper

let wrappedSetter<'view, 'value>(func: 'view * 'value -> unit) : IAvaloniaObject * obj -> unit =
let wrapper (control: IAvaloniaObject, value: obj) : unit =
let wrappedSetter<'view, 'value>(func: 'view * 'value -> unit) : AvaloniaObject * obj -> unit =
let wrapper (control: AvaloniaObject, value: obj) : unit =
let view = control :> obj :?> 'view
let value = value :?> 'value
func(view, value)
Expand Down Expand Up @@ -162,7 +162,7 @@ type AttrBuilder<'view>() =
static member CreateSubscription<'arg, 'owner when 'owner :> AvaloniaObject>(property: DirectProperty<'owner, 'arg>, func: 'arg -> unit, ?subPatchOptions: SubPatchOptions) : IAttr<'view> =
// subscribe to avalonia property
// TODO: extract to helpers module
let subscribeFunc (control: IAvaloniaObject, _handler: 'h) =
let subscribeFunc (control: AvaloniaObject, _handler: 'h) =
let cts = new CancellationTokenSource()
control
.GetObservable(property)
Expand All @@ -184,7 +184,7 @@ type AttrBuilder<'view>() =
static member CreateSubscription<'arg>(property: AvaloniaProperty<'arg>, func: 'arg -> unit, ?subPatchOptions: SubPatchOptions) : IAttr<'view> =
// subscribe to avalonia property
// TODO: extract to helpers module
let subscribeFunc (control: IAvaloniaObject, _handler: 'h) =
let subscribeFunc (control: AvaloniaObject, _handler: 'h) =
let cts = new CancellationTokenSource()
control
.GetObservable(property)
Expand All @@ -206,7 +206,7 @@ type AttrBuilder<'view>() =
static member CreateSubscription<'arg when 'arg :> RoutedEventArgs>(routedEvent: RoutedEvent<'arg>, func: 'arg -> unit, ?subPatchOptions: SubPatchOptions) : IAttr<'view> =
// subscribe to avalonia property
// TODO: extract to helpers module
let subscribeFunc (control: IControl, _handler: 'h) =
let subscribeFunc (control: Control, _handler: 'h) =
let cts = new CancellationTokenSource()
control
.GetObservable(routedEvent)
Expand All @@ -225,10 +225,10 @@ type AttrBuilder<'view>() =
/// <summary>
/// Create a Event Subscription Attribute for a .Net Event
/// </summary>
static member CreateSubscription<'arg>(name: string, factory: IAvaloniaObject * ('arg -> unit) * CancellationToken -> unit, func: 'arg -> unit, ?subPatchOptions: SubPatchOptions) =
static member CreateSubscription<'arg>(name: string, factory: AvaloniaObject * ('arg -> unit) * CancellationToken -> unit, func: 'arg -> unit, ?subPatchOptions: SubPatchOptions) =
// TODO: extract to helpers module
// subscribe to event
let subscribeFunc (control: IAvaloniaObject, _handler: 'h) =
let subscribeFunc (control: AvaloniaObject, _handler: 'h) =
let cts = new CancellationTokenSource()
factory(control, func, cts.Token)
cts
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.FuncUI/DSL/Base/AvaloniaObject.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open Avalonia.FuncUI.Types
[<AutoOpen>]
module AvaloniaObject =

type IAvaloniaObject with
type AvaloniaObject with

/// <summary>
/// Hook into the controls lifetime. This is called when the backing avalonia control is created.
Expand Down Expand Up @@ -35,6 +35,6 @@ module AvaloniaObject =
let _ = this.Bind(property = prop, source = readable.ImmediateObservable)
()

member this.Bind(prop: StyledPropertyBase<'value>, readable: #IReadable<'value>) : unit =
member this.Bind(prop: StyledProperty<'value>, readable: #IReadable<'value>) : unit =
let _ = this.Bind(property = prop, source = readable.ImmediateObservable)
()
2 changes: 1 addition & 1 deletion src/Avalonia.FuncUI/DSL/Base/Control.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Control =

type Control with

static member focusAdorner<'t, 'c when 't :> Control and 'c :> IControl>(value: ITemplate<'c>) : IAttr<'t> =
static member focusAdorner<'t, 'c when 't :> Control and 'c :> Control>(value: ITemplate<'c>) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<ITemplate<'c>>(Control.FocusAdornerProperty, value, ValueNone)

static member tag<'t when 't :> Control>(value: obj) : IAttr<'t> =
Expand Down
5 changes: 3 additions & 2 deletions src/Avalonia.FuncUI/DSL/Base/StyledElement.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Avalonia.FuncUI.DSL
open Avalonia.Controls
open Avalonia.Controls.Primitives

[<AutoOpen>]
module StyledElement =
Expand All @@ -15,8 +16,8 @@ module StyledElement =
static member name<'t when 't :> StyledElement>(name: string) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<string>(StyledElement.NameProperty, name, ValueNone)

static member templatedParent<'t when 't :> StyledElement>(template: ITemplatedControl) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<ITemplatedControl>(StyledElement.TemplatedParentProperty, template, ValueNone)
static member templatedParent<'t when 't :> StyledElement>(template: TemplatedControl) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<AvaloniaObject>(StyledElement.TemplatedParentProperty, template, ValueNone)

static member classes<'t when 't :> StyledElement>(value: Classes) : IAttr<'t> =
let getter : ('t -> Classes) = (fun control -> control.Classes)
Expand Down
3 changes: 0 additions & 3 deletions src/Avalonia.FuncUI/DSL/Carousel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,5 @@ module Carousel =

type Carousel with

static member isVirtualized<'t when 't :> Carousel>(value: bool) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<bool>(Carousel.IsVirtualizedProperty, value, ValueNone)

static member pageTransition<'t when 't :> Carousel>(transition: IPageTransition) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<IPageTransition>(Carousel.PageTransitionProperty, transition, ValueNone)
3 changes: 0 additions & 3 deletions src/Avalonia.FuncUI/DSL/ComboBox.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,3 @@ module ComboBox =

static member verticalContentAlignment<'t when 't :> ComboBox>(alignment: VerticalAlignment) : IAttr<'t> =
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)
2 changes: 1 addition & 1 deletion src/Avalonia.FuncUI/DSL/Decorator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Decorator =

type Decorator with

static member child<'t when 't :> Decorator>(value: IControl) : IAttr<'t> =
static member child<'t when 't :> Decorator>(value: Control) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty(Decorator.ChildProperty, value, ValueNone)

static member child<'t when 't :> Decorator>(value: IView option) : IAttr<'t> =
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.FuncUI/DSL/ItemsControl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ module ItemsControl =
static member dataItems<'t when 't :> ItemsControl>(data: IEnumerable) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<IEnumerable>(ItemsControl.ItemsProperty, data, ValueNone)

static member itemsPanel<'t when 't :> ItemsControl>(value: ITemplate<IPanel>) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<ITemplate<IPanel>>(ItemsControl.ItemsPanelProperty, value, ValueNone)
static member itemsPanel<'t when 't :> ItemsControl>(value: ITemplate<Panel>) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<ITemplate<Panel>>(ItemsControl.ItemsPanelProperty, value, ValueNone)

static member itemTemplate<'t when 't :> ItemsControl>(value: IDataTemplate) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<IDataTemplate>(ItemsControl.ItemTemplateProperty, value, ValueNone)
Expand Down
4 changes: 1 addition & 3 deletions src/Avalonia.FuncUI/DSL/ListBox.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ module ListBox =

static member selectionMode<'t when 't :> ListBox>(mode: SelectionMode) : IAttr<'t> =
AttrBuilder<'t>.CreateProperty<SelectionMode>(ListBox.SelectionModeProperty, mode, ValueNone)

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

22 changes: 0 additions & 22 deletions src/Avalonia.FuncUI/DSL/Panels/ItemsRepeater.fs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Avalonia.FuncUI/DataTemplateView.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type DataTemplateView<'data, 'childData, 'view when 'view :> IView>
| :? 'data, ValueNone -> true
| _ -> false

member this.Build (_data: obj) : IControl =
member this.Build (_data: obj) : Control =
let host = HostControl()

let update (data: 'data) : unit =
Expand All @@ -63,7 +63,7 @@ type DataTemplateView<'data, 'childData, 'view when 'view :> IView>
| _ -> ()
), this) |> ignore

host :> IControl
host :> Control

type DataTemplateView<'data, 'view when 'view :> IView> =
/// <summary>
Expand Down
11 changes: 8 additions & 3 deletions src/Avalonia.FuncUI/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ module internal Utils =
module internal Extensions =
open Avalonia.Interactivity
open System
open System.Reactive.Linq
open System.Threading

type IObservable<'a> with
member this.SubscribeWeakly(callback: 'a -> unit, target) =
Observable.subscribeWeakly(this, callback, target)

type IInteractive with
member this.Subscribe (callback: 'a -> unit, token: CancellationToken) =
let disposable = Observable.subscribe callback this
token.Register(fun () -> disposable.Dispose()) |> ignore

type Interactive with
member this.GetObservable<'args when 'args :> RoutedEventArgs>(routedEvent: RoutedEvent<'args>) : IObservable<'args> =
let sub = Func<IObserver<'args>, IDisposable>(fun observer ->
// push new update to subscribers
Expand All @@ -45,4 +49,5 @@ module internal Extensions =
this.AddDisposableHandler(routedEvent, handler, routedEvent.RoutingStrategies)
)

Observable.Create(sub)
{ new IObservable<'args>
with member this.Subscribe(observer: IObserver<'args>) = sub.Invoke(observer) }
10 changes: 5 additions & 5 deletions src/Avalonia.FuncUI/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module Types =
[<CustomEquality; NoComparison>]
type PropertyAccessor =
{ Name: string
Getter: (IAvaloniaObject -> obj) voption
Setter: (IAvaloniaObject * obj -> unit) voption }
Getter: (AvaloniaObject -> obj) voption
Setter: (AvaloniaObject * obj -> unit) voption }

override this.Equals (other: obj) : bool =
match other with
Expand Down Expand Up @@ -62,7 +62,7 @@ module Types =
[<CustomEquality; NoComparison>]
type Subscription =
{ Name: string
Subscribe: IControl * Delegate -> CancellationTokenSource
Subscribe: Control * Delegate -> CancellationTokenSource
Func: Delegate
FuncType: Type
Scope: obj }
Expand Down Expand Up @@ -147,7 +147,7 @@ module Types =
abstract member ViewKey: string voption
abstract member Attrs: IAttr list with get
abstract member ConstructorArgs: obj array with get
abstract member Outlet: (IAvaloniaObject -> unit) voption with get
abstract member Outlet: (AvaloniaObject -> unit) voption with get

type IView<'viewType> =
inherit IView
Expand All @@ -158,7 +158,7 @@ module Types =
ViewKey: string voption
Attrs: IAttr<'viewType> list
ConstructorArgs: obj array
Outlet: (IAvaloniaObject-> unit) voption }
Outlet: (AvaloniaObject-> unit) voption }

interface IView with
member this.ViewType = this.ViewType
Expand Down
4 changes: 2 additions & 2 deletions src/Avalonia.FuncUI/VirtualDom/VirtualDom.Delta.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module internal rec Delta =
[<CustomEquality; NoComparison>]
type SubscriptionDelta =
{ Name: string
Subscribe: IControl * Delegate -> CancellationTokenSource
Subscribe: Control * Delegate -> CancellationTokenSource
Func: Delegate option }

override this.Equals (other: obj) : bool =
Expand Down Expand Up @@ -116,7 +116,7 @@ module internal rec Delta =
Attrs: AttrDelta list
ConstructorArgs: obj array
KeyDidChange: bool
Outlet: (IAvaloniaObject -> unit) voption }
Outlet: (AvaloniaObject -> unit) voption }

static member From (view: IView, ?keyDidChange: bool) : ViewDelta =
{ ViewType = view.ViewType
Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.FuncUI/VirtualDom/VirtualDom.Differ.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ module internal rec Differ =
| Subscription' subscription ->
AttrDelta.Subscription (SubscriptionDelta.From subscription)

| InitFunction init ->
AttrDelta.SetupFunction init

| _ -> failwithf "no update operation is defined for '%A' next" next

let private reset (last: IAttr) : AttrDelta =
Expand All @@ -46,6 +49,9 @@ module internal rec Differ =
Subscribe = subscription.Subscribe
Func = None }

| InitFunction init ->
AttrDelta.SetupFunction init

| _ -> failwithf "no reset operation is defined for last '%A'" last

let private diffContentSingle (last: IView option) (next: IView option) : ViewDelta option =
Expand Down
Loading

0 comments on commit 4dee185

Please sign in to comment.