Comment 2 for bug 1361744

Revision history for this message
Zsombor Egri (zsombi) wrote :

So far focus was taken only by text inputs. Now that we have focus handling, a simple focus: true does not mean that activeFocus will also be taken when a previous component was having focus.

When the popover is long-pressed, it takes the focus, and then when closed, popover restores the focus to its "caller" that being the list item in the file list. So when the Dialog is opened, the list item is the active focus. In order to grab the focus you need to call forceActiveFocus() on Dialog completion. See the following code:

import QtQuick 2.2
import Ubuntu.Components 1.1
import Ubuntu.Components.Popups 1.0
import Ubuntu.Components.ListItems 1.0

MainView {
    width: units.gu(40)
    height: units.gu(71)

    Connections {
        target: window
        onActiveFocusItemChanged: print("activeFocus:", window.activeFocusItem)
    }

    Standard {
        id: caller
        objectName: "MainListItem"
        text: "Press to open popover"
        onClicked: PopupUtils.open(popoverComponent, caller)
    }

    Component {
        id: popoverComponent
        Popover {
            id: popover
            contentWidth: units.gu(20)
            contentHeight: line.height
            Standard {
                id: line
                objectName: "PopoverListItem"
                text: "Open dialog"
                onClicked: {
                    PopupUtils.close(popover)
                    PopupUtils.open(inputDialogComponent)
                }
            }
        }
    }

    Component {
        id: inputDialogComponent
        Dialog {
            id: inputDialog

            title: "Input Dialog"

            TextField {
                id: input
                text: "some text"
                focus: true
            }

            Button {
                text: "Close"
                onClicked: PopupUtils.close(inputDialog)
            }

// Component.onCompleted: input.forceActiveFocus()
        }
    }
}