OSDN Git Service

PostRequestクラスを追加
[opentween/open-tween.git] / OpenTween / ThemeManager.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 //
10 // This file is part of OpenTween.
11 //
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 #nullable enable
28
29 using System;
30 using System.Drawing;
31
32 namespace OpenTween
33 {
34     public sealed class ThemeManager : IDisposable
35     {
36         public bool IsDisposed { get; private set; } = false;
37
38         /// <summary>未読用フォント</summary>
39         public Font FontUnread { get; }
40
41         public Font FontUnreadBold { get; }
42
43         /// <summary>既読用フォント</summary>
44         public Font FontReaded { get; }
45
46         public Font FontReadedBold { get; }
47
48         /// <summary>発言詳細部用フォント</summary>
49         public Font FontDetail { get; }
50
51         /// <summary>入力欄フォント</summary>
52         public Font FontInputFont { get; }
53
54         /// <summary>未読用文字色</summary>
55         public Color ColorUnread { get; }
56
57         /// <summary>既読用文字色</summary>
58         public Color ColorRead { get; }
59
60         /// <summary>Fav用文字色</summary>
61         public Color ColorFav { get; }
62
63         /// <summary>片思い用文字色</summary>
64         public Color ColorOWL { get; }
65
66         /// <summary>Retweet用文字色</summary>
67         public Color ColorRetweet { get; }
68
69         /// <summary>選択中の行用文字色</summary>
70         public Color ColorHighLight { get; }
71
72         /// <summary>発言詳細部用色</summary>
73         public Color ColorDetail { get; }
74
75         /// <summary>発言詳細部用リンク文字色</summary>
76         public Color ColorDetailLink { get; }
77
78         /// <summary>発言詳細部用背景色</summary>
79         public Color ColorDetailBackcolor { get; }
80
81         /// <summary>自分の発言用背景色</summary>
82         public Color ColorSelf { get; }
83
84         /// <summary>自分宛返信用背景色</summary>
85         public Color ColorAtSelf { get; }
86
87         /// <summary>選択発言者の他の発言用背景色</summary>
88         public Color ColorTarget { get; }
89
90         /// <summary>選択発言中の返信先用背景色</summary>
91         public Color ColorAtTarget { get; }
92
93         /// <summary>選択発言者への返信発言用背景色</summary>
94         public Color ColorAtFromTarget { get; }
95
96         /// <summary>選択発言の唯一@先</summary>
97         public Color ColorAtTo { get; }
98
99         /// <summary>リスト部通常発言背景色</summary>
100         public Color ColorListBackcolor { get; }
101
102         /// <summary>入力欄背景色</summary>
103         public Color ColorInputBackcolor { get; }
104
105         /// <summary>入力欄文字色</summary>
106         public Color ColorInputFont { get; }
107
108         public Brush BrushSelf { get; }
109
110         public Brush BrushAtSelf { get; }
111
112         public Brush BrushTarget { get; }
113
114         public Brush BrushAtTarget { get; }
115
116         public Brush BrushAtFromTarget { get; }
117
118         public Brush BrushAtTo { get; }
119
120         public Brush BrushListBackcolor { get; }
121
122         public Brush BrushHighLight { get; }
123
124         /// <summary>Listにフォーカスがないときの選択行の背景色</summary>
125         public Brush BrushDeactiveSelection { get; }
126
127         public ThemeManager(SettingLocal settingLocal)
128         {
129             var fontConverter = new FontConverter();
130
131             this.FontUnread = ConvertStringToFont(fontConverter, settingLocal.FontUnreadStr)
132                 ?? new(SystemFonts.DefaultFont, FontStyle.Bold | FontStyle.Underline);
133
134             this.FontUnreadBold = new(this.FontUnread, FontStyle.Bold);
135
136             this.FontReaded = ConvertStringToFont(fontConverter, settingLocal.FontReadStr)
137                 ?? SystemFonts.DefaultFont;
138
139             this.FontReadedBold = new(this.FontReaded, FontStyle.Bold);
140
141             this.FontDetail = ConvertStringToFont(fontConverter, settingLocal.FontDetailStr)
142                 ?? SystemFonts.DefaultFont;
143
144             this.FontInputFont = ConvertStringToFont(fontConverter, settingLocal.FontInputFontStr)
145                 ?? SystemFonts.DefaultFont;
146
147             var colorConverter = new ColorConverter();
148
149             this.ColorUnread = ConvertStringToColor(colorConverter, settingLocal.ColorUnreadStr)
150                 ?? SystemColors.ControlText;
151
152             this.ColorRead = ConvertStringToColor(colorConverter, settingLocal.ColorReadStr)
153                 ?? SystemColors.ControlText;
154
155             this.ColorFav = ConvertStringToColor(colorConverter, settingLocal.ColorFavStr)
156                 ?? Color.FromKnownColor(KnownColor.Red);
157
158             this.ColorOWL = ConvertStringToColor(colorConverter, settingLocal.ColorOWLStr)
159                 ?? Color.FromKnownColor(KnownColor.Blue);
160
161             this.ColorRetweet = ConvertStringToColor(colorConverter, settingLocal.ColorRetweetStr)
162                 ?? Color.FromKnownColor(KnownColor.Green);
163
164             this.ColorHighLight = Color.FromKnownColor(KnownColor.HighlightText);
165
166             this.ColorDetail = ConvertStringToColor(colorConverter, settingLocal.ColorDetailStr)
167                 ?? Color.FromKnownColor(KnownColor.ControlText);
168
169             this.ColorDetailLink = ConvertStringToColor(colorConverter, settingLocal.ColorDetailLinkStr)
170                 ?? Color.FromKnownColor(KnownColor.Blue);
171
172             this.ColorDetailBackcolor = ConvertStringToColor(colorConverter, settingLocal.ColorDetailBackcolorStr)
173                 ?? Color.FromKnownColor(KnownColor.Window);
174
175             this.ColorSelf = ConvertStringToColor(colorConverter, settingLocal.ColorSelfStr)
176                 ?? Color.FromKnownColor(KnownColor.AliceBlue);
177
178             this.ColorAtSelf = ConvertStringToColor(colorConverter, settingLocal.ColorAtSelfStr)
179                 ?? Color.FromKnownColor(KnownColor.AntiqueWhite);
180
181             this.ColorTarget = ConvertStringToColor(colorConverter, settingLocal.ColorTargetStr)
182                 ?? Color.FromKnownColor(KnownColor.LemonChiffon);
183
184             this.ColorAtTarget = ConvertStringToColor(colorConverter, settingLocal.ColorAtTargetStr)
185                 ?? Color.FromKnownColor(KnownColor.LavenderBlush);
186
187             this.ColorAtFromTarget = ConvertStringToColor(colorConverter, settingLocal.ColorAtFromTargetStr)
188                 ?? Color.FromKnownColor(KnownColor.Honeydew);
189
190             this.ColorAtTo = ConvertStringToColor(colorConverter, settingLocal.ColorAtToStr)
191                 ?? Color.FromKnownColor(KnownColor.Pink);
192
193             this.ColorListBackcolor = ConvertStringToColor(colorConverter, settingLocal.ColorListBackcolorStr)
194                 ?? Color.FromKnownColor(KnownColor.Window);
195
196             this.ColorInputBackcolor = ConvertStringToColor(colorConverter, settingLocal.ColorInputBackcolorStr)
197                 ?? Color.FromKnownColor(KnownColor.LemonChiffon);
198
199             this.ColorInputFont = ConvertStringToColor(colorConverter, settingLocal.ColorInputFontStr)
200                 ?? Color.FromKnownColor(KnownColor.ControlText);
201
202             this.BrushSelf = new SolidBrush(this.ColorSelf);
203             this.BrushAtSelf = new SolidBrush(this.ColorAtSelf);
204             this.BrushTarget = new SolidBrush(this.ColorTarget);
205             this.BrushAtTarget = new SolidBrush(this.ColorAtTarget);
206             this.BrushAtFromTarget = new SolidBrush(this.ColorAtFromTarget);
207             this.BrushAtTo = new SolidBrush(this.ColorAtTo);
208             this.BrushListBackcolor = new SolidBrush(this.ColorListBackcolor);
209             this.BrushHighLight = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
210             this.BrushDeactiveSelection = new SolidBrush(Color.FromKnownColor(KnownColor.ButtonFace));
211         }
212
213         public void Dispose()
214         {
215             if (this.IsDisposed)
216                 return;
217
218             this.FontUnread.Dispose();
219             this.FontUnreadBold.Dispose();
220             this.FontReaded.Dispose();
221             this.FontReadedBold.Dispose();
222             this.FontDetail.Dispose();
223             this.FontInputFont.Dispose();
224             this.BrushSelf.Dispose();
225             this.BrushAtSelf.Dispose();
226             this.BrushTarget.Dispose();
227             this.BrushAtTarget.Dispose();
228             this.BrushAtFromTarget.Dispose();
229             this.BrushAtTo.Dispose();
230             this.BrushListBackcolor.Dispose();
231             this.BrushHighLight.Dispose();
232             this.BrushDeactiveSelection.Dispose();
233
234             this.IsDisposed = true;
235         }
236
237         public static Font? ConvertStringToFont(FontConverter converter, string? fontStr)
238         {
239             if (MyCommon.IsNullOrEmpty(fontStr))
240                 return null;
241
242             try
243             {
244                 return (Font)converter.ConvertFromString(fontStr);
245             }
246             catch
247             {
248                 return null;
249             }
250         }
251
252         public static string? ConvertFontToString(FontConverter converter, Font font, Font defaultValue)
253         {
254             static bool Equals(Font font1, Font font2)
255                 => font1.Name == font2.Name && font1.Size == font2.Size && font1.Unit == font2.Unit && font1.Style == font2.Style;
256
257             if (Equals(font, defaultValue))
258                 return null;
259
260             return converter.ConvertToString(font);
261         }
262
263         public static Color? ConvertStringToColor(ColorConverter converter, string? colorStr)
264         {
265             if (MyCommon.IsNullOrEmpty(colorStr))
266                 return null;
267
268             try
269             {
270                 return (Color)converter.ConvertFromString(colorStr);
271             }
272             catch
273             {
274                 return null;
275             }
276         }
277
278         public static string? ConvertColorToString(ColorConverter converter, Color color, Color defaultValue)
279         {
280             if (color == defaultValue)
281                 return null;
282
283             return converter.ConvertToString(color);
284         }
285
286         public static void ApplyGlobalUIFont(SettingLocal settingLocal)
287         {
288             var fontConverter = new FontConverter();
289             var font = ConvertStringToFont(fontConverter, settingLocal.FontUIGlobalStr);
290             if (font != null)
291                 OTBaseForm.GlobalFont = font;
292         }
293     }
294 }