dymokomi/luced-photo
Source of release 0.1.1
0.1.1 / src / folders_pane.luc
## The Library pane: the library's folders (folder_tree.luc), All Photos
## first, and the import. Picking a folder shows its photos (its subfolders'
## with them) in the Photos pane; an import goes into the folder picked. Its
## footer imports a folder or photos and makes a folder; a row's menu (right
## click) makes a folder at the top or inside it, renames or deletes it (its
## photos and folders go to the folder it was in), and moves the selected
## photos into it. The Trash, last, holds photos deleted from the Photos pane
## until its menu empties it: the photos and their files deleted for good.
## Below the folders, each camera plugged in has a row: an import of its new
## photos once its session is open.
from luce_ui.ui import Widget, Panel, VStack, Shelf, Spacer, Size, Button, ButtonStyle, ContextMenu, Command, TextPrompt, ConfirmDialog, Font, ScrollView
from interop import Connection
from photo_library import PhotoLibrary
from folder_tree import FolderTree
import folder_tree
from luce_ptp.ptp import Watcher
pub class FoldersPane:
pub let panel: Panel
pub let tree: FolderTree
let library: PhotoLibrary
let font: Font
let menu: ContextMenu
let prompt: TextPrompt
let confirm: ConfirmDialog
let new_command: Command
let inside_command: Command
let rename_command: Command
let delete_command: Command
let move_command: Command
let empty_command: Command
## Asked before the Trash is emptied (`confirm` asks about a folder otherwise).
var emptying: bool = false
var connections: list[Connection] = []
## The folder a menu or the prompt is about (-1 none), and what the prompt
## asks: "new" a folder's name (inside `target`, 0 at the top), "rename".
var target: int = -1
var asking: str = ""
## A folder was picked (0: All Photos, folder_tree.trash the Trash).
pub var on_pick: func(int) -> unit!? = none
## Move the selected photos into a folder.
pub var on_move_here: func(int) -> unit!? = none
pub var on_status: func(str) -> unit!? = none
## The cameras' rows, and the import asked of camera `at`.
let cameras: VStack
var camera_connections: list[Connection] = []
pub var on_camera_import: func(int) -> unit!? = none
pub func init(self, library: PhotoLibrary, font: Font, imports: list[Command]) -> unit!:
self.library = library
self.font = font
self.tree = FolderTree(library, font)
self.tree.on_pick = func (id: int) -> unit!:
if let pick = self.on_pick:
pick(id)
self.tree.on_rename = func (id: int) -> unit!:
self.target = id
self.rename()
self.tree.on_menu = func (id: int) -> unit!:
self.target = id
self.new_command = Command("New Folder…", id = "folder_new")
self.inside_command = Command("New Folder Inside…", id = "folder_inside")
self.rename_command = Command("Rename Folder…", id = "folder_rename")
self.delete_command = Command("Delete Folder…", id = "folder_delete")
self.move_command = Command("Move Selected Photos Here", id = "folder_move_here")
self.empty_command = Command("Empty Trash…", id = "folder_empty_trash")
self.connections.append(self.empty_command.on_trigger(self.ask_empty))
self.connections.append(self.new_command.on_trigger(func () -> unit!:
self.ask_new(0)))
self.connections.append(self.inside_command.on_trigger(func () -> unit!:
self.ask_new(self.target)))
self.connections.append(self.rename_command.on_trigger(self.rename))
self.connections.append(self.delete_command.on_trigger(self.ask_delete))
self.connections.append(self.move_command.on_trigger(func () -> unit!:
if let move = self.on_move_here:
move(self.target if self.target >= 0 or self.target == folder_tree.trash else 0)))
self.menu = ContextMenu(ScrollView(self.tree), [self.new_command, self.inside_command, self.rename_command, Command("", divider = true),
self.move_command, Command("", divider = true), self.delete_command,
self.empty_command], font = font)
self.connections.append(self.menu.on_open(self.fit_menu))
self.prompt = TextPrompt(font = font)
self.connections.append(self.prompt.on_submit(self.submitted))
self.confirm = ConfirmDialog(font = font)
self.connections.append(self.confirm.on_choose(self.chosen_delete))
self.cameras = VStack([], spacing = 2.0, padding = 6.0)
self.cameras.layout().set_visible(false)
let content = VStack([self.menu, self.cameras, self.prompt, self.confirm], spacing = 0.0)
content.layout().set_flex()
var buttons: list[Widget] = []
for (at, command) in imports.indexed():
buttons.append(self.shelf_button(["import", "open"][at] if at < 2 else "import", command))
let made = Button(icon = "folder-new", font = font)
made.set_style(ButtonStyle.plain)
self.connections.append(made.on_click(func () -> unit!:
self.ask_new(self.tree.picked)))
let shelf = Shelf(buttons + [made, Spacer()])
self.panel = Panel("Library", content, minimum = Size(170.0, 120.0), closable = false, footer = shelf, id = "folders")
func shelf_button(self, icon: str, command: Command) -> Button!:
let button = Button(icon = icon, font = self.font)
button.set_style(ButtonStyle.plain)
self.connections.append(button.on_click(func () -> unit!:
command.trigger()))
return button
## Show a row for each camera in `watcher`: its import once it is ready.
pub func show_cameras(self, watcher: Watcher) -> unit!:
for connection in self.camera_connections:
connection.disconnect()
self.camera_connections.clear()
var rows: list[Widget] = []
for at in 0..<watcher.count():
rows.append(self.camera_row(at, watcher.name(at), watcher.ready(at)))
self.cameras.set_children(rows)
self.cameras.layout().set_visible(rows.length > 0)
func camera_row(self, at: int, name: str, ready: bool) -> Button!:
let button = Button(text = f"Import from {name}" if ready else f"Connecting to {name}…", icon = "camera", font = self.font)
button.set_style(ButtonStyle.plain)
button.set_enabled(ready)
self.camera_connections.append(button.on_click(func () -> unit!:
if let start = self.on_camera_import:
start(at)))
return button
func fit_menu(self) -> unit!:
let folder = self.target > 0
for command in [self.inside_command, self.rename_command, self.delete_command]:
command.set_enabled(folder)
self.move_command.set_enabled(self.target != folder_tree.nowhere)
self.empty_command.set_enabled(self.target == folder_tree.trash and self.library.count_in(folder_tree.trash) > 0)
## Read the folders again (the counts too).
pub func refresh(self) -> unit!:
self.tree.refresh()
func ask_new(self, parent: int) -> unit!:
self.asking = "new"
self.target = parent if parent > 0 else 0
let inside = self.library.folders.position_of(self.target)
let where = f" in “{self.library.folders.name(inside)}”" if inside >= 0 else ""
self.prompt.open(f"Name the new folder{where}", "Untitled", accept = "Make")
func rename(self) -> unit!:
let at = self.library.folders.position_of(self.target)
if at < 0:
return
self.asking = "rename"
self.prompt.open("Rename the folder", self.library.folders.name(at), accept = "Rename")
func submitted(self, typed: str) -> unit!:
let name = typed.trim()
let asking = self.asking
self.asking = ""
if name == "":
return
if asking == "new":
let id = self.library.make_folder(name, self.target)
self.tree.reveal(id)
self.tell(f"Made the folder “{name}”.")
elif asking == "rename":
self.library.rename_folder(self.target, name)
self.tree.refresh()
func ask_delete(self) -> unit!:
let at = self.library.folders.position_of(self.target)
if at < 0:
return
self.confirm.open(f"Delete the folder “{self.library.folders.name(at)}”? Its photos and folders go to the folder it is in; no photo is deleted.", "Delete", "")
## Ask before the Trash's photos and their files are deleted for good.
func ask_empty(self) -> unit!:
let count = self.library.count_in(folder_tree.trash)
if count == 0:
return
self.emptying = true
self.confirm.open(f"Delete the {count} photo{"" if count == 1 else "s"} in the Trash for good? Their files are deleted from the library.", "Delete", "")
func chosen_delete(self, role: int) -> unit!:
if self.emptying:
self.emptying = false
if role != 0:
return
let count = self.library.empty_trash()
self.tree.refresh()
if let pick = self.on_pick:
pick(self.tree.picked)
self.tell(f"Deleted {count} photo{"" if count == 1 else "s"} for good.")
return
if role != 0 or self.target <= 0:
return
let picked = self.tree.picked
self.library.delete_folder(self.target)
self.tree.refresh()
if self.tree.picked != picked:
if let pick = self.on_pick:
pick(self.tree.picked)
func tell(self, text: str) -> unit!:
if let say = self.on_status:
say(text)
pub func close(self):
for connection in self.camera_connections:
connection.disconnect()
self.camera_connections.clear()
for connection in self.connections:
connection.disconnect()
self.connections.clear()