1- // Copyright (c) 2024 Files Community
2- // Licensed under the MIT License. See the LICENSE.
3-
41using Microsoft . Extensions . Logging ;
52using Microsoft . UI ;
63using Microsoft . UI . Windowing ;
129using Windows . ApplicationModel . Activation ;
1310using Windows . Globalization ;
1411using Windows . Storage ;
15- using IO = System . IO ;
16- using System . Threading . Tasks ;
12+ using System ;
1713using System . Linq ;
14+ using System . Threading . Tasks ;
15+ using System . IO ;
1816
1917namespace Files . App
2018{
2119 public sealed partial class MainWindow : WinUIEx . WindowEx
2220 {
23- private static MainWindow ? _Instance ;
24- public static MainWindow Instance => _Instance ??= new ( ) ;
21+ private static readonly Lazy < MainWindow > _Instance = new ( ( ) => new MainWindow ( ) ) ;
22+ public static MainWindow Instance => _Instance . Value ;
2523
2624 public nint WindowHandle { get ; }
2725
@@ -45,15 +43,9 @@ public MainWindow()
4543 AppWindow . SetIcon ( AppLifecycleHelper . AppIconPath ) ;
4644 }
4745
48- /// <summary>
49- /// Set the application's culture to match the system's current UI culture.
50- /// This ensures that the correct language is applied to all UI elements.
51- /// </summary>
5246 private void SetAppCulture ( )
5347 {
5448 var culture = CultureInfo . CurrentUICulture ;
55-
56- // Apply the system's UI culture across the application
5749 CultureInfo . DefaultThreadCurrentUICulture = culture ;
5850 CultureInfo . DefaultThreadCurrentCulture = culture ;
5951 ApplicationLanguages . PrimaryLanguageOverride = culture . Name ;
@@ -63,103 +55,135 @@ public void ShowSplashScreen()
6355 {
6456 var rootFrame = EnsureWindowIsInitialized ( ) ;
6557
66- rootFrame ? . Navigate ( typeof ( SplashScreenPage ) ) ;
58+ if ( rootFrame != null )
59+ {
60+ rootFrame . Navigate ( typeof ( SplashScreenPage ) ) ;
61+ }
6762 }
6863
6964 public async Task InitializeApplicationAsync ( object activatedEventArgs )
7065 {
7166 var rootFrame = EnsureWindowIsInitialized ( ) ;
7267
73- if ( rootFrame is null )
68+ if ( rootFrame == null )
7469 return ;
7570
7671 // Set system backdrop
7772 SystemBackdrop = new AppSystemBackdrop ( ) ;
7873
79- switch ( activatedEventArgs )
74+ try
8075 {
81- case ILaunchActivatedEventArgs launchArgs :
82- if ( launchArgs . Arguments is not null &&
83- ( CommandLineParser . SplitArguments ( launchArgs . Arguments , true ) [ 0 ] . EndsWith ( $ "files.exe", StringComparison . OrdinalIgnoreCase )
84- || CommandLineParser . SplitArguments ( launchArgs . Arguments , true ) [ 0 ] . EndsWith ( $ "files", StringComparison . OrdinalIgnoreCase ) ) )
85- {
86- var ppm = CommandLineParser . ParseUntrustedCommands ( launchArgs . Arguments ) ;
87- if ( ppm . IsEmpty ( ) )
88- rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
89- else
90- await InitializeFromCmdLineArgsAsync ( rootFrame , ppm ) ;
91- }
92- else if ( rootFrame . Content is null || rootFrame . Content is SplashScreenPage || ! MainPageViewModel . AppInstances . Any ( ) )
93- {
94- rootFrame . Navigate ( typeof ( MainPage ) , launchArgs . Arguments , new SuppressNavigationTransitionInfo ( ) ) ;
95- }
96- else if ( ! ( string . IsNullOrEmpty ( launchArgs . Arguments ) && MainPageViewModel . AppInstances . Count > 0 ) )
97- {
98- Win32Helper . BringToForegroundEx ( new ( WindowHandle ) ) ;
99- await NavigationHelpers . AddNewTabByPathAsync ( typeof ( ShellPanesPage ) , launchArgs . Arguments , true ) ;
100- }
101- else
102- {
76+ switch ( activatedEventArgs )
77+ {
78+ case ILaunchActivatedEventArgs launchArgs :
79+ await HandleLaunchArgsAsync ( rootFrame , launchArgs ) ;
80+ break ;
81+
82+ case IProtocolActivatedEventArgs protocolArgs :
83+ await HandleProtocolArgsAsync ( rootFrame , protocolArgs ) ;
84+ break ;
85+
86+ case IFileActivatedEventArgs fileArgs :
87+ HandleFileArgs ( rootFrame , fileArgs ) ;
88+ break ;
89+
90+ default :
10391 rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
104- }
105- break ;
92+ break ;
93+ }
94+ }
95+ catch ( Exception ex )
96+ {
97+ // Log or handle exceptions accordingly
98+ Console . WriteLine ( $ "Error during initialization: { ex . Message } ") ;
99+ }
100+ }
101+
102+ private async Task HandleLaunchArgsAsync ( Frame rootFrame , ILaunchActivatedEventArgs launchArgs )
103+ {
104+ var args = CommandLineParser . SplitArguments ( launchArgs . Arguments , true ) ;
105+ if ( args . Length > 0 &&
106+ ( args [ 0 ] . EndsWith ( "files.exe" , StringComparison . OrdinalIgnoreCase ) ||
107+ args [ 0 ] . EndsWith ( "files" , StringComparison . OrdinalIgnoreCase ) ) )
108+ {
109+ var ppm = CommandLineParser . ParseUntrustedCommands ( launchArgs . Arguments ) ;
110+ if ( ppm . IsEmpty ( ) )
111+ {
112+ rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
113+ }
114+ else
115+ {
116+ await InitializeFromCmdLineArgsAsync ( rootFrame , ppm ) ;
117+ }
118+ }
119+ else if ( rootFrame . Content is null || rootFrame . Content is SplashScreenPage || ! MainPageViewModel . AppInstances . Any ( ) )
120+ {
121+ rootFrame . Navigate ( typeof ( MainPage ) , launchArgs . Arguments , new SuppressNavigationTransitionInfo ( ) ) ;
122+ }
123+ else
124+ {
125+ Win32Helper . BringToForegroundEx ( new ( WindowHandle ) ) ;
126+ await NavigationHelpers . AddNewTabByPathAsync ( typeof ( ShellPanesPage ) , launchArgs . Arguments , true ) ;
127+ }
128+ }
106129
107- case IProtocolActivatedEventArgs eventArgs :
108- if ( eventArgs . Uri . AbsoluteUri == "files-uwp:" )
130+ private async Task HandleProtocolArgsAsync ( Frame rootFrame , IProtocolActivatedEventArgs eventArgs )
131+ {
132+ if ( eventArgs . Uri . AbsoluteUri == "files-uwp:" )
133+ {
134+ rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
135+ if ( MainPageViewModel . AppInstances . Count > 0 )
136+ {
137+ Win32Helper . BringToForegroundEx ( new ( WindowHandle ) ) ;
138+ }
139+ }
140+ else
141+ {
142+ var parsedArgs = eventArgs . Uri . Query . TrimStart ( '?' ) . Split ( '=' ) ;
143+ if ( parsedArgs . Length == 2 )
144+ {
145+ var unescapedValue = Uri . UnescapeDataString ( parsedArgs [ 1 ] ) ;
146+ var folder = ( StorageFolder ) await FilesystemTasks . Wrap ( ( ) => StorageFolder . GetFolderFromPathAsync ( unescapedValue ) . AsTask ( ) ) ;
147+ if ( folder != null && ! string . IsNullOrEmpty ( folder . Path ) )
109148 {
110- rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
111- if ( MainPageViewModel . AppInstances . Count > 0 )
112- {
113- Win32Helper . BringToForegroundEx ( new ( WindowHandle ) ) ;
114- }
149+ unescapedValue = folder . Path ;
115150 }
116- else
151+
152+ switch ( parsedArgs [ 0 ] )
117153 {
118- var parsedArgs = eventArgs . Uri . Query . TrimStart ( '?' ) . Split ( '=' ) ;
119- var unescapedValue = Uri . UnescapeDataString ( parsedArgs [ 1 ] ) ;
120- var folder = ( StorageFolder ) await FilesystemTasks . Wrap ( ( ) => StorageFolder . GetFolderFromPathAsync ( unescapedValue ) . AsTask ( ) ) ;
121- if ( folder is not null && ! string . IsNullOrEmpty ( folder . Path ) )
122- {
123- unescapedValue = folder . Path ;
124- }
125- switch ( parsedArgs [ 0 ] )
126- {
127- case "tab" :
128- rootFrame . Navigate ( typeof ( MainPage ) ,
129- new MainPageNavigationArguments ( ) { Parameter = TabBarItemParameter . Deserialize ( unescapedValue ) , IgnoreStartupSettings = true } ,
130- new SuppressNavigationTransitionInfo ( ) ) ;
131- break ;
132-
133- case "folder" :
134- rootFrame . Navigate ( typeof ( MainPage ) ,
135- new MainPageNavigationArguments ( ) { Parameter = unescapedValue , IgnoreStartupSettings = true } ,
136- new SuppressNavigationTransitionInfo ( ) ) ;
137- break ;
138-
139- case "cmd" :
140- var ppm = CommandLineParser . ParseUntrustedCommands ( unescapedValue ) ;
141- if ( ppm . IsEmpty ( ) )
142- rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
143- else
144- await InitializeFromCmdLineArgsAsync ( rootFrame , ppm ) ;
145- break ;
146- default :
154+ case "tab" :
155+ rootFrame . Navigate ( typeof ( MainPage ) , new MainPageNavigationArguments ( ) { Parameter = TabBarItemParameter . Deserialize ( unescapedValue ) , IgnoreStartupSettings = true } , new SuppressNavigationTransitionInfo ( ) ) ;
156+ break ;
157+
158+ case "folder" :
159+ rootFrame . Navigate ( typeof ( MainPage ) , new MainPageNavigationArguments ( ) { Parameter = unescapedValue , IgnoreStartupSettings = true } , new SuppressNavigationTransitionInfo ( ) ) ;
160+ break ;
161+
162+ case "cmd" :
163+ var ppm = CommandLineParser . ParseUntrustedCommands ( unescapedValue ) ;
164+ if ( ppm . IsEmpty ( ) )
147165 rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
148- break ;
149- }
150- }
151- break ;
166+ else
167+ await InitializeFromCmdLineArgsAsync ( rootFrame , ppm ) ;
168+ break ;
152169
153- case IFileActivatedEventArgs fileArgs :
154- if ( fileArgs . Files . Count > 0 )
155- {
156- rootFrame . Navigate ( typeof ( MainPage ) , fileArgs . Files , new SuppressNavigationTransitionInfo ( ) ) ;
170+ default :
171+ rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
172+ break ;
157173 }
158- break ;
159-
160- default :
174+ }
175+ else
176+ {
161177 rootFrame . Navigate ( typeof ( MainPage ) , null , new SuppressNavigationTransitionInfo ( ) ) ;
162- break ;
178+ }
179+ }
180+ }
181+
182+ private void HandleFileArgs ( Frame rootFrame , IFileActivatedEventArgs fileArgs )
183+ {
184+ if ( fileArgs . Files . Count > 0 )
185+ {
186+ rootFrame . Navigate ( typeof ( MainPage ) , fileArgs . Files , new SuppressNavigationTransitionInfo ( ) ) ;
163187 }
164188 }
165189
0 commit comments