123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
-
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Resources;
- using System.Windows.Threading;
- using Hardcodet.Wpf.TaskbarNotification.Interop;
- namespace Hardcodet.Wpf.TaskbarNotification
- {
-
-
-
- internal static class Util
- {
- public static readonly object SyncRoot = new object();
- #region IsDesignMode
- private static readonly bool isDesignMode;
-
-
-
- public static bool IsDesignMode
- {
- get { return isDesignMode; }
- }
- #endregion
- #region construction
- static Util()
- {
- isDesignMode =
- (bool)
- DependencyPropertyDescriptor.FromProperty(DesignerProperties.IsInDesignModeProperty,
- typeof (FrameworkElement))
- .Metadata.DefaultValue;
- }
- #endregion
- #region CreateHelperWindow
-
-
-
-
-
-
- public static Window CreateHelperWindow()
- {
- return new Window
- {
- Width = 0,
- Height = 0,
- ShowInTaskbar = false,
- WindowStyle = WindowStyle.None,
- AllowsTransparency = true,
- Opacity = 0
- };
- }
- #endregion
- #region WriteIconData
-
-
-
-
-
-
-
-
- public static bool WriteIconData(ref NotifyIconData data, NotifyCommand command)
- {
- return WriteIconData(ref data, command, data.ValidMembers);
- }
-
-
-
-
-
-
-
-
-
-
- public static bool WriteIconData(ref NotifyIconData data, NotifyCommand command, IconDataMembers flags)
- {
-
- if (IsDesignMode) return true;
- data.ValidMembers = flags;
- lock (SyncRoot)
- {
- return WinApi.Shell_NotifyIcon(command, ref data);
- }
- }
- #endregion
- #region GetBalloonFlag
-
-
-
-
- public static BalloonFlags GetBalloonFlag(this BalloonIcon icon)
- {
- switch (icon)
- {
- case BalloonIcon.None:
- return BalloonFlags.None;
- case BalloonIcon.Info:
- return BalloonFlags.Info;
- case BalloonIcon.Warning:
- return BalloonFlags.Warning;
- case BalloonIcon.Error:
- return BalloonFlags.Error;
- default:
- throw new ArgumentOutOfRangeException("icon");
- }
- }
- #endregion
- #region ImageSource to Icon
-
-
-
-
-
-
-
- public static Icon ToIcon(this ImageSource imageSource)
- {
- if (imageSource == null) return null;
- Uri uri = new Uri(imageSource.ToString());
- StreamResourceInfo streamInfo = Application.GetResourceStream(uri);
- if (streamInfo == null)
- {
- string msg = "The supplied image source '{0}' could not be resolved.";
- msg = string.Format(msg, imageSource);
- throw new ArgumentException(msg);
- }
- return new Icon(streamInfo.Stream);
- }
- #endregion
- #region evaluate listings
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static bool Is<T>(this T value, params T[] candidates)
- {
- if (candidates == null) return false;
- foreach (var t in candidates)
- {
- if (value.Equals(t)) return true;
- }
- return false;
- }
- #endregion
- #region match MouseEvent to PopupActivation
-
-
-
-
- public static bool IsMatch(this MouseEvent me, PopupActivationMode activationMode)
- {
- switch (activationMode)
- {
- case PopupActivationMode.LeftClick:
- return me == MouseEvent.IconLeftMouseUp;
- case PopupActivationMode.RightClick:
- return me == MouseEvent.IconRightMouseUp;
- case PopupActivationMode.LeftOrRightClick:
- return me.Is(MouseEvent.IconLeftMouseUp, MouseEvent.IconRightMouseUp);
- case PopupActivationMode.LeftOrDoubleClick:
- return me.Is(MouseEvent.IconLeftMouseUp, MouseEvent.IconDoubleClick);
- case PopupActivationMode.DoubleClick:
- return me.Is(MouseEvent.IconDoubleClick);
- case PopupActivationMode.MiddleClick:
- return me == MouseEvent.IconMiddleMouseUp;
- case PopupActivationMode.All:
-
- return me != MouseEvent.MouseMove;
- default:
- throw new ArgumentOutOfRangeException("activationMode");
- }
- }
- #endregion
- #region execute command
-
-
-
-
-
-
-
-
- public static void ExecuteIfEnabled(this ICommand command, object commandParameter, IInputElement target)
- {
- if (command == null) return;
- RoutedCommand rc = command as RoutedCommand;
- if (rc != null)
- {
-
- if (rc.CanExecute(commandParameter, target)) rc.Execute(commandParameter, target);
- }
- else if (command.CanExecute(commandParameter))
- {
- command.Execute(commandParameter);
- }
- }
- #endregion
-
-
-
-
- internal static Dispatcher GetDispatcher(this DispatcherObject source)
- {
-
- if (Application.Current != null) return Application.Current.Dispatcher;
-
- if (source.Dispatcher != null) return source.Dispatcher;
-
- return Dispatcher.CurrentDispatcher;
- }
-
-
-
-
-
-
-
-
-
- public static bool IsDataContextDataBound(this FrameworkElement element)
- {
- if (element == null) throw new ArgumentNullException("element");
- return element.GetBindingExpression(FrameworkElement.DataContextProperty) != null;
- }
- }
- }
|