/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Helpers/UI/UIHelpers.cs
196 строк
6 KB
yair100
Feature: Updated sidebar icons (#18705)
15 июл 2026, 02:41
Не верифицирован
15 июл 2026, 02:41
3789e75
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using CommunityToolkit.WinUI; using Files.App.Controls; using Files.App.Helpers.Application; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media.Imaging; using System.IO; namespace Files.App.Helpers { public static class UIHelpers { public static event PropertyChangedEventHandler? PropertyChanged; /// <summary> /// True if a user-editable text input currently owns keyboard focus within the given XamlRoot. /// Used to gate code that programmatically reassigns focus on a background-completion /// callback (e.g. folder-load done), so the user's typing isn't yanked away. /// </summary> public static bool IsTextInputFocused(XamlRoot? xamlRoot) { if (xamlRoot is null) return false; if (FocusManager.GetFocusedElement(xamlRoot) is TextBox or RichEditBox or PasswordBox or AutoSuggestBox) return true; // The Omnibar holds an internal focus DP that stays true while its suggestion popup is // open (the popup root sits outside the omnibar's visual ancestry, so GetFocusedElement // alone misses it). Consult that DP as a fallback. var omnibar = (MainWindow.Instance.Content as Frame)?.FindDescendant<Omnibar>(); return omnibar?.IsFocused == true; } private static bool canShowDialog = true; public static bool CanShowDialog { get => canShowDialog; private set { if (value == canShowDialog) return; canShowDialog = value; PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(nameof(CanShowDialog))); } } /// <summary> /// Displays a toast or dialog to indicate the result of /// a device ejection operation. /// </summary> /// <param name="type">Type of drive to eject</param> /// <param name="result">Only true implies a successful device ejection</param> /// <returns></returns> public static async Task ShowDeviceEjectResultAsync(Data.Items.DriveType type, bool result) { if (type != Data.Items.DriveType.CDRom && result) { Debug.WriteLine("Device successfully ejected"); SafetyExtensions.IgnoreExceptions(() => { AppToastNotificationHelper.ShowDriveEjectToast(); }); } else if (!result) { Debug.WriteLine("Can't eject device"); await DialogDisplayHelper.ShowDialogAsync( Strings.EjectNotificationErrorDialogHeader.GetLocalizedResource(), Strings.EjectNotificationErrorDialogBody.GetLocalizedResource()); } } public static async Task<ContentDialogResult> TryShowAsync(this ContentDialog dialog) { if (!canShowDialog) return ContentDialogResult.None; try { CanShowDialog = false; return await SetContentDialogRoot(dialog).ShowAsync(); } catch // A content dialog is already open { return ContentDialogResult.None; } finally { CanShowDialog = true; } } public static async Task<DialogResult> TryShowAsync<TViewModel>(this IDialog<TViewModel> dialog) where TViewModel : class, INotifyPropertyChanged { return (DialogResult)await ((ContentDialog)dialog).TryShowAsync(); } // WINUI3 private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog) { if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8)) { contentDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot; } return contentDialog; } public static void CloseAllDialogs() { if (MainWindow.Instance?.Content?.XamlRoot == null) return; var openedDialogs = VisualTreeHelper.GetOpenPopupsForXamlRoot(MainWindow.Instance.Content.XamlRoot); foreach (var item in openedDialogs) if (item.Child is ContentDialog dialog) dialog.Hide(); } private static IEnumerable<IconFileInfo> SidebarIconResources = LoadSidebarIconResources(); private static IconFileInfo ShieldIconResource = LoadShieldIconResource(); private static IconFileInfo SearchIconResource = LoadSearchIconResource(); public static IconFileInfo GetSidebarIconResourceInfo(int index) { var icons = UIHelpers.SidebarIconResources; return icons?.FirstOrDefault(x => x.Index == index); } public static async Task<BitmapImage?> GetSidebarIconResource(int index) { var iconInfo = GetSidebarIconResourceInfo(index); return iconInfo is not null ? await iconInfo.IconData.ToBitmapAsync() : null; } public static async Task<BitmapImage?> GetShieldIconResource() { return ShieldIconResource is not null ? await ShieldIconResource.IconData.ToBitmapAsync() : null; } public static async Task<BitmapImage?> GetSearchIconResource() { return SearchIconResource is not null ? await SearchIconResource.IconData.ToBitmapAsync() : null; } private static IEnumerable<IconFileInfo> LoadSidebarIconResources() { string imageres = Path.Combine(Constants.UserEnvironmentPaths.SystemRootPath, "System32", "imageres.dll"); var imageResList = Win32Helper.ExtractSelectedIconsFromDLL(imageres, new List<int>() { Constants.ImageRes.RecycleBin, Constants.ImageRes.Network, Constants.ImageRes.Folder }, 32); return imageResList; } private static IconFileInfo LoadShieldIconResource() { string imageres = Path.Combine(Constants.UserEnvironmentPaths.SystemRootPath, "System32", "imageres.dll"); var imageResList = Win32Helper.ExtractSelectedIconsFromDLL(imageres, new List<int>() { Constants.ImageRes.ShieldIcon }, 16); return imageResList.FirstOrDefault(); } private static IconFileInfo LoadSearchIconResource() { string imageres = Path.Combine(Constants.UserEnvironmentPaths.SystemRootPath, "System32", "imageres.dll"); var imageResList = Win32Helper.ExtractSelectedIconsFromDLL(imageres, new List<int>() { Constants.ImageRes.SearchIcon }, 48); return imageResList.FirstOrDefault(); } } }