-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
UIHelpers.cs
189 lines (165 loc) · 5.16 KB
/
UIHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.
using CommunityToolkit.WinUI.Notifications;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using System.IO;
using Windows.UI.Notifications;
namespace Files.App.Helpers
{
public static class UIHelpers
{
public static event PropertyChangedEventHandler? PropertyChanged;
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(() =>
{
var toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "EjectNotificationHeader".GetLocalizedResource()
},
new AdaptiveText()
{
Text = "EjectNotificationBody".GetLocalizedResource()
}
},
Attribution = new ToastGenericAttributionText()
{
Text = "SettingsAboutAppName".GetLocalizedResource()
}
}
},
ActivationType = ToastActivationType.Protocol
};
// Create the toast notification
var toastNotif = new ToastNotification(toastContent.GetXml());
// And send the notification
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
});
}
else if (!result)
{
Debug.WriteLine("Can't eject device");
await DialogDisplayHelper.ShowDialogAsync(
"EjectNotificationErrorDialogHeader".GetLocalizedResource(),
"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()
{
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();
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;
}
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.NetworkDrives,
Constants.ImageRes.Libraries,
Constants.ImageRes.ThisPC,
Constants.ImageRes.CloudDrives,
Constants.ImageRes.Folder,
Constants.ImageRes.OneDrive
}, 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.First();
}
}
}