Skip to content

Commit

Permalink
Fixed MIDIDebugger blocking message thread
Browse files Browse the repository at this point in the history
  • Loading branch information
FangCunWuChang committed Jan 27, 2024
1 parent cd0b9e7 commit e01ec73
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 43 deletions.
88 changes: 48 additions & 40 deletions src/audioCore/debug/MIDIDebugger.cpp
Original file line number Diff line number Diff line change
@@ -1,60 +1,68 @@
#include "MIDIDebugger.h"

MIDIDebugger::MIDIDebugger()
: Component("MIDIDebugger") {
this->messageOutput = std::make_unique<juce::TextEditor>();
this->messageOutput->setMultiLine(true);
this->messageOutput->setReadOnly(true);
this->messageOutput->setTabKeyUsedAsCharacter(true);
this->messageOutput->setScrollToShowCursor(true);
this->messageOutput->setScrollbarsShown(false);
this->addAndMakeVisible(this->messageOutput.get());
: AnimatedAppComponent() {
this->setFramesPerSecond(10);
}

void MIDIDebugger::resized() {
this->messageOutput->setBounds(this->getLocalBounds());
void MIDIDebugger::update() {
/** Nothing To Do */
}

void MIDIDebugger::paint(juce::Graphics& g) {
/** Color */
auto& laf = this->getLookAndFeel();
juce::Colour textColor = laf.findColour(
juce::TextEditor::ColourIds::textColourId);
juce::Colour backgroundColor = laf.findColour(
juce::TextEditor::ColourIds::backgroundColourId);

/** Size */
int paddingWidth = 10, paddingHeight = 10;
int lineHeight = 20;
float fontHeight = 15.0f;

/** Font */
juce::Font textFont(fontHeight);

/** Background */
g.setColour(backgroundColor);
g.fillAll();

/** Text */
g.setColour(textColor);
g.setFont(textFont);

int top = paddingHeight;
for (auto& i : this->mesList) {
juce::Rectangle<int> textRect(
paddingWidth, top,
this->getWidth() - paddingWidth * 2, lineHeight);
g.drawFittedText(i, textRect,
juce::Justification::centredLeft, 1, 1.f);
top += lineHeight;
}
}

void MIDIDebugger::addMessage(const juce::MidiMessage& message, bool isInput) {
juce::Time current = juce::Time::getCurrentTime();

juce::String text;
text += isInput ? "[I] " : "[O] ";
text += current.formatted("[%H:%M:%S] ");
text += (isInput ? "[I] " : "[O] ");
text += message.getDescription();
text += " | ";
text += juce::String::toHexString(
message.getRawData(), message.getRawDataSize(), 1);
text += "\n";
juce::MessageManager::callAsync(
[output = juce::Component::SafePointer(this->messageOutput.get()),
text, maxNum = this->maxNum] {
if (output) {
juce::String current = output->getText();
juce::StringArray list = juce::StringArray::fromLines(current);

list.add(text);
list.removeEmptyStrings(true);
if (list.size() > maxNum) {
list.removeRange(0, list.size() - maxNum);
}

output->setText(list.joinIntoString("\n"));
output->moveCaretToEnd();
}
});
this->mesList.insert(this->mesList.begin(), text);

if (this->mesList.size() > this->maxNum) {
this->mesList.resize(this->maxNum);
}
}

void MIDIDebugger::setMaxNum(int num) {
this->maxNum = std::max(num, 0);

juce::String current = this->messageOutput->getText();
juce::StringArray list = juce::StringArray::fromLines(current);

list.removeEmptyStrings(true);
if (list.size() > maxNum) {
list.removeRange(0, list.size() - std::max(maxNum, 0));
}

this->messageOutput->setText(list.joinIntoString("\n"));
this->messageOutput->moveCaretToEnd();
}

int MIDIDebugger::getMaxNum() const {
Expand Down
7 changes: 4 additions & 3 deletions src/audioCore/debug/MIDIDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

#include <JuceHeader.h>

class MIDIDebugger final : public juce::Component {
class MIDIDebugger final : public juce::AnimatedAppComponent {
public:
MIDIDebugger();

void resized() override;
void update() override;
void paint(juce::Graphics& g) override;

void addMessage(const juce::MidiMessage& message, bool isInput = true);

void setMaxNum(int num = 30);
int getMaxNum() const;

private:
std::unique_ptr<juce::TextEditor> messageOutput = nullptr;
std::list<juce::String> mesList;
int maxNum = 30;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(MIDIDebugger)
Expand Down

0 comments on commit e01ec73

Please sign in to comment.