Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.1

0.5.1 / src / prompts.luc

## One text prompt across the top of the window stands in for the small
## dialogs luced-2d does not have yet: it asks for a line of text — a size, a
## name, a path, an amount — and `apply` carries out what was asked for, or
## asks again with what was wrong. A mode of "" only shows a message.
from luce_ui.ui import TextPrompt, Font
from workspace import Workspace

pub class Prompts:
    pub let prompt: TextPrompt
    pub var mode: str = ""
    ## What an "export" asks for when the save panel is unavailable: JPEG's
    ## quality and matte.
    pub var export_quality: int = 90
    pub var export_matte: float = 1.0
    ## After a prompt is carried out, the window refreshes.
    pub var refreshed: func() -> unit!? = none
    let workspace: Workspace

    pub func init(self, workspace: Workspace, font: Font) -> unit!:
        self.workspace = workspace
        self.prompt = TextPrompt(font = font)

    ## Ask for `mode`'s text under `title`, starting from `value`.
    pub func ask(self, mode: str, title: str, value: str, accept: str) -> unit!:
        self.prompt.open(title, value, accept = accept)
        self.mode = mode

    ## Show a message with nothing to carry out.
    pub func tell(self, message: str) -> unit!:
        self.ask("", message, "", "OK")

    ## Run a command's `step`; a failure (nothing selected, no image on the
    ## clipboard, a group where pixels are needed) is told under `name`. The
    ## window refreshes after it.
    pub func attempt(self, name: str, step: func() -> unit!) -> unit!:
        step() catch failure:
            self.tell(f"{name}: {failure.message}")
            return
        if let refresh = self.refreshed:
            refresh()

    ## `attempt` as a command's action.
    pub func attempting(self, name: str, step: func() -> unit!) -> func() -> unit!:
        return func () -> unit!:
            self.attempt(name, step)

    pub func cancel(self):
        self.mode = ""
        self.prompt.layout().set_visible(false)

    func reject(self, text: str, message: str) -> unit!:
        self.prompt.open(message, text, accept = "Retry")

    ## The prompt submitted `text`: carry out its mode.
    pub func apply(self, text: str) -> unit!:
        let mode = self.mode
        let trimmed = text.trim()
        if mode == "":
            return
        let workspace = self.workspace
        if mode == "open":
            workspace.documents.open(trimmed) catch failure:
                self.reject(text, f"Could not open: {failure.message}")
                return
        elif mode == "rename":
            if trimmed == "":
                return
            workspace.edits.rename(trimmed) catch failure:
                self.reject(text, f"Could not rename: {failure.message}")
                return
        elif mode == "save":
            if trimmed == "":
                self.reject(text, "Enter a file name with an extension")
                return
            workspace.documents.save(trimmed) catch failure:
                self.reject(text, f"Could not save: {failure.message}")
                return
        elif mode == "export":
            if trimmed == "":
                self.reject(text, "Enter a file name ending .png or .jpg")
                return
            workspace.documents.export(trimmed, self.export_quality, self.export_matte) catch failure:
                self.reject(text, f"Could not export: {failure.message}")
                return
        elif mode == "border" or mode == "smooth":
            let most = 200 if mode == "border" else 100
            let amount = int(trimmed) catch failure:
                self.reject(text, f"Enter a whole number: 1..{most}")
                return
            if amount < 1 or amount > most:
                self.reject(text, f"Enter a whole number: 1..{most}")
                return
            if mode == "border":
                workspace.selection.border_selection(amount) catch failure:
                    self.reject(text, f"Could not make the border: {failure.message}")
                    return
            else:
                workspace.selection.smooth_selection(amount) catch failure:
                    self.reject(text, f"Could not smooth: {failure.message}")
                    return
        elif mode == "expand" or mode == "contract" or mode == "feather":
            let amount = int(trimmed) catch failure:
                self.reject(text, "Enter a whole number: 1..512")
                return
            if amount < 1 or amount > 512:
                self.reject(text, "Enter a whole number: 1..512")
                return
            let pixels = amount if mode == "expand" else (-amount if mode == "contract" else 0)
            workspace.selection.adjust_selection(pixels, amount if mode == "feather" else 0) catch failure:
                self.reject(text, f"Could not adjust: {failure.message}")
                return
        self.cancel()
        if let refresh = self.refreshed:
            refresh()