Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a task bar icon with wxPHP in Ubuntu/Unity?

After some struggle I have compiled the PHP module for wxPHP, and am writing a few scripts to see what it can do. My first demo creates a window and a task bar icon, and I cannot get the latter to work.

I am running this on Ubuntu 14.04 LTS.

Here is the script I am using:

<?php

// Create an icon (does not appear on Ubuntu 14.04 LTS)
$icon = new wxTaskBarIcon();

echo "Task bar available: " .
    (wxTaskBarIcon::IsAvailable() ? 'Yes' : 'No') . "\n";
echo "Create task bar icon: " . ($icon ? 'OK' : 'Failed') . "\n";

$wxIcon = new wxIcon('icon.ico');
$ok = $icon->setIcon($wxIcon);

echo "Set image for icon: " . ($ok ? 'OK' : 'Failed') . "\n";

// Create a window (works fine)
$main = new wxFrame(null, wxID_TOP, "Title" );
$main->Show();

wxEntry();

Now, I would imagine the "task bar" in Unity is the top bar featuring the clock, sound and networks controls, and not the app dock on the left hand side. I do get a blank grey app icon in the dock, when the PHP task is running, but it's a status icon at the top I am after.

The console outputs from the above script show everything is OK:

Task bar available: Yes
Create task bar icon: OK
Set image for icon: OK

I have tried changing the icon.ico to a non-existent file, and this pops a backtrace dialogue, which shows that the icon I am using is loading successfully (and presumably of an acceptable format).

Edit: further research indicates that:

  • earlier versions of Ubuntu prevented apps from using the taskbar unless specifically whitelisted (note that the gsettings keys referenced here are not found in my later version of Ubuntu)
  • later versions require something called AppIndicators

Thus, my guess at present is that my code would work if it was not for Unity preventing the operation - and that it would immediately in other distros such as Mint.

I did find this guide that explains how to whitelist on Ubuntu 14, but this is most unsatisfying; to do something so trivial one should not need to alter the trusted repo list. If I wish to distribute an application written in wxPHP, users will (and should) find this unacceptable.

There are a couple of avenues for research, which I am now seeking advice upon:

  • Do something differently in wxPHP, in case it (or upstream wxWidgets) has AppIndicators support (my guess is that it does not)
  • Detect if AppIndicators is running, and if it is send DBus messages using PHP to construct an icon (though I've looked for a DBus message spec for AppIndicators in vain)

Edit again: since this is a rather niche question, and since Ask Ubuntu carries a number of programming-related questions for the Ubuntu platform, I have asked a DBus-specific question over on that site.

like image 885
halfer Avatar asked Nov 09 '22 01:11

halfer


1 Answers

Broadly, it's not possible to create a taskbar icon on Ubuntu, at least via wxPHP or wxWidgets. This is a deliberate design decision from the Ubuntu usability team to introduce consistency and accessibility into the use of application-level status icons.

I raised this as a bug and obtained a lot of very useful information from the maintainer of wxPHP. I agree with him that this feature would be better upstream in wxWidgets, but from comments online it does not seem there is much appetite to add it there.

My short-term solution will be to set up a Python process to use its own App Indicator (python-appindicator in the Ubuntu repo) and communicate between the two processes (PHP to Python to change the icon, Python to PHP to send menu choice events). I expect to start with I'll be using wxFileSystemWatcher as a simple communication system, since ths works well inside the wxWidgets event loop.

A better solution would be to have appindicator bindings for PHP, but that sounds more involved, so I will leave that for now.

like image 91
halfer Avatar answered Nov 14 '22 21:11

halfer