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    UI_CHROME = 62  # height occupied by os window elements (magic number)
 14    UI_PANEL_HEIGHT = 28
 15
 16    def __init__(
 17        self,
 18        func: Callable,
 19        windowRatio: float = 2 / 3,
 20        windowScale: int = 1,
 21        canvasScale: int = 1,
 22        ui: bool = True,
 23    ):
 24        """
 25        Initialize the TestRun GUI environment.
 26
 27        Args:
 28            func: The function to execute and display in the GUI.
 29            windowRatio: The width/height ratio of the window.
 30            windowScale: The scaling factor for the window size relative to the screen. (0-1)
 31            canvasScale: The scale factor for the canvas rendering.
 32            ui: Whether to show UI controls (refresh and save buttons).
 33        """
 34        self._windowRatio = windowRatio
 35        if windowScale <= 0 or windowScale > 1:
 36            raise ValueError(
 37                "windowScale must be between 0 (exclusive) and 1 (inclusive)."
 38            )
 39        self._windowScale = windowScale
 40        self._canvasScale = canvasScale
 41        self._ui = ui
 42
 43        executeVanillaTest(self.run(func))
 44
 45    def run(self, func):
 46        """
 47        Set up and run the GUI window, rendering the provided function.
 48
 49        Args:
 50            func: The function to execute and render on the canvas.
 51        """
 52
 53        def getScreenSize():
 54            """
 55            Calculate the window size based on screen dimensions and windowRatio/windowScale.
 56
 57            Returns:
 58                tuple: (window width, window height)
 59            """
 60            screenW, screenH = NSScreen.mainScreen().frame().size
 61            availW, availH = screenW, screenH
 62            availH -= self.UI_CHROME  # Subtract space for OS window chrome
 63            if self._ui:
 64                availH -= self.UI_PANEL_HEIGHT  # Subtract space for UI panel
 65
 66            screenAspectRatio = screenW / screenH
 67
 68            isWiderThanScreen = self._windowRatio > screenAspectRatio
 69
 70            if isWiderThanScreen:
 71                w = availW * self._windowScale
 72                h = w / self._windowRatio
 73            else:
 74                h = availH * self._windowScale
 75                w = h * self._windowRatio
 76
 77            h += self.UI_CHROME  # Add back space for OS window chrome
 78            if self._ui:
 79                h += self.UI_PANEL_HEIGHT  # Add back space for UI panel
 80
 81            return w, h
 82
 83        def paintCanvas(sender=None):
 84            """
 85            Render the function output to the canvas and update the PDF document.
 86            """
 87            drawBot.newDrawing()
 88            func()
 89            pdf = drawBot.pdfImage()
 90            drawBot.endDrawing()
 91            self.w.canvas.setPDFDocument(pdf)
 92
 93        self.w = vanilla.Window(
 94            getScreenSize(),
 95            minSize=(400, 400),
 96        )
 97
 98        if self._ui:
 99            self.w.refreshBtn = vanilla.Button(
100                (-130, -22, 60, 14),
101                "Refresh",
102                sizeStyle="mini",
103                callback=paintCanvas,
104            )
105            self.w.saveBtn = vanilla.Button(
106                (-68, -22, 60, 14),
107                "Save File",
108                sizeStyle="mini",
109                callback=self.printPdf,
110            )
111            canvasH = -self.UI_PANEL_HEIGHT
112        else:
113            canvasH = -0
114
115        self.w.canvas = DrawView((0, 0, -0, canvasH))
116        self.w.canvas.setScale(self._canvasScale)
117
118        paintCanvas()
119
120        self.w.open()
121        self.w.center()
122
123    def printPdf(self, sender):
124        """
125        Save the current canvas as a PDF, SVG, or PNG file.
126        """
127        path = dialogs.putFile(fileTypes=["pdf", "svg", "png"])
128        if path:
129            drawBot.saveImage(path)
130
131
132if __name__ == "__main__":
133    TestRun(lambda: drawBot.newPage())
class TestRun:
 11class TestRun:
 12    """Execute a function in a GUI window using DrawBot and Vanilla."""
 13
 14    UI_CHROME = 62  # height occupied by os window elements (magic number)
 15    UI_PANEL_HEIGHT = 28
 16
 17    def __init__(
 18        self,
 19        func: Callable,
 20        windowRatio: float = 2 / 3,
 21        windowScale: int = 1,
 22        canvasScale: int = 1,
 23        ui: bool = True,
 24    ):
 25        """
 26        Initialize the TestRun GUI environment.
 27
 28        Args:
 29            func: The function to execute and display in the GUI.
 30            windowRatio: The width/height ratio of the window.
 31            windowScale: The scaling factor for the window size relative to the screen. (0-1)
 32            canvasScale: The scale factor for the canvas rendering.
 33            ui: Whether to show UI controls (refresh and save buttons).
 34        """
 35        self._windowRatio = windowRatio
 36        if windowScale <= 0 or windowScale > 1:
 37            raise ValueError(
 38                "windowScale must be between 0 (exclusive) and 1 (inclusive)."
 39            )
 40        self._windowScale = windowScale
 41        self._canvasScale = canvasScale
 42        self._ui = ui
 43
 44        executeVanillaTest(self.run(func))
 45
 46    def run(self, func):
 47        """
 48        Set up and run the GUI window, rendering the provided function.
 49
 50        Args:
 51            func: The function to execute and render on the canvas.
 52        """
 53
 54        def getScreenSize():
 55            """
 56            Calculate the window size based on screen dimensions and windowRatio/windowScale.
 57
 58            Returns:
 59                tuple: (window width, window height)
 60            """
 61            screenW, screenH = NSScreen.mainScreen().frame().size
 62            availW, availH = screenW, screenH
 63            availH -= self.UI_CHROME  # Subtract space for OS window chrome
 64            if self._ui:
 65                availH -= self.UI_PANEL_HEIGHT  # Subtract space for UI panel
 66
 67            screenAspectRatio = screenW / screenH
 68
 69            isWiderThanScreen = self._windowRatio > screenAspectRatio
 70
 71            if isWiderThanScreen:
 72                w = availW * self._windowScale
 73                h = w / self._windowRatio
 74            else:
 75                h = availH * self._windowScale
 76                w = h * self._windowRatio
 77
 78            h += self.UI_CHROME  # Add back space for OS window chrome
 79            if self._ui:
 80                h += self.UI_PANEL_HEIGHT  # Add back space for UI panel
 81
 82            return w, h
 83
 84        def paintCanvas(sender=None):
 85            """
 86            Render the function output to the canvas and update the PDF document.
 87            """
 88            drawBot.newDrawing()
 89            func()
 90            pdf = drawBot.pdfImage()
 91            drawBot.endDrawing()
 92            self.w.canvas.setPDFDocument(pdf)
 93
 94        self.w = vanilla.Window(
 95            getScreenSize(),
 96            minSize=(400, 400),
 97        )
 98
 99        if self._ui:
100            self.w.refreshBtn = vanilla.Button(
101                (-130, -22, 60, 14),
102                "Refresh",
103                sizeStyle="mini",
104                callback=paintCanvas,
105            )
106            self.w.saveBtn = vanilla.Button(
107                (-68, -22, 60, 14),
108                "Save File",
109                sizeStyle="mini",
110                callback=self.printPdf,
111            )
112            canvasH = -self.UI_PANEL_HEIGHT
113        else:
114            canvasH = -0
115
116        self.w.canvas = DrawView((0, 0, -0, canvasH))
117        self.w.canvas.setScale(self._canvasScale)
118
119        paintCanvas()
120
121        self.w.open()
122        self.w.center()
123
124    def printPdf(self, sender):
125        """
126        Save the current canvas as a PDF, SVG, or PNG file.
127        """
128        path = dialogs.putFile(fileTypes=["pdf", "svg", "png"])
129        if path:
130            drawBot.saveImage(path)

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

TestRun( func: Callable, windowRatio: float = 0.6666666666666666, windowScale: int = 1, canvasScale: int = 1, ui: bool = True)
17    def __init__(
18        self,
19        func: Callable,
20        windowRatio: float = 2 / 3,
21        windowScale: int = 1,
22        canvasScale: int = 1,
23        ui: bool = True,
24    ):
25        """
26        Initialize the TestRun GUI environment.
27
28        Args:
29            func: The function to execute and display in the GUI.
30            windowRatio: The width/height ratio of the window.
31            windowScale: The scaling factor for the window size relative to the screen. (0-1)
32            canvasScale: The scale factor for the canvas rendering.
33            ui: Whether to show UI controls (refresh and save buttons).
34        """
35        self._windowRatio = windowRatio
36        if windowScale <= 0 or windowScale > 1:
37            raise ValueError(
38                "windowScale must be between 0 (exclusive) and 1 (inclusive)."
39            )
40        self._windowScale = windowScale
41        self._canvasScale = canvasScale
42        self._ui = ui
43
44        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.
  • windowScale: The scaling factor for the window size relative to the screen. (0-1)
  • canvasScale: The scale factor for the canvas rendering.
  • ui: Whether to show UI controls (refresh and save buttons).
UI_CHROME = 62
UI_PANEL_HEIGHT = 28
def run(self, func):
 46    def run(self, func):
 47        """
 48        Set up and run the GUI window, rendering the provided function.
 49
 50        Args:
 51            func: The function to execute and render on the canvas.
 52        """
 53
 54        def getScreenSize():
 55            """
 56            Calculate the window size based on screen dimensions and windowRatio/windowScale.
 57
 58            Returns:
 59                tuple: (window width, window height)
 60            """
 61            screenW, screenH = NSScreen.mainScreen().frame().size
 62            availW, availH = screenW, screenH
 63            availH -= self.UI_CHROME  # Subtract space for OS window chrome
 64            if self._ui:
 65                availH -= self.UI_PANEL_HEIGHT  # Subtract space for UI panel
 66
 67            screenAspectRatio = screenW / screenH
 68
 69            isWiderThanScreen = self._windowRatio > screenAspectRatio
 70
 71            if isWiderThanScreen:
 72                w = availW * self._windowScale
 73                h = w / self._windowRatio
 74            else:
 75                h = availH * self._windowScale
 76                w = h * self._windowRatio
 77
 78            h += self.UI_CHROME  # Add back space for OS window chrome
 79            if self._ui:
 80                h += self.UI_PANEL_HEIGHT  # Add back space for UI panel
 81
 82            return w, h
 83
 84        def paintCanvas(sender=None):
 85            """
 86            Render the function output to the canvas and update the PDF document.
 87            """
 88            drawBot.newDrawing()
 89            func()
 90            pdf = drawBot.pdfImage()
 91            drawBot.endDrawing()
 92            self.w.canvas.setPDFDocument(pdf)
 93
 94        self.w = vanilla.Window(
 95            getScreenSize(),
 96            minSize=(400, 400),
 97        )
 98
 99        if self._ui:
100            self.w.refreshBtn = vanilla.Button(
101                (-130, -22, 60, 14),
102                "Refresh",
103                sizeStyle="mini",
104                callback=paintCanvas,
105            )
106            self.w.saveBtn = vanilla.Button(
107                (-68, -22, 60, 14),
108                "Save File",
109                sizeStyle="mini",
110                callback=self.printPdf,
111            )
112            canvasH = -self.UI_PANEL_HEIGHT
113        else:
114            canvasH = -0
115
116        self.w.canvas = DrawView((0, 0, -0, canvasH))
117        self.w.canvas.setScale(self._canvasScale)
118
119        paintCanvas()
120
121        self.w.open()
122        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):
124    def printPdf(self, sender):
125        """
126        Save the current canvas as a PDF, SVG, or PNG file.
127        """
128        path = dialogs.putFile(fileTypes=["pdf", "svg", "png"])
129        if path:
130            drawBot.saveImage(path)

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