From a2255e979f33a96af8df965e12c75b12fbeef832 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 9 Oct 2025 10:12:32 +0530 Subject: [PATCH] Only retransmit placements when actually needed --- kittens/choose_files/graphics.go | 39 ++++++++++++++++++++++++-------- kittens/choose_files/main.go | 19 ++++++++-------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/kittens/choose_files/graphics.go b/kittens/choose_files/graphics.go index 917e3a79f..155b88d1e 100644 --- a/kittens/choose_files/graphics.go +++ b/kittens/choose_files/graphics.go @@ -18,6 +18,15 @@ import ( var _ = fmt.Print +type placement struct { + gc *graphics.GraphicsCommand + x, y, x_offset int +} + +func (p placement) equal(o placement) bool { + return p.x == o.x && p.x_offset == o.x_offset && p.y == o.y +} + type GraphicsHandler struct { running_in_tmux bool image_id_counter, detection_file_id uint32 @@ -28,8 +37,8 @@ type GraphicsHandler struct { width, height int image_width, image_height int } - image_transmitted uint32 - has_placements bool + image_transmitted uint32 + current_placement, last_transmitted_placement placement } func (self *GraphicsHandler) Cleanup() { @@ -86,11 +95,21 @@ func (self *GraphicsHandler) Finalize(lp *loop.Loop) { } func (self *GraphicsHandler) ClearPlacements(lp *loop.Loop) { - if self.image_transmitted > 0 && self.has_placements { + self.current_placement.gc = nil +} + +func (self *GraphicsHandler) ApplyPlacements(lp *loop.Loop) { + if self.current_placement.gc == nil { g := self.new_graphics_command() g.SetAction(graphics.GRT_action_delete).SetDelete(graphics.GRT_delete_by_id).SetImageId(self.image_transmitted) _ = g.WriteWithPayloadToLoop(lp, nil) - self.has_placements = false + self.last_transmitted_placement.gc = nil + } else { + if self.last_transmitted_placement.gc == nil || !self.current_placement.equal(self.last_transmitted_placement) { + lp.MoveCursorTo(self.current_placement.x, self.current_placement.y) + _ = self.current_placement.gc.WriteWithPayloadToLoop(lp, nil) + self.last_transmitted_placement = self.current_placement + } } } @@ -222,17 +241,17 @@ func (self *GraphicsHandler) transmit(lp *loop.Loop, img *images.ImageData, m *i } -func (self *GraphicsHandler) place_image(lp *loop.Loop, x, y, px_width int, sz ScreenSize) { - self.has_placements = true +func (self *GraphicsHandler) place_image(x, y, px_width int, sz ScreenSize) { gc := self.new_graphics_command() gc.SetAction(graphics.GRT_action_display).SetImageId(self.image_transmitted).SetPlacementId(1).SetCursorMovement(graphics.GRT_cursor_static) if extra := px_width - self.last_rendered_image.image_width; extra > 1 { extra /= 2 x += extra / sz.cell_width - gc.SetXOffset(uint64(extra % sz.cell_width)) + self.current_placement.x_offset = extra % sz.cell_width + gc.SetXOffset(uint64(self.current_placement.x_offset)) } - lp.MoveCursorTo(x, y) - _ = gc.WriteWithPayloadToLoop(lp, nil) + self.current_placement.x, self.current_placement.y = x, y + self.current_placement.gc = gc } func (self *GraphicsHandler) RenderImagePreview(h *Handler, p *ImagePreview, x, y, width, height int) { @@ -245,7 +264,7 @@ func (self *GraphicsHandler) RenderImagePreview(h *Handler, p *ImagePreview, x, if err != nil { NewErrorPreview(fmt.Errorf("Failed to render image: %w", err)).Render(h, x, y, width, height) } else if self.image_transmitted > 0 { - self.place_image(h.lp, x, y, px_width, sz) + self.place_image(x, y, px_width, sz) } }() if self.last_rendered_image.p == p && self.last_rendered_image.width == width && self.last_rendered_image.height == height { diff --git a/kittens/choose_files/main.go b/kittens/choose_files/main.go index 6124ea5b5..383affc6e 100644 --- a/kittens/choose_files/main.go +++ b/kittens/choose_files/main.go @@ -238,6 +238,16 @@ func (h *Handler) draw_screen() (err error) { defer func() { h.state.mouse_state.UpdateHoveredIds() h.state.mouse_state.ApplyHoverStyles(h.lp) + h.graphics_handler.ApplyPlacements(h.lp) + if h.state.screen == NORMAL { // so that the cursor ends up in the right place + h.lp.MoveCursorTo(1, 1) + if h.state.DisplayTitle() { + h.lp.Println(h.state.WindowTitle()) + h.draw_search_bar(1) + } else { + h.draw_search_bar(0) + } + } h.lp.EndAtomicUpdate() }() h.lp.ClearScreenButNotGraphics() @@ -247,15 +257,6 @@ func (h *Handler) draw_screen() (err error) { case NORMAL: matches, is_complete := h.get_results() h.lp.SetWindowTitle(h.state.WindowTitle()) - defer func() { // so that the cursor ends up in the right place - h.lp.MoveCursorTo(1, 1) - if h.state.DisplayTitle() { - h.lp.Println(h.state.WindowTitle()) - h.draw_search_bar(1) - } else { - h.draw_search_bar(0) - } - }() y := SEARCH_BAR_HEIGHT + utils.IfElse(h.state.DisplayTitle(), 1, 0) footer_height, err := h.draw_footer() if err != nil {