/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Helpers/Layout/AdaptiveLayoutHelpers.cs
88 строк
3 KB
Josh65-2201
Code Quality: Simplified logic for adaptive layout (#18322)
10 апр 2026, 17:43
Не верифицирован
10 апр 2026, 17:43
a81c1df
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using Files.App.ViewModels.Previews; using Files.Shared.Helpers; namespace Files.App.Helpers { public static class AdaptiveLayoutHelpers { private static ILayoutSettingsService LayoutSettingsService { get; } = Ioc.Default.GetRequiredService<ILayoutSettingsService>(); private static IContentPageContext ContentPageContext { get; } = Ioc.Default.GetRequiredService<IContentPageContext>(); public static void ApplyAdaptativeLayout(LayoutPreferencesManager folderSettings, IList<ListedItem> filesAndFolders) { if (LayoutSettingsService.SyncFolderPreferencesAcrossDirectories) return; if (folderSettings.IsLayoutModeFixed || !folderSettings.IsAdaptiveLayoutEnabled) return; var layout = GetAdaptiveLayout(filesAndFolders); switch (layout) { case Layouts.Detail: folderSettings.ToggleLayoutModeDetailsView(false); break; case Layouts.Grid: folderSettings.ToggleLayoutModeGridView(false); break; } } private static Layouts GetAdaptiveLayout(IList<ListedItem> filesAndFolders) { var pathLayout = GetPathLayout(); if (pathLayout is not Layouts.None) return pathLayout; return GetContentLayout(filesAndFolders); } private static Layouts GetPathLayout() { var desktopIni = ContentPageContext.ShellPage?.ShellViewModel?.DesktopIni; if (desktopIni is null) return Layouts.None; var viewStateSection = desktopIni.FirstOrDefault(x => x.SectionName == "ViewState"); if (viewStateSection is null) return Layouts.None; var viewMode = viewStateSection.Parameters.FirstOrDefault(x => x.Key == "Mode").Value; return viewMode switch { "Pictures" => Layouts.Grid, "Videos" => Layouts.Grid, _ => Layouts.Detail, }; } private static Layouts GetContentLayout(IList<ListedItem> filesAndFolders) { int itemCount = filesAndFolders.Count; if (filesAndFolders.Count is 0) return Layouts.None; float mediaPercentage = 100f * filesAndFolders.Count(IsMedia) / itemCount; if (mediaPercentage > 60f) return Layouts.Grid; return Layouts.Detail; static bool IsMedia(ListedItem item) => !string.IsNullOrEmpty(item.FileExtension) && (FileExtensionHelpers.IsAudioFile(item.FileExtension) || FileExtensionHelpers.IsVideoFile(item.FileExtension) || FileExtensionHelpers.IsImageFile(item.FileExtension)); } private enum Layouts { None, // Don't decide. Another function to decide can be called afterwards if available. Detail, // Apply the layout Detail. Grid, // Apply the layout Grid. } } }