-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
233 lines (192 loc) · 7.55 KB
/
App.xaml.cs
File metadata and controls
233 lines (192 loc) · 7.55 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
using WF = System.Windows.Forms;
namespace DeepLocal
{
public partial class App : Application
{
public static bool AllowClose { get; private set; } = false;
private static WF.NotifyIcon? _tray;
private static Mutex? _singleInstanceMutex;
private WF.ToolStripMenuItem? _miOpen;
private WF.ToolStripMenuItem? _miHide;
// ===== DPI helper =====
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern uint GetDpiForWindow(IntPtr hwnd);
private static double GetScale(Window w)
{
try
{
var hwnd = new WindowInteropHelper(w).Handle;
if (hwnd != IntPtr.Zero)
{
uint dpi = GetDpiForWindow(hwnd);
return dpi / 96.0;
}
}
catch { }
return 1.0;
}
public static void PlaceWindowBottomRight(Window w, double marginDip = 16.0)
{
if (w == null) return;
var helper = new WindowInteropHelper(w);
var hwnd = helper.Handle;
var screen = hwnd != IntPtr.Zero
? WF.Screen.FromHandle(hwnd)
: WF.Screen.FromPoint(WF.Cursor.Position);
var waPx = screen.WorkingArea;
double scale = GetScale(w);
double widthDip = (w.ActualWidth > 0) ? w.ActualWidth : (!double.IsNaN(w.Width) && w.Width > 0 ? w.Width : 900);
double heightDip = (w.ActualHeight > 0) ? w.ActualHeight : (!double.IsNaN(w.Height) && w.Height > 0 ? w.Height : 600);
int widthPx = (int)Math.Round(widthDip * scale);
int heightPx = (int)Math.Round(heightDip * scale);
int marginPx = (int)Math.Round(marginDip * scale);
int leftPx = waPx.Right - marginPx - widthPx;
int topPx = waPx.Bottom - marginPx - heightPx;
w.WindowStartupLocation = WindowStartupLocation.Manual;
w.Left = leftPx / scale;
w.Top = topPx / scale;
double waLeftDip = waPx.Left / scale;
double waTopDip = waPx.Top / scale;
double waRightDip = waPx.Right / scale;
double waBottomDip = waPx.Bottom / scale;
if (w.Left < waLeftDip) w.Left = waLeftDip;
if (w.Top < waTopDip) w.Top = waTopDip;
if (w.Left + widthDip > waRightDip) w.Left = Math.Max(waLeftDip, waRightDip - widthDip);
if (w.Top + heightDip > waBottomDip) w.Top = Math.Max(waTopDip, waBottomDip - heightDip);
}
public static void ResnapAfterFirstLayout(Window w)
{
EventHandler? handler = null;
handler = (s, e) =>
{
if (w.ActualWidth <= 0 || w.ActualHeight <= 0) return;
PlaceWindowBottomRight(w);
w.LayoutUpdated -= handler!;
};
w.LayoutUpdated += handler!;
}
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
bool createdNew = false;
_singleInstanceMutex = new Mutex(true, "DeepLocal_OfflineTranslator_SingleInstance", out createdNew);
if (!createdNew)
{
Shutdown();
return;
}
if (_tray == null)
{
_tray = new WF.NotifyIcon
{
Visible = true,
Text = "DeepLocal – Offline Translator",
Icon = LoadTrayIcon()
};
var menu = new WF.ContextMenuStrip();
_miOpen = new WF.ToolStripMenuItem("Open", null, (_, __) => ShowMainWindow());
_miHide = new WF.ToolStripMenuItem("Hide window", null, (_, __) => HideMainWindow());
menu.Items.Add(_miOpen);
menu.Items.Add("Translate from clipboard (Alt+T)", null, async (_, __) =>
{
if (Current.MainWindow is MainWindow mw) await mw.TranslateClipboardAsync();
});
menu.Items.Add(_miHide);
menu.Items.Add(new WF.ToolStripSeparator());
menu.Items.Add("Exit", null, (_, __) =>
{
AllowClose = true;
Current.Shutdown();
});
_tray.ContextMenuStrip = menu;
_tray.DoubleClick += (_, __) => ToggleMainWindow();
}
var win = new MainWindow();
this.MainWindow = win;
PlaceWindowBottomRight(win);
win.SourceInitialized += (_, __) => PlaceWindowBottomRight(win);
win.Loaded += (_, __) => ResnapAfterFirstLayout(win);
win.Show();
win.Activate();
UpdateMenuItems();
}
private static System.Drawing.Icon LoadTrayIcon()
{
try
{
string baseDir = AppContext.BaseDirectory;
string ico = Path.Combine(baseDir, "Assets", "DeepLocal_UserIcon_Framed.ico");
if (File.Exists(ico)) return new System.Drawing.Icon(ico);
string alt = Path.Combine(baseDir, "Assets", "deeplocal.ico");
if (File.Exists(alt)) return new System.Drawing.Icon(alt);
}
catch { }
return System.Drawing.SystemIcons.Application;
}
private static Window? MainWin => Current?.MainWindow as Window;
private void ShowMainWindow()
{
var w = MainWin;
if (w == null) return;
w.Dispatcher.Invoke(() =>
{
PlaceWindowBottomRight(w);
ResnapAfterFirstLayout(w);
if (!w.IsVisible) w.Show();
w.ShowInTaskbar = true;
if (w.WindowState == WindowState.Minimized) w.WindowState = WindowState.Normal;
w.Activate();
UpdateMenuItems();
});
}
private void HideMainWindow()
{
var w = MainWin;
if (w == null) return;
w.Dispatcher.Invoke(() =>
{
w.Hide();
w.ShowInTaskbar = false;
UpdateMenuItems();
});
}
private void ToggleMainWindow()
{
var w = MainWin;
if (w == null) return;
bool visible = w.IsVisible && w.WindowState != WindowState.Minimized;
if (visible) HideMainWindow();
else ShowMainWindow();
}
private void UpdateMenuItems()
{
var w = MainWin;
if (w == null) return;
bool visible = w.IsVisible && w.WindowState != WindowState.Minimized;
if (_miOpen != null) _miOpen.Enabled = !visible;
if (_miHide != null) _miHide.Enabled = visible;
}
protected override void OnExit(ExitEventArgs e)
{
AllowClose = true;
if (_tray != null)
{
try { _tray.Visible = false; } catch { }
try { _tray.Dispose(); } catch { }
_tray = null;
}
if (_singleInstanceMutex != null)
{
try { _singleInstanceMutex.ReleaseMutex(); } catch { }
_singleInstanceMutex.Dispose();
_singleInstanceMutex = null;
}
base.OnExit(e);
}
}
}