Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.0

0.5.0 / src / new_canvas_dialog.luc

## File › New…, as Compositor's New Canvas: a size in pixels and a
## resolution, starting from the image on the clipboard when there is one
## (the "Clipboard" button puts its size back).
from luce_ui.ui import Text, HStack, Spacer, Dialog, NumberField, Button
from system_hooks import clipboard_picture_width, clipboard_picture_height
from workspace import Workspace
from kit import Kit

pub class NewCanvasDialog:
    pub let dialog: Dialog
    let kit: Kit
    let width_field: NumberField
    let height_field: NumberField
    let resolution_field: NumberField
    pub let cancel_button: Button
    pub var created: func() -> unit!? = none
    let workspace: Workspace

    pub func init(self, workspace: Workspace, kit: Kit) -> unit!:
        self.kit = kit
        self.workspace = workspace
        let font = kit.font
        self.width_field = NumberField(1280.0, 1.0, 65536.0, 1.0, suffix = " px", font = font, cells = 11.0)
        self.height_field = NumberField(800.0, 1.0, 65536.0, 1.0, suffix = " px", font = font, cells = 11.0)
        self.resolution_field = NumberField(72.0, 1.0, 9600.0, 1.0, suffix = " px/in", font = font, cells = 11.0)
        self.cancel_button = kit.button("Cancel", self.cancel)
        self.dialog = Dialog([Text("New Canvas", font = font), Text("A blank space for the next picture.", font = font, muted = true),
                              kit.dialog_row("Width", self.width_field, 110.0), kit.dialog_row("Height", self.height_field, 110.0),
                              kit.dialog_row("Resolution", self.resolution_field, 110.0),
                              HStack([kit.button("Clipboard Size", self.from_clipboard), Spacer(), self.cancel_button,
                                      kit.button("Create", self.create)], spacing = 8.0)], width = 440.0, spacing = 10.0)
        kit.dialog_keys(self.dialog, self.cancel, self.create)

    ## Open at the clipboard image's size if there is one, else the document's.
    pub func open(self) -> unit!:
        if not self.kit.may_open(self.dialog):
            return
        self.width_field.set_value(float(self.workspace.canvas.width()))
        self.height_field.set_value(float(self.workspace.canvas.height()))
        self.resolution_field.set_value(self.workspace.canvas.resolution())
        self.from_clipboard()
        self.dialog.open()

    func from_clipboard(self) -> unit!:
        let width = clipboard_picture_width() catch failure:
            return
        let height = clipboard_picture_height() catch failure:
            return
        if width > 0 and height > 0:
            self.width_field.set_value(float(width))
            self.height_field.set_value(float(height))

    func cancel(self) -> unit!:
        self.dialog.hide()

    func create(self) -> unit!:
        self.dialog.hide()
        self.workspace.documents.create(int(self.width_field.value() + 0.5), int(self.height_field.value() + 0.5))
        self.workspace.canvas.set_resolution(self.resolution_field.value())
        if let done = self.created:
            done()