Skip to content

Commit

Permalink
Optimize (#278)
Browse files Browse the repository at this point in the history
* Optimize

* .

* Sigh
  • Loading branch information
kerams authored Jan 20, 2025
1 parent 1d8fa5c commit 35f7f83
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/Fabulous.Avalonia/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ open Fabulous.ScalarAttributeDefinitions
open Fabulous.WidgetCollectionAttributeDefinitions

module ViewHelpers =
let private tryGetScalarValue (widget: Widget) (def: SimpleScalarAttributeDefinition<'data>) =
match Array.tryFind (fun (attr: ScalarAttribute) -> attr.Key = def.Key) widget.ScalarAttributes with
| None -> ValueNone
| Some attr -> ValueSome(unbox<'data> attr.Value)
let private simpleScalarAttributeExists (widget: Widget) (def: SimpleScalarAttributeDefinition<'data>) =
widget.ScalarAttributes |> Array.exists(fun attr -> attr.Key = def.Key)

let private tryGetWidgetCollectionValue (widget: Widget) (def: WidgetCollectionAttributeDefinition) =
match Array.tryFind (fun (attr: WidgetCollectionAttribute) -> attr.Key = def.Key) widget.WidgetCollectionAttributes with
| None -> ValueNone
| Some attr -> ValueSome attr.Value
let private widgetCollectionAttributeExists (widget: Widget) (def: WidgetCollectionAttributeDefinition) =
widget.WidgetCollectionAttributes
|> Array.exists(fun attr -> attr.Key = def.Key)

/// Extend the canReuseView function to check AvaloniaUI specific constraints
let rec canReuseView (prev: Widget) (curr: Widget) =
Expand All @@ -39,12 +36,8 @@ module ViewHelpers =
/// Check whether widgets have compatible automation ids.
/// Avalonia only allows setting the automation id once so we can't reuse a control if the id is not the same.
and private canReuseAutomationId (prev: Widget) (curr: Widget) =
let prevIdOpt = tryGetScalarValue prev AutomationProperties.AutomationId

let currIdOpt = tryGetScalarValue curr AutomationProperties.AutomationId

match prevIdOpt with
| ValueSome _ when prevIdOpt <> currIdOpt -> false
match AttributeHelpers.tryFindSimpleScalarAttribute AutomationProperties.AutomationId prev with
| ValueSome _ as prev -> prev = AttributeHelpers.tryFindSimpleScalarAttribute AutomationProperties.AutomationId curr
| _ -> true

/// TextBlock's text can be defined by both the Text and Inlines property
Expand All @@ -53,12 +46,12 @@ module ViewHelpers =
/// So, it's better to not reuse a TextBlock when we are about to switch between Text and Inlines
and canReuseTextBlock (prev: Widget) (curr: Widget) =
let switchingFromTextToInlines =
(tryGetScalarValue prev TextBlock.Text).IsSome
&& (tryGetWidgetCollectionValue curr TextBlock.Inlines).IsSome
simpleScalarAttributeExists prev TextBlock.Text
&& widgetCollectionAttributeExists curr TextBlock.Inlines

let switchingFromInlinesToText =
(tryGetWidgetCollectionValue prev TextBlock.Inlines).IsSome
&& (tryGetScalarValue curr TextBlock.Text).IsSome
widgetCollectionAttributeExists prev TextBlock.Inlines
&& simpleScalarAttributeExists curr TextBlock.Text

not switchingFromTextToInlines && not switchingFromInlinesToText

Expand Down

0 comments on commit 35f7f83

Please sign in to comment.