gyscos / cursive

A Text User Interface library for the Rust programming language
MIT License
4.28k stars 243 forks source link

Focus Management #398

Open theduke opened 4 years ago

theduke commented 4 years ago

Cursive is awesome.

The only thing I'm struggling with, and where I haven't seen much information in the code, is on focus management.

It would be great to have a tutorial or some more docs on how this is supposed to be handled.

For example, I have a "page" that has a hierarachy like this:

> CircularFocus
  > LinearLayout
    > ScrollView
      > List
        > Text
        > Text
        > ...
    > EditView

Note: this is simplified, there are a few BoxView etc inbetween.

Now what I'd like to do is just use Tab to cycle through the EditView and the individual List items.

By default, nothing seems to happen. The Edit has focus and I can't switch to anywhere.

So I tried to used a OnEventView to call c.focus_id(ID_OF_LIST_VIEW), and would expect the first list item to be selected, but that doesn't seem to work either.

Any help for this specifically, and a general explanation would be much appreciated.

gyscos commented 4 years ago

Hi, and thanks for the report!

It's possible the items in the list are not focusable. TextView for example will not be focusable; in that case the focus always come back to the EditView since it's the only view that accepts focus. Maybe you want to have buttons instead?

Note that ListView is now mostly a legacy view that can be in many cases replaced with a simple LinearView or a SelectView.

gyscos commented 4 years ago

As for some extra documentation, you're absolutely right! I'll prepare something.

gyscos commented 4 years ago

Right now there's no easy way to notify a view that it lost focus.

Note that if you implement your own view, you can check printer.focused to change the style depending on the focus state.

gyscos commented 4 years ago

Right now I added a paragraph to the view module: https://github.com/gyscos/cursive/blob/6244d6e28c122ae2091e1b2bf6b2b088659106da/src/view/mod.rs#L37

At some point I'll need to re-organize the wiki and doc comments into something more readable...