Skip to content

Commit 6ec9bec

Browse files
committed
UI: re-arranged keypad layout according punctuation frequency
1 parent 0b500d3 commit 6ec9bec

File tree

3 files changed

+84
-81
lines changed

3 files changed

+84
-81
lines changed

src/ui/keycode.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,11 @@ enum KeyCode : char {
1717
K_SEARCH = (char)-6,
1818
K_RUN = (char)-7,
1919
K_TOGGLE = (char)-8,
20-
K_FIND = (char)-9,
21-
K_LINE_UP = (char)-10,
22-
K_PAGE_UP = (char)-11,
23-
K_LINE_DOWN= (char)-12,
24-
K_PAGE_DOWN= (char)-13,
25-
K_TAG = (char)-14,
26-
K_EXT1 = (char)-15,
27-
K_EXT2 = (char)-16,
28-
K_EXT3 = (char)-17,
29-
K_EXT4 = (char)-18,
30-
K_EXT5 = (char)-19,
20+
K_LINE_UP = (char)-9,
21+
K_PAGE_UP = (char)-10,
22+
K_LINE_DOWN= (char)-11,
23+
K_PAGE_DOWN= (char)-12,
24+
K_TAG = (char)-13,
3125
K_NULL = 0,
3226
K_BACKSPACE = 8,
3327
K_TAB = 9,
@@ -88,14 +82,6 @@ inline bool isPrintable(KeyCode key) {
8882
return key >= K_SPACE && key <= K_TILDE;
8983
}
9084

91-
inline bool isNumber(KeyCode key) {
92-
return key >= K_0 && key <= K_9;
93-
}
94-
95-
inline bool isExtended(KeyCode key) {
96-
return key >= K_EXT5 && key <= K_EXT1;
97-
}
98-
9985
inline bool isArrow(KeyCode key) {
10086
return key == K_LINE_UP || key == K_PAGE_UP || key == K_LINE_DOWN || key == K_PAGE_DOWN;
10187
}

src/ui/keypad.cpp

Lines changed: 74 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "ui/keypad_icons.h"
1515
#include "keypad.h"
1616

17-
constexpr int ROW_LENGTHS[] = {7, 10, 9, 9, 8};
17+
constexpr int ROW_LENGTHS[] = {7, 10, 9, 9, 9};
1818
constexpr int MAX_ROWS = 5;
1919
constexpr int MAX_COLS = 10;
2020
constexpr int QWERTY_ROW = 1;
@@ -32,6 +32,12 @@ constexpr double MAX_HEIGHT_FACTOR = 0.48;
3232
// image height based on available rectangle
3333
constexpr double IMAGE_SIZE_FACTOR = 0.65;
3434

35+
// IMAGE_SIZE_FACTOR for the tag image
36+
constexpr double TAG_IMAGE_SIZE_FACTOR = 0.42;
37+
38+
// IMAGE_SIZE_FACTOR for the page up/down images
39+
constexpr double PAGE_IMAGE_SIZE_FACTOR = 0.52;
40+
3541
// https://materialui.co/colors
3642
KeypadTheme MODERN_DARK_THEME = {
3743
._bg = 0x121212, // Material UI standard dark background
@@ -59,55 +65,55 @@ constexpr RawKey KEYS[MAX_ROWS][MAX_COLS] = {
5965
},
6066
// QWERTY
6167
{
62-
{K_q, K_Q, K_1, K_EXCLAIM},
63-
{K_w, K_W, K_2, K_AT},
64-
{K_e, K_E, K_3, K_HASH},
65-
{K_r, K_R, K_4, K_DOLLAR},
66-
{K_t, K_T, K_5, K_PERCENT},
67-
{K_y, K_Y, K_6, K_CARET},
68-
{K_u, K_U, K_7, K_AMPERSAND},
69-
{K_i, K_I, K_8, K_ASTERISK},
70-
{K_o, K_O, K_9, K_LPAREN},
71-
{K_p, K_P, K_0, K_RPAREN}
68+
{K_q, K_1, K_1, K_Q},
69+
{K_w, K_2, K_2, K_W},
70+
{K_e, K_3, K_3, K_E},
71+
{K_r, K_4, K_4, K_R},
72+
{K_t, K_5, K_5, K_T},
73+
{K_y, K_6, K_6, K_Y},
74+
{K_u, K_7, K_7, K_U},
75+
{K_i, K_8, K_8, K_I},
76+
{K_o, K_9, K_9, K_O},
77+
{K_p, K_0, K_0, K_P}
7278
},
7379
// ASDF
7480
{
75-
{K_a, K_A, K_BACKTICK, K_TILDE},
76-
{K_s, K_S, K_MINUS, K_UNDERSCORE},
77-
{K_d, K_D, K_EQUALS, K_PLUS},
78-
{K_f, K_F, K_LBRACKET, K_LBRACE},
79-
{K_g, K_G, K_RBRACKET, K_RBRACE},
80-
{K_h, K_H, K_BACKSLASH, K_PIPE},
81-
{K_j, K_J, K_SEMICOLON, K_COLON},
82-
{K_k, K_K, K_APOSTROPHE, K_QUOTE},
83-
{K_l, K_L, K_HASH, K_TAG},
81+
{K_a, K_COMMA, K_SLASH, K_A},
82+
{K_s, K_EQUALS, K_HASH, K_S},
83+
{K_d, K_LPAREN, K_SEMICOLON, K_D},
84+
{K_f, K_RPAREN, K_QUESTION, K_F},
85+
{K_g, K_QUOTE, K_AMPERSAND, K_G},
86+
{K_h, K_APOSTROPHE, K_DOLLAR, K_H},
87+
{K_j, K_PERIOD, K_EXCLAIM, K_J},
88+
{K_k, K_MINUS, K_AT, K_K},
89+
{K_l, K_ASTERISK, K_BACKSLASH, K_L},
8490
{K_NULL}
8591
},
8692
// ZXC
8793
{
8894
{K_TOGGLE, K_TOGGLE, K_TOGGLE, K_TOGGLE},
89-
{K_z, K_Z, K_LESS, K_COMMA},
90-
{K_x, K_X, K_GREATER, K_PERIOD},
91-
{K_c, K_C, K_QUESTION, K_SLASH},
92-
{K_v, K_V, K_PLUS, K_EXT2},
93-
{K_b, K_B, K_ASTERISK, K_EXT3},
94-
{K_n, K_N, K_LPAREN, K_EXT4},
95-
{K_m, K_M, K_RPAREN, K_EXT5},
95+
{K_z, K_UNDERSCORE, K_CARET, K_Z},
96+
{K_x, K_PLUS, K_LBRACE, K_X},
97+
{K_c, K_COLON, K_RBRACE, K_C},
98+
{K_v, K_LBRACKET, K_PIPE, K_V},
99+
{K_b, K_RBRACKET, K_PERCENT, K_B},
100+
{K_n, K_LESS, K_BACKTICK, K_N},
101+
{K_m, K_GREATER, K_TILDE, K_M},
96102
{K_BACKSPACE, K_BACKSPACE, K_BACKSPACE, K_BACKSPACE},
97103
{K_NULL}
98104
},
99105
// FUNCs, SPACE
100106
{
101107
{K_LINE_UP, K_PAGE_UP, K_LINE_UP, K_PAGE_UP},
102108
{K_LINE_DOWN, K_PAGE_DOWN, K_LINE_DOWN, K_PAGE_DOWN},
103-
{K_LPAREN, K_LBRACKET, K_LPAREN, K_LBRACKET},
109+
{K_LPAREN, K_SLASH, K_COMMA, K_LBRACKET},
104110
{K_SPACE, K_SPACE, K_SPACE, K_SPACE},
105-
{K_RPAREN, K_RBRACKET, K_RPAREN, K_RBRACKET},
111+
{K_RPAREN, K_HASH, K_EQUALS, K_RBRACKET},
112+
{K_TAG, K_TAG, K_TAG, K_TAG},
106113
{K_ENTER, K_ENTER, K_ENTER, K_ENTER},
107114
{K_NULL},
108115
{K_NULL},
109116
{K_NULL},
110-
{K_NULL}
111117
}
112118
};
113119

@@ -144,6 +150,7 @@ KeypadDrawContext::KeypadDrawContext(int charWidth, int charHeight) :
144150
_charWidth(charWidth),
145151
_charHeight(charHeight),
146152
_imageSize(IMAGE_SIZE),
153+
_punctuation(false),
147154
_keySet(kLower) {
148155
if (!_cutImage.decode(img_cut, img_cut_len) ||
149156
!_copyImage.decode(img_copy, img_copy_len) ||
@@ -200,8 +207,11 @@ KeyCode KeypadDrawContext::getKey(RawKey key) const {
200207
}
201208

202209
void KeypadDrawContext::layoutHeight(int padding) {
203-
const int imageSize = static_cast<int>((padding * 2 + _charHeight) * IMAGE_SIZE_FACTOR);
210+
const int baseSize = padding * 2 + _charHeight;
211+
const int imageSize = static_cast<int>(baseSize * IMAGE_SIZE_FACTOR);
204212
if (imageSize < _imageSize - 2 || imageSize > _imageSize + 2) {
213+
const int tagImageSize = static_cast<int>(baseSize * TAG_IMAGE_SIZE_FACTOR);
214+
const int pageImageSize = static_cast<int>(baseSize * PAGE_IMAGE_SIZE_FACTOR);
205215
_cutImage.resize(imageSize, imageSize);
206216
_copyImage.resize(imageSize, imageSize);
207217
_pasteImage.resize(imageSize, imageSize);
@@ -211,26 +221,46 @@ void KeypadDrawContext::layoutHeight(int padding) {
211221
_backImage.resize(imageSize, imageSize);
212222
_enterImage.resize(imageSize, imageSize);
213223
_searchImage.resize(imageSize, imageSize);
214-
_lineUpImage.resize(imageSize, imageSize);
215-
_pageUpImage.resize(imageSize, imageSize);
216-
_lineDownImage.resize(imageSize, imageSize);
217-
_pageDownImage.resize(imageSize, imageSize);
218224
_toggleImage.resize(imageSize, imageSize);
219-
_tagImage.resize(imageSize, imageSize);
225+
_lineUpImage.resize(pageImageSize, pageImageSize);
226+
_lineDownImage.resize(pageImageSize, pageImageSize);
227+
_pageDownImage.resize(pageImageSize, pageImageSize);
228+
_pageUpImage.resize(pageImageSize, pageImageSize);
229+
_tagImage.resize(tagImageSize, tagImageSize);
220230
_imageSize = imageSize;
221231
}
222232
}
223233

234+
void KeypadDrawContext::onClick(RawKey key) {
235+
switch (getKey(key)) {
236+
case K_ENTER:
237+
case K_RPAREN:
238+
case K_RBRACKET:
239+
case K_RBRACE:
240+
_keySet = kLower;
241+
_punctuation = false;
242+
break;
243+
default:
244+
_punctuation = (_keySet == kSymbol || _keySet == kNumber);
245+
break;
246+
}
247+
}
248+
224249
void KeypadDrawContext::toggle() {
225-
_keySet = static_cast<Keyset>((_keySet + 1) % kSize);
250+
if (_punctuation) {
251+
_keySet = kLower;
252+
_punctuation = false;
253+
} else {
254+
_keySet = static_cast<Keyset>((_keySet + 1) % kSize);
255+
}
226256
}
227257

228258
//
229259
// Key
230260
//
231261
Key::Key(const RawKey &k) :
232262
_key(k) {
233-
_printable = isPrintable(k._lower) || isExtended(k._lower);
263+
_printable = isPrintable(k._lower);
234264
}
235265

236266
int Key::color(const KeypadTheme *theme) const {
@@ -243,22 +273,6 @@ int Key::color(const KeypadTheme *theme) const {
243273
return result;
244274
}
245275

246-
char Key::getKey(const KeypadDrawContext *context) const {
247-
char result;
248-
KeyCode keyCode = context->getKey(_key);
249-
switch (keyCode) {
250-
case K_EXT1: result = (char)164; break;
251-
case K_EXT2: result = (char)172; break;
252-
case K_EXT3: result = (char)182; break;
253-
case K_EXT4: result = (char)222; break;
254-
case K_EXT5: result = (char)223; break;
255-
default:
256-
result = keyCode;
257-
break;
258-
}
259-
return result;
260-
}
261-
262276
void Key::draw(const KeypadTheme *theme, const KeypadDrawContext *context, bool pressed) const {
263277
int rc = 5;
264278
int pad = 2;
@@ -273,7 +287,7 @@ void Key::draw(const KeypadTheme *theme, const KeypadDrawContext *context, bool
273287
int ycT = _y + rc + pad; // y center for top arcs
274288
int ycB = by - rc; // y center for bottom arcs
275289

276-
char keyChar = getKey(context);
290+
char keyChar = context->getKey(_key);
277291
bool printable = _printable && keyChar != K_TAG;
278292

279293
// Set background color
@@ -331,7 +345,7 @@ bool Key::inside(int x, int y) const {
331345
y <= _yEnd);
332346
}
333347

334-
void Key::onClick(const KeypadDrawContext *context) const {
348+
void Key::onClick(KeypadDrawContext *context) const {
335349
auto *event = new MAEvent();
336350
event->type = EVENT_TYPE_KEY_PRESSED;
337351
event->nativeKey = 0;
@@ -341,6 +355,7 @@ void Key::onClick(const KeypadDrawContext *context) const {
341355
break;
342356
case K_ENTER:
343357
event->key = SB_KEY_ENTER;
358+
context->onClick(_key);
344359
break;
345360
case K_SPACE:
346361
event->key = SB_KEY_SPACE;
@@ -382,7 +397,8 @@ void Key::onClick(const KeypadDrawContext *context) const {
382397
event->key = SB_KEY_CTRL('t');
383398
break;
384399
default:
385-
event->key = (unsigned char)getKey(context);
400+
event->key = (unsigned char)context->getKey(_key);
401+
context->onClick(_key);
386402
break;
387403
}
388404
maPushEvent(event);
@@ -452,7 +468,7 @@ void Keypad::layout(int x, int y, int w, int h) {
452468
keyWidth = _width / cols;
453469
} else if (isArrow(key->_key._lower)) {
454470
keyWidth = static_cast<int>(keyWidth * 1.2);
455-
} else if (!key->_printable) {
471+
} else if (!key->_printable && key->_key._lower != K_TAG) {
456472
const int numKeys = 2;
457473
keyWidth = (_width - ((cols - numKeys) * keyW)) / numKeys;
458474
} else if (key->_key._lower == K_SPACE) {

src/ui/keypad.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,28 @@ struct KeypadImage : ImageCodec {
3939
};
4040

4141
enum Keyset {
42-
kLower, kUpper, kNumber, kSymbol, kSize
42+
kLower, kNumber, kSymbol, kUpper, kSize
4343
};
4444

4545
struct RawKey {
4646
const KeyCode _lower;
47-
const KeyCode _upper;
4847
const KeyCode _number;
4948
const KeyCode _symbol;
49+
const KeyCode _upper;
5050
};
5151

5252
struct KeypadDrawContext {
5353
explicit KeypadDrawContext(int charWidth, int charHeight);
5454
const KeypadImage *getImage(const RawKey &keycode) const;
5555
KeyCode getKey(RawKey rawKey) const;
5656
void layoutHeight(int padding);
57+
void onClick(RawKey key);
5758
void toggle();
5859

5960
int _charWidth;
6061
int _charHeight;
6162
int _imageSize;
63+
bool _punctuation;
6264
Keyset _keySet;
6365

6466
KeypadImage _cutImage;
@@ -83,9 +85,8 @@ struct Key {
8385

8486
int color(const KeypadTheme *theme) const;
8587
void draw(const KeypadTheme *theme, const KeypadDrawContext *context, bool pressed) const;
86-
char getKey(const KeypadDrawContext *context) const;
8788
bool inside(int x, int y) const;
88-
void onClick(const KeypadDrawContext *context) const;
89+
void onClick(KeypadDrawContext *context) const;
8990

9091
int _x{};
9192
int _y{};

0 commit comments

Comments
 (0)