source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.PluginInfrastructure.UI/SplashScreen.cs @ 13353

Last change on this file since 13353 was 13353, checked in by gkronber, 4 years ago

#2522: added assembly attribute for release code name and updated AboutDialog as well as SplashScreen to show information of the EntryAssembly (in our case HeuristicLab-3.3.exe)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.UI {
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      var entryAssembly = Assembly.GetEntryAssembly();
45      RegisterPluginManagerEventHandlers();
46
47      versionLabel.Text = "Version " + entryAssembly.GetFileVersion() + " " 
48        + entryAssembly.GetCustomAttributes().OfType<AssemblyInformationalVersionAttribute>().First().InformationalVersion; // code name
49      infoLabel.Text = "";
50
51      var attr = entryAssembly.GetCustomAttributes().OfType<AssemblyCopyrightAttribute>().Single();
52      copyrightLabel.Text = "Copyright " + attr.Copyright;
53
54      fadeTimer = new Timer();
55      fadeTimer.Tick += fadeTimer_Elapsed;
56      fadeTimer.Interval = initialInterval;
57    }
58
59    #region events
60    private void RegisterPluginManagerEventHandlers() {
61      pluginManager.ApplicationStarted += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
62      pluginManager.ApplicationStarting += new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
63      pluginManager.Initializing += new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
64      pluginManager.Initialized += new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
65      pluginManager.PluginLoaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
66      pluginManager.PluginUnloaded += new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
67    }
68
69    private void DeregisterPluginManagerEventHandlers() {
70      pluginManager.ApplicationStarted -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarted);
71      pluginManager.ApplicationStarting -= new EventHandler<PluginInfrastructureEventArgs>(manager_ApplicationStarting);
72      pluginManager.Initializing -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initializing);
73      pluginManager.Initialized -= new EventHandler<PluginInfrastructureEventArgs>(manager_Initialized);
74      pluginManager.PluginLoaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginLoaded);
75      pluginManager.PluginUnloaded -= new EventHandler<PluginInfrastructureEventArgs>(manager_PluginUnloaded);
76    }
77
78    private void manager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
79      SafeUpdateMessage("Unloaded " + e.Entity);
80    }
81
82    private void manager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
83      SafeUpdateMessage("Loaded " + e.Entity);
84    }
85
86    private void manager_Initialized(object sender, PluginInfrastructureEventArgs e) {
87      SafeUpdateMessage("Initialized");
88    }
89
90    private void manager_Initializing(object sender, PluginInfrastructureEventArgs e) {
91      SafeUpdateMessage("Initializing");
92    }
93
94    private void manager_ApplicationStarting(object sender, PluginInfrastructureEventArgs e) {
95      SafeUpdateMessage("Starting " + e.Entity);
96    }
97
98    private void manager_ApplicationStarted(object sender, PluginInfrastructureEventArgs e) {
99      SafeUpdateMessage("Started " + e.Entity);
100    }
101    // called from event handlers
102    private void SafeUpdateMessage(string msg) {
103      try {
104        Invoke((Action<string>)UpdateMessage, msg);
105      } catch (ObjectDisposedException) { }
106    }
107
108    // each tick of the timer reduce opacity and restart timer
109    private void fadeTimer_Elapsed(object sender, EventArgs e) {
110      // only called from local timer: no need to invoke here
111      FadeOut();
112    }
113    #endregion
114
115    public void Show(string initialText) {
116      if (InvokeRequired) Invoke((Action<string>)Show, initialText);
117      else {
118        Opacity = 1;
119        infoLabel.Text = initialText;
120        ResetFadeTimer();
121        Show();
122      }
123    }
124
125    public void Show(IWin32Window owner, string initialText) {
126      if (InvokeRequired) Invoke((Action<IWin32Window, string>)Show, owner, initialText);
127      else {
128        Opacity = 1;
129        infoLabel.Text = initialText;
130        ResetFadeTimer();
131        Show(owner);
132      }
133    }
134
135    private void ResetFadeTimer() {
136      // wait initialInterval again for the first tick
137      fadeTimer.Stop();
138      fadeTimer.Interval = initialInterval;
139      fadeTimer.Start();
140    }
141
142    private void UpdateMessage(string msg) {
143      ResetFadeTimer();
144      infoLabel.Text = msg;
145      Application.DoEvents(); // force immediate update of splash screen control
146    }
147
148    // reduces opacity of the splashscreen one step and restarts the fade-timer
149    private void FadeOut() {
150      fadeTimer.Stop();
151      fadeTimer.Interval = FADE_INTERVAL;
152      if (this.Opacity > 0) {
153        Opacity -= 0.1;
154        fadeTimer.Start();
155      } else {
156        Opacity = 0;
157        fadeTimer.Stop();
158        Hide();
159      }
160    }
161
162    protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
163      // deregister events when form is closing
164      DeregisterPluginManagerEventHandlers();
165      base.OnClosing(e);
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.