Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced

Source of release 0.4.1

0.4.1 / src / editor / views.luc

## Named view components expose only the controls that the workspace updates.
## Geometry uses font cells and text lines; continuous GPU content still fills
## the remaining area. Menus and toolbars share the same native actions.
from luce_ui.ui import Font, Text, TextEditor, TableView, VStack, Toolbar, Menu, Button, ButtonStyle, Shelf, Spacer, Size, Panel, IconKind, DStack, DockPosition, ContextMenu, CommandPalette, TextPrompt, ConfirmDialog, ColorEditor, Command
from interop import Connection
from luce_gpu.gpu import Color
from editor.actions import EditorActions
from editor.file_actions import FileActions
from workspace.explorer import Explorer
from workspace.status import FileStatus
import workspace.paths as paths
import editor.file_items as file_items

## The file pane owns a list and a context menu whose entries follow the click:
## a file, a folder or empty space each get their own commands.
pub class Sidebar:
    pub let listing: TableView
    pub let body: Panel
    pub let context: ContextMenu
    let files: FileActions
    pub let connections: list[Connection]

    pub func init(self, explorer: Explorer, actions: EditorActions, font: Font) -> unit!:
        let initial = FileStatus()
        let listing = TableView(file_items.columns(), file_items.present(explorer.entries, initial, [], Color(), Color()), font = font)
        let context = ContextMenu(listing, actions.files.background_menu(), font = font)
        self.context = context
        self.listing = listing
        self.files = actions.files
        # The pane's actions on its shelf: new file, new folder, refresh; delete alone at the right.
        self.connections = []
        let shelf = Shelf([shelf_button("file-new", actions.files.new_file, font, self.connections), shelf_button("folder-new", actions.files.new_folder, font, self.connections),
                           shelf_button("reload", actions.files.refresh, font, self.connections), Spacer(), shelf_button("trash", actions.files.delete, font, self.connections)])
        let panel = Panel("FILES", context, icon = IconKind.folder_open, minimum = Size(140.0, 100.0), closable = false, footer = shelf)
        panel.set_detail(paths.name(explorer.directory))
        self.body = panel

    ## Chosen just before the menu opens, once the click has updated the selection.
    pub func refresh_menu(self) -> unit!:
        self.context.set_commands(self.files.menu_for_selection())

pub class OutputPane:
    pub let output: TextEditor
    pub let body: Panel
    pub let context: ContextMenu
    pub let connections: list[Connection]

    pub func init(self, actions: EditorActions, font: Font) -> unit!:
        let output = TextEditor("Compiler and program output will appear here.\n", read_only = true, line_numbers = false, font = font, wrap = true)
        actions.output.bind(output)
        let context = ContextMenu(output, actions.output.all, font = font)
        self.context = context
        self.output = output
        self.connections = []
        let shelf = Shelf([Spacer(), shelf_button("eraser-tool", actions.output.clear, font, self.connections)])
        self.body = Panel("OUTPUT", context, icon = IconKind.terminal, minimum = Size(180.0, 60.0), closable = false, footer = shelf)

pub class EditorView:
    pub let sidebar: Sidebar
    pub let dock: DStack
    pub let output: OutputPane
    pub let status: Text
    pub let palette: CommandPalette
    pub let prompt: TextPrompt
    pub let dialog: ConfirmDialog
    pub let color: ColorEditor
    pub let menu: Toolbar
    pub let body: ContextMenu

    pub func init(self, explorer: Explorer, actions: EditorActions, font: Font) -> unit!:
        let sidebar = Sidebar(explorer, actions, font)
        let output = OutputPane(actions, font)
        let status = Text("Ready", font = font)
        let sep = Command("", divider = true)
        let menu = Toolbar([
            Menu("File", [actions.new_tab, actions.files.new_file, actions.files.new_folder, sep, actions.save, actions.save_all, sep, actions.settings, actions.theme, actions.ai_settings, sep, actions.discard_changes, actions.exit], font = font),
            Menu("Edit", actions.editing.all, font = font),
            Menu("Build", [actions.build, actions.run, sep, actions.stop], font = font),
            Menu("View", [actions.ask_ai, actions.write_ai, actions.spellcheck_ai, sep, actions.palette, actions.word_wrap, sep, actions.fold, actions.fold_all, actions.unfold_all, sep, actions.next_pane, actions.previous_pane, sep, actions.reload], font = font),
            Spacer(),
            Button(action = actions.save, font = font),
            Button(action = actions.build, font = font),
            Button(action = actions.run, font = font),
            Button(action = actions.stop, font = font)
        ], font = font)
        self.menu = menu
        let panes = DStack([sidebar.body, output.body], font = font)
        panes.move(output.body, sidebar.body, DockPosition.right)
        let palette = CommandPalette(actions.all, font = font)
        let prompt = TextPrompt(font = font)
        let dialog = ConfirmDialog(font = font)
        let color = ColorEditor(font = font)
        self.palette = palette
        self.prompt = prompt
        self.dialog = dialog
        self.color = color
        self.sidebar = sidebar
        self.dock = panes
        self.output = output
        self.status = status
        # The global status bar keeps only workspace-wide state; per-editor details
        # (syntax, caret position, encoding) live in each pane's own status bar.
        let statusbar = Toolbar([status, Spacer()], font = font)
        let content = VStack([menu, panes, statusbar, palette, prompt, dialog, color], spacing = 0.0)
        self.body = ContextMenu(content, [actions.palette, actions.settings, actions.theme, actions.reload, actions.next_pane], font = font)

## A pane-shelf button: 25 points square, plain, triggering `command`; its hint is the command's name.
func shelf_button(icon: str, command: Command, font: Font, connections: list[Connection]) -> Button!:
    let button = Button(icon = icon, font = font)
    button.set_style(ButtonStyle.plain)
    button.layout().set_size(25.0, 25.0)
    connections.append(button.on_click(func () -> unit!:
        command.trigger()))
    return button