Skip to content

Commit

Permalink
#1219 Add menu entry "Edit compartment-wide Lua code"
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Sep 26, 2024
1 parent 743677f commit 3a27560
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
42 changes: 39 additions & 3 deletions main/src/infrastructure/ui/header_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ use crate::infrastructure::ui::{
deserialize_data_object, deserialize_data_object_from_json, dry_run_lua_script,
get_text_from_clipboard, menus, serialize_data_object, serialize_data_object_to_json,
serialize_data_object_to_lua, DataObject, GroupFilter, GroupPanel, IndependentPanelManager,
MappingRowsPanel, PlainTextEngine, ScriptEditorInput, SearchExpression, SerializationFormat,
SharedIndependentPanelManager, SharedMainState, SimpleScriptEditorPanel, SourceFilter,
UntaggedDataObject,
LuaCompartmentCommonScriptEngine, MappingRowsPanel, PlainTextEngine, ScriptEditorInput,
SearchExpression, SerializationFormat, SharedIndependentPanelManager, SharedMainState,
SimpleScriptEditorPanel, SourceFilter, UntaggedDataObject,
};
use crate::infrastructure::ui::{dialog_util, CompanionAppPresenter};
use anyhow::{bail, Context};
Expand Down Expand Up @@ -139,6 +139,36 @@ impl HeaderPanel {
self.open_extra_panel(editor);
}

fn edit_compartment_common_lua(&self) {
let compartment = self.active_compartment();
let session = self.session();
let initial_notes = session
.borrow()
.compartment_common_lua(compartment)
.to_owned();
let weak_session = self.session.clone();
let input = ScriptEditorInput {
initial_value: initial_notes,
engine: Box::new(LuaCompartmentCommonScriptEngine::new()),
help_url: "",
set_value: move |edited_notes| {
let weak_session = weak_session.clone();
if let Some(session) = weak_session.upgrade() {
session.borrow_mut().change_with_notification(
SessionCommand::ChangeCompartment(
compartment,
CompartmentCommand::SetCommonLua(edited_notes),
),
None,
weak_session,
)
}
},
};
let editor = SimpleScriptEditorPanel::new(input);
self.open_extra_panel(editor);
}

fn open_extra_panel(&self, panel: impl View + 'static) {
open_child_panel_dyn(&self.extra_panel, panel, self.view.require_window());
}
Expand Down Expand Up @@ -548,6 +578,10 @@ impl HeaderPanel {
),
],
),
item(
"Edit compartment-wide Lua code",
MainMenuAction::EditCompartmentWideLuaCode,
),
labeled_separator("Unit-related"),
// Unit scope
menu(
Expand Down Expand Up @@ -855,6 +889,7 @@ impl HeaderPanel {
self.create_compartment_preset_workspace(true)
}
MainMenuAction::ReloadAllCompartmentPresets => self.reload_all_compartment_presets(),
MainMenuAction::EditCompartmentWideLuaCode => self.edit_compartment_common_lua(),
MainMenuAction::OpenPotBrowser => {
self.show_pot_browser();
}
Expand Down Expand Up @@ -3115,6 +3150,7 @@ enum MainMenuAction {
EditCompartmentParameter(CompartmentKind, RangeInclusive<CompartmentParamIndex>),
SendFeedbackNow,
LogDebugInfo,
EditCompartmentWideLuaCode,
CreateCompartmentPresetWorkspace,
CreateCompartmentPresetWorkspaceIncludingFactoryPresets,
}
Expand Down
25 changes: 25 additions & 0 deletions main/src/infrastructure/ui/simple_script_editor_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,31 @@ impl ScriptEngine for LuaFeedbackScriptEngine {
}
}

pub struct LuaCompartmentCommonScriptEngine {
lua: SafeLua,
}

impl LuaCompartmentCommonScriptEngine {
pub fn new() -> Self {
Self {
lua: SafeLua::new().unwrap(),
}
}
}

impl ScriptEngine for LuaCompartmentCommonScriptEngine {
fn compile(&self, code: &str) -> Result<Box<dyn Script>, Box<dyn Error>> {
let env = self.lua.create_fresh_environment(false)?;
self.lua
.compile_as_function("Feedback script", code, env.clone())?;
Ok(Box::new(()))
}

fn file_extension(&self) -> &'static str {
".lua"
}
}

pub struct PlainTextEngine;

impl ScriptEngine for PlainTextEngine {
Expand Down

0 comments on commit 3a27560

Please sign in to comment.