terminal/kitty-gfx: assign an internal image id

This commit is contained in:
Mitchell Hashimoto
2023-08-20 16:59:20 -07:00
parent b3a3ca1182
commit e84f82bd97
2 changed files with 15 additions and 1 deletions

View File

@ -108,6 +108,9 @@ fn transmit(
}; };
img.deinit(alloc); img.deinit(alloc);
// After the image is added, set the ID in case it changed
result.id = img.id;
return result; return result;
} }
@ -182,8 +185,15 @@ fn loadAndAddImage(
var img = try Image.load(alloc, cmd); var img = try Image.load(alloc, cmd);
errdefer img.deinit(alloc); errdefer img.deinit(alloc);
// If the image has no ID, we assign one
const storage = &terminal.screen.kitty_images;
if (img.id == 0) {
img.id = storage.next_id;
storage.next_id +%= 1;
}
// Store our image // Store our image
try terminal.screen.kitty_images.addImage(alloc, img); try storage.addImage(alloc, img);
return img; return img;
} }

View File

@ -16,6 +16,10 @@ pub const ImageStorage = struct {
const ImageMap = std.AutoHashMapUnmanaged(u32, Image); const ImageMap = std.AutoHashMapUnmanaged(u32, Image);
const PlacementMap = std.AutoHashMapUnmanaged(PlacementKey, Placement); const PlacementMap = std.AutoHashMapUnmanaged(PlacementKey, Placement);
/// This is the next automatically assigned ID. We start mid-way
/// through the u32 range to avoid collisions with buggy programs.
next_id: u32 = 2147483647,
/// The set of images that are currently known. /// The set of images that are currently known.
images: ImageMap = .{}, images: ImageMap = .{},