perlshrakusystem-traynotification-area

How to check system tray availability programmatically?


Upon launching QSyncthingTray without any panel/trayer running, it displays an error dialog:

I couldn't detect any system tray on this system.
                                         [ OK ]

and exits upon pressing [ OK ].

Curiously though, if I have QSyncthingTray already running as a tray icon and kill my panel, QSyncthingTray doesn't quit, and upon restarting the panel QSyncthingTray is still there...

For my ~/.xsession & ~/.xinitrc startup, I'd like to add a looped check whether a tray is present, pseudocode:

loop:
  if system-tray.is-available:
    run QSyncthingTray
    done
  else sleep 1

How do I do the if system-tray.is-available?
The preferable language is Perl, also interested in Raku and POSIX sh. Other languages are welcome as well.


Solution

  • QSyncthingTray uses isSystemTrayAvailable() from Qt c++ lib.

    You can do the same.

    is_tray_available.cpp:

    // program that exits with zero code when tray is available
    // when not available (or program crashes) it should return non-zero
    
    #include <QApplication>
    #include <QSystemTrayIcon>
    
    int main(int argc, char** argv)
    {
      QApplication app(argc, argv);
      if(QSystemTrayIcon::isSystemTrayAvailable()) return 0;
      else return 1;
    }
    

    QT is unique in that you need to use a full-blown build system to build programs using it:

    is_tray_available.pro:

    QT += widgets
    TARGET = is_tray_available
    SOURCES += is_tray_available.cpp
    

    Save both files in the same dir, execute qmake, make and voilĂ , you have an executable that you can use in a shell script.