DBus notify as a Message alternative

Post your must have Gambas Components/Controls/Classes/Modules here.
Post Reply
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

DBus notify as a Message alternative

Post by BruceSteers »

I have made a class that can be used almost the same as Message.class.
Only it does not open a modal window it uses DBus notifications.

It works a bit different to Message so here is what to consider if you want to use DBMessage not Message...
  • The notification is not a Modal window so it will not lock your application while open like Message will.
  • If not using DBNotify.Wait then the notification will time out and disappear after a few seconds (7 seconds on my system)
There are 2 ways to use DBMessage...

DBMessage.Wait = False
This pops open the notification, if the user clicks a button then the DBMessage_Click(Value As Integer) event fires giving the button number that was pressed.
Clicking the notification window not a button will either show a value of 0 or the Default if given.
Public Sub Main()

  DBMessage.Wait = False
  DBMessage.AttachTo = Me ' set this to make the DBMessage_Click() event trigger in this class.
  DBMessage.Question("The Title", "The Text", ["Button 1" , "Button 2"])
  ' code will continue to run without waiting

End

Public Sub DBMessage_Click(Value As Integer) ' triggers if the user presses a button

  Print "The user clicked button " & Value

End


DBMessage.Wait = True
This pops open the notification and every 6 seconds it renews it to keep it open. It had to be done this way as there is no way to tell if a notification has timed out and gone so the best option was to keep refreshing it.
The application will now be locked in an event loop waiting for the user to press something. On selection it does not fire the Click event but a command like DBMessage.Question() will return the value.

Public Sub Main()

  DBMessage.Wait = True
  Dim iAnswer As Integer = DBMessage.Question("The Title", "The Text", ["Button 1" , "Button 2"])

  ' now the code does not continue to run until user presses the notification

  Print "The user clicked button " & iAnswer

End



Commands....
  • DBMessage() or DBMessage.Info() ' info icon
  • DBMessage.Question() ' ? icon
  • DBMessage.Warning() ' warning icon
  • DBMessage.Error() ' error icon
  • DBMessage.Custom() ' any stock icon
    '' Use ANY stock icon image, freedesktop stock icon specifications can be found here.. https://specifications.freedesktop.org/ ... 01s04.html
    Static Public Sub Custom(Title As String, Text As String, Optional Buttons As String[], {Default} As Integer = -1, StockIcon As String = "info") As Integer
Static Properties....
  • DBMessage.Wait ' turn Wait on or off
  • DBMessage.Priority ' Default is 1 DBMessage.Normal , can be Low, Normal, High or Urgent , it reset to Normal after each use.
  • DBMessage.LastValue ' the last value from the last use.
  • DBMessage.AttachTo ' give the form you want to get the DBMessage_Click() event, default it Application.MainWindow if set or Windows[0]
So in summary this is not a "perfect" Message replacement but an alternative should it suit your needs.

The way i have implemented a "Wait" option is just a hack and works pretty much okay on my MATE desktop but is not so great on some other desktops where is does not renew quite so seamlessly. Eg, on mate the notification renewing is barely noticeable but on Gnome-Shell it closes and opens more noticeably.

But if you have a need for non vital messages/notifications with option buttons that can time out then this should be ideal.

In the project is the DBMessage.class you can easily add to your own project and a tester application for trying it out.

Note: the icon names can be somewhat hit or miss if using non standard names that might not exist in the current icon theme. see https://specifications.freedesktop.org/ ... 01s04.html for the official freedesktop naming specs.


Have fun
Attachments
Screenshot at 2023-11-26 14-54-44.png
Screenshot at 2023-11-26 14-54-44.png (162.35 KiB) Viewed 4253 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DBus notify as a Message alternative

Post by BruceSteers »

I have made a few changes to this...

I removed Title as the first argument for all commands and went more the Message.class route.
Now there is DBMessage.Title property which by default it is Application.Name but you can set DBMessage.Title before calling DBMessage to use a different one. After calling DBMessage the title is reset unless you set DBMessage.KeepTitle = True to keep it.

Also added DBMessage.UniqueID, Like Title set it before calling DBMessage so you can use and close multiple notifications with different ID's at the same time.

In the DBMessage_Click() event you can check DBMessage.ClickID to get the ID of the clicked notification.

Renamed DBMessage.Release to DBMessage.Close and now has (Optional ID As String) argument.

Now use DBMessage.Kill to kill the DBusSignal handler when your program exits.

Modified the test app...
It can now perform a search for all installed default freedesktop icon names for your current desktop theme.
according to a copy of https://specifications.freedesktop.org/ ... 01s04.html in the project folder.

it can also now open or close 2 different notifications with different IDs

Have fun :)
Attachments
Screenshot at 2023-11-27 17-27-18.png
Screenshot at 2023-11-27 17-27-18.png (159.42 KiB) Viewed 4234 times
DBMessage-0.0.3.tar.gz
(62.75 KiB) Downloaded 145 times
If at first you don't succeed , try doing something differently.
BruceS
Post Reply