OSDN Git Service

fb14cd57d7c7c3c41ac015e10aee20c7f82ff83f
[opentween/open-tween.git] / OpenTween / WaitingDialog.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.ComponentModel;
25 using System.Linq;
26 using System.Text;
27 using System.Threading;
28 using System.Threading.Tasks;
29 using System.Windows.Forms;
30
31 namespace OpenTween
32 {
33     /// <summary>
34     /// タスクが待機中であることを表示するダイアログ
35     /// </summary>
36     /// <remarks>
37     /// 一定時間 (Timeout プロパティで指定する) 以上経ってもタスクが完了しない場合にダイアログを表示します。
38     /// EnableCancellation メソッドを使用することでキャンセル機能を提供することも可能です。
39     /// </remarks>
40     public partial class WaitingDialog : OTBaseForm
41     {
42         private readonly Lazy<CancellationTokenSource> cancellationTokenSource;
43
44         private bool cancellationEnabled = false;
45
46         /// <summary>
47         /// ダイアログを表示せずに待機する最短の待ち時間 (デフォルト0.5秒)
48         /// </summary>
49         public TimeSpan Timeout { get; set; }
50
51         /// <summary>
52         /// ダイアログに表示するメッセージ
53         /// </summary>
54         public string Message
55         {
56             get => this.labelMessage.Text;
57             set => this.labelMessage.Text = value;
58         }
59
60         public WaitingDialog()
61         {
62             this.InitializeComponent();
63
64             this.cancellationTokenSource = new Lazy<CancellationTokenSource>();
65
66             this.Timeout = TimeSpan.FromMilliseconds(500);
67         }
68
69         public WaitingDialog(string message)
70             : this()
71         {
72             this.Message = message;
73         }
74
75         /// <summary>
76         /// キャンセル機能を有効にし、キャンセルを通知するための CancellationToken を返します
77         /// </summary>
78         public CancellationToken EnableCancellation()
79         {
80             this.cancellationEnabled = true;
81             this.ControlBox = true;
82
83             var cts = this.cancellationTokenSource.Value;
84             return cts.Token;
85         }
86
87         public Task WaitForAsync(Task task)
88             => this.WaitForAsync(this.ConvertTaskWithValue(task));
89
90         public Task WaitForAsync(IWin32Window owner, Task task)
91             => this.WaitForAsync(owner, this.ConvertTaskWithValue(task));
92
93         public Task<T> WaitForAsync<T>(Task<T> task)
94             => this.WaitForAsync(null, task);
95
96         /// <summary>
97         /// タスクを待機し、状況に応じて待機中ダイアログを表示します
98         /// </summary>
99         /// <param name="owner">ダイアログのオーナー</param>
100         /// <param name="task">待機するタスク</param>
101         public Task<T> WaitForAsync<T>(IWin32Window owner, Task<T> task)
102         {
103             return Task.Run(async () =>
104             {
105                 // 指定された秒数以内で完了すればダイアログは表示しない
106                 var timeout = Task.Delay(this.Timeout);
107                 if (await Task.WhenAny(task, timeout) != timeout)
108                     return await task;
109
110                 var dialogTask = this.InvokeAsync(() => this.ShowDialog(owner));
111
112                 // キャンセルされずにタスクが先に完了したらダイアログを閉じる
113                 if (await Task.WhenAny(task, dialogTask) != dialogTask)
114                     await this.InvokeAsync(() => this.DialogResult = DialogResult.OK);
115
116                 return await task;
117             });
118         }
119
120         /// <summary>Task を Task&lt;T&gt; に変換したいだけ</summary>
121         private async Task<int> ConvertTaskWithValue(Task task)
122         {
123             await task.ConfigureAwait(false);
124             return 0;
125         }
126
127         private void ProgressDialog_FormClosing(object sender, FormClosingEventArgs e)
128         {
129             if (this.cancellationEnabled)
130             {
131                 if (e.CloseReason == CloseReason.UserClosing)
132                 {
133                     var cts = this.cancellationTokenSource.Value;
134                     cts.Cancel();
135                 }
136             }
137         }
138
139         private void ProgressDialog_KeyDown(object sender, KeyEventArgs e)
140         {
141             if (this.cancellationEnabled)
142             {
143                 if (e.KeyCode == Keys.Escape)
144                 {
145                     var cts = this.cancellationTokenSource.Value;
146                     cts.Cancel();
147
148                     this.DialogResult = DialogResult.Cancel;
149                 }
150             }
151         }
152     }
153 }