slint icon indicating copy to clipboard operation
slint copied to clipboard

Add a gained_focus / lost_focus callbacks to FocusScope

Open FlorentBecker opened this issue 3 years ago • 3 comments
trafficstars

It would be useful to be able to run arbitrary code when a focussable element gains focus / loses. My use case is replacing a "passive" widget with an "active" (editable) one when it gets focus.

FlorentBecker avatar May 05 '22 15:05 FlorentBecker

Thanks for the suggestion. Some discussion about this also hapenned in https://github.com/slint-ui/slint/issues/1206

Regarding your use case, maybe you can just do

if (focus_scope.has-focus) : MyEditableWidget {
 //...
}

ogoffart avatar May 05 '22 16:05 ogoffart

Thanks for the suggestion, here is an example of what I'm trying to do. It works if you click on the message to start editing, the Text is replaced with the TextInput. I'd like the user to be able to give the focus to the Text, and then have the Text be replaced by the (focussed) TextInput. I presume the solution with an if won't work, since i needs to exist so that t can give it the focus.

export Demo := Window {
    property<bool> editing: false;
    property<string> greeting: "Click to edit!";


    t := Text {
        text: root.greeting;
        font-size: 24px;
        visible: !root.editing;

        callback activated;
        activated => {
                i.text = root.greeting;
                root.editing = true;
                i.focus();
            }

        TouchArea {
            clicked => { parent.activated() } 
        }

        // FocusScope {
        //     gained-focus => { parent.activated() }
        // }
    }

    i := TextInput {
        text: root.greeting;
        font-size: 24px;
        color: purple;
        visible: root.editing;
        accepted => {
            root.greeting = self.text;
            root.editing = false;
        }
    }
}

FlorentBecker avatar May 05 '22 17:05 FlorentBecker

Maybe have a generic way to react to property changes?

I am still not sure what you actually want to achieve here:-(

hunger avatar May 05 '22 19:05 hunger

Fixed with https://github.com/slint-ui/slint/pull/3650

ogoffart avatar Oct 17 '23 15:10 ogoffart