An Icon Overlay Notifier for Halfwit
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,
Effect = new DropShadowEffect { BlurRadius = 2, Color = SystemColors.InfoColor, ShadowDepth = 0 },
}
}
};
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.
Big thanks to Pete Brown whose post Creating Dynamic Windows 7 Taskbar Overlay Icons served as the inspiration for this plug-in! I'll make the plug-in available as a download (it's only an 8KB DLL) once Halfwit 2 has been released.
Last revised: 03 Dec, 2010 11:50 PM History
Trackbacks
- rimonabantexcellence site title | http://www.rimonabantexcellence.com/t.php?ahr0cdovl21hdhroyw1pbhrvbi5uzxqvagfszndpdc1py29ulw92zxjsyxk/cmv2axnpb249na==