[code] Avoid init/deinit-ing uPy in each editor redraw

This commit is contained in:
Romain Goyet
2018-06-04 14:57:10 +02:00
committed by EmilieNumworks
parent b52590e2f7
commit caff93cda0
4 changed files with 24 additions and 16 deletions

View File

@@ -53,6 +53,7 @@ void EditorController::didBecomeFirstResponder() {
}
void EditorController::viewWillAppear() {
m_editorView.loadSyntaxHighlighter();
m_editorView.setCursorLocation(strlen(m_editorView.text()));
}
@@ -60,6 +61,7 @@ void EditorController::viewDidDisappear() {
m_menuController->scriptContentEditionDidFinish();
delete[] m_areaBuffer;
m_areaBuffer = nullptr;
m_editorView.unloadSyntaxHighlighter();
}
bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events::Event event) {

View File

@@ -12,15 +12,15 @@ public:
void setTextAreaDelegate(TextAreaDelegate * delegate) {
m_textArea.setDelegate(delegate);
}
const char * text() const {
return m_textArea.text();
}
const char * text() const { return m_textArea.text(); }
void setText(char * textBuffer, size_t textBufferSize) {
m_textArea.setText(textBuffer, textBufferSize);
}
bool setCursorLocation(int location) {
return m_textArea.setCursorLocation(location);
}
void loadSyntaxHighlighter() { m_textArea.loadSyntaxHighlighter(); };
void unloadSyntaxHighlighter() { m_textArea.unloadSyntaxHighlighter(); };
void scrollViewDidChangeOffset(ScrollViewDataSource * scrollViewDataSource) override;
void didBecomeFirstResponder() override;
private:

View File

@@ -58,15 +58,19 @@ static inline int TokenLength(mp_lexer_t * lex) {
}
}
PythonTextArea::ContentView::ContentView(KDText::FontSize fontSize) :
TextArea::ContentView(fontSize)
{
void PythonTextArea::ContentView::loadSyntaxHighlighter() {
assert(m_pythonHeap == nullptr);
m_pythonHeap = static_cast<char *>(malloc(k_pythonHeapSize));
if (m_pythonHeap != nullptr) {
MicroPython::init(m_pythonHeap, m_pythonHeap + k_pythonHeapSize);
}
}
PythonTextArea::ContentView::~ContentView() {
if (m_pythonHeap) {
void PythonTextArea::ContentView::unloadSyntaxHighlighter() {
if (m_pythonHeap != nullptr) {
MicroPython::deinit();
free(m_pythonHeap);
m_pythonHeap = nullptr;
}
}
@@ -85,7 +89,7 @@ void PythonTextArea::ContentView::clearRect(KDContext * ctx, KDRect rect) const
void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char * text, size_t length, int fromColumn, int toColumn) const {
LOG_DRAW("Drawing \"%.*s\"\n", length, text);
if (!m_pythonHeap) {
if (m_pythonHeap == nullptr) {
drawStringAt(
ctx,
line,
@@ -98,8 +102,6 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char
return;
}
MicroPython::init(m_pythonHeap, m_pythonHeap + k_pythonHeapSize);
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
/* We're using the MicroPython lexer to do syntax highlighting on a per-line
@@ -151,8 +153,6 @@ void PythonTextArea::ContentView::drawLine(KDContext * ctx, int line, const char
mp_lexer_free(lex);
nlr_pop();
}
MicroPython::deinit();
}
KDRect PythonTextArea::ContentView::dirtyRectFromCursorPosition(size_t index, bool lineBreak) const {

View File

@@ -12,11 +12,18 @@ public:
m_contentView(fontSize)
{
}
void loadSyntaxHighlighter() { m_contentView.loadSyntaxHighlighter(); }
void unloadSyntaxHighlighter() { m_contentView.unloadSyntaxHighlighter(); }
protected:
class ContentView : public TextArea::ContentView {
public:
ContentView(KDText::FontSize fontSize);
~ContentView();
ContentView(KDText::FontSize fontSize) :
TextArea::ContentView(fontSize),
m_pythonHeap(nullptr)
{
}
void loadSyntaxHighlighter();
void unloadSyntaxHighlighter();
void clearRect(KDContext * ctx, KDRect rect) const override;
void drawLine(KDContext * ctx, int line, const char * text, size_t length, int fromColumn, int toColumn) const override;
KDRect dirtyRectFromCursorPosition(size_t index, bool lineBreak) const override;
@@ -27,7 +34,6 @@ protected:
private:
const ContentView * nonEditableContentView() const override { return &m_contentView; }
ContentView m_contentView;
char * m_pythonHeap;
};
}