Skip to content

Commit

Permalink
Feature: Open minimized window on Windows startup (files-community#14309
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hishitetsu authored Dec 28, 2023
1 parent a481974 commit 1c18f6e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
67 changes: 50 additions & 17 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,21 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)

async Task ActivateAsync()
{
// Initialize and activate MainWindow
MainWindow.Instance.Activate();
// Get AppActivationArguments
var appActivationArguments = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
var isStartupTask = appActivationArguments.Data is Windows.ApplicationModel.Activation.IStartupTaskActivatedEventArgs;

// Wait for the Window to initialize
await Task.Delay(10);
if (!isStartupTask)
{
// Initialize and activate MainWindow
MainWindow.Instance.Activate();

SplashScreenLoadingTCS = new TaskCompletionSource();
MainWindow.Instance.ShowSplashScreen();
// Wait for the Window to initialize
await Task.Delay(10);

// Get AppActivationArguments
var appActivationArguments = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
SplashScreenLoadingTCS = new TaskCompletionSource();
MainWindow.Instance.ShowSplashScreen();
}

// Start tracking app usage
if (appActivationArguments.Data is Windows.ApplicationModel.Activation.IActivatedEventArgs activationEventArgs)
Expand All @@ -90,6 +94,21 @@ async Task ActivateAsync()
AppLifecycleHelper.ConfigureAppCenter();
#endif

var userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
var isLeaveAppRunning = userSettingsService.GeneralSettingsService.LeaveAppRunning;

if (isStartupTask && !isLeaveAppRunning)
{
// Initialize and activate MainWindow
MainWindow.Instance.Activate();

// Wait for the Window to initialize
await Task.Delay(10);

SplashScreenLoadingTCS = new TaskCompletionSource();
MainWindow.Instance.ShowSplashScreen();
}

// TODO: Replace with DI
QuickAccessManager = Ioc.Default.GetRequiredService<QuickAccessManager>();
HistoryWrapper = Ioc.Default.GetRequiredService<StorageHistoryWrapper>();
Expand All @@ -105,15 +124,28 @@ async Task ActivateAsync()

Logger.LogInformation($"App launched. Launch args type: {appActivationArguments.Data.GetType().Name}");

// Wait for the UI to update
await SplashScreenLoadingTCS!.Task.WithTimeoutAsync(TimeSpan.FromMilliseconds(500));
SplashScreenLoadingTCS = null;
if (!(isStartupTask && isLeaveAppRunning))
{
// Wait for the UI to update
await SplashScreenLoadingTCS!.Task.WithTimeoutAsync(TimeSpan.FromMilliseconds(500));
SplashScreenLoadingTCS = null;

// Create a system tray icon
SystemTrayIcon = new SystemTrayIcon().Show();
// Create a system tray icon
SystemTrayIcon = new SystemTrayIcon().Show();

_ = AppLifecycleHelper.InitializeAppComponentsAsync();
_ = MainWindow.Instance.InitializeApplicationAsync(appActivationArguments.Data);
_ = MainWindow.Instance.InitializeApplicationAsync(appActivationArguments.Data);
}

await AppLifecycleHelper.InitializeAppComponentsAsync();

if (isStartupTask && isLeaveAppRunning)
{
// Create a system tray icon when initialization is done
SystemTrayIcon = new SystemTrayIcon().Show();
App.Current.Exit();
}
else
await AppLifecycleHelper.CheckAppUpdate();
}
}

Expand All @@ -122,11 +154,12 @@ async Task ActivateAsync()
/// </summary>
public async Task OnActivatedAsync(AppActivationArguments activatedEventArgs)
{
Logger.LogInformation($"The app is being activated. Activation type: {activatedEventArgs.Data.GetType().Name}");
var activatedEventArgsData = activatedEventArgs.Data;
Logger.LogInformation($"The app is being activated. Activation type: {activatedEventArgsData.GetType().Name}");

// InitializeApplication accesses UI, needs to be called on UI thread
await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(()
=> MainWindow.Instance.InitializeApplicationAsync(activatedEventArgs.Data));
=> MainWindow.Instance.InitializeApplicationAsync(activatedEventArgsData));
}

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ await Task.WhenAll(

FileTagsHelper.UpdateTagsDb();

await CheckAppUpdate();

static Task OptionalTaskAsync(Task task, bool condition)
{
if (condition)
Expand Down
11 changes: 5 additions & 6 deletions src/Files.App/Utils/Taskbar/SystemTrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SystemTrayIcon : IDisposable

private bool _notifyIconCreated;

private DispatcherQueueTimer _timer;
private DateTime _lastLaunchDate;

// Properties

Expand Down Expand Up @@ -266,18 +266,17 @@ private void ShowContextMenu()
private void OnLeftClicked()
{
// Prevents duplicate launch
if (_timer?.IsRunning ?? false)
if (DateTime.Now - _lastLaunchDate < TimeSpan.FromSeconds(1))
return;

if (Program.Pool is not null)
{
_timer ??= DispatcherQueue.GetForCurrentThread().CreateTimer();
_timer.Interval = TimeSpan.FromSeconds(1);
_timer.IsRepeating = false;
_timer.Start();
_lastLaunchDate = DateTime.Now;

_ = Launcher.LaunchUriAsync(new Uri("files-uwp:"));
}
else
MainWindow.Instance.Activate();
}

private void OnDocumentationClicked()
Expand Down

0 comments on commit 1c18f6e

Please sign in to comment.