Start work on displaying font sampler images

This commit is contained in:
Kovid Goyal
2024-05-08 21:09:39 +05:30
parent 314b2444c7
commit 6d751b94f6
3 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,71 @@
package choose_fonts
import (
"fmt"
"strings"
"kitty/tools/tui/graphics"
"kitty/tools/tui/loop"
)
var _ = fmt.Print
type image struct {
id, image_number uint32
current_file string
}
type graphics_manager struct {
main, bold, italic, bi, extra image
images [5]*image
}
func (g *graphics_manager) initialize(lp *loop.Loop) {
g.images = [5]*image{&g.main, &g.bold, &g.italic, &g.bi, &g.extra}
payload := []byte("123")
buf := strings.Builder{}
gc := &graphics.GraphicsCommand{}
gc.SetImageNumber(7891230).SetTransmission(graphics.GRT_transmission_direct).SetDataWidth(1).SetDataHeight(1).SetFormat(
graphics.GRT_format_rgb).SetDataSize(uint64(len(payload)))
d := func() uint32 {
im := gc.ImageNumber()
im++
gc.SetImageNumber(im)
_ = gc.WriteWithPayloadTo(&buf, payload)
return im
}
for _, img := range g.images {
img.image_number = d()
}
lp.QueueWriteString(buf.String())
}
func (g *graphics_manager) on_response(gc *graphics.GraphicsCommand) (err error) {
if gc.ResponseMessage() != "OK" {
return fmt.Errorf("Failed to load image with error: %s", gc.ResponseMessage())
}
for _, img := range g.images {
if img.image_number == gc.ImageNumber() {
img.id = gc.ImageId()
break
}
}
return
}
func (g *graphics_manager) finalize(lp *loop.Loop) {
buf := strings.Builder{}
gc := &graphics.GraphicsCommand{}
gc.SetAction(graphics.GRT_action_delete).SetDelete(graphics.GRT_free_by_number)
d := func(n uint32) {
gc.SetImageNumber(n)
gc.WriteWithPayloadTo(&buf, nil)
}
d(g.main.image_number)
d(g.bold.image_number)
d(g.italic.image_number)
d(g.bi.image_number)
d(g.extra.image_number)
lp.QueueWriteString(buf.String())
}

View File

@@ -38,6 +38,7 @@ func main() (rc int, err error) {
return "", nil
}
lp.OnWakeup = h.on_wakeup
lp.OnEscapeCode = h.on_escape_code
lp.OnFinalize = func() string {
h.finalize()
lp.SetCursorVisible(true)

View File

@@ -7,6 +7,7 @@ import (
"sync"
"kitty/tools/tui"
"kitty/tools/tui/graphics"
"kitty/tools/tui/loop"
"kitty/tools/tui/readline"
"kitty/tools/utils"
@@ -42,6 +43,7 @@ type handler struct {
render_count uint
render_lines tui.RenderLines
text_style TextStyle
graphics_manager graphics_manager
// Listing
rl *readline.Readline
@@ -275,6 +277,7 @@ func (h *handler) initialize() {
h.variable_data_requested_for = utils.NewSet[string](256)
h.draw_screen()
initialize_variable_data_cache()
h.graphics_manager.initialize(h.lp)
go func() {
h.set_worker_error(kitty_font_backend.query("list_monospaced_fonts", nil, &h.fonts))
h.lp.WakeupMainThread()
@@ -284,6 +287,7 @@ func (h *handler) initialize() {
func (h *handler) finalize() {
h.lp.SetCursorVisible(true)
h.lp.SetCursorShape(loop.BLOCK_CURSOR, true)
h.graphics_manager.finalize(h.lp)
}
func (h *handler) on_query_response(key, val string, valid bool) error {
@@ -389,4 +393,15 @@ func (h *handler) on_text(text string, from_key_event bool, in_bracketed_paste b
return
}
func (h *handler) on_escape_code(etype loop.EscapeCodeType, payload []byte) error {
switch etype {
case loop.APC:
gc := graphics.GraphicsCommandFromAPC(payload)
if gc != nil {
return h.graphics_manager.on_response(gc)
}
}
return nil
}
// }}}