core: use arrays instead of WriteReq for desktop notifications

This commit is contained in:
Gregory Anders
2023-11-15 11:09:27 -06:00
parent 86b7442f3c
commit 689199251a
3 changed files with 12 additions and 34 deletions

View File

@ -722,18 +722,8 @@ pub fn handleMessage(self: *Surface, msg: Message) !void {
return; return;
} }
const title: [:0]const u8 = switch (notification.title) { const title = std.mem.sliceTo(&notification.title, 0);
.small => |v| v.data[0..v.len :0], const body = std.mem.sliceTo(&notification.body, 0);
// Stream handler only sends small messages
else => unreachable,
};
const body: [:0]const u8 = switch (notification.body) {
.small => |v| v.data[0..v.len :0],
// Stream handler only sends small messages
else => unreachable,
};
try self.showDesktopNotification(title, body); try self.showDesktopNotification(title, body);
}, },
} }

View File

@ -49,10 +49,10 @@ pub const Message = union(enum) {
/// Show a desktop notification. /// Show a desktop notification.
desktop_notification: struct { desktop_notification: struct {
/// Desktop notification title. /// Desktop notification title.
title: WriteReq, title: [63:0]u8,
/// Desktop notification body. /// Desktop notification body.
body: WriteReq, body: [255:0]u8,
}, },
}; };

View File

@ -2394,28 +2394,16 @@ const StreamHandler = struct {
title: []const u8, title: []const u8,
body: []const u8, body: []const u8,
) !void { ) !void {
// Subtract one to leave room for the sentinel var message = apprt.surface.Message{ .desktop_notification = undefined };
const max_length = apprt.surface.Message.WriteReq.Small.Max - 1;
if (title.len >= max_length or body.len >= max_length) {
log.warn("requested notification is too long", .{});
return;
}
var req_title: apprt.surface.Message.WriteReq.Small = .{}; const title_len = @min(title.len, message.desktop_notification.title.len);
@memcpy(req_title.data[0..title.len], title); @memcpy(message.desktop_notification.title[0..title_len], title[0..title_len]);
req_title.data[title.len] = 0; message.desktop_notification.title[title_len] = 0;
req_title.len = @intCast(title.len);
var req_body: apprt.surface.Message.WriteReq.Small = .{}; const body_len = @min(body.len, message.desktop_notification.body.len);
@memcpy(req_body.data[0..body.len], body); @memcpy(message.desktop_notification.body[0..body_len], body[0..body_len]);
req_body.data[body.len] = 0; message.desktop_notification.body[body_len] = 0;
req_body.len = @intCast(body.len);
_ = self.ev.surface_mailbox.push(.{ _ = self.ev.surface_mailbox.push(message, .{ .forever = {} });
.desktop_notification = .{
.title = .{ .small = req_title },
.body = .{ .small = req_body },
},
}, .{ .forever = {} });
} }
}; };