Saturday, May 19, 2012

Gtk.StatusIcon: What Every App Needs

Another singleton? No, the quit button! I needed discussion on where all the singletons end, so out of other various aspects of common program classes I choose the little icon that every app needs. I almost wrote-up another blog for an http-server and another for the task queue, yet then decided it is easier to look at the actual source repository for those. Even if I included those or others, they all need some way the application exits, manually. The app-icon usually provides that exit in its menu.

Older implementations of the app-icons are smaller and not scalable. Those worked well on the system tray for menu access. Newer implementations are scalable and useful in other areas of the screen besides the system tray. Apple deviates from Microsoft on where they located the icons and menu, as Apple combines the system tray with the task bar as the single-click action bar. I do like the simplicity of multiple action bars over multitudes of menu bars in the clutter of windows. I think real-time games proved the intuitiveness of iconic action bars over previous user interfaces. Even the latest mobiles have sorted their display with scrolls of actions icons.

Based on my previous singleton examples, I included code below, so you can start your own system-tray or action icon. I also included the initialization for GTK and some options, like quit. It is the bare essentials.

Related Posts: Part 1 2 3 4


using System ;

namespace Application
    {
    [Program.Singleton]
    sealed class StatusIcon : Gtk.StatusIcon
        {
        Menu         menu      = new Menu() ;
     
        static StatusIcon()
            {
            Gtk.Application.Init() ;
            }

        private StatusIcon()
            {
            Program.Initialize   += initialize ;
            Program.Run          += run ;
            }
     
        ~StatusIcon()
            {
            Program.Initialize   -= initialize ;
            Program.Run          -= run ;
            }
         
        void initialize()
            {
            Tooltip       = "Application" ;
            Stock         = Gtk.Stock.DialogQuestion ;
            Visible       = true ;
            Activate     += (o,e) => popup() ;
            PopupMenu    += (o,e) => popup() ;
            }
     
        void run()
            {
            Gtk.Application.Run() ;
            }

        void popup()
            {
            PresentMenu( menu, 0, Gtk.Global.CurrentEventTime ) ;
            }

        class Menu : Gtk.Menu
            {
            public Menu()
                {
                Gtk.MenuItem mi ;
                Append( mi = new Gtk.MenuItem( "Option _1" ) ) ;
                Append( mi = new Gtk.MenuItem( "Option _2" ) ) ;
                Append( mi = new Gtk.MenuItem( "Quit" ) ) ;
                mi.Activated += (o,e) => Gtk.Application.Quit() ;
                ShowAll() ;
                }
            }
        }
    }