Skip to content

Commit 4494f6d

Browse files
committed
Fix toast notification issues
1 parent 0c1ecd4 commit 4494f6d

File tree

4 files changed

+153
-144
lines changed

4 files changed

+153
-144
lines changed

SmartImage/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private static async Task Main(string[] args)
154154
* Register events
155155
*/
156156

157-
ToastNotificationManagerCompat.OnActivated += AppInterface.OnToastActivated;
157+
ToastNotificationManagerCompat.OnActivated += AppToast.OnToastActivated;
158158

159159
Console.OutputEncoding = Encoding.Unicode;
160160

@@ -204,7 +204,7 @@ private static async Task Main(string[] args)
204204
OnSearchCompleted(obj, eventArgs, _cancellationToken);
205205

206206
if (Config.Notification) {
207-
AppInterface.ShowToast(obj, eventArgs);
207+
AppToast.ShowToast(obj, eventArgs);
208208
}
209209
};
210210

SmartImage/SmartImage.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414

1515

16-
<PublishTrimmed>true</PublishTrimmed>
1716

1817

1918
<!-- <RuntimeIdentifiers>win10-x64;ubuntu.16.04-x64</RuntimeIdentifiers> -->
@@ -44,6 +43,8 @@
4443
<DefineConstants>TRACE;JETBRAINS_ANNOTATIONS</DefineConstants>
4544
<PublishSingleFile>true</PublishSingleFile>
4645
<NoWarn>1701;1702;1416</NoWarn>
46+
<PublishTrimmed>true</PublishTrimmed>
47+
4748

4849
<!-- <NoWarn>CA1416</NoWarn> -->
4950
</PropertyGroup>

SmartImage/UI/AppInterface.cs

Lines changed: 11 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ internal static partial class AppInterface
166166
Status = "You can also drag and drop a file to run a search."
167167
};
168168

169+
/// <summary>
170+
/// Name in ASCII art
171+
/// </summary>
172+
internal const string NAME_BANNER =
173+
" ____ _ ___\n" +
174+
" / ___| _ __ ___ __ _ _ __| |_|_ _|_ __ ___ __ _ __ _ ___\n" +
175+
@" \___ \| '_ ` _ \ / _` | '__| __|| || '_ ` _ \ / _` |/ _` |/ _ \" + "\n" +
176+
" ___) | | | | | | (_| | | | |_ | || | | | | | (_| | (_| | __/\n" +
177+
@" |____/|_| |_| |_|\__,_|_| \__|___|_| |_| |_|\__,_|\__, |\___|" + "\n" +
178+
" |___/\n";
179+
169180
static AppInterface()
170181
{
171182
// NOTE: Static initializer must be AFTER MainMenuDialog
@@ -192,146 +203,5 @@ static AppInterface()
192203
};
193204
}
194205
}
195-
196-
#region Toast
197-
198-
public static void ShowToast(object sender, SearchCompletedEventArgs args)
199-
{
200-
Debug.WriteLine($"Building toast", C_DEBUG);
201-
var bestResult = args.Detailed;
202-
203-
var builder = new ToastContentBuilder();
204-
var button = new ToastButton();
205-
var button2 = new ToastButton();
206-
207-
button2.SetContent("Dismiss")
208-
.AddArgument(Elements.ARG_KEY_ACTION, Elements.ARG_VALUE_DISMISS);
209-
210-
button.SetContent("Open")
211-
.AddArgument(Elements.ARG_KEY_ACTION, $"{bestResult.Value.Url}");
212-
213-
builder.AddButton(button)
214-
.AddButton(button2)
215-
.AddText("Search complete")
216-
.AddText($"{bestResult}")
217-
.AddText($"Results: {Client.Results.Count}");
218-
219-
if (Config.Notification && Config.NotificationImage) {
220-
221-
var imageResult = args.FirstDirect.Value;
222-
223-
if (imageResult != null) {
224-
var path = Path.GetTempPath();
225-
226-
string file = ImageHelper.Download(imageResult.Direct, path);
227-
228-
if (file == null) {
229-
int i = 0;
230-
231-
var imageResults = args.Direct.Value;
232-
233-
do {
234-
file = ImageHelper.Download(imageResults[i++].Direct, path);
235-
236-
} while (String.IsNullOrWhiteSpace(file) && i < imageResults.Length);
237-
238-
}
239-
240-
if (file != null) {
241-
242-
file = GetHeroImage(path, file);
243-
244-
Debug.WriteLine($"{nameof(AppInterface)}: Downloaded {file}", C_INFO);
245-
246-
builder.AddHeroImage(new Uri(file));
247-
248-
AppDomain.CurrentDomain.ProcessExit += (sender2, args2) =>
249-
{
250-
File.Delete(file);
251-
};
252-
}
253-
254-
}
255-
256-
257-
}
258-
259-
builder.SetBackgroundActivation();
260-
261-
//...
262-
263-
builder.Show();
264-
265-
// ToastNotificationManager.CreateToastNotifier();
266-
}
267-
268-
private static string GetHeroImage(string path, string file)
269-
{
270-
var bytes = File.ReadAllBytes(file).Length;
271-
var kiloBytes = MathHelper.ConvertToUnit(bytes, MetricPrefix.Kilo);
272-
bool tooBig = kiloBytes >= MAX_IMG_SIZE_KB;
273-
274-
if (tooBig) {
275-
var bitmap = new Bitmap(file);
276-
var newSize = new Size(Convert.ToInt32(bitmap.Width / 2), Convert.ToInt32(bitmap.Height / 2));
277-
Bitmap bitmap2 = ImageHelper.ResizeImage(bitmap, newSize);
278-
279-
if (bitmap2 != null) {
280-
string s = Path.Combine(path, Path.GetTempFileName());
281-
bitmap2.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
282-
bytes = File.ReadAllBytes(file).Length;
283-
kiloBytes = MathHelper.ConvertToUnit(bytes, MetricPrefix.Kilo);
284-
285-
Debug.WriteLine($"-> {bytes} {kiloBytes} | {s}");
286-
file = s;
287-
}
288-
289-
}
290-
291-
return file;
292-
}
293-
294-
295-
public static void OnToastActivated(ToastNotificationActivatedEventArgsCompat compat)
296-
{
297-
// NOTE: Does not return if invoked from background
298-
299-
// Obtain the arguments from the notification
300-
301-
var arguments = ToastArguments.Parse(compat.Argument);
302-
303-
foreach (var argument in arguments) {
304-
Debug.WriteLine($"Toast argument: {argument}", C_DEBUG);
305-
306-
if (argument.Key == Elements.ARG_KEY_ACTION) {
307-
308-
if (argument.Value == Elements.ARG_VALUE_DISMISS) {
309-
break;
310-
}
311-
312-
WebUtilities.OpenUrl(argument.Value);
313-
}
314-
}
315-
316-
if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated()) {
317-
//
318-
Environment.Exit(0);
319-
}
320-
}
321-
322-
#endregion
323-
324-
/// <summary>
325-
/// Name in ASCII art
326-
/// </summary>
327-
public const string NAME_BANNER =
328-
" ____ _ ___\n" +
329-
" / ___| _ __ ___ __ _ _ __| |_|_ _|_ __ ___ __ _ __ _ ___\n" +
330-
@" \___ \| '_ ` _ \ / _` | '__| __|| || '_ ` _ \ / _` |/ _` |/ _ \" + "\n" +
331-
" ___) | | | | | | (_| | | | |_ | || | | | | | (_| | (_| | __/\n" +
332-
@" |____/|_| |_| |_|\__,_|_| \__|___|_| |_| |_|\__,_|\__, |\___|" + "\n" +
333-
" |___/\n";
334-
335-
private const int MAX_IMG_SIZE_KB = 200;
336206
}
337207
}

SmartImage/UI/AppToast.cs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
using System.Diagnostics;
2+
using System.Drawing;
3+
using Kantan.Diagnostics;
4+
using Kantan.Net;
5+
using Kantan.Numeric;
6+
using Microsoft.Toolkit.Uwp.Notifications;
7+
using SmartImage.Lib;
8+
using SmartImage.Lib.Utilities;
9+
10+
namespace SmartImage.UI;
11+
12+
internal static class AppToast
13+
{
14+
internal static void ShowToast(object sender, SearchCompletedEventArgs args)
15+
{
16+
Debug.WriteLine($"Building toast", LogCategories.C_DEBUG);
17+
var bestResult = args.Detailed;
18+
19+
var builder = new ToastContentBuilder();
20+
var button = new ToastButton();
21+
var button2 = new ToastButton();
22+
23+
button2.SetContent("Dismiss")
24+
.AddArgument(AppInterface.Elements.ARG_KEY_ACTION, AppInterface.Elements.ARG_VALUE_DISMISS);
25+
26+
button.SetContent("Open")
27+
.AddArgument(AppInterface.Elements.ARG_KEY_ACTION, $"{bestResult.Value.Url}");
28+
29+
builder.AddButton(button)
30+
.AddButton(button2)
31+
.AddText("Search complete")
32+
.AddText($"{bestResult}")
33+
.AddText($"Results: {Program.Client.Results.Count}");
34+
35+
if (Program.Config.Notification && Program.Config.NotificationImage) {
36+
37+
var imageResult = args.FirstDirect.Value;
38+
39+
if (imageResult != null) {
40+
var path = Path.GetTempPath();
41+
42+
string file = ImageHelper.Download(imageResult.Direct, path);
43+
44+
if (file == null) {
45+
int i = 0;
46+
47+
var imageResults = args.Direct.Value;
48+
49+
do {
50+
file = ImageHelper.Download(imageResults[i++].Direct, path);
51+
52+
} while (String.IsNullOrWhiteSpace(file) && i < imageResults.Length);
53+
54+
}
55+
56+
if (file != null) {
57+
58+
file = GetHeroImage(path, file);
59+
60+
Debug.WriteLine($"{nameof(AppInterface)}: Downloaded {file}", LogCategories.C_INFO);
61+
62+
builder.AddHeroImage(new Uri(file));
63+
64+
AppDomain.CurrentDomain.ProcessExit += (sender2, args2) =>
65+
{
66+
File.Delete(file);
67+
};
68+
}
69+
70+
}
71+
72+
73+
}
74+
75+
builder.SetBackgroundActivation();
76+
77+
//...
78+
79+
builder.Show();
80+
81+
// ToastNotificationManager.CreateToastNotifier();
82+
}
83+
84+
private static string GetHeroImage(string path, string file)
85+
{
86+
var bytes = File.ReadAllBytes(file).Length;
87+
var kiloBytes = MathHelper.ConvertToUnit(bytes, MetricPrefix.Kilo);
88+
bool tooBig = kiloBytes >= MAX_IMG_SIZE_KB;
89+
90+
if (tooBig) {
91+
var bitmap = new Bitmap(file);
92+
var newSize = new Size(Convert.ToInt32(bitmap.Width / 2), Convert.ToInt32(bitmap.Height / 2));
93+
Bitmap bitmap2 = ImageHelper.ResizeImage(bitmap, newSize);
94+
95+
if (bitmap2 != null) {
96+
string s = Path.Combine(path, Path.GetTempFileName());
97+
bitmap2.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
98+
bytes = File.ReadAllBytes(file).Length;
99+
kiloBytes = MathHelper.ConvertToUnit(bytes, MetricPrefix.Kilo);
100+
101+
Debug.WriteLine($"-> {bytes} {kiloBytes} | {s}");
102+
file = s;
103+
}
104+
105+
}
106+
107+
return file;
108+
}
109+
110+
internal static void OnToastActivated(ToastNotificationActivatedEventArgsCompat compat)
111+
{
112+
// NOTE: Does not return if invoked from background
113+
114+
// Obtain the arguments from the notification
115+
116+
var arguments = ToastArguments.Parse(compat.Argument);
117+
118+
foreach (var argument in arguments) {
119+
Debug.WriteLine($"Toast argument: {argument}", LogCategories.C_DEBUG);
120+
121+
if (argument.Key == AppInterface.Elements.ARG_KEY_ACTION) {
122+
123+
if (argument.Value == AppInterface.Elements.ARG_VALUE_DISMISS) {
124+
break;
125+
}
126+
127+
WebUtilities.OpenUrl(argument.Value);
128+
}
129+
}
130+
131+
if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated()) {
132+
//
133+
Environment.Exit(0);
134+
}
135+
}
136+
137+
private const int MAX_IMG_SIZE_KB = 200;
138+
}

0 commit comments

Comments
 (0)