Ir al contenido
← Todos los ejemplos

Ejemplo 14

Open panel

open one of the panels the terminal itself offers, check it is

Categoría
Espacio de trabajo: lee o cambia gráficos y paneles
Permisos
workspace.readworkspace.write
Antes de ejecutarlo
a terminal window
Qué hace
CHANGES YOUR WORKSPACE — opens (or focuses, if already
Cómo ejecutarlo
in TTT, open "T0 Lang", open this file, grant workspace.read and
Lenguaje
>=0.1.1, <0.2.0
Host
>=0.1.3, <0.2.0
Necesita
workspace >=0.1.2, <0.2.0

Código

// Purpose: open one of the panels the terminal itself offers, check it is
// open and in focus, then give the focus back to the chart that had it.
// Category: workspace
// Permissions: workspace.read, workspace.write
// Preconditions: a terminal window
// Side effects: CHANGES YOUR WORKSPACE — opens (or focuses, if already
// open) the first panel kind the terminal offers, then focuses the chart
// that was active before.
// Run: in TTT, open "T0 Lang", open this file, grant workspace.read and
// workspace.write, Run.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.3, <0.2.0";
requires "workspace" ">=0.1.2, <0.2.0";

fn main() -> Result<Unit, Error> {
    // The rule: the first kind the terminal's own registry lists. No panel
    // name is invented here.
    let kinds = workspace.panel_kinds();
    if kinds.is_empty() {
        print("this terminal offers no panel to open");
        return Ok(());
    }
    let kind = kinds.first().expect("checked above");
    let was_active = workspace.active_chart();

    match workspace.open_panel("Astrology") {
        Ok(_) => print("unexpected: an unknown panel kind was opened"),
        Err(e) => print("refused as it should be: " + e.code),
    }

    let panel = workspace.open_panel(kind)?;
    print("opened " + panel.kind + " (" + panel.title + ")");
    let mut found = false;
    for p in workspace.panels() {
        if p.panel == panel.panel {
            found = true;
            print(if p.focused { "it is open and in focus" } else { "it is open but NOT in focus" });
        }
    }
    if !found {
        print("it is NOT among the open panels");
    }

    match was_active {
        Some(chart) => {
            workspace.focus_chart(chart.chart)?;
            print("focus back on " + chart.symbol);
        }
        None => print("no chart was active before"),
    }
    return Ok(());
}