classes.c90_test_run

  1import vanilla
  2from vanilla import dialogs
  3from vanilla.test.testTools import executeVanillaTest
  4import drawBot
  5from drawBot.ui.drawView import DrawView
  6from AppKit import NSScreen
  7from typing import Callable
  8
  9
 10class TestRun:
 11    """Execute a function in a GUI window using DrawBot and Vanilla."""
 12
 13    def __init__(
 14        self,
 15        func: Callable,
 16        windowRatio: float = 2 / 3,
 17        windowSize: int = 1,
 18        scale: int = 1,
 19        ui: bool = True,
 20    ):
 21        """
 22        Initialize the TestRun GUI environment.
 23
 24        Args:
 25            func: The function to execute and display in the GUI.
 26            windowRatio: The width/height ratio of the window.
 27            windowSize: The scaling factor for the window size relative to the screen.
 28            scale: The scale factor for the canvas rendering.
 29            ui: Whether to show UI controls (refresh and save buttons).
 30        """
 31        self._windowRatio = windowRatio
 32        self._windowSize = windowSize
 33        self._scale = scale
 34        self._ui = ui
 35
 36        executeVanillaTest(self.run(func))
 37
 38    def run(self, func):
 39        """
 40        Set up and run the GUI window, rendering the provided function.
 41
 42        Args:
 43            func: The function to execute and render on the canvas.
 44        """
 45
 46        def getScreenSize():
 47            """
 48            Calculate the window size based on screen dimensions and windowRatio/windowSize.
 49
 50            Returns:
 51                tuple: (window width, window height)
 52            """
 53            screenSize = NSScreen.mainScreen().frame().size
 54            isLandscape = self._windowRatio >= 1
 55            if isLandscape:
 56                screenW = screenSize.width * self._windowSize
 57                screenH = screenW / self._windowRatio
 58            else:
 59                screenH = screenSize.height * self._windowSize
 60                screenW = screenH * self._windowRatio
 61            return screenW, screenH
 62
 63        def paintCanvas(sender=None):
 64            """
 65            Render the function output to the canvas and update the PDF document.
 66            """
 67            drawBot.newDrawing()
 68            func()
 69            pdf = drawBot.pdfImage()
 70            drawBot.endDrawing()
 71            self.w.canvas.setPDFDocument(pdf)
 72
 73        self.w = vanilla.Window(
 74            getScreenSize(),
 75            minSize=(400, 400),
 76        )
 77
 78        if self._ui:
 79            self.w.refreshBtn = vanilla.Button(
 80                (-130, -22, 60, 14),
 81                "Refresh",
 82                sizeStyle="mini",
 83                callback=paintCanvas,
 84            )
 85            self.w.saveBtn = vanilla.Button(
 86                (-68, -22, 60, 14),
 87                "Save File",
 88                sizeStyle="mini",
 89                callback=self.printPdf,
 90            )
 91            canvasH = -28
 92        else:
 93            canvasH = -0
 94
 95        self.w.canvas = DrawView((0, 0, -0, canvasH))
 96        self.w.canvas.setScale(self._scale)
 97
 98        paintCanvas()
 99
100        self.w.open()
101        self.w.center()
102
103    def printPdf(self, sender):
104        """
105        Save the current canvas as a PDF, SVG, or PNG file.
106        """
107        path = dialogs.putFile(fileTypes=["pdf", "svg", "png"])
108        if path:
109            drawBot.saveImage(path)
110
111
112if __name__ == "__main__":
113    TestRun(lambda: drawBot.newPage())
class TestRun:
 11class TestRun:
 12    """Execute a function in a GUI window using DrawBot and Vanilla."""
 13
 14    def __init__(
 15        self,
 16        func: Callable,
 17        windowRatio: float = 2 / 3,
 18        windowSize: int = 1,
 19        scale: int = 1,
 20        ui: bool = True,
 21    ):
 22        """
 23        Initialize the TestRun GUI environment.
 24
 25        Args:
 26            func: The function to execute and display in the GUI.
 27            windowRatio: The width/height ratio of the window.
 28            windowSize: The scaling factor for the window size relative to the screen.
 29            scale: The scale factor for the canvas rendering.
 30            ui: Whether to show UI controls (refresh and save buttons).
 31        """
 32        self._windowRatio = windowRatio
 33        self._windowSize = windowSize
 34        self._scale = scale
 35        self._ui = ui
 36
 37        executeVanillaTest(self.run(func))
 38
 39    def run(self, func):
 40        """
 41        Set up and run the GUI window, rendering the provided function.
 42
 43        Args:
 44            func: The function to execute and render on the canvas.
 45        """
 46
 47        def getScreenSize():
 48            """
 49            Calculate the window size based on screen dimensions and windowRatio/windowSize.
 50
 51            Returns:
 52                tuple: (window width, window height)
 53            """
 54            screenSize = NSScreen.mainScreen().frame().size
 55            isLandscape = self._windowRatio >= 1
 56            if isLandscape:
 57                screenW = screenSize.width * self._windowSize
 58                screenH = screenW / self._windowRatio
 59            else:
 60                screenH = screenSize.height * self._windowSize
 61                screenW = screenH * self._windowRatio
 62            return screenW, screenH
 63
 64        def paintCanvas(sender=None):
 65            """
 66            Render the function output to the canvas and update the PDF document.
 67            """
 68            drawBot.newDrawing()
 69            func()
 70            pdf = drawBot.pdfImage()
 71            drawBot.endDrawing()
 72            self.w.canvas.setPDFDocument(pdf)
 73
 74        self.w = vanilla.Window(
 75            getScreenSize(),
 76            minSize=(400, 400),
 77        )
 78
 79        if self._ui:
 80            self.w.refreshBtn = vanilla.Button(
 81                (-130, -22, 60, 14),
 82                "Refresh",
 83                sizeStyle="mini",
 84                callback=paintCanvas,
 85            )
 86            self.w.saveBtn = vanilla.Button(
 87                (-68, -22, 60, 14),
 88                "Save File",
 89                sizeStyle="mini",
 90                callback=self.printPdf,
 91            )
 92            canvasH = -28
 93        else:
 94            canvasH = -0
 95
 96        self.w.canvas = DrawView((0, 0, -0, canvasH))
 97        self.w.canvas.setScale(self._scale)
 98
 99        paintCanvas()
100
101        self.w.open()
102        self.w.center()
103
104    def printPdf(self, sender):
105        """
106        Save the current canvas as a PDF, SVG, or PNG file.
107        """
108        path = dialogs.putFile(fileTypes=["pdf", "svg", "png"])
109        if path:
110            drawBot.saveImage(path)

Execute a function in a GUI window using DrawBot and Vanilla.

TestRun( func: Callable, windowRatio: float = 0.6666666666666666, windowSize: int = 1, scale: int = 1, ui: bool = True)
14    def __init__(
15        self,
16        func: Callable,
17        windowRatio: float = 2 / 3,
18        windowSize: int = 1,
19        scale: int = 1,
20        ui: bool = True,
21    ):
22        """
23        Initialize the TestRun GUI environment.
24
25        Args:
26            func: The function to execute and display in the GUI.
27            windowRatio: The width/height ratio of the window.
28            windowSize: The scaling factor for the window size relative to the screen.
29            scale: The scale factor for the canvas rendering.
30            ui: Whether to show UI controls (refresh and save buttons).
31        """
32        self._windowRatio = windowRatio
33        self._windowSize = windowSize
34        self._scale = scale
35        self._ui = ui
36
37        executeVanillaTest(self.run(func))

Initialize the TestRun GUI environment.

Arguments:
  • func: The function to execute and display in the GUI.
  • windowRatio: The width/height ratio of the window.
  • windowSize: The scaling factor for the window size relative to the screen.
  • scale: The scale factor for the canvas rendering.
  • ui: Whether to show UI controls (refresh and save buttons).
def run(self, func):
 39    def run(self, func):
 40        """
 41        Set up and run the GUI window, rendering the provided function.
 42
 43        Args:
 44            func: The function to execute and render on the canvas.
 45        """
 46
 47        def getScreenSize():
 48            """
 49            Calculate the window size based on screen dimensions and windowRatio/windowSize.
 50
 51            Returns:
 52                tuple: (window width, window height)
 53            """
 54            screenSize = NSScreen.mainScreen().frame().size
 55            isLandscape = self._windowRatio >= 1
 56            if isLandscape:
 57                screenW = screenSize.width * self._windowSize
 58                screenH = screenW / self._windowRatio
 59            else:
 60                screenH = screenSize.height * self._windowSize
 61                screenW = screenH * self._windowRatio
 62            return screenW, screenH
 63
 64        def paintCanvas(sender=None):
 65            """
 66            Render the function output to the canvas and update the PDF document.
 67            """
 68            drawBot.newDrawing()
 69            func()
 70            pdf = drawBot.pdfImage()
 71            drawBot.endDrawing()
 72            self.w.canvas.setPDFDocument(pdf)
 73
 74        self.w = vanilla.Window(
 75            getScreenSize(),
 76            minSize=(400, 400),
 77        )
 78
 79        if self._ui:
 80            self.w.refreshBtn = vanilla.Button(
 81                (-130, -22, 60, 14),
 82                "Refresh",
 83                sizeStyle="mini",
 84                callback=paintCanvas,
 85            )
 86            self.w.saveBtn = vanilla.Button(
 87                (-68, -22, 60, 14),
 88                "Save File",
 89                sizeStyle="mini",
 90                callback=self.printPdf,
 91            )
 92            canvasH = -28
 93        else:
 94            canvasH = -0
 95
 96        self.w.canvas = DrawView((0, 0, -0, canvasH))
 97        self.w.canvas.setScale(self._scale)
 98
 99        paintCanvas()
100
101        self.w.open()
102        self.w.center()

Set up and run the GUI window, rendering the provided function.

Arguments:
  • func: The function to execute and render on the canvas.
def printPdf(self, sender):
104    def printPdf(self, sender):
105        """
106        Save the current canvas as a PDF, SVG, or PNG file.
107        """
108        path = dialogs.putFile(fileTypes=["pdf", "svg", "png"])
109        if path:
110            drawBot.saveImage(path)

Save the current canvas as a PDF, SVG, or PNG file.