Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.PluginInfrastructure/3.3/Starter/SplashScreen.cs @ 17514

Last change on this file since 17514 was 16102, checked in by abeham, 6 years ago

#2213: some pending changes exploring the topic

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using System.Reflection;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure.Manager;
27
28namespace HeuristicLab.PluginInfrastructure.Starter {
29  internal partial class SplashScreen : Form {
30    private const int FADE_INTERVAL = 50;
31    private Timer fadeTimer;
32    private int initialInterval;
33    private PluginManager pluginManager;
34
35    internal SplashScreen() {
36      InitializeComponent();
37    }
38
39    internal SplashScreen(PluginManager manager, int initialInterval)
40      : this() {
41      this.initialInterval = initialInterval;
42      this.pluginManager = manager;
43
44      RegisterPluginManagerEventHandlers();
45
46      versionLabel.Text = "Version " + AssemblyHelpers.GetFileVersion(GetType().Assembly);
47      infoLabel.Text = "";
48
49      var attr = (AssemblyCopyrightAttribute)this.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false).Single();
50      copyrightLabel.Text = "Copyright " + attr.Copyright;
51
52      fadeTimer = new Timer();
53      fadeTimer.Tick += fadeTimer_Elapsed;
54      fadeTimer.Interval = initialInterval;
55    }
56
57    #region events
58    private void RegisterPluginManagerEventHandlers() {
59      pluginManager.ApplicationStarted += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
60      pluginManager.ApplicationStarting += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
61      pluginManager.Initializing += new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
62      pluginManager.Initialized += new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
63      pluginManager.PluginLoaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
64      pluginManager.PluginUnloaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
65    }
66
67    private void DeregisterPluginManagerEventHandlers() {
68      pluginManager.ApplicationStarted -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
69      pluginManager.ApplicationStarting -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
70      pluginManager.Initializing -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
71      pluginManager.Initialized -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
72      pluginManager.PluginLoaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
73      pluginManager.PluginUnloaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
74    }
75
76    private void manager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
77      SafeUpdateMessage("Unloaded " + e.Entity);
78    }
79
80    private void manager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
81      SafeUpdateMessage("Loaded " + e.Entity);
82    }
83
84    private void manager_Initialized(object sender, PluginInfrastructureEventArgs e) {
85      SafeUpdateMessage("Initialized");
86    }
87
88    private void manager_Initializing(object sender, PluginInfrastructureEventArgs e) {
89      SafeUpdateMessage("Initializing");
90    }
91
92    private void manager_ApplicationStarting(object sender, PluginInfrastructureEventArgs e) {
93      SafeUpdateMessage("Starting " + e.Entity);
94    }
95
96    private void manager_ApplicationStarted(object sender, PluginInfrastructureEventArgs e) {
97      SafeUpdateMessage("Started " + e.Entity);
98    }
99    // called from event handlers
100    private void SafeUpdateMessage(string msg) {
101      try {
102        if (InvokeRequired) {
103          Invoke((Action<string>)UpdateMessage, msg);
104        } else {
105          UpdateMessage(msg);
106        }
107      } catch (ObjectDisposedException) { }
108    }
109
110    // each tick of the timer reduce opacity and restart timer
111    private void fadeTimer_Elapsed(object sender, EventArgs e) {
112      // only called from local timer: no need to invoke here
113      FadeOut();
114    }
115    #endregion
116
117    public void Show(string initialText) {
118      if (InvokeRequired) Invoke((Action<string>)Show, initialText);
119      else {
120        Opacity = 1;
121        infoLabel.Text = initialText;
122        ResetFadeTimer();
123        Show();
124      }
125    }
126
127    public void Show(IWin32Window owner, string initialText) {
128      if (InvokeRequired) Invoke((Action<IWin32Window, string>)Show, owner, initialText);
129      else {
130        Opacity = 1;
131        infoLabel.Text = initialText;
132        ResetFadeTimer();
133        Show(owner);
134      }
135    }
136
137    private void ResetFadeTimer() {
138      // wait initialInterval again for the first tick
139      fadeTimer.Stop();
140      fadeTimer.Interval = initialInterval;
141      fadeTimer.Start();
142    }
143
144    private void UpdateMessage(string msg) {
145      ResetFadeTimer();
146      infoLabel.Text = msg;
147      Application.DoEvents(); // force immediate update of splash screen control
148    }
149
150    // reduces opacity of the splashscreen one step and restarts the fade-timer
151    private void FadeOut() {
152      fadeTimer.Stop();
153      fadeTimer.Interval = FADE_INTERVAL;
154      if (this.Opacity > 0) {
155        Opacity -= 0.1;
156        fadeTimer.Start();
157      } else {
158        Opacity = 0;
159        fadeTimer.Stop();
160        Hide();
161      }
162    }
163
164    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
165      // deregister events when form is closing
166      DeregisterPluginManagerEventHandlers();
167      base.OnClosing(e);
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.