// What a user can do in the UI. The controls on the screen, the command palette and
// `step` use this one list (design UI-2).
///|
/// An operation of the user.
#derive(Eq, Debug)
pub(all) enum Action {
OpenProject(@app.Project)
/// Opens the dialog that asks for the path.
AddProject
RemoveProject(@app.Project)
/// Gives the primary sidebar this state.
SetPrimarySidebar(PrimarySidebar)
/// Opens the dialog that asks before the stop.
StopApp
}
///|
pub extend Action with Eq::{equal, not_equal}
///|
pub extend Action with @debug.Debug::{to_repr}
///|
/// What an action is, without the details that can change. Two projects can have the
/// same name, and a project can get a different place in the list.
#derive(Eq, Debug)
pub(all) enum ActionId {
OpenId(@app.ProjectId)
AddId
RemoveId(@app.ProjectId)
PrimarySidebarId
StopId
}
///|
pub extend ActionId with Eq::{equal, not_equal}
///|
pub extend ActionId with @debug.Debug::{to_repr}
///|
pub fn Action::id(self : Action) -> ActionId {
match self {
OpenProject(project) => OpenId(project.id)
AddProject => AddId
RemoveProject(project) => RemoveId(project.id)
SetPrimarySidebar(_) => PrimarySidebarId
StopApp => StopId
}
}
///|
/// What stays the same while an action is the same one, as the key of its element in
/// the page. A different action then gets a new element, also at the same place. If
/// that occurs between the press and the release of a button, then the browser gives no
/// click.
fn ActionId::key(self : ActionId) -> String {
match self {
OpenId(project) => "open-\{project.to_int()}"
AddId => "add"
RemoveId(project) => "remove-\{project.to_int()}"
PrimarySidebarId => "primary-sidebar"
StopId => "stop"
}
}
///|
/// The user sees the same actions in both lists. The server changes the time when a
/// project opens, but the page does not show it and no action uses it.
fn look_same(
one : ReadOnlyArray[Action],
other : ReadOnlyArray[Action],
) -> Bool {
let shown = (action : Action) => {
match action {
OpenProject(project) => OpenProject({ ..project, opened: None, })
RemoveProject(project) => RemoveProject({ ..project, opened: None, })
AddProject | SetPrimarySidebar(_) | StopApp => action
}
}
one.map(shown) == other.map(shown)
}
///|
/// A group of actions in the command palette.
#derive(Eq, Debug)
pub(all) enum ActionGroup {
Projects
Commands
}
///|
pub extend ActionGroup with Eq::{equal, not_equal}
///|
pub extend ActionGroup with @debug.Debug::{to_repr}
///|
/// The actions of a view, also the ones that are disabled now: each project in the
/// order of the list, and then the commands. A small window has no primary sidebar.
pub fn actions(model : Model) -> ReadOnlyArray[Action] {
let commands = [AddProject]
if model.open_project() is Some(project) {
commands.push(RemoveProject(project))
}
if model.window is Wide {
commands.push(SetPrimarySidebar(model.primary_sidebar.other()))
}
commands.push(StopApp)
let projects = model.projects.unwrap_or([])
ReadOnlyArray::from_array(
[..projects.iter().map(project => OpenProject(project)), ..commands],
)
}
///|
/// The text of the action for the user.
pub fn Action::title(self : Action) -> String {
match self {
OpenProject(project) => project.name
AddProject => "Add project…"
RemoveProject(_) => "Remove project from list"
SetPrimarySidebar(Hidden) => "Hide primary sidebar"
SetPrimarySidebar(Shown) => "Show primary sidebar"
StopApp => "Stop Mission Control"
}
}
///|
pub fn Action::group(self : Action) -> ActionGroup {
match self {
OpenProject(_) => Projects
AddProject | RemoveProject(_) | SetPrimarySidebar(_) | StopApp => Commands
}
}
///|
/// Words that find the action in the command palette, and that its title does not have.
pub fn Action::search_words(self : Action) -> Array[String] {
match self {
OpenProject(project) => [project.path]
AddProject => ["create", "new", "folder"]
RemoveProject(project) => ["delete", "forget", project.name, project.path]
SetPrimarySidebar(_) =>
["toggle", "collapse", "expand", "panel", "side bar"]
StopApp => ["quit", "exit", "close"]
}
}
///|
/// The user can do the action now. `step` ignores an action that is not enabled.
pub fn Action::enabled(self : Action, model : Model) -> Bool {
match self {
// The page has the list, so it can open a project by itself.
OpenProject(_) => true
SetPrimarySidebar(_) => model.window is Wide
AddProject | RemoveProject(_) | StopApp => model.connection is Connected
}
}
///|
/// The action has each word of the query in its title or in one of its search words.
/// The order of the words and their case make no difference.
pub fn finds(query : String, title : String, words : Array[String]) -> Bool {
let texts = [title, ..words].map(lower_case)
words_of(lower_case(query))
.iter()
.all(word => texts.iter().any(text => text.contains(word)))
}
///|
/// The text in lower case. `String::to_lower` changes only the letters A to Z.
extern "js" fn lower_case(text : String) -> String =
#| (text) => text.toLowerCase()
///|
/// The words of a text, which each kind of space separates.
extern "js" fn words_of(text : String) -> FixedArray[String] =
#| (text) => text.split(/\s+/).filter(word => word !== "")