프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkusAutoUpdate / src / NetSparkle.UI.WPF / ToastNotification.xaml.cs @ 63946dd5

이력 | 보기 | 이력해설 | 다운로드 (5.22 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Timers;
4
using System.Windows;
5

    
6
namespace NetSparkleUpdater.UI.WPF
7
{
8
    /// <summary>
9
    /// Interaction logic for ToastNotification.xaml
10
    /// </summary>
11
    public partial class ToastNotification : Window
12
    {
13
        private Timer _goUpTimer;
14
        private Timer _goDownTimer;
15
        private Timer _pauseTimer;
16
        private double _startPosX;
17
        private double _startPosY;
18
        private bool _hasInitiatedShutdown = false;
19

    
20
        private double _workAreaHeight;
21
        private double _workAreaWidth;
22

    
23
        public ToastNotification()
24
        {
25
            InitializeComponent();
26
            Topmost = true;
27
            // Toast doesn't need to be shown in task bar
28
            ShowInTaskbar = false;
29
            // Create and run timer for animation
30

    
31
            _goUpTimer = new Timer();
32
            // AutoReset = false so that timer stops when we want it to: https://stackoverflow.com/a/18280560/3938401
33
            _goUpTimer.AutoReset = false;
34
            _goUpTimer.Interval = 25;
35
            _goUpTimer.Elapsed += GoUpTimerTick;
36
            _goUpTimer.Start();
37

    
38
            _goDownTimer = new Timer();
39
            _goDownTimer.AutoReset = false;
40
            _goDownTimer.Interval = 25;
41
            _goDownTimer.Elapsed += GoDownTimerTick;
42

    
43
            _pauseTimer = new Timer();
44
            _pauseTimer.AutoReset = false;
45
            _pauseTimer.Interval = 15000;
46
            _pauseTimer.Elapsed += PauseTimerTick;
47

    
48
            _workAreaHeight = System.Windows.SystemParameters.WorkArea.Height;
49
            _workAreaWidth = System.Windows.SystemParameters.WorkArea.Width;
50
            Left = _startPosX = _workAreaWidth - Width;
51
            Top = _startPosY = _workAreaHeight + 10;
52
            Loaded += ToastNotification_Loaded;
53
        }
54

    
55
        private void ToastNotification_Loaded(object sender, RoutedEventArgs e)
56
        {
57
            // Begin animation
58
            _goUpTimer.Start();
59
        }
60

    
61
        public Action<List<AppCastItem>> ClickAction { get; set; }
62

    
63
        public List<AppCastItem> Updates { get; set; }
64
        
65
        private void PauseTimerTick(object sender, EventArgs e)
66
        {
67
            Dispatcher.Invoke(() =>
68
            {
69
                _pauseTimer.Stop();
70
                _pauseTimer.Dispose();
71
                _goDownTimer.Start();
72
            });
73
        }
74

    
75
        void GoUpTimerTick(object sender, EventArgs e)
76
        {
77
            Dispatcher.InvokeAsync(() =>
78
            {
79
                // If window is fully visible stop the timer
80
                if (_startPosY < _workAreaHeight - Height)
81
                {
82
                    _goUpTimer.Stop();
83
                    _goUpTimer.Dispose();
84
                    _pauseTimer.Start();
85
                }
86
                else
87
                {
88
                    Left = _startPosX;
89
                    Top = _startPosY;
90
                    _goUpTimer.Enabled = true;
91
                }
92
                // Lift window by 5 pixels
93
                _startPosY -= 5;
94
            });
95
        }
96

    
97
        private void GoDownTimerTick(object sender, EventArgs e)
98
        {
99
            Dispatcher.InvokeAsync(() =>
100
            {
101
                // If window is fully visible stop the timer
102
                if (_startPosY > _workAreaHeight + Height)
103
                {
104
                    _goDownTimer.Stop();
105
                    CloseToastMessage();
106
                }
107
                else
108
                {
109
                    Left = _startPosX;
110
                    Top = _startPosY;
111
                    _goDownTimer.Enabled = true;
112
                }
113
                // Lower window by 5 pixels
114
                _startPosY += 5;
115
            });
116
        }
117

    
118
        private void ToastNotifier_Click(object sender, EventArgs e)
119
        {
120
            ClickAction?.Invoke(Updates);
121
            CloseToastMessage();
122
        }
123

    
124
        private void CloseToastMessage()
125
        {
126
            Dispatcher.InvokeAsync(() =>
127
            {
128
                // make sure all the timers are stopped
129
                _pauseTimer.Stop();
130
                _pauseTimer.Dispose();
131
                _goUpTimer.Stop();
132
                _goUpTimer.Dispose();
133
                _goUpTimer.Stop();
134
                _goUpTimer.Dispose();
135
                Close();
136
                if (!_hasInitiatedShutdown)
137
                {
138
                    _hasInitiatedShutdown = true;
139
                    Dispatcher.InvokeShutdown();
140
                }
141
            });
142
        }
143

    
144
        /// <summary>
145
        /// Show the toast
146
        /// </summary>
147
        /// <param name="message">Main message of the toast</param>
148
        /// <param name="callToAction">Text of the hyperlink</param>
149
        /// <param name="seconds">How long to show before it goes back down</param>
150
        public void Show(string message, string callToAction, int seconds)
151
        {
152
            NotificationTitle.Text = message;
153
            NotificationLink.Inlines.Clear();
154
            NotificationLink.Inlines.Add(callToAction);
155
            _pauseTimer.Interval = 1000 * seconds;
156
            Show();
157
        }
158

    
159
        private void NotificationLink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
160
        {
161
            ClickAction?.Invoke(Updates);
162
            CloseToastMessage();
163
        }
164
    }
165
}
클립보드 이미지 추가 (최대 크기: 500 MB)