-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dev/2.0] close context menu on window events (#845)
* [DevToys 2.0] Close context menu on window events * Added support for MacOS * removed comment
- Loading branch information
Showing
9 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/app/dev/DevToys.Blazor/Core/Services/IWindowService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace DevToys.Blazor.Core.Services; | ||
|
||
public interface IWindowService | ||
{ | ||
event EventHandler<EventArgs>? WindowLostFocus; | ||
|
||
event EventHandler<EventArgs>? WindowLocationChanged; | ||
|
||
event EventHandler<EventArgs>? WindowSizeChanged; | ||
|
||
event EventHandler<EventArgs>? WindowClosing; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/app/dev/platforms/desktop/DevToys.MacOS/Core/WindowService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using DevToys.Blazor.Core.Services; | ||
using Foundation; | ||
|
||
namespace DevToys.MacOS.Core; | ||
|
||
internal sealed class WindowService : IWindowService | ||
{ | ||
public event EventHandler<EventArgs>? WindowLostFocus; | ||
public event EventHandler<EventArgs>? WindowLocationChanged; | ||
public event EventHandler<EventArgs>? WindowSizeChanged; | ||
public event EventHandler<EventArgs>? WindowClosing; | ||
|
||
public WindowService() | ||
{ | ||
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSWindowDidResignMainNotification"), OnWindowLostFocus); | ||
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSWindowWillMoveNotification"), OnWindowLocationChanged); | ||
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSWindowWillStartLiveResizeNotification"), OnWindowSizeChanged); | ||
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("NSWindowWillCloseNotification"), OnWindowClosing); | ||
} | ||
|
||
public void OnWindowLostFocus(NSNotification notification) | ||
{ | ||
WindowLostFocus?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
public void OnWindowLocationChanged(NSNotification notification) | ||
{ | ||
WindowLocationChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
public void OnWindowSizeChanged(NSNotification notification) | ||
{ | ||
WindowSizeChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
public void OnWindowClosing(NSNotification notification) | ||
{ | ||
WindowClosing?.Invoke(this, EventArgs.Empty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/app/dev/platforms/desktop/DevToys.Windows/Core/WindowService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Windows; | ||
using DevToys.Blazor.Core.Services; | ||
|
||
namespace DevToys.Windows.Core; | ||
|
||
internal sealed class WindowService : IWindowService | ||
{ | ||
private Window? _window; | ||
|
||
public event EventHandler<EventArgs>? WindowLostFocus; | ||
public event EventHandler<EventArgs>? WindowLocationChanged; | ||
public event EventHandler<EventArgs>? WindowSizeChanged; | ||
public event EventHandler<EventArgs>? WindowClosing; | ||
|
||
internal void SetWindow(Window window) | ||
{ | ||
Guard.IsNull(_window); | ||
Guard.IsNotNull(window); | ||
_window = window; | ||
|
||
_window.LostFocus += Window_LostFocus; | ||
_window.LocationChanged += Window_LocationChanged; | ||
_window.SizeChanged += Window_SizeChanged; | ||
_window.Closing += Window_Closing; | ||
} | ||
|
||
private void Window_LostFocus(object sender, RoutedEventArgs e) | ||
{ | ||
WindowLostFocus?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
private void Window_LocationChanged(object? sender, EventArgs e) | ||
{ | ||
WindowLocationChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
private void Window_SizeChanged(object sender, SizeChangedEventArgs e) | ||
{ | ||
WindowSizeChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
|
||
private void Window_Closing(object? sender, CancelEventArgs e) | ||
{ | ||
WindowClosing?.Invoke(this, EventArgs.Empty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters