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 App = @import("App.zig");
const Window = @import("Window.zig"); const Window = @import("Window.zig");
const UnsafePasteWindow = @import("UnsafePasteWindow.zig");
const inspector = @import("inspector.zig"); const inspector = @import("inspector.zig");
const gtk_key = @import("key.zig"); const gtk_key = @import("key.zig");
const c = @import("c.zig"); const c = @import("c.zig");
const UnsafePasteWindow = @import("UnsafePasteWindow.zig");
const log = std.log.scoped(.gtk); const log = std.log.scoped(.gtk);
/// This is detected by the OpenGL renderer to move to a single-threaded /// This is detected by the OpenGL renderer to move to a single-threaded
@ -550,8 +549,12 @@ fn gtkClipboardRead(
defer c.g_free(cstr); defer c.g_free(cstr);
const str = std.mem.sliceTo(cstr, 0); const str = std.mem.sliceTo(cstr, 0);
self.core_surface.completeClipboardRequest(req.state, str, false) catch |err| { self.core_surface.completeClipboardRequest(
if (err == error.UnsafePaste) { req.state,
str,
false,
) catch |err| switch (err) {
error.UnsafePaste => {
// Create a dialog and ask the user if they want to paste anyway. // Create a dialog and ask the user if they want to paste anyway.
UnsafePasteWindow.create( UnsafePasteWindow.create(
self.app, self.app,
@ -562,9 +565,9 @@ fn gtkClipboardRead(
log.err("failed to create unsafe paste window err={}", .{window_err}); log.err("failed to create unsafe paste window err={}", .{window_err});
}; };
return; 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, core_surface: CoreSurface,
request: ClipboardRequest, request: ClipboardRequest,
) !void { ) !void {
// Create the window // Create the window
const window = c.gtk_window_new(); const window = c.gtk_window_new();
const gtk_window: *c.GtkWindow = @ptrCast(window); const gtk_window: *c.GtkWindow = @ptrCast(window);
errdefer c.gtk_window_destroy(gtk_window); errdefer c.gtk_window_destroy(gtk_window);
c.gtk_window_set_title(gtk_window, "Unsafe Paste!"); c.gtk_window_set_title(gtk_window, "Warning: Potentially Unsafe Paste");
c.gtk_window_set_default_size(gtk_window, 400, 275); c.gtk_window_set_default_size(gtk_window, 600, 275);
c.gtk_window_set_resizable(gtk_window, 0); c.gtk_window_set_resizable(gtk_window, 0);
_ = c.g_signal_connect_data( _ = c.g_signal_connect_data(
window, window,
@ -94,14 +93,10 @@ fn init(
} }
fn gtkDestroy(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void { fn gtkDestroy(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void {
const self = userdataSelf(ud.?); const self: *UnsafePaste = @ptrCast(@alignCast(ud orelse return));
self.destroy(); self.destroy();
} }
fn userdataSelf(ud: *anyopaque) *UnsafePaste {
return @ptrCast(@alignCast(ud));
}
const PrimaryView = struct { const PrimaryView = struct {
root: *c.GtkWidget, root: *c.GtkWidget,
text: *c.GtkTextView, text: *c.GtkTextView,
@ -109,8 +104,8 @@ const PrimaryView = struct {
pub fn init(root: *UnsafePaste, data: []const u8) !PrimaryView { pub fn init(root: *UnsafePaste, data: []const u8) !PrimaryView {
// All our widgets // All our widgets
const label = c.gtk_label_new( const label = c.gtk_label_new(
\\ Pasting this text into the terminal may be dangerous as \\ Pasting this text into the terminal may be dangerous as
\\ unintended commands may be be executed. \\ it looks like some commands may be executed.
); );
const buf = unsafeBuffer(data); const buf = unsafeBuffer(data);
defer c.g_object_unref(buf); defer c.g_object_unref(buf);
@ -205,10 +200,13 @@ const ButtonsView = struct {
} }
fn gtkPasteClick(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void { fn gtkPasteClick(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void {
// Requeue the paste with force.
const self: *UnsafePaste = @ptrCast(@alignCast(ud)); const self: *UnsafePaste = @ptrCast(@alignCast(ud));
self.core_surface.completeClipboardRequest(
// Requeue the paste, this time forcing it. self.pending_req,
self.core_surface.completeClipboardRequest(self.pending_req, self.data, true) catch |err| { self.data,
true,
) catch |err| {
std.log.err("Failed to requeue clipboard request: {}", .{err}); std.log.err("Failed to requeue clipboard request: {}", .{err});
}; };