Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.PluginInfrastructure/3.3/Starter/SplashScreen.cs @ 16079

Last change on this file since 16079 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 6.2 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        Invoke((Action<string>)UpdateMessage, msg);
103      }
104      catch (ObjectDisposedException) { }
105    }
106
107    // each tick of the timer reduce opacity and restart timer
108    private void fadeTimer_Elapsed(object sender, EventArgs e) {
109      // only called from local timer: no need to invoke here
110      FadeOut();
111    }
112    #endregion
113
114    public void Show(string initialText) {
115      if (InvokeRequired) Invoke((Action<string>)Show, initialText);
116      else {
117        Opacity = 1;
118        infoLabel.Text = initialText;
119        ResetFadeTimer();
120        Show();
121      }
122    }
123
124    public void Show(IWin32Window owner, string initialText) {
125      if (InvokeRequired) Invoke((Action<IWin32Window, string>)Show, owner, initialText);
126      else {
127        Opacity = 1;
128        infoLabel.Text = initialText;
129        ResetFadeTimer();
130        Show(owner);
131      }
132    }
133
134    private void ResetFadeTimer() {
135      // wait initialInterval again for the first tick
136      fadeTimer.Stop();
137      fadeTimer.Interval = initialInterval;
138      fadeTimer.Start();
139    }
140
141    private void UpdateMessage(string msg) {
142      ResetFadeTimer();
143      infoLabel.Text = msg;
144      Application.DoEvents(); // force immediate update of splash screen control
145    }
146
147    // reduces opacity of the splashscreen one step and restarts the fade-timer
148    private void FadeOut() {
149      fadeTimer.Stop();
150      fadeTimer.Interval = FADE_INTERVAL;
151      if (this.Opacity > 0) {
152        Opacity -= 0.1;
153        fadeTimer.Start();
154      } else {
155        Opacity = 0;
156        fadeTimer.Stop();
157        Hide();
158      }
159    }
160
161    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
162      // deregister events when form is closing
163      DeregisterPluginManagerEventHandlers();
164      base.OnClosing(e);
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.