1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using Antmicro.Renode.Utilities;
9 using Xwt;
10 using Antmicro.Renode.UI;
11 
12 namespace Antmicro.Renode.UI.Progress
13 {
14     public class ProgressWidget : Widget
15     {
ProgressWidget()16         public ProgressWidget()
17         {
18             ProgressMonitor = new ProgressMonitor { Handler = new ProgressWidgetMonitor(this) };
19 
20             progressBar = new ProgressBar();
21             text = new Label();
22             var box = new HBox();
23             box.PackStart(progressBar);
24             box.PackStart(text, true);
25             Content = box;
26         }
27 
28         public ProgressMonitor ProgressMonitor { get; private set; }
29 
30         private readonly Label text;
31         private readonly ProgressBar progressBar;
32 
33         private class ProgressWidgetMonitor : IProgressMonitorHandler
34         {
ProgressWidgetMonitor(ProgressWidget widget)35             public ProgressWidgetMonitor(ProgressWidget widget)
36             {
37                 this.widget = widget;
38             }
39 
Finish(int id)40             public void Finish(int id)
41             {
42                 ApplicationExtensions.InvokeInUIThread(() =>
43                 {
44                     widget.Visible = false;
45                 });
46             }
47 
Update(int id, string description, int? progress)48             public void Update(int id, string description, int? progress)
49             {
50                 ApplicationExtensions.InvokeInUIThread(() =>
51                 {
52                     widget.Visible = true;
53                     widget.text.Text = description;
54                     widget.text.Visible = !string.IsNullOrEmpty(description);
55 
56                     if(progress.HasValue)
57                     {
58                         widget.progressBar.Indeterminate = false;
59                         widget.progressBar.Fraction = progress.Value / 100.0;
60                     }
61                     else
62                     {
63                         widget.progressBar.Indeterminate = true;
64                     }
65                 });
66             }
67 
68             private readonly ProgressWidget widget;
69         }
70     }
71 }
72 
73