Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-2d

Source of release 0.5.0

0.5.0 / src / app.luc

## luced-2d's application: it loads the settings, builds the workspace and the
## window's panels, opens any files named on the command line, and runs the
## window until it closes.
from luce_ui.ui import Application, Font
from interop import Connection
from workspace import Workspace
from luce_image.canvas import Canvas
from panels import Panels
from actions import Actions
from settings import Store
from tool_memory import tools_path, restore_tools, remember_tools
from dock_layout import layout_path
from update_check import UpdateCheck
import theme
from luce_std import crash
from luce_std.crash import Hook
from recovery import save_for_recovery

## The version the update check compares; keep it equal to package.prisma's.
pub let version: str = "0.5.0"

pub class Luce2D:
    pub let app: Application
    pub let workspace: Workspace
    pub let panels: Panels
    let connections: list[Connection]
    let update: UpdateCheck
    ## The crash hook that saves the document in front; `close` removes it.
    let recovery: Hook

    pub func init(self) -> unit!:
        # Crash reports first, so anything below that traps leaves one. After a
        # crash, luce-ui starts luced-2d again to show the report in its window.
        crash.enable()
        let font = Font()
        # Settings come first: a new canvas takes their size, the window their theme.
        let store = Store()
        let settings = store.load()
        let workspace = Workspace(settings.canvas_width, settings.canvas_height)
        let actions = Actions()
        # The tool options as the last run left them, the tool in hand with them.
        restore_tools(workspace, tools_path())
        let panels = Panels(workspace, actions, font, store)
        panels.choose_tool(workspace.tools.tool)
        # The panels as the last run left them.
        panels.dock_layout.restore(layout_path())
        let app = Application(panels.body, title = "luced-2d", width = 1400, height = 900, theme = theme.editor_theme(workspace.look))
        panels.files.app = app
        # Keys go to the canvas from the start, so a tool's letter picks the tool.
        app.focus(panels.view.layout())
        app.set_close_handler(panels.files.can_quit)
        # A trap saves the document in front, if it has unsaved work, for recovery.
        let recovery = app.on_crash(func () -> unit!:
            save_for_recovery(workspace))
        panels.settings_dialog.app = app
        panels.scripts.app = app
        panels.brush.app = app
        # Whether a newer release is out, asked in the background and told once.
        let update = UpdateCheck(version)
        update.start()
        let connections: list[Connection] = []
        connections.append(app.on_frame(func (elapsed: float) -> unit!:
            _ = elapsed
            panels.view.settle()
            panels.view.march()
            panels.show_opening()
            panels.scripts.poll()
            panels.adjust.session.poll()
            panels.rail.poll()
            panels.color_range.session.poll()
            panels.replace_color.session.poll()
            panels.select_mask.session.poll()
            panels.catalog.poll()
            panels.dock_layout.poll()
            panels.settle_focus()
            panels.refresh_if_changed()
            if update.poll():
                panels.status.set_text(update.notice)))
        app.set_commands(actions.all)
        # What input could not do (a filter on a group, a file that would not
        # open) is said in a prompt rather than ending the run.
        app.set_error_handler(panels.prompts.tell)
        # The window never waits for tiles coming back from disk: a coarser
        # level stands in until they arrive (headless runs draw them at once).
        Canvas.set_progressive_loading(true)
        panels.settings_dialog.apply()
        if store.warning != "":
            panels.status.set_text(store.warning)
        self.app = app
        self.workspace = workspace
        self.panels = panels
        self.connections = connections
        self.update = update
        self.recovery = recovery

    pub func run(self, arguments: list[str]) -> unit!:
        # Luce's `main` gets the arguments without the program: each is a file to open.
        for argument in arguments:
            if argument.length > 0 and argument != "--smoke":
                self.workspace.documents.open(argument)
                self.panels.refresh()
        self.app.run(frame_limit = 3 if "--smoke" in arguments else 0)

    pub func close(self):
        remember_tools(self.workspace, tools_path()) catch failure:
            _ = failure
        self.panels.dock_layout.save() catch failure:
            _ = failure
        self.update.close()
        for connection in self.connections:
            connection.disconnect()
        self.connections.clear()
        # the hook holds the workspace: removed before the application closes
        self.recovery.remove()
        self.panels.close()
        self.app.close()
        # Nothing reads tiles now: the scratch file and its folder can go.
        Canvas.release_scratch_file()