1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
25 using System.Collections.Generic;
28 using System.Windows.Forms;
30 using System.Windows.Forms.Design;
31 using System.ComponentModel;
37 /// API 実行回数制限に到達するまでの目安を表示する ToolStripItem
39 [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
40 public class ToolStripAPIGauge : ToolStripStatusLabel
42 public ToolStripAPIGauge()
46 this.DisplayStyle = ToolStripItemDisplayStyle.Text;
53 [RefreshProperties(RefreshProperties.Repaint)]
54 public int GaugeHeight
56 get => this._GaugeHeight;
59 this._GaugeHeight = value;
61 this.UpdateGaugeBounds();
65 private int _GaugeHeight = 8;
71 public ApiLimit? ApiLimit
73 get => this._ApiLimit;
76 this._ApiLimit = value;
78 this.UpdateRemainMinutes();
80 this.UpdateGaugeBounds();
84 private ApiLimit? _ApiLimit = null;
90 public string? ApiEndpoint
92 get => this._ApiEndpoint;
95 if (MyCommon.IsNullOrEmpty(value))
98 this._ApiEndpoint = null;
103 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[value];
105 if (this._ApiEndpoint != value)
107 // ApiEndpointが変更されているので更新する
108 this._ApiEndpoint = value;
109 this.ApiLimit = apiLimit;
113 // ApiLimitが変更されていれば更新する
114 if (this._ApiLimit == null ||
115 !this._ApiLimit.Equals(apiLimit))
117 this.ApiLimit = apiLimit;
122 private string? _ApiEndpoint = null;
125 [EditorBrowsable(EditorBrowsableState.Never)]
126 public new string Text
129 set => base.Text = value;
133 [EditorBrowsable(EditorBrowsableState.Never)]
134 public new string ToolTipText
136 get => base.ToolTipText;
137 set => base.ToolTipText = value;
140 [DefaultValue(ToolStripItemDisplayStyle.Text)]
141 [RefreshProperties(RefreshProperties.Repaint)]
142 public new ToolStripItemDisplayStyle DisplayStyle
144 get => base.DisplayStyle;
145 set => base.DisplayStyle = value;
148 protected double remainMinutes = -1;
150 protected virtual void UpdateRemainMinutes()
152 if (this._ApiLimit != null)
153 this.remainMinutes = (this._ApiLimit.AccessLimitResetDate - DateTimeUtc.Now).TotalMinutes;
155 this.remainMinutes = -1;
158 protected override void OnPaint(PaintEventArgs e)
162 if (this.apiGaugeBounds != Rectangle.Empty)
163 g.FillRectangle(Brushes.LightBlue, this.apiGaugeBounds);
165 if (this.timeGaugeBounds != Rectangle.Empty)
166 g.FillRectangle(Brushes.LightPink, this.timeGaugeBounds);
171 #region from Tween v1.1.0.0
173 // The code in this region block is based on code written by the following authors:
174 // (C) 2010 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
175 // (C) 2010 Moz (@syo68k)
177 internal Rectangle apiGaugeBounds = Rectangle.Empty;
178 internal Rectangle timeGaugeBounds = Rectangle.Empty;
180 protected virtual void UpdateGaugeBounds()
182 if (this._ApiLimit == null || this._GaugeHeight < 1)
184 this.apiGaugeBounds = Rectangle.Empty;
185 this.timeGaugeBounds = Rectangle.Empty;
189 var apiGaugeValue = (double)this._ApiLimit.AccessLimitRemain / this._ApiLimit.AccessLimitCount;
190 this.apiGaugeBounds = new Rectangle(
192 (this.Height - this._GaugeHeight * 2) / 2,
193 (int)(this.Width * apiGaugeValue),
197 var timeGaugeValue = this.remainMinutes >= 15 ? 1.00 : this.remainMinutes / 15;
198 this.timeGaugeBounds = new Rectangle(
200 this.apiGaugeBounds.Top + this._GaugeHeight,
201 (int)(this.Width * timeGaugeValue),
206 protected virtual void UpdateText()
208 string remainCountText;
212 if (this._ApiLimit == null || this.remainMinutes < 0)
214 remainCountText = "???";
215 maxCountText = "???";
220 remainCountText = this._ApiLimit.AccessLimitRemain.ToString();
221 maxCountText = this._ApiLimit.AccessLimitCount.ToString();
222 minuteText = Math.Ceiling(this.remainMinutes).ToString();
225 var endpointText = MyCommon.IsNullOrEmpty(this._ApiEndpoint) ? "unknown" : this._ApiEndpoint;
227 var textFormat = "API {0}/{1}";
228 this.Text = string.Format(textFormat, remainCountText, maxCountText);
230 var toolTipTextFormat =
231 "API rest {0} {1}/{2}" + Environment.NewLine +
232 "(reset after {3} minutes)";
234 this.ToolTipText = string.Format(toolTipTextFormat, endpointText, remainCountText, maxCountText, minuteText);