[poincare/parser] Add context

This commit is contained in:
Léa Saviot
2020-01-16 11:13:22 +01:00
parent 158a45e94d
commit 756eeeb2d8
38 changed files with 182 additions and 156 deletions

View File

@@ -9,6 +9,7 @@
#include <escher/view_controller.h>
#include <escher/warning_controller.h>
#include <ion/storage.h>
#include <poincare/context.h>
/* An app is fed events and outputs drawing calls.
*
@@ -63,6 +64,7 @@ public:
View * modalView();
virtual int numberOfTimers();
virtual Timer * timerAtIndex(int i);
virtual Poincare::Context * localContext() { return nullptr; }
protected:
App(Snapshot * snapshot, ViewController * rootViewController, I18n::Message warningMessage = (I18n::Message)0);
ModalViewController m_modalViewController;

View File

@@ -0,0 +1,11 @@
#ifndef ESCHER_CONTEXT_PROVIDER_H
#define ESCHER_CONTEXT_PROVIDER_H
#include <poincare/context.h>
class ContextProvider {
public:
virtual Poincare::Context * context() const { return nullptr; }
};
#endif

View File

@@ -20,6 +20,7 @@ public:
m_delegate(delegate)
{}
void setDelegates(InputEventHandlerDelegate * inputEventHandlerDelegate, LayoutFieldDelegate * delegate) { m_inputEventHandlerDelegate = inputEventHandlerDelegate; m_delegate = delegate; }
Poincare::Context * context() const;
bool isEditing() const override { return m_contentView.isEditing(); }
void setEditing(bool isEditing) override;
void clearLayout() { m_contentView.clearLayout(); }
@@ -77,7 +78,7 @@ private:
Poincare::Layout * selectionEnd() { return &m_selectionEnd; }
void addSelection(Poincare::Layout addedLayout);
bool resetSelection(); // returns true if the selection was indeed reset
void copySelection();
void copySelection(Poincare::Context * context);
bool selectionIsEmpty() const;
void deleteSelection();

View File

@@ -1,12 +1,13 @@
#ifndef ESCHER_LAYOUT_FIELD_DELEGATE_H
#define ESCHER_LAYOUT_FIELD_DELEGATE_H
#include <escher/context_provider.h>
#include <ion/events.h>
#include <poincare/layout.h>
class LayoutField;
class LayoutFieldDelegate {
class LayoutFieldDelegate : public ContextProvider{
public:
virtual bool layoutFieldShouldFinishEditing(LayoutField * layoutField, Ion::Events::Event event) = 0;
virtual bool layoutFieldDidReceiveEvent(LayoutField * layoutField, Ion::Events::Event event) = 0;

View File

@@ -1,9 +1,11 @@
#ifndef ESCHER_SELECTABLE_TABLE_VIEW_DELEGATE_H
#define ESCHER_SELECTABLE_TABLE_VIEW_DELEGATE_H
#include <escher/context_provider.h>
class SelectableTableView;
class SelectableTableViewDelegate {
class SelectableTableViewDelegate : public ContextProvider {
public:
/* withinTemporarySelection flag indicates when the selection change happens
* in a temporary deselection: indeed, when reloading the data of the table,

View File

@@ -40,7 +40,7 @@ bool ExpressionField::isEditing() const {
const char * ExpressionField::text() {
if (!editionIsInTextField()) {
m_layoutField.layout().serializeParsedExpression(m_textField.draftTextBuffer(), k_textFieldBufferSize);
m_layoutField.layout().serializeParsedExpression(m_textField.draftTextBuffer(), k_textFieldBufferSize, m_layoutField.context());
}
return m_textField.draftTextBuffer();
}

View File

@@ -159,7 +159,7 @@ bool LayoutField::ContentView::resetSelection() {
return true;
}
void LayoutField::ContentView::copySelection() {
void LayoutField::ContentView::copySelection(Context * context) {
if (selectionIsEmpty()) {
return;
}
@@ -167,7 +167,7 @@ void LayoutField::ContentView::copySelection() {
char buffer[bufferSize];
if (m_selectionStart == m_selectionEnd) {
m_selectionStart.serializeParsedExpression(buffer, bufferSize);
m_selectionStart.serializeParsedExpression(buffer, bufferSize, context);
if (buffer[0] == 0) {
int offset = 0;
if (m_selectionStart.type() == LayoutNode::Type::VerticalOffsetLayout) {
@@ -283,6 +283,10 @@ void LayoutField::setEditing(bool isEditing) {
}
}
Context * LayoutField::context() const {
return (m_delegate != nullptr) ? m_delegate->context() : nullptr;
}
CodePoint LayoutField::XNTCodePoint(CodePoint defaultXNTCodePoint) {
CodePoint xnt = m_contentView.cursor()->layout().XNTCodePoint();
if (xnt != UCodePointNull) {
@@ -340,7 +344,7 @@ bool LayoutField::handleEventWithText(const char * text, bool indentation, bool
} else if ((strcmp(text, "[") == 0) || (strcmp(text, "]") == 0)) {
m_contentView.cursor()->addEmptyMatrixLayout();
} else {
Expression resultExpression = Expression::Parse(text);
Expression resultExpression = Expression::Parse(text, nullptr);
if (resultExpression.isUninitialized()) {
// The text is not parsable (for instance, ",") and is added char by char.
KDSize previousLayoutSize = minimalSizeForOptimalDisplay();
@@ -468,7 +472,7 @@ bool LayoutField::privateHandleEvent(Ion::Events::Event event) {
return true;
}
if (event == Ion::Events::Copy && isEditing()) {
m_contentView.copySelection();
m_contentView.copySelection(context());
return true;
}
if (event == Ion::Events::Clear && isEditing()) {

View File

@@ -149,7 +149,7 @@ bool SelectableTableView::handleEvent(Ion::Events::Event event) {
if (!l.isUninitialized()) {
constexpr int bufferSize = TextField::maxBufferSize();
char buffer[bufferSize];
l.serializeParsedExpression(buffer, bufferSize);
l.serializeParsedExpression(buffer, bufferSize, m_delegate == nullptr ? nullptr : m_delegate->context());
Clipboard::sharedClipboard()->store(buffer);
return true;
}