Skip to content

Commit

Permalink
ui/cocoa: Stop application when window closed. #15
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed Jun 29, 2024
1 parent da85091 commit fedfdf8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ui/internal/cocoa/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
didMove WindowEvent = 1
didMiniaturize WindowEvent = 2
didDeminiaturize WindowEvent = 3
shouldClose WindowEvent = 4
)

// EventHandler - handler functions that accepts the updated window as parameter
Expand Down Expand Up @@ -201,6 +202,10 @@ func (wnd *Window) OnDidMove(fn EventHandler) {
wnd.callbacks[didMove] = fn
}

func (wnd *Window) OnShouldClose(fn EventHandler) {
wnd.callbacks[shouldClose] = fn
}

//export onWindowEvent
func onWindowEvent(id C.int, eventID C.int, x C.int, y C.int, w C.int, h C.int) {
windowID := int(id)
Expand Down
11 changes: 11 additions & 0 deletions ui/internal/cocoa/windowdelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const int DID_MOVE_EVENT = 1;
const int DID_MINIATURIZE_EVENT = 2;
const int DID_DEMINIATURIZE_EVENT = 3;
const int SHOULD_CLOSE_EVENT = 4;

@implementation WindowDelegate

Expand Down Expand Up @@ -36,6 +37,16 @@ - (void)windowDidDeminiaturize:(NSNotification *)notification {
DID_DEMINIATURIZE_EVENT);
}

- (BOOL)windowShouldClose:(NSWindow *)sender {
// TODO: In the future, we should send the event to Go and let Go decide
// and allow the Go side to optionally trigger some alternative
// behavior for secondary windows.
// triggerEvent([self goWindowID], sender, @"windowShouldClose",
// SHOULD_CLOSE_EVENT);
[NSApp stop:sender];
return YES;
}

@end

void triggerEvent(int goWindowID, NSWindow *movedWindow, NSString *eventTitle,
Expand Down
4 changes: 4 additions & 0 deletions ui/window_cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (w *Window) Update(nextComponent spot.Control) bool {
func (w *Window) Mount(parent spot.Control) any {
w.ref = cocoa.NewCenteredWindow(w.Title, w.Width, w.Height)
w.ref.SetAllowsResizing(w.Resizable)
// w.ref.SetCloseButtonEnabled(false)
// w.ref.OnShouldClose(func(wnd *cocoa.Window) {
// fmt.Println("Window closed")
// })

w.ref.MakeKeyAndOrderFront()
w.ref.AddDefaultQuitMenu()
Expand Down

0 comments on commit fedfdf8

Please sign in to comment.