Skip to content

Commit

Permalink
ui: fix script console to use a ListView widget for performance (inst…
Browse files Browse the repository at this point in the history
…ead of TextEdit) and auto-scroll to the bottom when appending new items
  • Loading branch information
JamesDunne committed Mar 5, 2025
1 parent 822dfb3 commit df16496
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
1 change: 0 additions & 1 deletion bsnes/target-bsnes/program/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ struct Program : Lock, Emulator::Platform {
asIScriptEngine *engine;

string location;
string console;
} scriptHostState;

vector<tuple<shared_pointer<HID::Device>, uint>> escapeKeys;
Expand Down
13 changes: 6 additions & 7 deletions bsnes/target-bsnes/program/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ auto Program::scriptMessage(const string& msg, bool alert, ::Script::MessageLeve
auto levelName = ::Script::nameMessageLevel(level);

// append to stdout:
printf("[%s] %.*s\n", levelName, msg.size(), msg.data());
if (level != ::Script::MessageLevel::MSG_INFO) {
printf("[%s] %.*s\n", levelName, msg.size(), msg.data());
} else {
printf("%.*s\n", msg.size(), msg.data());
}

// append to script console:
scriptHostState.console.append("[");
scriptHostState.console.append(levelName);
scriptHostState.console.append("] ");
scriptHostState.console.append(msg);
scriptHostState.console.append("\n");
scriptConsole.update();
scriptConsole.appendItem(msg, level);

// alert in status bar:
if (alert) {
Expand Down
41 changes: 30 additions & 11 deletions bsnes/target-bsnes/tools/script-console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@ auto ScriptConsole::create() -> void {
setCollapsible();
setVisible(false);

#if 0 && defined(Hiro_SourceEdit)
consoleView.setFont(Font().setFamily(Font::Mono).setSize(10));
#else
consoleView.setFont(Font().setFamily(Font::Mono));
#endif
consoleView.setEditable(false);
consoleView.setWordWrap(false);
consoleView.reset();
consoleView.setBatchable(true);

nameLabel.setText("no script loaded");
loadButton.setText("Clear").onActivate([&] {
program.scriptHostState.console = "";
update();
clear();
});
}

auto ScriptConsole::appendItem(const string& msg, ::Script::MessageLevel level) -> void {
auto item = ListViewItem().setText(msg);
switch (level) {
case ::Script::MessageLevel::MSG_DEBUG:
item.setIcon(Icon::Prompt::Question);
break;
case ::Script::MessageLevel::MSG_WARN:
item.setIcon(Icon::Prompt::Warning);
break;
case ::Script::MessageLevel::MSG_ERROR:
item.setIcon(Icon::Prompt::Error);
break;
case ::Script::MessageLevel::MSG_INFO:
default:
item.setIcon(Icon::Prompt::Information);
break;
}

consoleView.append(item);
// NOTE(jsd): hack to scroll the item into view
item.setFocused();
}

auto ScriptConsole::clear() -> void {
consoleView.reset();
}

auto ScriptConsole::update() -> void {
nameLabel.setText(program.scriptHostState.location);
consoleView.setText(program.scriptHostState.console);
consoleView.setTextCursor(program.scriptHostState.console.size());
}
8 changes: 3 additions & 5 deletions bsnes/target-bsnes/tools/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,15 @@ struct ManifestViewer : VerticalLayout {

struct ScriptConsole : VerticalLayout {
auto create() -> void;
auto appendItem(const string& msg, ::Script::MessageLevel level = ::Script::MessageLevel::MSG_INFO) -> void;
auto clear() -> void;
auto update() -> void;

public:
HorizontalLayout informationLayout{this, Size{~0, 0}};
Label nameLabel{&informationLayout, Size{~0, 0}};
Button loadButton{&informationLayout, Size{80_sx, 0}};
#if 0 && defined(Hiro_SourceEdit)
SourceEdit consoleView{this, Size{~0, ~0}};
#else
TextEdit consoleView{this, Size{~0, ~0}};
#endif
ListView consoleView{this, Size{~0, ~0}};
};

struct ToolsWindow : Window {
Expand Down

0 comments on commit df16496

Please sign in to comment.