예제 #1
0
        public NotifyToken ShowMessage(System.Windows.FrameworkElement control, int?timeout,
                                       NotifyPosition position = NotifyPosition.ActiveScreen, bool onlyCloseByToken = false)
        {
            TaskbarIcon.GetCustomPopupPosition func = null;
            switch (position)
            {
            case NotifyPosition.ActiveWindowCenter:
                func = () =>
                {
                    var rect = WindowManager.CurrentWindow.Rect;
                    return(new NotifyIcon.Interop.Point()
                    {
                        X = (int)(rect.X + rect.Width / 2 - control.ActualWidth / 2),
                        Y = (int)(rect.Y + rect.Height / 2 - control.ActualHeight / 2)
                    });
                };
                break;

            case NotifyPosition.ActiveScreen:
                func = () =>
                {
                    var screen = Screen.FromHandle(WindowManager.CurrentWindow.Handle);
                    if (screen.Equals(Screen.PrimaryScreen))
                    {
                        return(TrayIcon.GetPopupTrayPosition());
                    }

                    var bounds = screen.Bounds;
                    return(new NotifyIcon.Interop.Point()
                    {
                        X = bounds.X + bounds.Width,
                        Y = bounds.Y + bounds.Height
                    });
                };
                break;

            case NotifyPosition.Default:
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(position) + " not supported", position, null);
            }

            TrayIcon.CustomPopupPosition = func;
            return(TrayIcon.ShowCustomBalloon(control, PopupAnimation.None, timeout, onlyCloseByToken));
        }
예제 #2
0
 public void Notify(string message, NotifyType type = NotifyType.Success, NotifyPosition position = NotifyPosition.TopRight)
 {
     TempData["Notify_Message"]  = message;
     TempData["Notify_Type"]     = type.ToString().Uncapitalize();
     TempData["Notify_Position"] = position.ToString().Uncapitalize();
 }
예제 #3
0
파일: Notify.cs 프로젝트: metaseed/Metakey
        public static MessageToken <TipItem> ShowMessage(System.Windows.FrameworkElement balloon,
                                                         ObservableCollection <TipItem> data, int?timeout,
                                                         NotifyPosition position = NotifyPosition.ActiveScreen, PopupAnimation animation = PopupAnimation.None)
        {
            var dispatcher = balloon.Dispatcher;

            if (!dispatcher.CheckAccess())
            {
                return(dispatcher.Invoke(DispatcherPriority.Normal,
                                         (Func <MessageToken <TipItem> >)(() =>
                                                                          ShowMessage(balloon, data, timeout, position, animation))) as
                       MessageToken <TipItem>);
            }

            if (balloon == null)
            {
                throw new ArgumentNullException("balloon");
            }
            if (timeout.HasValue && timeout < 500)
            {
                var msg = "Invalid timeout of {0} milliseconds. Timeout must be at least 500 ms";
                msg = String.Format(msg, timeout);
                throw new ArgumentOutOfRangeException("timeout", msg);
            }

            if (LogicalTreeHelper.GetParent(balloon) is Popup parent)
            {
                parent.Child = null;
                var msg =
                    "Cannot display control [{0}] in a new balloon popup - that control already has a parent. You may consider creating new balloons every time you want to show one.";
                msg = String.Format(msg, balloon);
                throw new InvalidOperationException(msg);
            }

            var popup = new Popup();

            popup.AllowsTransparency = true;
            popup.PopupAnimation     = animation;
            popup.Placement          = PlacementMode.AbsolutePoint;
            popup.StaysOpen          = true;
            popup.DataContext        = data;

            Point point;

            switch (position)
            {
            case NotifyPosition.Caret:
            {
                var rect = Utils.Window.GetCurrentWindowCaretPosition();
                var X    = (rect.Left + rect.Width / 2 - balloon.ActualWidth / 2);
                var Y    = (rect.Bottom + rect.Height / 2 - balloon.ActualHeight / 2);
                if (X == 0 && Y == 0)
                {
                    goto case NotifyPosition.ActiveWindowCenter;
                }

                point = new Point(X, Y);
                break;
            }

            case NotifyPosition.ActiveWindowCenter:
            {
                var rect = Utils.Window.GetCurrentWindowRect();
                var X    = (rect.X + rect.Width / 2 - balloon.ActualWidth / 2);
                var Y    = (rect.Y + rect.Height / 2 - balloon.ActualHeight / 2);
                point = new Point(X, Y);
                break;
            }

            case NotifyPosition.ActiveScreen:
            {
                var screen = Screen.FromHandle(Utils.Window.CurrentWindowHandle);
                if (screen.Equals(Screen.PrimaryScreen))
                {
                    var p = TrayInfo.GetTrayLocation();
                    point = new Point(p.X, p.Y);
                    break;
                }

                var bounds = screen.Bounds;
                var X      = bounds.X + bounds.Width;
                var Y      = bounds.Y + bounds.Height;
                point = new Point(X, Y);
                break;
            }

            case NotifyPosition.Default:
            {
                var p = TrayInfo.GetTrayLocation();
                point = new Point(p.X, p.Y);
                break;
            }


            default:
                throw new ArgumentOutOfRangeException(nameof(position) + " not supported", position, null);
            }


            popup.Child            = balloon;
            popup.HorizontalOffset = point.X + 1;
            popup.VerticalOffset   = point.Y - 2;
            balloon.Focusable      = true;

            IInputElement element = null;

            popup.Opened += (s, a) =>
            {
                element = Keyboard.FocusedElement;
                var source = (HwndSource)PresentationSource.FromVisual(balloon);
                var handle = source.Handle;
                PInvokes.SetForegroundWindow(handle);
                Keyboard.Focus(balloon);
            };

            popup.IsOpen = true;
            popup.Focus();

            var r = new MessageToken <TipItem>(popup);

            popup.Closed += (s, a) =>
            {
                Keyboard.Focus(element);
                r.Close();
            };

            void TimerTick(object sender, EventArgs e)
            {
                r.Timer.Tick -= TimerTick;
                r.Close();
            }

            if (timeout.HasValue)
            {
                r.Timer = new DispatcherTimer {
                    Interval = TimeSpan.FromMilliseconds(timeout.Value)
                };
                r.Timer.Tick += TimerTick;
                r.Timer.Start();
            }

            return(r);
        }
예제 #4
0
 public void Notify(string message, NotifyType type = NotifyType.Success, NotifyPosition position = NotifyPosition.TopRight)
 {
     TempData["Notify_Message"] = message;
     TempData["Notify_Type"] = type.ToString().Uncapitalize();
     TempData["Notify_Position"] = position.ToString().Uncapitalize();
 }
예제 #5
0
 public static void Send(Client client, NotifyType type, NotifyPosition pos, string msg, int time)
 {
     Trigger.ClientEvent(client, "notify", type, pos, msg, time);
 }