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 : ToolStripButton
40 public ToolStripAPIGauge()
43 this.Text = "API ???/???";
44 this.ToolTipText = "API rest ???/???" + Environment.NewLine + "(reset after ??? minutes)";
46 this.DisplayStyle = ToolStripItemDisplayStyle.Text;
53 [RefreshProperties(RefreshProperties.Repaint)]
54 public int GaugeHeight
56 get { return this._GaugeHeight; }
59 this._GaugeHeight = value;
61 this.UpdateGaugeBounds();
65 private int _GaugeHeight = 8;
71 public ApiLimit ApiLimit
73 get { return this._ApiLimit; }
76 this._ApiLimit = value;
78 this.UpdateRemainMinutes();
80 this.UpdateGaugeBounds();
84 private ApiLimit _ApiLimit = null;
87 [EditorBrowsable(EditorBrowsableState.Never)]
88 public new string Text
90 get { return base.Text; }
91 set { base.Text = value; }
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public new string ToolTipText
98 get { return base.ToolTipText; }
99 set { base.ToolTipText = value; }
102 [DefaultValue(ToolStripItemDisplayStyle.Text)]
103 [RefreshProperties(RefreshProperties.Repaint)]
104 public new ToolStripItemDisplayStyle DisplayStyle
106 get { return base.DisplayStyle; }
107 set { base.DisplayStyle = value; }
110 protected double remainMinutes = -1;
112 protected virtual void UpdateRemainMinutes()
114 if (this._ApiLimit != null)
115 this.remainMinutes = (this._ApiLimit.AccessLimitResetDate - DateTime.Now).TotalMinutes;
117 this.remainMinutes = -1;
120 protected override void OnPaint(PaintEventArgs e)
124 if (this.apiGaugeBounds != Rectangle.Empty)
125 g.FillRectangle(Brushes.LightBlue, this.apiGaugeBounds);
127 if (this.timeGaugeBounds != Rectangle.Empty)
128 g.FillRectangle(Brushes.LightPink, this.timeGaugeBounds);
133 #region from Tween v1.1.0.0
135 // The code in this region block is based on code written by the following authors:
136 // (C) 2010 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
137 // (C) 2010 Moz (@syo68k)
139 internal Rectangle apiGaugeBounds = Rectangle.Empty;
140 internal Rectangle timeGaugeBounds = Rectangle.Empty;
142 protected virtual void UpdateGaugeBounds()
144 if (this._ApiLimit == null || this._GaugeHeight < 1)
146 this.apiGaugeBounds = Rectangle.Empty;
147 this.timeGaugeBounds = Rectangle.Empty;
151 var apiGaugeValue = (double)this._ApiLimit.AccessLimitRemain / this._ApiLimit.AccessLimitCount;
152 this.apiGaugeBounds = new Rectangle(
154 (this.Height - this._GaugeHeight * 2) / 2,
155 (int)(this.Width * apiGaugeValue),
159 var timeGaugeValue = this.remainMinutes >= 60 ? 1.00 : this.remainMinutes / 60;
160 this.timeGaugeBounds = new Rectangle(
162 this.apiGaugeBounds.Top + this._GaugeHeight,
163 (int)(this.Width * timeGaugeValue),
168 protected virtual void UpdateText()
170 string remainCountText;
174 if (this._ApiLimit == null)
176 remainCountText = "???";
177 maxCountText = "???";
182 remainCountText = this._ApiLimit.AccessLimitRemain.ToString();
183 maxCountText = this._ApiLimit.AccessLimitCount.ToString();
184 minuteText = Math.Ceiling(this.remainMinutes).ToString();
187 var textFormat = "API {0}/{1}";
188 this.Text = string.Format(textFormat, remainCountText, maxCountText);
190 var toolTipTextFormat =
191 "API rest {0}/{1}" + Environment.NewLine +
192 "(reset after {2} minutes)";
194 this.ToolTipText = String.Format(toolTipTextFormat, remainCountText, maxCountText, minuteText);