-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
152 lines (135 loc) · 5.5 KB
/
Copy pathApp.xaml.cs
File metadata and controls
152 lines (135 loc) · 5.5 KB
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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
namespace TapLingo
{
public partial class App : Application
{
private Window? _mainWindow;
public App()
{
InitializeComponent();
UnhandledException += (_, e) =>
{
// רישום שגיאות לקובץ (WinUI 3 unpackaged לא תמיד מציג stack trace)
TryLogError(e.Exception);
};
}
/// <summary>
/// הפעלה ראשונה של האפליקציה
/// </summary>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
// טיפול בארגומנטים של שורת פקודה
var cmdArgs = Environment.GetCommandLineArgs();
string? textToTranslate = null;
bool openTranslateWindowEmpty = false;
if (cmdArgs.Length > 1)
{
var firstArg = cmdArgs[1];
// פקודות מיוחדות
if (firstArg.Equals("--register", StringComparison.OrdinalIgnoreCase))
{
UriProtocolHandler.Register();
ShowMessageBox("התוכנה נרשמה בהצלחה כ-protocol handler.\nעכשיו תוכל להשתמש ב-Click to Do.");
Exit();
return;
}
if (firstArg.Equals("--unregister", StringComparison.OrdinalIgnoreCase))
{
UriProtocolHandler.Unregister();
ShowMessageBox("הרישום בוטל.");
Exit();
return;
}
// פתיחת חלונית תרגום ריקה להזנה ידנית
if (firstArg.Equals("--translate", StringComparison.OrdinalIgnoreCase))
{
openTranslateWindowEmpty = true;
textToTranslate = cmdArgs.Length > 2 ? ExtractText(cmdArgs[2]) : string.Empty;
}
else
{
textToTranslate = ExtractText(firstArg);
}
}
var settings = SettingsManager.Load();
if (openTranslateWindowEmpty || !string.IsNullOrWhiteSpace(textToTranslate))
{
_mainWindow = new TranslationWindow(textToTranslate ?? string.Empty, settings);
}
else
{
_mainWindow = new SettingsWindow(settings);
}
_mainWindow.Activate();
}
/// <summary>
/// חילוץ טקסט מארגומנט (תומך ב-URI scheme של התוכנה, טקסט ישיר, או נתיב לקובץ txt)
/// </summary>
internal static string ExtractText(string arg)
{
if (string.IsNullOrWhiteSpace(arg)) return string.Empty;
// URI scheme: TapLingo://translate?text=Hello
if (arg.StartsWith("TapLingo:", StringComparison.OrdinalIgnoreCase))
{
try
{
var uri = new Uri(arg);
var query = uri.Query;
if (!string.IsNullOrEmpty(query))
{
var queryStr = query.TrimStart('?');
foreach (var pair in queryStr.Split('&'))
{
var kv = pair.Split('=', 2);
if (kv.Length == 2 && kv[0].Equals("text", StringComparison.OrdinalIgnoreCase))
{
return Uri.UnescapeDataString(kv[1]);
}
}
}
var path = uri.AbsolutePath.TrimStart('/');
return Uri.UnescapeDataString(path);
}
catch
{
return arg.Substring("TapLingo:".Length).TrimStart('/');
}
}
// ייתכן שהועבר נתיב לקובץ טקסט
if (File.Exists(arg) && arg.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
try { return File.ReadAllText(arg); } catch { /* fallthrough */ }
}
return arg;
}
private static void ShowMessageBox(string message)
{
// ב-WinUI 3 אין MessageBox מובנה; משתמשים ב-Win32 P/Invoke
Native.MessageBoxW(IntPtr.Zero, message, "TapLingo", 0x40 /* MB_ICONINFORMATION */);
}
private static void TryLogError(Exception ex)
{
try
{
var logDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"TapLingo");
Directory.CreateDirectory(logDir);
var logFile = Path.Combine(logDir, "errors.log");
File.AppendAllText(logFile,
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {ex}\n\n");
}
catch { /* nothing we can do */ }
}
}
/// <summary>עטיפת P/Invoke קטנה להודעות מערכת</summary>
internal static class Native
{
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
public static extern int MessageBoxW(IntPtr hWnd, string text, string caption, uint type);
}
}