From 2c0dbab7baa4fe6868a209664bb00fd1c7878851 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 25 Mar 2023 16:26:30 -0700 Subject: [PATCH] apprt/gtk: always confirm when surface is closed --- src/apprt/gtk.zig | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/apprt/gtk.zig b/src/apprt/gtk.zig index 5d6937aa7..ce5e7d60f 100644 --- a/src/apprt/gtk.zig +++ b/src/apprt/gtk.zig @@ -636,7 +636,29 @@ pub const Surface = struct { /// Close this surface. pub fn close(self: *Surface) void { - self.window.closeSurface(self); + // I'd like to make this prettier one day: + // - Make the "Yes" button red + // - Make the "No" button default + // + const alert = c.gtk_message_dialog_new( + self.window.window, + c.GTK_DIALOG_MODAL, + c.GTK_MESSAGE_QUESTION, + c.GTK_BUTTONS_YES_NO, + "Close this terminal?", + ); + c.gtk_message_dialog_format_secondary_text( + @ptrCast(*c.GtkMessageDialog, alert), + "There is still a running process in the terminal. " ++ + "Closing the terminal will kill this process. " ++ + "Are you sure you want to close the terminal?" ++ + "Click 'No' to cancel and return to your terminal.", + ); + + _ = c.g_signal_connect_data(alert, "response", c.G_CALLBACK(>kCloseConfirmation), self, null, G_CONNECT_DEFAULT); + + c.gtk_widget_show(alert); + //self.window.closeSurface(self); } pub fn newTab(self: *Surface) !void { @@ -1010,6 +1032,18 @@ pub const Surface = struct { }; } + fn gtkCloseConfirmation( + alert: *c.GtkMessageDialog, + response: c.gint, + ud: ?*anyopaque, + ) callconv(.C) void { + c.gtk_window_destroy(@ptrCast(*c.GtkWindow, alert)); + if (response == c.GTK_RESPONSE_YES) { + const self = userdataSelf(ud.?); + self.window.closeSurface(self); + } + } + fn userdataSelf(ud: *anyopaque) *Surface { return @ptrCast(*Surface, @alignCast(@alignOf(Surface), ud)); }