helloSystem / Menu

Global menu bar written in Qt
43 stars 14 forks source link

Delay before textChanged triggers a returnPressed #96

Closed probonopd closed 2 years ago

probonopd commented 2 years ago

Continuation from https://github.com/helloSystem/Menu/issues/65#issuecomment-1279948338:

Could we have a short (~300 ms?) delay before textChanged triggers a returnPressed, so that while typing the filtering would not happen all the time but only after a short period of inactivity? This would make the menu look less "busy" when you are searching within the menus of a large application (such as GIMP)

cc @jsm222

probonopd commented 2 years ago

Similarly, should we only start showing search results once the user has entered at least 2 characters?

probonopd commented 2 years ago

https://user-images.githubusercontent.com/2480569/196247229-e4fbb97b-5184-4fab-9e0e-294ae1c5df09.mp4

probonopd commented 2 years ago

https://wiki.qt.io/Delay_action_to_wait_for_user_interaction describes this exact use case:

The simplest solution to this problem is to use a QTimer. This allows you to, for instance, implement a text filter that waits for the user to stop typing and then run the query.

In appmenuwidget.h, under private:, add:

     QTimer *m_typingTimer;

In appmenuwidget.cpp, add #include <QTimer> and under AppMenuWidget::AppMenuWidget(QWidget *parent) initialize it like this:

AppMenuWidget::AppMenuWidget(QWidget *parent)
    : QWidget(parent),
      m_typingTimer(new QTimer(this))

In appmenuwidget.cpp,

    // connect(searchLineEdit,&QLineEdit::textChanged,this,&AppMenuWidget::searchMenu);
    // Do not do this immediately, but rather delayed
    // https://wiki.qt.io/Delay_action_to_wait_for_user_interaction
    m_typingTimer->setSingleShot(true); // Ensure the timer will fire only once after it was started
    connect( m_typingTimer, &QTimer::timeout, this, &AppMenuWidget::searchMenu);

In appmenuwidget.cpp, under void AppMenuWidget::searchEditingDone() {, add

m_typingTimer->start(300); // https://wiki.qt.io/Delay_action_to_wait_for_user_interaction

The result is that the searching starts only after 300 ms, this makes searching much more pleasant in applications with large menus.

BUT: The search box never gets cleared and the search results get never reset, and the ArrowDown button doesn't work properly anymore in the search menu...

probonopd commented 2 years ago

Think I solved it.