More versatile selection commands

This commit is contained in:
2021-10-07 20:51:18 -06:00
parent 9fdf00248c
commit 9b2d69526d
2 changed files with 74 additions and 32 deletions

View File

@@ -166,28 +166,7 @@
(+ "Available commands:\n"
(commandNames.join "\n"))) [])
(method selectionCommand [entries]
(let [lastSelectedEntries _selectedEntries]
(set _selectedEntries entries)
(ui.onSelectionChanged entries lastSelectedEntries)) [])
(defCommand SelectEntry [e OneEntry]
(selectionCommand [e]))
(defCommand ToggleEntrySelection [e OneEntry]
(let [newSelection (_selectedEntries.copy)]
(unless (newSelection.remove e)
(newSelection.push e))
(selectionCommand newSelection)))
(defCommand SelectEntries [entries (Entries null null)]
(selectionCommand entries) [])
(defCommand SelectAllEntries []
(selectionCommand (for =>id e archive.entries e)))
(defCommand SelectLastChangeSet []
(selectionCommand lastChangeSet))
(load "SelectionCommands.kiss")
(defCommand PrintSelectedEntries [entries (SelectedEntries null null)]
(doFor e entries (ui.displayMessage (archive.fullString e))) [])
@@ -220,12 +199,7 @@
(removeTags archive e tagsToRemove))
entries) // TODO this includes entries that didn't have the tag in the changeset
(defCommand SelectByTags [tagsBoolExp (Text null)]
(SelectEntries (filter archive.entries ->e (tagsMatch archive e tagsBoolExp))))
(defCommand SelectByComponents [componentsBoolExp (Text null)]
(SelectEntries (filter archive.entries ->e (componentsMatch e componentsBoolExp))))
(defCommand AddFiles [entries (SelectedEntries 1 null)
// TODO add File and Files as an argument type for commands, ArchiveUI
// TODO make tkinter file browser externs and use tkinter as the file picking mechanism for CLI
@@ -254,7 +228,4 @@
(withWritableComponents archive e [scaleComponent Scale]
(set scaleComponent scale))
(addComponent archive e Scale scale)))
entries)
(defCommand SelectByName [name (Text null)]
(SelectEntries (nameSystem.getEntries name)) []))
entries))

View File

@@ -0,0 +1,71 @@
// Every command that can select entries has variants that can toggleSelect,
// addSelect or deSelect instead of setting the selection directly
// TODO The three varations of selection are all O(n) when they would't need to be if _selectedEntries was a Map
// TODO these commands could avoid calling onSelectionChanged if they don't actually modify the selection
(method selectionCommand [:Array<Entry> entries]
(let [lastSelectedEntries _selectedEntries]
(set _selectedEntries entries)
(ui.onSelectionChanged entries lastSelectedEntries)) [])
(method addSelectionCommand [:Array<Entry> entries]
(let [lastSelectedEntries (_selectedEntries.copy)]
(doFor e entries
(unless (_selectedEntries.contains e)
(_selectedEntries.push e)))
(ui.onSelectionChanged _selectedEntries lastSelectedEntries)) [])
(method deSelectionCommand [:Array<Entry> entries]
(let [lastSelectedEntries (_selectedEntries.copy)]
(doFor e entries
(_selectedEntries.remove e))
(ui.onSelectionChanged _selectedEntries lastSelectedEntries)) [])
(method toggleSelectionCommand [:Array<Entry> entries]
(let [lastSelectedEntries (_selectedEntries.copy)]
(doFor e entries
(unless (_selectedEntries.remove e)
(_selectedEntries.push e)))
(ui.onSelectionChanged _selectedEntries lastSelectedEntries)) [])
// Define all 4 variants of the command that selects according to the behavior in body
(defMacro defSelectCommand [name args &body body]
(let [nameStr (symbolNameValue name)
addName (symbol "Add$nameStr")
deSelectName (symbol "De$nameStr")
toggleName (symbol "Toggle$nameStr")]
`{
(defCommand ,name ,args
(selectionCommand {,@body}))
(defCommand ,addName ,args
(addSelectionCommand {,@body}))
(defCommand ,deSelectName ,args
(deSelectionCommand {,@body}))
(defCommand ,toggleName ,args
(toggleSelectionCommand {,@body}))
}))
(defSelectCommand SelectEntry [e OneEntry]
[e])
(defSelectCommand SelectEntries [entries (Entries null null)]
entries)
(defSelectCommand SelectAllEntries []
(for =>id e archive.entries e))
(defSelectCommand SelectLastChangeSet []
lastChangeSet)
(defSelectCommand SelectByTags [tagsBoolExp (Text null)]
(filter archive.entries ->e (tagsMatch archive e tagsBoolExp)))
(defSelectCommand SelectByComponents [componentsBoolExp (Text null)]
(filter archive.entries ->e (componentsMatch e componentsBoolExp)))
(defSelectCommand SelectByName [name (Text null)]
(nameSystem.getEntries name))