OSDN Git Service

PostClass.CreatedAtの型をDateTimeUtcに変更
[opentween/open-tween.git] / OpenTween / OTBaseForm.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 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.Drawing;
26 using System.Linq;
27 using System.Text;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Windows.Forms;
31
32 namespace OpenTween
33 {
34     /// <summary>
35     /// OpenTween で使用する全てのフォームの基底となるクラス
36     /// </summary>
37     public class OTBaseForm : Form
38     {
39         /// <summary>
40         /// 全てのフォームで共通して使用する UI フォント
41         /// </summary>
42         /// <remarks>
43         /// SettingLocal.xml に FontUIGlobalStr 要素を追加する事で変更できます
44         /// </remarks>
45         public static Font GlobalFont { get; set; }
46
47         /// <summary>
48         /// デザイン時のスケールと現在のスケールの比
49         /// </summary>
50         /// <remarks>
51         /// 例えば、デザイン時が 96 dpi (96.0, 96.0) で実行時が 120dpi (120.0, 120.0) の場合は 1.25, 1.25 が返ります
52         /// </remarks>
53         public SizeF CurrentScaleFactor { get; private set; }
54
55         private readonly SynchronizationContext synchronizationContext;
56
57         protected OTBaseForm()
58         {
59             this.CurrentScaleFactor = new SizeF(1.0f, 1.0f);
60             this.synchronizationContext = SynchronizationContext.Current;
61
62             this.Load += (o, e) =>
63             {
64                 // デフォルトの UI フォントを変更
65                 if (OTBaseForm.GlobalFont != null)
66                     this.Font = OTBaseForm.GlobalFont;
67             };
68         }
69
70         public Task InvokeAsync(Action x)
71             => this.InvokeAsync(new Func<int>(() => { x(); return 0; }));
72
73         public Task InvokeAsync(Func<Task> x)
74             => this.InvokeAsync<Task>(x).Unwrap();
75
76         public Task<T> InvokeAsync<T>(Func<Task<T>> x)
77             => this.InvokeAsync<Task<T>>(x).Unwrap();
78
79         /// <summary>
80         /// <see cref="Control.Invoke"/> メソッドのTask版みたいなやつ
81         /// </summary>
82         public Task<T> InvokeAsync<T>(Func<T> x)
83         {
84             var tcs = new TaskCompletionSource<T>();
85             this.synchronizationContext.Post(_ =>
86             {
87                 try
88                 {
89                     var ret = x();
90                     tcs.SetResult(ret);
91                 }
92                 catch (Exception ex)
93                 {
94                     tcs.SetException(ex);
95                 }
96             }, null);
97
98             return tcs.Task;
99         }
100
101         /// <summary>
102         /// source で指定されたフォントのスタイルを維持しつつ GlobalFont に置き換えた Font を返します
103         /// </summary>
104         protected Font ReplaceToGlobalFont(Font source)
105         {
106             if (OTBaseForm.GlobalFont == null)
107                 return source;
108
109             return new Font(OTBaseForm.GlobalFont.Name, source.Size, source.Style);
110         }
111
112         protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
113         {
114             base.ScaleControl(factor, specified);
115
116             const float baseDpi = 96.0f;
117
118             this.CurrentScaleFactor = new SizeF(
119                 this.CurrentAutoScaleDimensions.Width / baseDpi,
120                 this.CurrentAutoScaleDimensions.Height / baseDpi);
121         }
122
123         /// <summary>
124         /// 標準の ListView のスケーリングでは不十分な処理を補います
125         /// </summary>
126         public static void ScaleChildControl(ListView listview, SizeF factor)
127         {
128             // カラム幅
129             foreach (ColumnHeader col in listview.Columns)
130             {
131                 col.Width = ScaleBy(factor.Width, col.Width);
132             }
133         }
134
135         /// <summary>
136         /// 標準の VScrollBar のスケーリングでは不十分な処理を補います
137         /// </summary>
138         public static void ScaleChildControl(VScrollBar scrollBar, SizeF factor)
139         {
140             scrollBar.Width = ScaleBy(factor.Width, scrollBar.Width);
141         }
142
143         /// <summary>
144         /// 標準の ImageList のスケーリングでは不十分な処理を補います
145         /// </summary>
146         public static void ScaleChildControl(ImageList imageList, SizeF factor)
147         {
148             imageList.ImageSize = ScaleBy(factor, imageList.ImageSize);
149         }
150
151         public static Size ScaleBy(SizeF factor, Size size)
152         {
153             return Size.Round(new SizeF(size.Width * factor.Width, size.Height * factor.Height));
154         }
155
156         public static int ScaleBy(float factor, int size)
157         {
158             return (int)Math.Round(size * factor);
159         }
160     }
161 }