[code] Search function in the code Toolbox.

When the user types a letter, the toolbox scrolls to the first row that begins
with said letter (without considering if it is lower or upper case). If
there is not such a row, the toolbox scrolls to the first row that has a
letter higher than the typed letter.

Change-Id: I353d23560d8a4d618d96426c393b024e7fb487af
This commit is contained in:
Léa Saviot
2017-11-20 13:23:55 +01:00
parent 95bf921138
commit 92789330de
4 changed files with 55 additions and 1 deletions

View File

@@ -1,7 +1,10 @@
#include "toolbox.h" #include "toolbox.h"
#include "../shared/toolbox_helpers.h" #include "../shared/toolbox_helpers.h"
#include <assert.h> #include <assert.h>
extern "C" {
#include <string.h> #include <string.h>
#include <ctype.h>
}
namespace Code { namespace Code {
@@ -211,6 +214,17 @@ void Toolbox::setAction(Action action) {
m_action = action; m_action = action;
} }
bool Toolbox::handleEvent(Ion::Events::Event event) {
if (::Toolbox::handleEvent(event)) {
return true;
}
if (event.hasText() && strlen(event.text()) == 1) {
scrollToLetter(event.text()[0]);
return true;
}
return false;
}
KDCoordinate Toolbox::rowHeight(int j) { KDCoordinate Toolbox::rowHeight(int j) {
if (typeAtLocation(0, j) == ::Toolbox::LeafCellType) { if (typeAtLocation(0, j) == ::Toolbox::LeafCellType) {
if (m_messageTreeModel->label() != I18n::Message::IfStatementMenu) { if (m_messageTreeModel->label() != I18n::Message::IfStatementMenu) {
@@ -260,5 +274,34 @@ int Toolbox::maxNumberOfDisplayedRows() {
return k_maxNumberOfDisplayedRows; return k_maxNumberOfDisplayedRows;
} }
void Toolbox::scrollToLetter(char letter) {
char lowerLetter = tolower(letter);
// We look for a child MessageTree that starts with the wanted letter.
for (int i = 0; i < m_messageTreeModel->numberOfChildren(); i++) {
char currentFirstLetterLowered = tolower(I18n::translate(m_messageTreeModel->children(i)->label())[0]);
if (currentFirstLetterLowered == lowerLetter) {
scrollToAndSelectChild(i);
return;
}
}
// We did not find a child MessageTree that starts with the wanted letter.
// We scroll to the first child MessageTree that starts with a letter higher
// than the wanted letter.
for (int i = 0; i < m_messageTreeModel->numberOfChildren(); i++) {
char currentFirstLetterLowered = tolower(I18n::translate(m_messageTreeModel->children(i)->label())[0]);
if (currentFirstLetterLowered >= lowerLetter && currentFirstLetterLowered <= 'z') {
scrollToAndSelectChild(i);
return;
}
}
}
void Toolbox::scrollToAndSelectChild(int i) {
assert(i >=0 && i<m_messageTreeModel->numberOfChildren());
m_selectableTableView.deselectTable();
m_selectableTableView.scrollToCell(0, i);
m_selectableTableView.selectCellAtLocation(0, i);
}
} }

View File

@@ -1,8 +1,9 @@
#ifndef CODE_TOOLBOX_H #ifndef CODE_TOOLBOX_H
#define CODE_TOOLBOX_H #define CODE_TOOLBOX_H
#include <escher.h>
#include <apps/i18n.h> #include <apps/i18n.h>
#include <escher.h>
#include <ion/events.h>
#include <kandinsky/text.h> #include <kandinsky/text.h>
namespace Code { namespace Code {
@@ -12,6 +13,9 @@ public:
typedef void (*Action)(void * sender, const char * text); typedef void (*Action)(void * sender, const char * text);
Toolbox(); Toolbox();
void setAction(Action action); void setAction(Action action);
// StackViewController
bool handleEvent(Ion::Events::Event event) override;
protected: protected:
KDCoordinate rowHeight(int j) override; KDCoordinate rowHeight(int j) override;
bool selectLeaf(ToolboxMessageTree * selectedMessageTree) override; bool selectLeaf(ToolboxMessageTree * selectedMessageTree) override;
@@ -26,6 +30,8 @@ private:
constexpr static KDCoordinate k_nodeRowHeight = 40; constexpr static KDCoordinate k_nodeRowHeight = 40;
constexpr static KDCoordinate k_leafRowHeight = 40; constexpr static KDCoordinate k_leafRowHeight = 40;
constexpr static KDText::FontSize k_fontSize = KDText::FontSize::Small; constexpr static KDText::FontSize k_fontSize = KDText::FontSize::Small;
void scrollToLetter(char letter);
void scrollToAndSelectChild(int i);
Action m_action; Action m_action;
MessageTableCellWithMessage m_leafCells[k_maxNumberOfDisplayedRows]; MessageTableCellWithMessage m_leafCells[k_maxNumberOfDisplayedRows];
MessageTableCellWithChevron m_nodeCells[k_maxNumberOfDisplayedRows]; MessageTableCellWithChevron m_nodeCells[k_maxNumberOfDisplayedRows];

View File

@@ -4,5 +4,6 @@
int isupper(int c); int isupper(int c);
int isxdigit(int c); int isxdigit(int c);
int isdigit(int c); int isdigit(int c);
int tolower(int c);
#endif #endif

View File

@@ -11,3 +11,7 @@ int isxdigit(int c) {
int isdigit(int c) { int isdigit(int c) {
return (c >= '0' && c <= '9'); return (c >= '0' && c <= '9');
} }
int tolower(int c) {
return isupper(c) ? 'a' + c - 'A' : c;
}