Draw renders the treeview widget.
(cvs *canvas.Canvas, meta *widgetapi.Meta)
| 616 | |
| 617 | // Draw renders the treeview widget. |
| 618 | func (tv *TreeView) Draw(cvs *canvas.Canvas, meta *widgetapi.Meta) error { |
| 619 | tv.mu.Lock() |
| 620 | defer tv.mu.Unlock() |
| 621 | tv.updateVisibleNodes() |
| 622 | tv.advanceSpinners(time.Now()) |
| 623 | visibleNodes := tv.visibleNodes |
| 624 | totalHeight := len(visibleNodes) |
| 625 | width := cvs.Area().Dx() |
| 626 | tv.canvasWidth = width // Set canvasWidth here |
| 627 | tv.canvasHeight = cvs.Area().Dy() |
| 628 | |
| 629 | // Log canvas dimensions |
| 630 | tv.logger.Printf("Canvas Area: Dx=%d, Dy=%d", tv.canvasWidth, tv.canvasHeight) |
| 631 | |
| 632 | if tv.canvasWidth <= 0 || tv.canvasHeight <= 0 { |
| 633 | return fmt.Errorf("canvas too small") |
| 634 | } |
| 635 | |
| 636 | // Calculate the maximum valid scroll offset |
| 637 | maxScrollOffset := tv.totalContentHeight - tv.canvasHeight |
| 638 | if maxScrollOffset < 0 { |
| 639 | maxScrollOffset = 0 |
| 640 | } |
| 641 | |
| 642 | // Clamp scrollOffset to ensure it stays within valid bounds |
| 643 | if tv.scrollOffset > maxScrollOffset { |
| 644 | tv.scrollOffset = maxScrollOffset |
| 645 | tv.logger.Printf("Clamped scrollOffset to maxScrollOffset: %d", tv.scrollOffset) |
| 646 | } |
| 647 | if tv.scrollOffset < 0 { |
| 648 | tv.scrollOffset = 0 |
| 649 | tv.logger.Printf("Clamped scrollOffset to 0") |
| 650 | } |
| 651 | |
| 652 | tv.logger.Printf("Starting Draw with scrollOffset: %d, totalHeight: %d, canvasHeight: %d", tv.scrollOffset, totalHeight, tv.canvasHeight) |
| 653 | |
| 654 | // Clear the canvas |
| 655 | if err := cvs.Clear(); err != nil { |
| 656 | return err |
| 657 | } |
| 658 | |
| 659 | // Determine the range of nodes to draw |
| 660 | start := tv.scrollOffset |
| 661 | end := tv.scrollOffset + tv.canvasHeight |
| 662 | if end > len(visibleNodes) { |
| 663 | end = len(visibleNodes) |
| 664 | } |
| 665 | |
| 666 | // Slice the visibleNodes to only the range to draw |
| 667 | nodesToDraw := visibleNodes[start:end] |
| 668 | |
| 669 | // Draw nodes |
| 670 | if err := tv.drawNode(cvs, nodesToDraw); err != nil { |
| 671 | tv.logger.Printf("Error drawing nodes: %v", err) |
| 672 | return err |
| 673 | } |
| 674 | |
| 675 | // Draw scroll indicators if needed |
nothing calls this directly
no test coverage detected