apprt/gtk: copy change for unsafe paste window

This commit is contained in:
Mitchell Hashimoto
2023-11-04 11:24:48 -07:00
parent a38220eade
commit ab9a9b6eb1
2 changed files with 20 additions and 19 deletions

View File

@ -13,12 +13,11 @@ const CoreSurface = @import("../../Surface.zig");
const App = @import("App.zig");
const Window = @import("Window.zig");
const UnsafePasteWindow = @import("UnsafePasteWindow.zig");
const inspector = @import("inspector.zig");
const gtk_key = @import("key.zig");
const c = @import("c.zig");
const UnsafePasteWindow = @import("UnsafePasteWindow.zig");
const log = std.log.scoped(.gtk);
/// This is detected by the OpenGL renderer to move to a single-threaded
@ -550,8 +549,12 @@ fn gtkClipboardRead(
defer c.g_free(cstr);
const str = std.mem.sliceTo(cstr, 0);
self.core_surface.completeClipboardRequest(req.state, str, false) catch |err| {
if (err == error.UnsafePaste) {
self.core_surface.completeClipboardRequest(
req.state,
str,
false,
) catch |err| switch (err) {
error.UnsafePaste => {
// Create a dialog and ask the user if they want to paste anyway.
UnsafePasteWindow.create(
self.app,
@ -562,9 +565,9 @@ fn gtkClipboardRead(
log.err("failed to create unsafe paste window err={}", .{window_err});
};
return;
}
},
log.err("failed to complete clipboard request err={}", .{err});
else => log.err("failed to complete clipboard request err={}", .{err}),
};
}

View File

@ -55,13 +55,12 @@ fn init(
core_surface: CoreSurface,
request: ClipboardRequest,
) !void {
// Create the window
const window = c.gtk_window_new();
const gtk_window: *c.GtkWindow = @ptrCast(window);
errdefer c.gtk_window_destroy(gtk_window);
c.gtk_window_set_title(gtk_window, "Unsafe Paste!");
c.gtk_window_set_default_size(gtk_window, 400, 275);
c.gtk_window_set_title(gtk_window, "Warning: Potentially Unsafe Paste");
c.gtk_window_set_default_size(gtk_window, 600, 275);
c.gtk_window_set_resizable(gtk_window, 0);
_ = c.g_signal_connect_data(
window,
@ -94,14 +93,10 @@ fn init(
}
fn gtkDestroy(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void {
const self = userdataSelf(ud.?);
const self: *UnsafePaste = @ptrCast(@alignCast(ud orelse return));
self.destroy();
}
fn userdataSelf(ud: *anyopaque) *UnsafePaste {
return @ptrCast(@alignCast(ud));
}
const PrimaryView = struct {
root: *c.GtkWidget,
text: *c.GtkTextView,
@ -109,8 +104,8 @@ const PrimaryView = struct {
pub fn init(root: *UnsafePaste, data: []const u8) !PrimaryView {
// All our widgets
const label = c.gtk_label_new(
\\ Pasting this text into the terminal may be dangerous as
\\ unintended commands may be be executed.
\\ Pasting this text into the terminal may be dangerous as
\\ it looks like some commands may be executed.
);
const buf = unsafeBuffer(data);
defer c.g_object_unref(buf);
@ -205,10 +200,13 @@ const ButtonsView = struct {
}
fn gtkPasteClick(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void {
// Requeue the paste with force.
const self: *UnsafePaste = @ptrCast(@alignCast(ud));
// Requeue the paste, this time forcing it.
self.core_surface.completeClipboardRequest(self.pending_req, self.data, true) catch |err| {
self.core_surface.completeClipboardRequest(
self.pending_req,
self.data,
true,
) catch |err| {
std.log.err("Failed to requeue clipboard request: {}", .{err});
};