An Icon Overlay Notifier for Halfwit

You are looking at revision 4 of this page, which may be out of date. View the latest version.

Here's an example of a StatusesReceivedEvent handler for Halfwit 2 that overlays the number of new tweets on your taskbar icon in Windows 7.

First, the PlugIn class to register our handler:

public class PlugIn : IHalfwitPlugIn
{
    public void Initialize(Autofac.ContainerBuilder builder)
    {
        builder.RegisterType<Handler>().AsImplementedInterfaces();
    }
}

Pretty straight forward. Now the handler itself:

class Handler : IHalfwitEventHandler<StatusesReceivedEvent>
{
    int _count = 0;

    public void Handle(StatusesReceivedEvent e)
    {
        var wnd = Application.Current.MainWindow;
        if (wnd.TaskbarItemInfo == null)
        {
            wnd.TaskbarItemInfo = new TaskbarItemInfo();
            wnd.Activated += (w, a) =>
                {
                    // hide notifications when the user switches to Halfwit
                    ((Window)w).TaskbarItemInfo.Overlay = null;
                    _count = 0;
                };
        }

        // don't show notifications if the user is in Halfwit
        if (wnd.IsActive) return;

        _count += e.Items.Count();

        var grid = new Grid
        {
            Width = 20,
            Height = 20,
            Children =
            {
                new Ellipse { Fill = Brushes.DarkRed },
                new TextBlock
                {
                    Text = _count > 99 ? "!" : _count.ToString(),
                    TextAlignment = TextAlignment.Center,
                    Foreground = Brushes.White,
                    FontSize = 11,
                    FontWeight = FontWeights.Bold,
                    Height = 16,
                    VerticalAlignment = VerticalAlignment.Center,
                }
            }
        };

        var bmp = new RenderTargetBitmap(20, 20, 96, 96, PixelFormats.Default);
        grid.Arrange(new Rect(0, 0, 20, 20));

        bmp.Render(grid);

        wnd.TaskbarItemInfo.Overlay = (ImageSource)bmp;
    }
}

So we're finding the application's main window and setting its TaskbarItemInfo property if it doesn't have one. Then, if Halfwit's main window isn't the active window, we're rendering the total number of new tweets as white text on a dark red ellipse. When the user switches back to Halfwit, we clear the icon overlay and reset the count.

Taskbar Icon Overlay

Big thanks to Pete Brown whose post Creating Dynamic Windows 7 Taskbar Overlay Icons served as the inspiration for this plug-in! The plug-in is available to download via the Halfwit 2 Plug-Ins page.

halfwit mvvm ioc windows-7
Posted by: Matt Hamilton
Last revised: 04 Dec, 2010 04:48 AM History
You are looking at revision 4 of this page, which may be out of date. View the latest version.