Exemple #1
0
 private void ShowToast(ToastInfo toastInfo)
 {
     _toastQueue.Enqueue(toastInfo);
     if (!IsToastVisible)
     {
         ProcessToastQueue();
     }
 }
        void ShowNext()
        {
            if (!positioner.HasEmptySlot() || !toastsQueue.Any())
            {
                return;
            }

            ToastInfo info = toastsQueue[0];

            toastsQueue.RemoveAt(0);

            var content = new ToastContentControl()
            {
                Toast = info.toast
            };

            content.Content = info.toast.ViewModel;
            content.Style   = Style;
            if (ContentTemplateSelector == null)
            {
                content.ContentTemplate = ContentTemplate ?? NotificationServiceTemplatesHelper.DefaultCustomToastTemplate;
            }
            content.ContentTemplateSelector = ContentTemplateSelector;

            try {
                info.win = new ToastWindow();
            } catch {
                content.TimeOutCommand.Execute(null);
                return;
            }

            info.win.Content     = content;
            info.win.DataContext = info.toast.ViewModel;

            if (double.IsNaN(content.Width) || double.IsNaN(content.Height))
            {
                throw new InvalidOperationException("The height or width of a custom notification can not be set to Auto");
            }

            System.Windows.Point position = positioner.Add(info, content.Width, content.Height);

            info.win.Left = position.X;
            info.win.Top  = position.Y;

            try {
                info.win.Show();
            } catch (System.ComponentModel.Win32Exception) {
                content.TimeOutCommand.Execute(null);
                return;
            }
            info.timer.Tick += (s, e) => {
                content.TimeOutCommand.Execute(null);
                info.timer.Stop();
            };
            info.timer.Start();
        }
        void RemoveVisibleToast(CustomNotification toast, NotificationResult result)
        {
            ToastInfo info = GetVisibleToastInfo(toast);

            if (info == null)
            {
                return;
            }
            info.win.Close();
            info.timer.Stop();
            positioner.Remove(info);
            info.source.SetResult(result);
        }
        internal void Hide(CustomNotification toast)
        {
            ToastInfo info = toastsQueue.FirstOrDefault(i => i.toast == toast);

            if (info != null)
            {
                toastsQueue.Remove(info);
                info.source.SetResult(NotificationResult.ApplicationHidden);
                return;
            }
            RemoveVisibleToast(toast, NotificationResult.ApplicationHidden);
            ShowNext();
        }
        public Task <NotificationResult> ShowAsync(CustomNotification toast, int msDuration = 3000)
        {
            var info = new ToastInfo {
                toast = toast,
                timer = new Timer()
                {
                    Interval = msDuration
                },
                source = new TaskCompletionSource <NotificationResult>()
            };

            toastsQueue.Add(info);
            ShowNext();
            return(info.source.Task);
        }
Exemple #6
0
    // Update is called once per frame
    void Update()
    {
        if (!toasting)
        {
            if (toastQueue.Count > 0)
            {
                ToastInfo tInfo = toastQueue.Dequeue();
                switch (tInfo.type)
                {
                case ToastType.Error:
                    toastBg.color = new Color(0.5f, 0.2f, 0f);
                    title.text    = "错误";
                    break;

                case ToastType.Info:
                    toastBg.color = new Color(0, 0, 0);
                    title.text    = "通知";
                    break;

                case ToastType.Other:
                    toastBg.color = new Color(0.8f, 0.6f, 0.2f);
                    title.text    = "其他";
                    break;
                }
                content.text = tInfo.data;
                toastBg.gameObject.SetActive(true);
                toasting = true;
            }
        }
        else
        {
            count += Time.deltaTime;
            if (count > timeLimit)
            {
                toastBg.gameObject.SetActive(false);
                toasting = false;
                count    = 0;
            }
        }
    }
Exemple #7
0
        private async void ProcessToastQueue()
        {
            // For simplicity's sake, we stop the timer on tick, and re-start it only when needed
            _toastTimer.Stop();

            // Ensure we can always reset to a safe state, should something go wrong
            if (_toastQueue.Count == 0)
            {
                _currentToast        = null;
                _currentToastHandled = false;
                VisualStateManager.GoToState(this, "ToastHidden", true);

                if (ShowSystemStatusBar)
                {
                    Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();
                }
                return;
            }

            // If a toast is already visible, we need to do the hide animation for that toast before showing
            // the show animation for the next toast.
            if (IsToastVisible)
            {
                VisualStateManager.GoToState(this, "ToastHidden", true);
                await Task.Delay(250); // This delay is obviously dependent on the duration of the visual state transition storyboard
            }

            // Get the next toast from the queue
            var toastInfo = _toastQueue.Dequeue();

            // Informational toasts and normal toasts have different UI
            if (_infoToast != null)
            {
                _infoToast.Visibility = toastInfo.IsInformational ? Visibility.Visible : Visibility.Collapsed;
            }
            if (_normalToast != null)
            {
                _normalToast.Visibility = toastInfo.IsInformational ? Visibility.Collapsed : Visibility.Visible;
            }

            if (toastInfo.IsInformational)
            {
                if (_infoToastText != null)
                {
                    _infoToastText.Text = toastInfo.Text ?? string.Empty; // TextBlock.Text property can not be set to null!
                }
            }
            else
            {
                if (_toastTitle != null)
                {
                    _toastTitle.Text       = toastInfo.Title ?? string.Empty;
                    _toastTitle.Visibility = string.IsNullOrEmpty(toastInfo.Title)
                        ? Visibility.Collapsed
                        : Visibility.Visible;
                }
                if (_toastText != null)
                {
                    _toastText.Text = toastInfo.Text ?? string.Empty;
                }
            }

            if (ShowSystemStatusBar)
            {
                await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();

                await Task.Delay(50);
            }

            // Finally, transition to the visible state
            VisualStateManager.GoToState(this, "ToastVisible", true);

            // Set instance state and kick off timer
            _currentToast        = toastInfo;
            _currentToastHandled = false;
            _toastTimer.Start();
        }
Exemple #8
0
    public void AddToast(ToastType type, string message)
    {
        ToastInfo toast = new ToastInfo(type, message);

        toastQueue.Enqueue(toast);
    }
Exemple #9
0
        private async void ProcessToastQueue()
        {
            // For simplicity's sake, we stop the timer on tick, and re-start it only when needed
            _toastTimer.Stop();

            // Ensure we can always reset to a safe state, should something go wrong
            if (_toastQueue.Count == 0)
            {
                _currentToast = null;
                _currentToastHandled = false;
                VisualStateManager.GoToState(this, "ToastHidden", true);

                if (ShowSystemStatusBar)
                {
                    Windows.UI.ViewManagement.StatusBar.GetForCurrentView().ShowAsync();
                }
                return;
            }

            // If a toast is already visible, we need to do the hide animation for that toast before showing 
            // the show animation for the next toast.
            if (IsToastVisible)
            {
                VisualStateManager.GoToState(this, "ToastHidden", true);
                await Task.Delay(250); // This delay is obviously dependent on the duration of the visual state transition storyboard
            }

            // Get the next toast from the queue
            var toastInfo = _toastQueue.Dequeue();

            // Informational toasts and normal toasts have different UI
            if (_infoToast != null)
            {
                _infoToast.Visibility = toastInfo.IsInformational ? Visibility.Visible : Visibility.Collapsed;
            }
            if (_normalToast != null)
            {
                _normalToast.Visibility = toastInfo.IsInformational ? Visibility.Collapsed : Visibility.Visible;
            }

            if (toastInfo.IsInformational)
            {
                if (_infoToastText != null)
                {
                    _infoToastText.Text = toastInfo.Text ?? string.Empty; // TextBlock.Text property can not be set to null!
                }
            }
            else
            {
                if (_toastTitle != null)
                {
                    _toastTitle.Text = toastInfo.Title ?? string.Empty;
                    _toastTitle.Visibility = string.IsNullOrEmpty(toastInfo.Title)
                        ? Visibility.Collapsed
                        : Visibility.Visible;
                }
                if (_toastText != null)
                {
                    _toastText.Text = toastInfo.Text ?? string.Empty;
                }
            }

            if (ShowSystemStatusBar)
            {
                await Windows.UI.ViewManagement.StatusBar.GetForCurrentView().HideAsync();
                await Task.Delay(50);
            }

            // Finally, transition to the visible state
            VisualStateManager.GoToState(this, "ToastVisible", true);

            // Set instance state and kick off timer
            _currentToast = toastInfo;
            _currentToastHandled = false;
            _toastTimer.Start();
        }
Exemple #10
0
 private void ShowToast(ToastInfo toastInfo)
 {
     _toastQueue.Enqueue(toastInfo);
     if (!IsToastVisible)
     {
         ProcessToastQueue();
     }
 }
Exemple #11
0
        private IEnumerator Play()
        {
            isToastPlaying = true;

            while (true)
            {
                ToastInfo toastInfo = toastQueue.Peek();
                textGenerator.Populate(toastInfo.Msg, settings);
                float width  = 0;
                float height = 0;

                IList <UILineInfo> lineInfos = textGenerator.lines;
                if (textGenerator.lineCount == 1)
                {
                    IList <UICharInfo> charInfos = textGenerator.characters;
                    int count = charInfos.Count;
                    for (int i = 0; i < count; i++)
                    {
                        width += charInfos[i].charWidth;
                    }

                    height = lineInfos[0].height;
                }
                else
                {
                    IList <UICharInfo> charInfos = textGenerator.characters;
                    int   count    = charInfos.Count;
                    float curWidth = charInfos[0].charWidth;
                    for (int i = 1; i < count; i++)
                    {
                        if (charInfos[i - 1].charWidth > 0 && charInfos[i].charWidth < float.Epsilon)
                        {
                            if (width < curWidth)
                            {
                                width = curWidth;
                            }

                            curWidth = 0;
                        }
                        else
                        {
                            curWidth += charInfos[i].charWidth;
                        }
                    }

                    for (int i = 0; i < textGenerator.lineCount; i++)
                    {
                        height += lineInfos[i].height;
                    }
                }

                toastText.text = toastInfo.Msg;
                if (width + PADDING_LEFT_F * 2 > maxWidth)
                {
                    width = maxWidth - PADDING_LEFT_F * 2;
                }

                toastRectTrans.sizeDelta = new Vector2(width, height);
                bgRectTrans.sizeDelta    = new Vector2(width + PADDING_LEFT_F, height + PADDING_TOP_F);

                float playTime = 0;
                switch (toastInfo.ShowTime)
                {
                case ToastTime.Long:
                    playTime = LONG_PLAY_TIME_F;
                    break;

                case ToastTime.Middle:
                    playTime = MIDDLE_PLAY_TIME_F;
                    break;

                case ToastTime.Short:
                    playTime = SHORT_PLAY_TIME_F;
                    break;
                }

                WaitForSeconds animWait = new WaitForSeconds(ANIM_TIME_F * .1f);
                for (int i = 0; i < 10; i++)
                {
                    canvasGroup.alpha += .1f;
                    yield return(animWait);
                }

                yield return(new WaitForSeconds(playTime));

                for (int i = 0; i < 10; i++)
                {
                    canvasGroup.alpha -= .1f;
                    yield return(animWait);
                }

                toastQueue.Dequeue();

                if (toastQueue.Count > 0)
                {
                    yield return(new WaitForSeconds(INTERVAL_TIME_F));
                }
                else
                {
                    break;
                }
            }
        }