dymokomi/luced
Source of release 0.4.1
0.4.1 / src / editor / file_actions.luc
## File commands own prompt and clipboard state; the workspace owns filesystem
## operations and open documents. The right-click menu is context sensitive: the
## selection decides which of these actions it offers. Selection is captured
## before a modal prompt opens.
from luce_ui.ui import Command, TextPrompt
from interop import Connection
from workspace.controller import Workspace
from editor.journal import Journal
from luce_clipboard import clipboard
import workspace.paths as paths
pub class FileActions:
pub let new_file: Command
pub let new_folder: Command
pub let new_package: Command
pub let new_tool: Command
pub let new_application: Command
pub let cut: Command
pub let copy: Command
pub let paste: Command
pub let copy_path: Command
pub let rename: Command
pub let delete: Command
pub let refresh: Command
pub let show_hidden: Command
let separator: Command
pub let all: list[Command]
var workspace: Workspace? = none
var prompt: TextPrompt? = none
var operation: str = ""
var target: str = ""
var clip_path: str = ""
var clip_move: bool = false
var hidden_label: bool = false
var journal: Journal? = none
pub func init(self) -> unit!:
let new_file = Command("New File…", id = "new_file")
let new_folder = Command("New Folder…", id = "new_folder")
let new_package = Command("New Package…", id = "new_package")
let new_tool = Command("New Tool…", id = "new_tool")
let new_application = Command("New Application…", id = "new_application")
let cut = Command("Cut", id = "cut_file")
let copy = Command("Copy", id = "copy_file")
let paste = Command("Paste", id = "paste_file")
let copy_path = Command("Copy Path", id = "copy_path")
let rename = Command("Rename…", id = "rename_file")
let delete = Command("Delete…", id = "delete_file")
let refresh = Command("Refresh Files", id = "refresh_files")
let show_hidden = Command("Show Hidden Files", id = "show_hidden")
self.new_file = new_file
self.new_folder = new_folder
self.new_package = new_package
self.new_tool = new_tool
self.new_application = new_application
self.cut = cut
self.copy = copy
self.paste = paste
self.copy_path = copy_path
self.rename = rename
self.delete = delete
self.refresh = refresh
self.show_hidden = show_hidden
self.separator = Command("", divider = true)
self.all = [new_file, new_folder, new_package, new_tool, new_application, cut, copy, paste, copy_path, rename, delete, refresh, show_hidden]
## Give the file actions the session journal, so the right-click menu and each file operation
## leave a breadcrumb before handing off to the UI or the filesystem.
pub func set_journal(self, journal: Journal) -> unit:
self.journal = journal
func record(self, message: str) -> unit:
if let journal = self.journal:
journal.record(message)
pub func connect(self, workspace: Workspace, prompt: TextPrompt) -> list[Connection]!:
self.workspace = workspace
self.prompt = prompt
return [self.new_file.on_trigger(self.create_file), self.new_folder.on_trigger(self.create_folder),
self.new_package.on_trigger(self.create_package), self.new_tool.on_trigger(self.create_tool), self.new_application.on_trigger(self.create_application),
self.cut.on_trigger(self.cut_entry), self.copy.on_trigger(self.copy_entry),
self.paste.on_trigger(self.paste_entry), self.copy_path.on_trigger(self.copy_filename),
self.rename.on_trigger(self.rename_file), self.delete.on_trigger(self.delete_file),
self.refresh.on_trigger(workspace.refresh_files), self.show_hidden.on_trigger(self.toggle_hidden),
prompt.on_submit(self.submit)]
## Menus offered by the file pane, one per kind of right-click target.
pub func file_menu(self) -> list[Command]:
return [self.cut, self.copy, self.copy_path, self.separator, self.rename, self.delete]
pub func folder_menu(self) -> list[Command]:
return [self.new_file, self.new_folder, self.separator, self.paste, self.copy_path, self.separator, self.rename, self.delete]
pub func background_menu(self) -> list[Command]:
return [self.new_file, self.new_folder, self.separator, self.new_package, self.new_tool, self.new_application, self.separator, self.paste, self.separator, self.show_hidden, self.refresh]
## Refresh enabled states, then choose the menu for what was clicked: a file,
## a folder, or empty space (also the parent row, which selects nothing).
pub func menu_for_selection(self) -> list[Command]:
if let workspace = self.workspace:
self.update()
if let entry = workspace.selected_entry():
self.record("file pane menu: " + ("folder " if entry.directory else "file ") + entry.path)
return self.folder_menu() if entry.directory else self.file_menu()
self.record("file pane menu: background")
return self.background_menu()
self.record("file pane menu: no workspace")
return self.background_menu()
pub func update(self):
let workspace = self.workspace else return
self.paste.set_enabled(self.clip_path != "")
self.cut.set_enabled(false)
self.copy.set_enabled(false)
self.rename.set_enabled(false)
self.delete.set_enabled(false)
self.copy_path.set_enabled(false)
if let entry = workspace.selected_entry():
self.cut.set_enabled(true)
self.copy.set_enabled(not entry.directory)
self.rename.set_enabled(true)
self.delete.set_enabled(true)
self.copy_path.set_enabled(true)
let shown = workspace.hidden_shown()
if shown != self.hidden_label:
self.hidden_label = shown
self.show_hidden.set_text("Hide Hidden Files" if shown else "Show Hidden Files") catch failure: _ = failure
func create_file(self) -> unit!:
self.create("file")
func create_folder(self) -> unit!:
self.create("folder")
# A new project of each kind: luc creates the directory and its starter files.
func create_package(self) -> unit!:
self.create("package")
func create_tool(self) -> unit!:
self.create("tool")
func create_application(self) -> unit!:
self.create("application")
func create(self, operation: str) -> unit!:
let workspace = self.workspace else return
let prompt = self.prompt else return
self.record("create " + operation)
self.operation = operation
self.target = workspace.directory()
if let entry = workspace.selected_entry():
if entry.directory:
self.target = entry.path
var title = "New Folder"
if operation == "file": title = "New File"
elif operation == "package": title = "New Package (a library other projects add)"
elif operation == "tool": title = "New Tool (a terminal program)"
elif operation == "application": title = "New Application (a desktop window)"
prompt.open(title, accept = "Create")
func cut_entry(self) -> unit!:
let workspace = self.workspace else return
let entry = workspace.selected_entry() else return
self.record("cut " + entry.path)
self.clip_path = entry.path
self.clip_move = true
workspace.report("Cut " + entry.name)
func copy_entry(self) -> unit!:
let workspace = self.workspace else return
let entry = workspace.selected_entry() else return
self.record("copy " + entry.path)
self.clip_path = entry.path
self.clip_move = false
workspace.report("Copied " + entry.name)
func paste_entry(self) -> unit!:
let workspace = self.workspace else return
if self.clip_path == "":
return
self.record("paste " + self.clip_path)
var target = workspace.directory()
if let entry = workspace.selected_entry():
if entry.directory:
target = entry.path
workspace.paste_into(target, self.clip_path, self.clip_move)
if self.clip_move:
self.clip_path = ""
func toggle_hidden(self) -> unit!:
let workspace = self.workspace else return
workspace.toggle_hidden()
## Save the active document, prompting for a name when it is still untitled.
pub func save_document(self) -> unit!:
let workspace = self.workspace else return
let document = workspace.current_document() else return
if document.is_untitled():
let prompt = self.prompt else return
self.operation = "save_as"
self.target = workspace.directory()
prompt.open("Save As", accept = "Save")
else:
workspace.save()
func rename_file(self) -> unit!:
let workspace = self.workspace else return
let prompt = self.prompt else return
let entry = workspace.selected_entry() else return
self.record("rename " + entry.path)
self.operation = "rename"
self.target = entry.path
prompt.open("Rename " + paths.short(entry.name, 45), entry.name, accept = "Rename")
func delete_file(self) -> unit!:
let workspace = self.workspace else return
let prompt = self.prompt else return
let entry = workspace.selected_entry() else return
self.record("delete " + entry.path)
self.operation = "delete"
self.target = entry.path
prompt.open("Delete " + paths.short(entry.name, 40) + (" and its contents?" if entry.directory else "?"), accept = "Delete", editable = false)
func copy_filename(self) -> unit!:
let workspace = self.workspace else return
let entry = workspace.selected_entry() else return
clipboard.write_text(entry.path)
## Clear any pending file-prompt operation, so another owner (the AI prompt)
## can reuse the shared TextPrompt without triggering a stale file action.
pub func reset_operation(self) -> unit!:
self.operation = ""
func submit(self, text: str) -> unit!:
let workspace = self.workspace else return
let operation = self.operation
let target = self.target
self.operation = ""
if operation == "file":
workspace.create_file(target, text)
elif operation == "folder":
workspace.create_folder(target, text)
elif operation == "package" or operation == "tool" or operation == "application":
workspace.create_project(target, text, operation)
elif operation == "rename":
workspace.rename_entry(target, text)
elif operation == "delete":
workspace.delete_entry(target)
elif operation == "save_as":
workspace.save_untitled(target, text)