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.
23 using System.Collections.Generic;
26 using System.Windows.Forms;
28 using System.Windows.Forms.Design;
29 using System.ComponentModel;
35 /// API 実行回数制限に到達するまでの目安を表示する ToolStripItem
37 [ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All)]
38 public class ToolStripAPIGauge : ToolStripStatusLabel
40 public ToolStripAPIGauge()
44 this.DisplayStyle = ToolStripItemDisplayStyle.Text;
51 [RefreshProperties(RefreshProperties.Repaint)]
52 public int GaugeHeight
54 get => this._GaugeHeight;
57 this._GaugeHeight = value;
59 this.UpdateGaugeBounds();
63 private int _GaugeHeight = 8;
69 public ApiLimit ApiLimit
71 get => this._ApiLimit;
74 this._ApiLimit = value;
76 this.UpdateRemainMinutes();
78 this.UpdateGaugeBounds();
82 private ApiLimit _ApiLimit = null;
88 public string ApiEndpoint
90 get => this._ApiEndpoint;
93 if (string.IsNullOrEmpty(value))
96 this._ApiEndpoint = null;
101 var apiLimit = MyCommon.TwitterApiInfo.AccessLimit[value];
103 if (this._ApiEndpoint != value)
105 // ApiEndpointが変更されているので更新する
106 this._ApiEndpoint = value;
107 this.ApiLimit = apiLimit;
111 // ApiLimitが変更されていれば更新する
112 if (this._ApiLimit == null ||
113 !this._ApiLimit.Equals(apiLimit))
115 this.ApiLimit = apiLimit;
120 private string _ApiEndpoint = null;
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public new string Text
127 set => base.Text = value;
131 [EditorBrowsable(EditorBrowsableState.Never)]
132 public new string ToolTipText
134 get => base.ToolTipText;
135 set => base.ToolTipText = value;
138 [DefaultValue(ToolStripItemDisplayStyle.Text)]
139 [RefreshProperties(RefreshProperties.Repaint)]
140 public new ToolStripItemDisplayStyle DisplayStyle
142 get => base.DisplayStyle;
143 set => base.DisplayStyle = value;
146 protected double remainMinutes = -1;
148 protected virtual void UpdateRemainMinutes()
150 if (this._ApiLimit != null)
151 this.remainMinutes = (this._ApiLimit.AccessLimitResetDate - DateTimeUtc.Now).TotalMinutes;
153 this.remainMinutes = -1;
156 protected override void OnPaint(PaintEventArgs e)
160 if (this.apiGaugeBounds != Rectangle.Empty)
161 g.FillRectangle(Brushes.LightBlue, this.apiGaugeBounds);
163 if (this.timeGaugeBounds != Rectangle.Empty)
164 g.FillRectangle(Brushes.LightPink, this.timeGaugeBounds);
169 #region from Tween v1.1.0.0
171 // The code in this region block is based on code written by the following authors:
172 // (C) 2010 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
173 // (C) 2010 Moz (@syo68k)
175 internal Rectangle apiGaugeBounds = Rectangle.Empty;
176 internal Rectangle timeGaugeBounds = Rectangle.Empty;
178 protected virtual void UpdateGaugeBounds()
180 if (this._ApiLimit == null || this._GaugeHeight < 1)
182 this.apiGaugeBounds = Rectangle.Empty;
183 this.timeGaugeBounds = Rectangle.Empty;
187 var apiGaugeValue = (double)this._ApiLimit.AccessLimitRemain / this._ApiLimit.AccessLimitCount;
188 this.apiGaugeBounds = new Rectangle(
190 (this.Height - this._GaugeHeight * 2) / 2,
191 (int)(this.Width * apiGaugeValue),
195 var timeGaugeValue = this.remainMinutes >= 15 ? 1.00 : this.remainMinutes / 15;
196 this.timeGaugeBounds = new Rectangle(
198 this.apiGaugeBounds.Top + this._GaugeHeight,
199 (int)(this.Width * timeGaugeValue),
204 protected virtual void UpdateText()
206 string remainCountText;
210 if (this._ApiLimit == null || this.remainMinutes < 0)
212 remainCountText = "???";
213 maxCountText = "???";
218 remainCountText = this._ApiLimit.AccessLimitRemain.ToString();
219 maxCountText = this._ApiLimit.AccessLimitCount.ToString();
220 minuteText = Math.Ceiling(this.remainMinutes).ToString();
223 var endpointText = string.IsNullOrEmpty(this._ApiEndpoint) ? "unknown" : this._ApiEndpoint;
225 var textFormat = "API {0}/{1}";
226 this.Text = string.Format(textFormat, remainCountText, maxCountText);
228 var toolTipTextFormat =
229 "API rest {0} {1}/{2}" + Environment.NewLine +
230 "(reset after {3} minutes)";
232 this.ToolTipText = String.Format(toolTipTextFormat, endpointText, remainCountText, maxCountText, minuteText);