Skip to content

Commit 01b623d

Browse files
committed
Implement asynchronous startup with splash screen
- Adds a splash screen to provide visual feedback during application startup. - The initialization logic has been refactored to be fully asynchronous. Heavy tasks, such as database loading, are now executed on a background thread.
1 parent e8eb5c9 commit 01b623d

File tree

5 files changed

+104
-50
lines changed

5 files changed

+104
-50
lines changed

src/CodeSnip/App.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
5-
xmlns:local="clr-namespace:CodeSnip"
6-
StartupUri="MainWindow.xaml" >
5+
xmlns:local="clr-namespace:CodeSnip" >
76
<Application.Resources>
87
<ResourceDictionary>
98
<ResourceDictionary.MergedDictionaries>

src/CodeSnip/App.xaml.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
using ControlzEx.Theming;
2-
using System.Windows;
3-
using System.Windows.Media;
1+
using System.Windows;
42

53
namespace CodeSnip
64
{
75
public partial class App : Application
86
{
7+
protected override async void OnStartup(StartupEventArgs e)
8+
{
9+
base.OnStartup(e);
910

10-
//protected override void OnStartup(StartupEventArgs e)
11-
//{
12-
// base.OnStartup(e);
11+
var splashScreen = new Views.SplashScreenView.SplashScreen();
12+
splashScreen.Show();
1313

14-
// ThemeManager.Current.AddTheme(new Theme("CustomDarkRed", "CustomDarkRed", "Dark", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));
15-
// ThemeManager.Current.AddTheme(new Theme("CustomLightRed", "CustomLightRed", "Light", "Red", Colors.DarkRed, Brushes.DarkRed, true, false));
14+
var mainWindow = new MainWindow();
15+
await PerformInitializationAsync(mainWindow);
16+
MainWindow = mainWindow;
17+
mainWindow.Show();
1618

17-
// ThemeManager.Current.AddTheme(RuntimeThemeGenerator.Current.GenerateRuntimeTheme("Dark", Colors.Red));
18-
// ThemeManager.Current.AddTheme(RuntimeThemeGenerator.Current.GenerateRuntimeTheme("Light", Colors.Red));
19-
//}
19+
splashScreen.Close();
20+
}
21+
private async Task PerformInitializationAsync(MainWindow mainWindow)
22+
{
2023

24+
if (mainWindow.DataContext is MainViewModel mainViewModel)
25+
{
26+
await mainViewModel.InitializeAsync();
27+
}
28+
29+
}
2130
}
2231

2332
}

src/CodeSnip/MainViewModel.cs

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,13 @@ public MainViewModel(IFlyoutService flyoutService)
112112
_flyoutService = flyoutService;
113113
try
114114
{
115-
116-
_databaseService.InitializeDatabaseIfNeeded();
117-
118115
_menuOpenIcon = Application.Current.Resources["MenuOpen"] as Geometry;
119116
_menuCloseIcon = Application.Current.Resources["MenuClose"] as Geometry;
120117
if (_menuOpenIcon == null || _menuCloseIcon == null)
121118
{
122119
throw new InvalidOperationException("Icons not found in resources.");
123120
}
124-
IsLoadSnippetEnabled = !settingsService.LoadOnStartup;
125-
ShowEmptyLanguages = settingsService.ShowEmptyLanguages;
126-
ShowEmptyCategories = settingsService.ShowEmptyCategories;
127121

128-
if (settingsService.LoadOnStartup)
129-
{
130-
LoadSnippets();
131-
if (settingsService.LastSnippet != null)
132-
{
133-
RestoreSelectedSnippetState(settingsService.LastSnippet);
134-
}
135-
}
136-
IsFilteringEnabled = settingsService.EnableFiltering;
137-
EnableBraceStyleFolding = settingsService.EnableBraceStyleFolding;
138-
EnablePythonFolding = settingsService.EnablePythonFolding;
139-
EnableXmlFolding = settingsService.EnableXmlFolding;
140122
SplitViewOpenPaneLength = settingsService.PanelLength;
141123
WindowX = settingsService.WindowX;
142124
WindowY = settingsService.WindowY;
@@ -148,6 +130,13 @@ public MainViewModel(IFlyoutService flyoutService)
148130
opt.HighlightCurrentLine = settingsService.HighlightLine;
149131
opt.IndentationSize = settingsService.IntendationSize;
150132

133+
IsFilteringEnabled = settingsService.EnableFiltering;
134+
EnableBraceStyleFolding = settingsService.EnableBraceStyleFolding;
135+
EnablePythonFolding = settingsService.EnablePythonFolding;
136+
EnableXmlFolding = settingsService.EnableXmlFolding;
137+
ShowEmptyLanguages = settingsService.ShowEmptyLanguages;
138+
ShowEmptyCategories = settingsService.ShowEmptyCategories;
139+
151140
}
152141
catch (InvalidOperationException ex)
153142
{
@@ -159,6 +148,47 @@ public MainViewModel(IFlyoutService flyoutService)
159148
}
160149

161150
}
151+
public async Task InitializeAsync()
152+
{
153+
await Task.Run(() => _databaseService.InitializeDatabaseIfNeeded());
154+
155+
IsLoadSnippetEnabled = !settingsService.LoadOnStartup;
156+
157+
if (settingsService.LoadOnStartup)
158+
{
159+
var languages = await Task.Run(() => _databaseService.GetSnippets());
160+
161+
PopulateLanguagesCollection(languages);
162+
163+
if (settingsService.LastSnippet != null && settingsService.LoadOnStartup)
164+
{
165+
RestoreSelectedSnippetState(settingsService.LastSnippet);
166+
}
167+
}
168+
}
169+
170+
private void PopulateLanguagesCollection(IEnumerable<Language> languages)
171+
{
172+
Languages.Clear();
173+
foreach (var lang in languages)
174+
{
175+
bool languageHasAnySnippets = false;
176+
foreach (var cat in lang.Categories)
177+
{
178+
bool categoryHasSnippets = cat.Snippets.Any();
179+
if (categoryHasSnippets)
180+
{
181+
languageHasAnySnippets = true;
182+
}
183+
184+
// A category is visible if the setting is on, OR if it has snippets.
185+
cat.IsVisible = ShowEmptyCategories || categoryHasSnippets;
186+
}
187+
// A language is visible if the setting is on, OR if it has any snippets.
188+
lang.IsVisible = ShowEmptyLanguages || languageHasAnySnippets;
189+
Languages.Add(lang);
190+
}
191+
}
162192

163193
// Call from the MainWindow constructor
164194
public void InitializeEditor(TextEditor textEditor)
@@ -371,7 +401,7 @@ private void OpenSettings()
371401
tmpSnippet.Id);
372402
}
373403
}
374-
404+
375405
settingsService.SaveSettings();
376406
});
377407
}
@@ -581,24 +611,7 @@ public void OnWindowClosing(CancelEventArgs e)
581611
public void LoadSnippets()
582612
{
583613
var languages = _databaseService.GetSnippets();
584-
Languages.Clear();
585-
foreach (var lang in languages)
586-
{
587-
bool languageHasAnySnippets = false;
588-
foreach (var cat in lang.Categories)
589-
{
590-
bool categoryHasSnippets = cat.Snippets.Any();
591-
if (categoryHasSnippets)
592-
{
593-
languageHasAnySnippets = true;
594-
}
595-
// A category is visible if the setting is on, OR if it has snippets.
596-
cat.IsVisible = ShowEmptyCategories || categoryHasSnippets;
597-
}
598-
// A language is visible if the setting is on, OR if it has any snippets.
599-
lang.IsVisible = ShowEmptyLanguages || languageHasAnySnippets;
600-
Languages.Add(lang);
601-
}
614+
PopulateLanguagesCollection(languages);
602615
}
603616

604617
private void SaveSettings()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<mah:MetroWindow x:Class="CodeSnip.Views.SplashScreenView.SplashScreen"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
7+
mc:Ignorable="d"
8+
Height="110" Width="110"
9+
WindowStyle="None"
10+
AllowsTransparency="True"
11+
Background="Transparent"
12+
WindowStartupLocation="CenterScreen"
13+
ShowInTaskbar="False"
14+
Topmost="True"
15+
ShowTitleBar="False" SizeToContent="WidthAndHeight" BorderBrush="Transparent"
16+
ShowCloseButton="False" ShowMaxRestoreButton="False" ShowMinButton="False" >
17+
<Grid>
18+
<mah:ProgressRing IsActive="True" Width="100" Height="100" IsLarge="True" />
19+
</Grid>
20+
</mah:MetroWindow>
21+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using MahApps.Metro.Controls;
2+
3+
namespace CodeSnip.Views.SplashScreenView
4+
{
5+
public partial class SplashScreen : MetroWindow
6+
{
7+
public SplashScreen()
8+
{
9+
InitializeComponent();
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)