Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 6 additions & 241 deletions Files/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@
using System.IO;
using System.Linq;
using System.Collections.ObjectModel;
using Windows.Devices.Enumeration;
using Windows.Devices.Portable;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls.Primitives;
using Files.Enums;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Management.Deployment;
using Windows.Storage.Streams;
using Windows.System;
using Microsoft.UI.Xaml.Controls;
using Files.View_Models;

namespace Files
Expand Down Expand Up @@ -57,14 +49,13 @@ public static ProHome OccupiedInstance
public static Dialogs.PropertiesDialog propertiesDialog { get; set; }
public static Dialogs.LayoutDialog layoutDialog { get; set; }
public static Dialogs.AddItemDialog addItemDialog { get; set; }
private DeviceWatcher watcher;
public static ObservableCollection<SidebarItem> sideBarItems = new ObservableCollection<SidebarItem>();
public static ObservableCollection<WSLDistroItem> linuxDistroItems = new ObservableCollection<WSLDistroItem>();
public static SettingsViewModel AppSettings = new SettingsViewModel();
public static SettingsViewModel AppSettings { get; set; }

public App()
{
this.InitializeComponent();
this.InitializeComponent();
this.Suspending += OnSuspending;
consentDialog = new Dialogs.ConsentDialog();
propertiesDialog = new Dialogs.PropertiesDialog();
Expand All @@ -76,8 +67,8 @@ public App()
Clipboard_ContentChanged(null, null);
AppCenter.Start("682666d1-51d3-4e4a-93d0-d028d43baaa0", typeof(Analytics), typeof(Crashes));

AppSettings = new SettingsViewModel();
SetPropertiesFromLocalSettings();

PopulatePinnedSidebarItems();
DetectWSLDistros();
}
Expand Down Expand Up @@ -267,217 +258,6 @@ private void SetPropertiesFromLocalSettings()



}



public void PopulateDrivesListWithLocalDisks()
{
var driveLetters = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList();
driveLetters.ForEach(async roots =>
{
try
{
var content = string.Empty;
string icon = null;
if (!(await KnownFolders.RemovableDevices.GetFoldersAsync()).Select(x => x.Path).ToList().Contains(roots.Name))
{
// TODO: Display Custom Names for Local Disks as well
if(InstanceTabsView.NormalizePath(roots.Name) != InstanceTabsView.NormalizePath("A:")
&& InstanceTabsView.NormalizePath(roots.Name) != InstanceTabsView.NormalizePath("B:"))
{
content = $"Local Disk ({roots.Name.TrimEnd('\\')})";
icon = "\uEDA2";
}
else
{
content = $"Floppy Disk ({roots.Name.TrimEnd('\\')})";
icon = "\uE74E";
}


await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
async () =>
{
Visibility capacityBarVis = Visibility.Visible;
ulong totalSpaceProg = 0;
ulong freeSpaceProg = 0;
string free_space_text = "Unknown";
string total_space_text = "Unknown";

try
{
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(roots.Name);
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });

var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);

sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);

free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
}
catch (UnauthorizedAccessException)
{
capacityBarVis = Visibility.Collapsed;
}
catch (NullReferenceException)
{
capacityBarVis = Visibility.Collapsed;
}

App.foundDrives.Add(new DriveItem()
{
driveText = content,
glyph = icon,
maxSpace = totalSpaceProg,
spaceUsed = totalSpaceProg - freeSpaceProg,
tag = roots.Name,
progressBarVisibility = capacityBarVis,
spaceText = free_space_text + " free of " + total_space_text,
});
});
}

}
catch (UnauthorizedAccessException e)
{
Debug.WriteLine(e.Message);
}

});
}

private async void Watcher_EnumerationCompleted(DeviceWatcher sender, object args)
{
try
{
PopulateDrivesListWithLocalDisks();
}
catch (UnauthorizedAccessException)
{
await consentDialog.ShowAsync();
}
DeviceAdded(sender, null);

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
() =>
{
App.foundDrives.Add(new DriveItem()
{
driveText = "OneDrive",
tag = "OneDrive",
cloudGlyphVisibility = Visibility.Visible,
driveGlyphVisibility = Visibility.Collapsed
});
});
}

private void DeviceUpdated(DeviceWatcher sender, DeviceInformationUpdate args)
{
Debug.WriteLine("Devices updated");
}


private async void DeviceRemoved(DeviceWatcher sender, DeviceInformationUpdate args)
{
var devices = DriveInfo.GetDrives().Select(x => x.RootDirectory.Root).ToList().OrderBy(x => x.Root.FullName).ToList();

foreach (DriveItem driveItem in foundDrives)
{
if (!driveItem.tag.Equals("OneDrive"))
{
if (!devices.Any(x => x.Name == driveItem.tag) || devices.Equals(null))
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
() =>
{
foundDrives.Remove(driveItem);
});
return;

}
}

}
}

private async void DeviceAdded(DeviceWatcher sender, DeviceInformation args)
{
try
{
var devices = (await KnownFolders.RemovableDevices.GetFoldersAsync()).OrderBy(x => x.Path);
foreach (StorageFolder device in devices)
{
var letter = device.Path;
if (!foundDrives.Any(x => x.tag == letter))
{
var content = device.DisplayName;
string icon = null;
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low,
async () =>
{
if (content.Contains("DVD"))
{
icon = "\uE958";
}
else
{
icon = "\uE88E";
}

ulong totalSpaceProg = 0;
ulong freeSpaceProg = 0;
string free_space_text = "Unknown";
string total_space_text = "Unknown";
Visibility capacityBarVis = Visibility.Visible;
try
{
StorageFolder drive = await StorageFolder.GetFolderFromPathAsync(letter);
var retrivedProperties = await drive.Properties.RetrievePropertiesAsync(new string[] { "System.FreeSpace", "System.Capacity" });

var sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).GigaBytes;
freeSpaceProg = Convert.ToUInt64(sizeAsGBString);

sizeAsGBString = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).GigaBytes;
totalSpaceProg = Convert.ToUInt64(sizeAsGBString);

free_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.FreeSpace"]).ToString();
total_space_text = ByteSizeLib.ByteSize.FromBytes((ulong)retrivedProperties["System.Capacity"]).ToString();
}
catch (UnauthorizedAccessException)
{
capacityBarVis = Visibility.Collapsed;
}
catch (NullReferenceException)
{
capacityBarVis = Visibility.Collapsed;
}

if (!foundDrives.Any(x => x.tag == letter))
{
foundDrives.Add(new DriveItem()
{
driveText = content,
glyph = icon,
maxSpace = totalSpaceProg,
spaceUsed = totalSpaceProg - freeSpaceProg,
tag = letter,
progressBarVisibility = capacityBarVis,
spaceText = free_space_text + " free of " + total_space_text,
});
}
});

}
}
}
catch (UnauthorizedAccessException)
{
await consentDialog.ShowAsync();
}
}

public static List<string> LinesToRemoveFromFile = new List<string>();
Expand Down Expand Up @@ -662,7 +442,6 @@ public static IReadOnlyList<ContentDialog> FindDisplayedContentDialogs<T>()

public static PasteState PS { get; set; } = new PasteState();
public static List<string> pathsToDeleteAfterPaste = new List<string>();
public static ObservableCollection<DriveItem> foundDrives = new ObservableCollection<DriveItem>();

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
Expand Down Expand Up @@ -710,12 +489,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)


}
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
watcher.Added += DeviceAdded;
watcher.Removed += DeviceRemoved;
watcher.Updated += DeviceUpdated;
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
watcher.Start();

// Ensure the current window is active
Window.Current.Activate();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
Expand Down Expand Up @@ -776,18 +550,12 @@ protected override void OnActivated(IActivatedEventArgs args)
rootFrame.Navigate(typeof(InstanceTabsView), @trimmedPath, new SuppressNavigationTransitionInfo());
}
// Ensure the current window is active.
watcher = DeviceInformation.CreateWatcher(StorageDevice.GetDeviceSelector());
watcher.Added += DeviceAdded;
watcher.Removed += DeviceRemoved;
watcher.Updated += DeviceUpdated;
watcher.EnumerationCompleted += Watcher_EnumerationCompleted;
watcher.Start();
Window.Current.Activate();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
return;
}

rootFrame.Navigate(typeof(InstanceTabsView), null, new SuppressNavigationTransitionInfo());

// Ensure the current window is active.
Expand Down Expand Up @@ -819,10 +587,7 @@ private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
if(watcher.Status == DeviceWatcherStatus.Started || watcher.Status == DeviceWatcherStatus.EnumerationCompleted)
{
watcher.Stop();
}
AppSettings.Dispose();
deferral.Complete();
}
}
Expand Down
16 changes: 8 additions & 8 deletions Files/BaseLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
// Add item jumping handler
Window.Current.CoreWindow.CharacterReceived += Page_CharacterReceived;
var parameters = (string)eventArgs.Parameter;
if (App.FormFactor == Enums.FormFactorMode.Regular)
if (App.AppSettings.FormFactor == Enums.FormFactorMode.Regular)
{
Frame rootFrame = Window.Current.Content as Frame;
InstanceTabsView instanceTabsView = rootFrame.Content as InstanceTabsView;
Expand All @@ -111,31 +111,31 @@ protected override void OnNavigatedTo(NavigationEventArgs eventArgs)
App.OccupiedInstance.instanceViewModel.AddItemsToCollectionAsync(App.OccupiedInstance.instanceViewModel.Universal.path);
App.Clipboard_ContentChanged(null, null);

if (parameters.Equals(App.DesktopPath))
if (parameters.Equals(App.AppSettings.DesktopPath))
{
App.OccupiedInstance.PathText.Text = "Desktop";
}
else if (parameters.Equals(App.DocumentsPath))
else if (parameters.Equals(App.AppSettings.DocumentsPath))
{
App.OccupiedInstance.PathText.Text = "Documents";
}
else if (parameters.Equals(App.DownloadsPath))
else if (parameters.Equals(App.AppSettings.DownloadsPath))
{
App.OccupiedInstance.PathText.Text = "Downloads";
}
else if (parameters.Equals(App.PicturesPath))
else if (parameters.Equals(App.AppSettings.PicturesPath))
{
App.OccupiedInstance.PathText.Text = "Pictures";
}
else if (parameters.Equals(App.MusicPath))
else if (parameters.Equals(App.AppSettings.MusicPath))
{
App.OccupiedInstance.PathText.Text = "Music";
}
else if (parameters.Equals(App.OneDrivePath))
else if (parameters.Equals(App.AppSettings.OneDrivePath))
{
App.OccupiedInstance.PathText.Text = "OneDrive";
}
else if (parameters.Equals(App.VideosPath))
else if (parameters.Equals(App.AppSettings.VideosPath))
{
App.OccupiedInstance.PathText.Text = "Videos";
}
Expand Down
Loading