fabulous-dev / Fabulous

Declarative UI framework for cross-platform mobile & desktop apps, using MVU and F# functional programming
https://fabulous.dev
Apache License 2.0
1.13k stars 122 forks source link

Maui VisualElement changes #1004

Closed edgarfgp closed 1 year ago

edgarfgp commented 1 year ago
TimLariviere commented 1 year ago

Unfortunately IsFocusedProperty is read-only and will throw an exception if we try to set it. I replaced it with a custom attribute calling Focus() / Unfocus() instead. Also merged the IsFocus field with its 2 events Focused and Unfocused to make it easier to work with.

Here's an example for using it:

module App =
    type FieldFocused =
        | Entry1
        | Entry2
        | Entry3

    type Model = { Focus: FieldFocused option }

    type Msg =
        | TextChanged of string
        | FocusChanged of FieldFocused * bool
        | SetFocus of FieldFocused option

    let init () = { Focus = None }

    let update msg model =
        match msg with
        | TextChanged _ -> model
        | FocusChanged (field, isFocused) ->
            if isFocused then
                { model with Focus = Some field }
            else
                { model with Focus = None }

        | SetFocus field -> { model with Focus = field }

    let focusChanged field args =
        FocusChanged (field, args)

    let view model =
        Application(
            ContentPage(
                "Title",
                (VStack(spacing = 20.) {
                    let text = match model.Focus with None -> "None" | Some f -> f.ToString()
                    Label($"Field currently selected: {text}")

                    Entry("Entry1", TextChanged)
                        .focus(model.Focus = Some Entry1, focusChanged Entry1)

                    Entry("Entry2", TextChanged)
                        .focus(model.Focus = Some Entry2, focusChanged Entry2)

                    Entry("Entry3", TextChanged)
                        .focus(model.Focus = Some Entry3, focusChanged Entry3)

                    Button("Set focus on Entry1", SetFocus (Some Entry1))
                    Button("Set focus on Entry2", SetFocus (Some Entry2))
                    Button("Set focus on Entry3", SetFocus (Some Entry3))
                    Button("Unfocus", SetFocus None)
                })
                    .margin(20.)
            )
        )
edgarfgp commented 1 year ago

Wow this looks perfect 👍 . We could do the same for v2 XF ?