27
27
using static Flow . Launcher . ViewModel . SettingWindowViewModel ;
28
28
using DataObject = System . Windows . DataObject ;
29
29
using System . Windows . Media ;
30
+ using System . Windows . Interop ;
31
+ using System . Runtime . InteropServices ;
30
32
31
33
namespace Flow . Launcher
32
34
{
33
35
public partial class MainWindow
34
36
{
35
37
#region Private Fields
36
38
39
+ [ DllImport ( "user32.dll" , SetLastError = true , CharSet = CharSet . Unicode ) ]
40
+ public static extern IntPtr SetForegroundWindow ( IntPtr hwnd ) ;
41
+
37
42
private readonly Storyboard _progressBarStoryboard = new Storyboard ( ) ;
38
43
private bool isProgressBarStoryboardPaused ;
39
44
private Settings _settings ;
40
45
private NotifyIcon _notifyIcon ;
41
- private ContextMenu contextMenu ;
46
+ private ContextMenuStrip contextMenu ;
42
47
private MainViewModel _viewModel ;
43
48
private bool _animating ;
44
49
MediaPlayer animationSound = new MediaPlayer ( ) ;
@@ -258,64 +263,69 @@ private void InitializePosition()
258
263
private void UpdateNotifyIconText ( )
259
264
{
260
265
var menu = contextMenu ;
261
- ( ( MenuItem ) menu . Items [ 0 ] ) . Header = InternationalizationManager . Instance . GetTranslation ( "iconTrayOpen" ) + " (" + _settings . Hotkey + ")" ;
262
- ( ( MenuItem ) menu . Items [ 1 ] ) . Header = InternationalizationManager . Instance . GetTranslation ( "GameMode" ) ;
263
- ( ( MenuItem ) menu . Items [ 2 ] ) . Header = InternationalizationManager . Instance . GetTranslation ( "PositionReset" ) ;
264
- ( ( MenuItem ) menu . Items [ 3 ] ) . Header = InternationalizationManager . Instance . GetTranslation ( "iconTraySettings" ) ;
265
- ( ( MenuItem ) menu . Items [ 4 ] ) . Header = InternationalizationManager . Instance . GetTranslation ( "iconTrayExit" ) ;
266
+ // ((MenuItem)menu.Items[0]).Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + " (" + _settings.Hotkey + ")";
267
+ // ((MenuItem)menu.Items[1]).Header = InternationalizationManager.Instance.GetTranslation("GameMode");
268
+ // ((MenuItem)menu.Items[2]).Header = InternationalizationManager.Instance.GetTranslation("PositionReset");
269
+ // ((MenuItem)menu.Items[3]).Header = InternationalizationManager.Instance.GetTranslation("iconTraySettings");
270
+ // ((MenuItem)menu.Items[4]).Header = InternationalizationManager.Instance.GetTranslation("iconTrayExit");
266
271
267
272
}
268
273
269
274
private void InitializeNotifyIcon ( )
270
275
{
271
276
_notifyIcon = new NotifyIcon
272
277
{
273
- Text = Infrastructure . Constant . FlowLauncher ,
278
+ Text = Infrastructure . Constant . FlowLauncherFullName ,
274
279
Icon = Properties . Resources . app ,
275
280
Visible = ! _settings . HideNotifyIcon
276
281
} ;
277
282
278
- contextMenu = new ContextMenu ( ) ;
283
+ var contextMenu = new ContextMenu ( ) ;
279
284
280
285
var openIcon = new FontIcon
281
286
{
282
287
Glyph = "\ue71e "
283
288
} ;
284
289
var open = new MenuItem
285
290
{
286
- Header = InternationalizationManager . Instance . GetTranslation ( "iconTrayOpen" ) + " (" + _settings . Hotkey + ")" , Icon = openIcon
291
+ Header = InternationalizationManager . Instance . GetTranslation ( "iconTrayOpen" ) + " (" + _settings . Hotkey + ")" ,
292
+ Icon = openIcon
287
293
} ;
288
294
var gamemodeIcon = new FontIcon
289
295
{
290
296
Glyph = "\ue7fc "
291
297
} ;
292
298
var gamemode = new MenuItem
293
299
{
294
- Header = InternationalizationManager . Instance . GetTranslation ( "GameMode" ) , Icon = gamemodeIcon
300
+ Header = InternationalizationManager . Instance . GetTranslation ( "GameMode" ) ,
301
+ Icon = gamemodeIcon
295
302
} ;
296
303
var positionresetIcon = new FontIcon
297
304
{
298
305
Glyph = "\ue73f "
299
306
} ;
300
307
var positionreset = new MenuItem
301
308
{
302
- Header = InternationalizationManager . Instance . GetTranslation ( "PositionReset" ) , Icon = positionresetIcon
309
+ Header = InternationalizationManager . Instance . GetTranslation ( "PositionReset" ) ,
310
+ Icon = positionresetIcon
303
311
} ;
304
312
var settingsIcon = new FontIcon
305
313
{
306
314
Glyph = "\ue713 "
307
315
} ;
308
316
var settings = new MenuItem
309
317
{
310
- Header = InternationalizationManager . Instance . GetTranslation ( "iconTraySettings" ) , Icon = settingsIcon
318
+ Header = InternationalizationManager . Instance . GetTranslation ( "iconTraySettings" ) ,
319
+ Icon = settingsIcon
311
320
} ;
312
321
var exitIcon = new FontIcon
313
322
{
314
323
Glyph = "\ue7e8 "
315
324
} ;
316
325
var exit = new MenuItem
317
326
{
318
- Header = InternationalizationManager . Instance . GetTranslation ( "iconTrayExit" ) , Icon = exitIcon
327
+ Header = InternationalizationManager . Instance . GetTranslation ( "iconTrayExit" ) ,
328
+ Icon = exitIcon
319
329
} ;
320
330
321
331
open . Click += ( o , e ) => _viewModel . ToggleFlowLauncher ( ) ;
@@ -333,17 +343,22 @@ private void InitializeNotifyIcon()
333
343
contextMenu . Items . Add ( settings ) ;
334
344
contextMenu . Items . Add ( exit ) ;
335
345
336
- _notifyIcon . ContextMenuStrip = new ContextMenuStrip ( ) ; // it need for close the context menu. if not, context menu can't close.
337
346
_notifyIcon . MouseClick += ( o , e ) =>
338
347
{
339
348
switch ( e . Button )
340
349
{
341
350
case MouseButtons . Left :
342
351
_viewModel . ToggleFlowLauncher ( ) ;
343
352
break ;
344
-
345
353
case MouseButtons . Right :
354
+
346
355
contextMenu . IsOpen = true ;
356
+ // Get context menu handle and bring it to the foreground
357
+ if ( PresentationSource . FromVisual ( contextMenu ) is HwndSource hwndSource )
358
+ {
359
+ _ = SetForegroundWindow ( hwndSource . Handle ) ;
360
+ }
361
+ contextMenu . Focus ( ) ;
347
362
break ;
348
363
}
349
364
} ;
0 commit comments