ProgressBar.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System.Text;
  2. using System;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Specifies the style that a <see cref="ProgressBar"/> uses to indicate the progress of an operation.
  6. /// </summary>
  7. public enum ProgressBarStyle {
  8. /// <summary>
  9. /// Indicates progress by increasing the number of segmented blocks in a <see cref="ProgressBar"/>.
  10. /// </summary>
  11. Blocks,
  12. /// <summary>
  13. /// Indicates progress by increasing the size of a smooth, continuous bar in a <see cref="ProgressBar"/>.
  14. /// </summary>
  15. Continuous,
  16. /// <summary>
  17. /// Indicates progress by continuously scrolling a block across a <see cref="ProgressBar"/> in a marquee fashion.
  18. /// </summary>
  19. MarqueeBlocks,
  20. /// <summary>
  21. /// Indicates progress by continuously scrolling a block across a <see cref="ProgressBar"/> in a marquee fashion.
  22. /// </summary>
  23. MarqueeContinuous
  24. }
  25. /// <summary>
  26. ///Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.
  27. /// </summary>
  28. public enum ProgressBarFormat {
  29. /// <summary>
  30. /// A simple visual presentation showing only the progress bar.
  31. /// </summary>
  32. Simple,
  33. /// <summary>
  34. /// A simple visual presentation showing the progress bar overlaid with the percentage.
  35. /// </summary>
  36. SimplePlusPercentage,
  37. }
  38. /// <summary>
  39. /// A Progress Bar view that can indicate progress of an activity visually.
  40. /// </summary>
  41. /// <remarks>
  42. /// <para>
  43. /// <see cref="ProgressBar"/> can operate in two modes, percentage mode, or
  44. /// activity mode. The progress bar starts in percentage mode and
  45. /// setting the Fraction property will reflect on the UI the progress
  46. /// made so far. Activity mode is used when the application has no
  47. /// way of knowing how much time is left, and is started when the <see cref="Pulse"/> method is called.
  48. /// Call <see cref="Pulse"/> repeatedly as progress is made.
  49. /// </para>
  50. /// </remarks>
  51. public class ProgressBar : View {
  52. bool _isActivity;
  53. int [] _activityPos;
  54. int _delta;
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="ProgressBar"/> class, starts in percentage mode and uses relative layout.
  57. /// </summary>
  58. public ProgressBar ()
  59. {
  60. SetInitialProperties ();
  61. }
  62. void SetInitialProperties ()
  63. {
  64. Height = 1; // This will be updated when Bounds is updated in ProgressBar_LayoutStarted
  65. CanFocus = false;
  66. _fraction = 0;
  67. LayoutStarted += ProgressBar_LayoutStarted;
  68. Initialized += ProgressBar_Initialized;
  69. }
  70. void ProgressBar_Initialized (object sender, EventArgs e)
  71. {
  72. ColorScheme = new ColorScheme (ColorScheme ?? SuperView?.ColorScheme ?? Colors.ColorSchemes ["Base"]) {
  73. HotNormal = new Attribute (Color.BrightGreen, Color.Gray)
  74. };
  75. }
  76. void ProgressBar_LayoutStarted (object sender, EventArgs e)
  77. {
  78. Bounds = new Rect (Bounds.Location, new Size (Bounds.Width, 1));
  79. }
  80. float _fraction;
  81. /// <summary>
  82. /// Gets or sets the <see cref="ProgressBar"/> fraction to display, must be a value between 0 and 1.
  83. /// </summary>
  84. /// <value>The fraction representing the progress.</value>
  85. public float Fraction {
  86. get => _fraction;
  87. set {
  88. _fraction = Math.Min (value, 1);
  89. _isActivity = false;
  90. SetNeedsDisplay ();
  91. }
  92. }
  93. ProgressBarStyle _progressBarStyle;
  94. /// <summary>
  95. /// Gets/Sets the progress bar style based on the <see cref="Terminal.Gui.ProgressBarStyle"/>
  96. /// </summary>
  97. public ProgressBarStyle ProgressBarStyle {
  98. get => _progressBarStyle;
  99. set {
  100. _progressBarStyle = value;
  101. switch (value) {
  102. case ProgressBarStyle.Blocks:
  103. SegmentCharacter = CM.Glyphs.BlocksMeterSegment;
  104. break;
  105. case ProgressBarStyle.Continuous:
  106. SegmentCharacter = CM.Glyphs.ContinuousMeterSegment;
  107. break;
  108. case ProgressBarStyle.MarqueeBlocks:
  109. SegmentCharacter = CM.Glyphs.BlocksMeterSegment;
  110. break;
  111. case ProgressBarStyle.MarqueeContinuous:
  112. SegmentCharacter = CM.Glyphs.ContinuousMeterSegment;
  113. break;
  114. }
  115. SetNeedsDisplay ();
  116. }
  117. }
  118. /// <summary>
  119. /// Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.
  120. /// </summary>
  121. public ProgressBarFormat ProgressBarFormat { get; set; }
  122. private Rune _segmentCharacter = CM.Glyphs.BlocksMeterSegment;
  123. /// <summary>
  124. /// Segment indicator for meter views.
  125. /// </summary>
  126. public Rune SegmentCharacter {
  127. get => _segmentCharacter;
  128. set {
  129. _segmentCharacter = value;
  130. SetNeedsDisplay ();
  131. }
  132. }
  133. /// <summary>
  134. /// Gets or sets the text displayed on the progress bar. If set to an empty string and <see cref="ProgressBarFormat"/> is
  135. /// <see cref="ProgressBarFormat.SimplePlusPercentage"/> the percentage will be displayed.
  136. /// If <see cref="ProgressBarStyle"/> is a marquee style, the text will be displayed.
  137. /// </summary>
  138. public override string Text {
  139. get => string.IsNullOrEmpty (base.Text) ? $"{_fraction * 100:F0}%" : base.Text;
  140. set {
  141. if (ProgressBarStyle == ProgressBarStyle.MarqueeBlocks || ProgressBarStyle == ProgressBarStyle.MarqueeContinuous) {
  142. base.Text = value;
  143. }
  144. }
  145. }
  146. bool _bidirectionalMarquee = true;
  147. /// <summary>
  148. /// Specifies if the <see cref="ProgressBarStyle.MarqueeBlocks"/> or the
  149. /// <see cref="ProgressBarStyle.MarqueeContinuous"/> styles is unidirectional
  150. /// or bidirectional.
  151. /// </summary>
  152. public bool BidirectionalMarquee {
  153. get => _bidirectionalMarquee;
  154. set {
  155. _bidirectionalMarquee = value;
  156. SetNeedsDisplay ();
  157. }
  158. }
  159. /// <summary>
  160. /// Notifies the <see cref="ProgressBar"/> that some progress has taken place.
  161. /// </summary>
  162. /// <remarks>
  163. /// If the <see cref="ProgressBar"/> is percentage mode, it switches to activity
  164. /// mode. If is in activity mode, the marker is moved.
  165. /// </remarks>
  166. public void Pulse ()
  167. {
  168. if (_activityPos == null) {
  169. PopulateActivityPos ();
  170. }
  171. if (!_isActivity) {
  172. _isActivity = true;
  173. _delta = 1;
  174. } else {
  175. for (var i = 0; i < _activityPos.Length; i++) {
  176. _activityPos [i] += _delta;
  177. }
  178. if (_activityPos [^1] < 0) {
  179. for (var i = 0; i < _activityPos.Length; i++) {
  180. _activityPos [i] = i - _activityPos.Length + 2;
  181. }
  182. _delta = 1;
  183. } else if (_activityPos [0] >= Bounds.Width) {
  184. if (_bidirectionalMarquee) {
  185. for (var i = 0; i < _activityPos.Length; i++) {
  186. _activityPos [i] = Bounds.Width + i - 2;
  187. }
  188. _delta = -1;
  189. } else {
  190. PopulateActivityPos ();
  191. }
  192. }
  193. }
  194. SetNeedsDisplay ();
  195. }
  196. ///<inheritdoc/>
  197. public override void OnDrawContent (Rect contentArea)
  198. {
  199. Driver.SetAttribute (GetHotNormalColor ());
  200. Move (0, 0);
  201. if (_isActivity) {
  202. for (int i = 0; i < Bounds.Width; i++)
  203. if (Array.IndexOf (_activityPos, i) != -1) {
  204. Driver.AddRune (SegmentCharacter);
  205. } else {
  206. Driver.AddRune ((Rune)' ');
  207. }
  208. } else {
  209. int mid = (int)(_fraction * Bounds.Width);
  210. int i;
  211. for (i = 0; i < mid & i < Bounds.Width; i++) {
  212. Driver.AddRune (SegmentCharacter);
  213. }
  214. for (; i < Bounds.Width; i++) {
  215. Driver.AddRune ((Rune)' ');
  216. }
  217. }
  218. if (ProgressBarFormat != ProgressBarFormat.Simple && !_isActivity) {
  219. var tf = new TextFormatter () {
  220. Alignment = TextAlignment.Centered,
  221. Text = Text
  222. };
  223. Attribute attr = new Attribute (ColorScheme.HotNormal.Foreground, ColorScheme.HotNormal.Background);
  224. if (_fraction > .5) {
  225. attr = new Attribute (ColorScheme.HotNormal.Background, ColorScheme.HotNormal.Foreground);
  226. }
  227. tf?.Draw (BoundsToScreen (Bounds),
  228. attr,
  229. ColorScheme.Normal,
  230. SuperView?.BoundsToScreen (SuperView.Bounds) ?? default,
  231. fillRemaining: false);
  232. }
  233. }
  234. void PopulateActivityPos ()
  235. {
  236. _activityPos = new int [Math.Min (Frame.Width / 3, 5)];
  237. for (var i = 0; i < _activityPos.Length; i++) {
  238. _activityPos [i] = i - _activityPos.Length + 1;
  239. }
  240. }
  241. ///<inheritdoc/>
  242. public override bool OnEnter (View view)
  243. {
  244. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  245. return base.OnEnter (view);
  246. }
  247. }