///|
/// The entry of an installed app in the application menu of Linux, and the place of its
/// icon. `mise run desktop:install` writes them.
#derive(Eq, Debug)
struct MenuEntry {
file : @core.FolderPath
text : String
icon_theme : @core.FolderPath
icon_file : String
}
///|
/// The entry of an app that is installed with these variables. There is none on Windows
/// and macOS. There is also none when `MISSION_CONTROL_HOME` gives the base folder: the
/// menu does not set this variable, so the app would use the default folder.
///
/// The name of the file and of the icon is the identifier of the app. Its windows have the
/// same class, so the desktop knows which entry a window belongs to.
pub fn MenuEntry::of_env(
place : InstallPlace,
platform : @core.Platform,
env : Map[String, String],
) -> MenuEntry? raise @core.BaseFolderError {
guard platform is Linux &&
env.get("MISSION_CONTROL_HOME") is (None | Some("")) else {
return None
}
let data = @core.linux_data_folder(env)
let id = app_identifier(place.instance())
let program = place.program().to_string()
let text =
$|[Desktop Entry]
$|Type=Application
$|Name=\{window_title(place.instance())}
$|Comment=List and open the projects on this machine
$|TryExec=\{escaped(program)}
$|Exec=\{exec_argument(program)}
$|Icon=\{id}
$|Terminal=false
$|Categories=Development;
$|StartupWMClass=\{id}
$|StartupNotify=false
$|
Some({
file: from(data, "applications/\{id}.desktop"),
text,
icon_theme: from(data, "icons/hicolor"),
icon_file: "\{id}.png",
})
}
///|
/// The text in the form of a string value of a desktop entry.
fn escaped(text : String) -> String {
text
.replace_all(old="\\", new="\\\\")
.replace_all(old="\n", new="\\n")
.replace_all(old="\r", new="\\r")
.replace_all(old="\t", new="\\t")
}
///|
/// The text as one argument of the key `Exec`. The menu first reads the string value, then
/// the quotes, and then the codes with a percent sign.
fn exec_argument(text : String) -> String {
let quoted = text
.replace_all(old="\\", new="\\\\")
.replace_all(old="\"", new="\\\"")
.replace_all(old="`", new="\\`")
.replace_all(old="$", new="\\$")
.replace_all(old="%", new="%%")
escaped("\"\{quoted}\"")
}
///|
/// The path of the file of the entry.
pub fn MenuEntry::file(self : MenuEntry) -> @core.FolderPath {
self.file
}
///|
/// The text of the file of the entry.
pub fn MenuEntry::text(self : MenuEntry) -> String {
self.text
}
///|
/// The JSON form. `scripts/desktop-install.mbtx` reads it, because a script cannot import a
/// module of the workspace. An icon of 48 pixels is at `48x48/apps/` in the
/// folder `icon_theme`.
pub impl ToJson for MenuEntry with fn to_json(self) {
{
"file": self.file.to_string(),
"text": self.text,
"icon_theme": self.icon_theme.to_string(),
"icon_file": self.icon_file,
}
}
///|
pub extend MenuEntry with Eq::{not_equal, equal}
///|
pub extend MenuEntry with @debug.Debug::{to_repr}
///|
pub extend MenuEntry with ToJson::{to_json}