Skip to content

Commit d85a64f

Browse files
authored
Merge pull request #1014 from softworkz/submit_new_event
BrowserWindow: Add OnBoundsChanged event
2 parents 53698d1 + c8f1cdf commit d85a64f

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

src/ElectronNET.API/API/BrowserWindow.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ public event Action OnMove
186186
remove => RemoveEvent(value, Id);
187187
}
188188

189+
/// <summary>
190+
/// Emitted when the window is moved or resized.
191+
/// </summary>
192+
/// <remarks>
193+
/// While not being an original Electron event, this one includes the bounds values,
194+
/// saving the additional roundtrip for calling <see cref="GetBoundsAsync"/>.
195+
/// </remarks>
196+
public event Action<Rectangle> OnBoundsChanged
197+
{
198+
add => AddEvent(value, Id);
199+
remove => RemoveEvent(value, Id);
200+
}
201+
189202
/// <summary>
190203
/// macOS: Emitted once when the window is moved to a new position.
191204
/// </summary>

src/ElectronNET.Host/api/browserWindows.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/browserWindows.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ElectronNET.Host/api/browserWindows.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ export = (socket: Socket, app: Electron.App) => {
139139
});
140140
});
141141

142+
socket.on("register-browserWindow-bounds-changed", (id) => {
143+
const window = getWindowById(id);
144+
const cb = () => electronSocket.emit("browserWindow-bounds-changed" + id, window.getBounds());
145+
window.on("resize", cb);
146+
window.on("move", cb);
147+
});
148+
142149
socket.on("register-browserWindow-moved", (id) => {
143150
getWindowById(id).on("moved", () => {
144151
electronSocket.emit("browserWindow-moved" + id);

src/ElectronNET.IntegrationTests/Tests/BrowserWindowTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,57 @@ public async Task Represented_filename_and_edited_flags()
257257

258258
win.SetDocumentEdited(false);
259259
}
260+
261+
[IntegrationFact]
262+
public async Task BoundsChanged_event_fires_with_updated_bounds()
263+
{
264+
BrowserWindow window = null;
265+
266+
try
267+
{
268+
window = await Electron.WindowManager.CreateWindowAsync(
269+
new BrowserWindowOptions { Show = false, Width = 300, Height = 200 },
270+
"about:blank");
271+
272+
var tcs = new TaskCompletionSource<Rectangle>(TaskCreationOptions.RunContinuationsAsynchronously);
273+
window.OnBoundsChanged += bounds => tcs.TrySetResult(bounds);
274+
275+
await Task.Delay(500.ms());
276+
277+
var target = new Rectangle { X = 25, Y = 35, Width = 420, Height = 310 };
278+
window.SetBounds(target);
279+
280+
var completed = await Task.WhenAny(tcs.Task, Task.Delay(3.seconds()));
281+
completed.Should().Be(tcs.Task);
282+
283+
var observed = await tcs.Task;
284+
285+
observed.Width.Should().Be(target.Width);
286+
observed.Height.Should().Be(target.Height);
287+
}
288+
finally
289+
{
290+
window?.Destroy();
291+
}
292+
}
293+
294+
[IntegrationFact]
295+
public async Task BoundsChanged_event_can_fire_on_resize_of_existing_window()
296+
{
297+
var win = this.MainWindow;
298+
299+
var tcs = new TaskCompletionSource<Rectangle>(TaskCreationOptions.RunContinuationsAsynchronously);
300+
win.OnBoundsChanged += bounds => tcs.TrySetResult(bounds);
301+
302+
await Task.Delay(500.ms());
303+
win.SetBounds(new Rectangle { X = 10, Y = 10, Width = 560, Height = 440 });
304+
305+
var completed = await Task.WhenAny(tcs.Task, Task.Delay(3.seconds()));
306+
completed.Should().Be(tcs.Task);
307+
308+
var boundsObserved = await tcs.Task;
309+
boundsObserved.Width.Should().Be(560);
310+
boundsObserved.Height.Should().Be(440);
311+
}
260312
}
261313
}

0 commit comments

Comments
 (0)