-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Feature: Added option to change default layout mode #10089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f2a0c3c
Feature: Added setting to change default layout mode
yaira2 ce20f71
Fixed property name
yaira2 5888b88
Merge branch 'main' into ya/DefaultLayoutMode
yaira2 f4878f1
Merge branch 'main' into ya/DefaultLayoutMode
yaira2 6671e7c
Update AdaptiveLayoutHelpers.cs
yaira2 03fcf4c
Merge branch 'main' into ya/DefaultLayoutMode
yaira2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,137 @@ | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Files.Backend.Services.Settings; | ||
using Files.App.Filesystem; | ||
using Files.App.ViewModels; | ||
using Files.App.ViewModels.Previews; | ||
using Files.Backend.Services.Settings; | ||
using Files.Shared.Enums; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Windows.Storage; | ||
using System.Collections.Generic; | ||
using Files.App.Filesystem; | ||
|
||
namespace Files.App.Helpers | ||
{ | ||
public static class AdaptiveLayoutHelpers | ||
{ | ||
public static bool PredictLayoutMode(FolderSettingsViewModel folderSettings, string path, IList<ListedItem> filesAndFolders) | ||
{ | ||
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>(); | ||
public static class AdaptiveLayoutHelpers | ||
{ | ||
public static bool PredictLayoutMode(FolderSettingsViewModel folderSettings, string path, IList<ListedItem> filesAndFolders) | ||
{ | ||
IUserSettingsService userSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>(); | ||
|
||
if (!userSettingsService.PreferencesSettingsService.ForceLayoutPreferencesOnAllDirectories | ||
&& folderSettings.IsAdaptiveLayoutEnabled | ||
&& !folderSettings.IsLayoutModeFixed) | ||
{ | ||
Action layoutDetails = () => folderSettings.ToggleLayoutModeDetailsView(false); | ||
Action layoutTiles = () => folderSettings.ToggleLayoutModeTiles(false); | ||
Action layoutGridView = () => folderSettings.ToggleLayoutModeGridView(folderSettings.GridViewSize); | ||
if (!userSettingsService.PreferencesSettingsService.ForceLayoutPreferencesOnAllDirectories | ||
&& folderSettings.IsAdaptiveLayoutEnabled | ||
&& !folderSettings.IsLayoutModeFixed | ||
&& folderSettings.LayoutMode == FolderLayoutModes.Adaptive) | ||
{ | ||
Action layoutDetails = () => folderSettings.ToggleLayoutModeDetailsView(false); | ||
Action layoutTiles = () => folderSettings.ToggleLayoutModeTiles(false); | ||
Action layoutGridView = () => folderSettings.ToggleLayoutModeGridView(folderSettings.GridViewSize); | ||
|
||
bool desktopIniFound = false; | ||
bool desktopIniFound = false; | ||
|
||
if (string.IsNullOrWhiteSpace(path)) | ||
{ | ||
return false; | ||
} | ||
if (string.IsNullOrWhiteSpace(path)) | ||
{ | ||
return false; | ||
} | ||
|
||
var iniPath = System.IO.Path.Combine(path, "desktop.ini"); | ||
var iniContents = NativeFileOperationsHelper.ReadStringFromFile(iniPath)?.Trim(); | ||
if (!string.IsNullOrEmpty(iniContents)) | ||
{ | ||
var parser = new IniParser.Parser.IniDataParser(); | ||
parser.Configuration.ThrowExceptionsOnError = false; | ||
var data = parser.Parse(iniContents); | ||
if (data != null) | ||
{ | ||
var viewModeSection = data.Sections.FirstOrDefault(x => "ViewState".Equals(x.SectionName, StringComparison.OrdinalIgnoreCase)); | ||
if (viewModeSection != null) | ||
{ | ||
var folderTypeKey = viewModeSection.Keys.FirstOrDefault(s => "FolderType".Equals(s.KeyName, StringComparison.OrdinalIgnoreCase)); | ||
if (folderTypeKey != null) | ||
{ | ||
var setLayout = (folderTypeKey.Value) switch | ||
{ | ||
"Documents" => layoutDetails, | ||
"Pictures" => layoutGridView, | ||
"Music" => layoutDetails, | ||
"Videos" => layoutGridView, | ||
_ => layoutDetails | ||
}; | ||
setLayout(); | ||
desktopIniFound = true; | ||
} | ||
} | ||
} | ||
} | ||
var iniPath = System.IO.Path.Combine(path, "desktop.ini"); | ||
var iniContents = NativeFileOperationsHelper.ReadStringFromFile(iniPath)?.Trim(); | ||
if (!string.IsNullOrEmpty(iniContents)) | ||
{ | ||
var parser = new IniParser.Parser.IniDataParser(); | ||
parser.Configuration.ThrowExceptionsOnError = false; | ||
var data = parser.Parse(iniContents); | ||
if (data != null) | ||
{ | ||
var viewModeSection = data.Sections.FirstOrDefault(x => "ViewState".Equals(x.SectionName, StringComparison.OrdinalIgnoreCase)); | ||
if (viewModeSection != null) | ||
{ | ||
var folderTypeKey = viewModeSection.Keys.FirstOrDefault(s => "FolderType".Equals(s.KeyName, StringComparison.OrdinalIgnoreCase)); | ||
if (folderTypeKey != null) | ||
{ | ||
var setLayout = (folderTypeKey.Value) switch | ||
{ | ||
"Documents" => layoutDetails, | ||
"Pictures" => layoutGridView, | ||
"Music" => layoutDetails, | ||
"Videos" => layoutGridView, | ||
_ => layoutDetails | ||
}; | ||
setLayout(); | ||
desktopIniFound = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (desktopIniFound) | ||
{ | ||
return true; | ||
} | ||
if (filesAndFolders.Count == 0) | ||
{ | ||
return false; | ||
} | ||
if (desktopIniFound) | ||
{ | ||
return true; | ||
} | ||
if (filesAndFolders.Count == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
int allItemsCount = filesAndFolders.Count; | ||
int allItemsCount = filesAndFolders.Count; | ||
|
||
int mediaCount; | ||
int imagesCount; | ||
int foldersCount; | ||
int miscFilesCount; | ||
int mediaCount; | ||
int imagesCount; | ||
int foldersCount; | ||
int miscFilesCount; | ||
|
||
float mediaPercentage; | ||
float imagesPercentage; | ||
float foldersPercentage; | ||
float miscFilesPercentage; | ||
float mediaPercentage; | ||
float imagesPercentage; | ||
float foldersPercentage; | ||
float miscFilesPercentage; | ||
|
||
mediaCount = filesAndFolders.Where((item) => | ||
{ | ||
return !string.IsNullOrEmpty(item.FileExtension) && MediaPreviewViewModel.ContainsExtension(item.FileExtension.ToLowerInvariant()); | ||
}).Count(); | ||
imagesCount = filesAndFolders.Where((item) => | ||
{ | ||
return !string.IsNullOrEmpty(item.FileExtension) && ImagePreviewViewModel.ContainsExtension(item.FileExtension.ToLowerInvariant()); | ||
}).Count(); | ||
foldersCount = filesAndFolders.Where((item) => item.PrimaryItemAttribute == StorageItemTypes.Folder).Count(); | ||
miscFilesCount = allItemsCount - (mediaCount + imagesCount + foldersCount); | ||
mediaCount = filesAndFolders.Where((item) => | ||
{ | ||
return !string.IsNullOrEmpty(item.FileExtension) && MediaPreviewViewModel.ContainsExtension(item.FileExtension.ToLowerInvariant()); | ||
}).Count(); | ||
imagesCount = filesAndFolders.Where((item) => | ||
{ | ||
return !string.IsNullOrEmpty(item.FileExtension) && ImagePreviewViewModel.ContainsExtension(item.FileExtension.ToLowerInvariant()); | ||
}).Count(); | ||
foldersCount = filesAndFolders.Where((item) => item.PrimaryItemAttribute == StorageItemTypes.Folder).Count(); | ||
miscFilesCount = allItemsCount - (mediaCount + imagesCount + foldersCount); | ||
|
||
mediaPercentage = (float)((float)mediaCount / (float)allItemsCount) * 100.0f; | ||
imagesPercentage = (float)((float)imagesCount / (float)allItemsCount) * 100.0f; | ||
foldersPercentage = (float)((float)foldersCount / (float)allItemsCount) * 100.0f; | ||
miscFilesPercentage = (float)((float)miscFilesCount / (float)allItemsCount) * 100.0f; | ||
mediaPercentage = (float)((float)mediaCount / (float)allItemsCount) * 100.0f; | ||
imagesPercentage = (float)((float)imagesCount / (float)allItemsCount) * 100.0f; | ||
foldersPercentage = (float)((float)foldersCount / (float)allItemsCount) * 100.0f; | ||
miscFilesPercentage = (float)((float)miscFilesCount / (float)allItemsCount) * 100.0f; | ||
|
||
// Decide layout mode | ||
// Decide layout mode | ||
|
||
// Mostly files + folders, lesser media and image files | Mostly folders | ||
if ((foldersPercentage + miscFilesPercentage) > Constants.AdaptiveLayout.LargeThreshold) | ||
{ | ||
layoutDetails(); | ||
} | ||
// Mostly images, probably an images folder | ||
else if (imagesPercentage > Constants.AdaptiveLayout.ExtraLargeThreshold | ||
|| (imagesPercentage > Constants.AdaptiveLayout.MediumThreshold | ||
&& (mediaPercentage + miscFilesPercentage + foldersPercentage) > Constants.AdaptiveLayout.SmallThreshold | ||
&& (miscFilesPercentage + foldersPercentage) < Constants.AdaptiveLayout.ExtraSmallThreshold)) | ||
{ | ||
layoutGridView(); | ||
} | ||
// Mostly media i.e. sound files, videos | ||
else if (mediaPercentage > Constants.AdaptiveLayout.ExtraLargeThreshold | ||
|| (mediaPercentage > Constants.AdaptiveLayout.MediumThreshold | ||
&& (imagesPercentage + miscFilesPercentage + foldersPercentage) > Constants.AdaptiveLayout.SmallThreshold | ||
&& (miscFilesPercentage + foldersPercentage) < Constants.AdaptiveLayout.ExtraSmallThreshold)) | ||
{ | ||
layoutDetails(); | ||
} | ||
else | ||
{ | ||
layoutDetails(); | ||
} | ||
// Mostly files + folders, lesser media and image files | Mostly folders | ||
if ((foldersPercentage + miscFilesPercentage) > Constants.AdaptiveLayout.LargeThreshold) | ||
{ | ||
layoutDetails(); | ||
} | ||
// Mostly images, probably an images folder | ||
else if (imagesPercentage > Constants.AdaptiveLayout.ExtraLargeThreshold | ||
|| (imagesPercentage > Constants.AdaptiveLayout.MediumThreshold | ||
&& (mediaPercentage + miscFilesPercentage + foldersPercentage) > Constants.AdaptiveLayout.SmallThreshold | ||
&& (miscFilesPercentage + foldersPercentage) < Constants.AdaptiveLayout.ExtraSmallThreshold)) | ||
{ | ||
layoutGridView(); | ||
} | ||
// Mostly media i.e. sound files, videos | ||
else if (mediaPercentage > Constants.AdaptiveLayout.ExtraLargeThreshold | ||
|| (mediaPercentage > Constants.AdaptiveLayout.MediumThreshold | ||
&& (imagesPercentage + miscFilesPercentage + foldersPercentage) > Constants.AdaptiveLayout.SmallThreshold | ||
&& (miscFilesPercentage + foldersPercentage) < Constants.AdaptiveLayout.ExtraSmallThreshold)) | ||
{ | ||
layoutDetails(); | ||
} | ||
else | ||
{ | ||
layoutDetails(); | ||
} | ||
|
||
return true; | ||
} | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this prevents adaptive layout mode from working because folderSettings.LayoutMode is never set to Adaptive. Even in adaptive mode folderSettings.LayoutMode is still either details or grid or tiles. The above check for IsAdaptiveLayoutEnabled is what we want.