Skip to content

Commit cfefaf9

Browse files
committed
UI: experimental keypad to replace android keypad
1 parent 5137115 commit cfefaf9

File tree

10 files changed

+49
-49
lines changed

10 files changed

+49
-49
lines changed

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,6 @@ public void run() {
326326
return result;
327327
}
328328

329-
public int getDensity() {
330-
DisplayMetrics metrics = new DisplayMetrics();
331-
getWindowManager().getDefaultDisplay().getMetrics(metrics);
332-
return metrics.densityDpi;
333-
}
334-
335329
public String getIpAddress() {
336330
String result = "";
337331
try {

src/platform/android/jni/editor.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct StatusMessage {
3737
_scroll = editor->getScroll();
3838
}
3939

40-
void toggleEnabled(const TextEditInput *editor) {
40+
void toggleStatus(const TextEditInput *editor) {
4141
statusEnabled = !statusEnabled;
4242
setDirty(editor);
4343
}
@@ -156,7 +156,7 @@ void Runtime::editSource(strlib::String loadPath, bool restoreOnExit) {
156156
if (_keypad != nullptr) {
157157
_output->addInput(_keypad);
158158
} else {
159-
_keypad = new KeypadInput(false, false, charWidth, charHeight, _density);
159+
_keypad = new KeypadInput(false, false, charWidth, charHeight);
160160
_output->addInput(_keypad);
161161
}
162162

@@ -197,7 +197,7 @@ void Runtime::editSource(strlib::String loadPath, bool restoreOnExit) {
197197
switch (event.key) {
198198
case SB_KEY_F(2): case SB_KEY_F(3): case SB_KEY_F(4): case SB_KEY_F(5): case SB_KEY_F(6):
199199
case SB_KEY_F(7): case SB_KEY_F(8): case SB_KEY_F(10): case SB_KEY_F(11): case SB_KEY_F(12):
200-
case SB_KEY_MENU: case SB_KEY_ESCAPE: case SB_KEY_BREAK:
200+
case SB_KEY_MENU: case SB_KEY_ESCAPE: case SB_KEY_BREAK: case SB_KEY_CTRL('o'):
201201
// unhandled keys
202202
redraw = false;
203203
break;
@@ -243,17 +243,8 @@ void Runtime::editSource(strlib::String loadPath, bool restoreOnExit) {
243243
widget->paste(text);
244244
free(text);
245245
break;
246-
case SB_KEY_CTRL('o'):
247-
_output->selectScreen(USER_SCREEN1);
248-
showCompletion(true);
249-
_output->redraw();
250-
_state = kActiveState;
251-
waitForBack();
252-
_output->selectScreen(FORM_SCREEN);
253-
_state = kEditState;
254-
break;
255246
case SB_KEY_CTRL('t'):
256-
statusMessage.toggleEnabled(editWidget);
247+
statusMessage.toggleStatus(editWidget);
257248
break;
258249
default:
259250
redraw = widget->edit(event.key, _output->getScreenWidth(), charWidth);

src/platform/android/jni/runtime.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ Runtime::Runtime(android_app *app) :
234234
_looper = ALooper_forThread();
235235
_sensorManager = ASensorManager_getInstance();
236236
memset(&_sensors, 0, sizeof(_sensors));
237-
_density = getInteger("getDensity");
238237
}
239238

240239
Runtime::~Runtime() {

src/platform/android/jni/runtime.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ struct Runtime : public System {
8686
private:
8787
void editSource(String loadPath, bool restoreOnExit) override;
8888

89-
int _density;
9089
bool _keypadActive;
9190
bool _hasFocus;
9291
Graphics *_graphics;

src/platform/android/webui/package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,27 @@
55
"type": "module",
66
"dependencies": {
77
"@emotion/react": "^11.14.0",
8-
"@emotion/styled": "^11.14.0",
9-
"@mui/icons-material": "^7.0.2",
10-
"@mui/material": "^7.0.2",
11-
"@mui/x-data-grid": "^8.2.0",
8+
"@emotion/styled": "^11.14.1",
9+
"@mui/icons-material": "^7.2.0",
10+
"@mui/material": "^7.2.0",
11+
"@mui/x-data-grid": "^8.8.0",
1212
"react": "^18.3.1",
1313
"react-dom": "^18.3.1",
1414
"react-router-dom": "^6.27.0",
1515
"react-router-hash-link": "^2.4.3"
1616
},
1717
"devDependencies": {
18-
"@eslint/js": "^9.26.0",
19-
"@vitejs/plugin-react": "^4.4.1",
20-
"eslint": "^9.26.0",
18+
"@eslint/js": "^9.31.0",
19+
"@vitejs/plugin-react": "^4.6.0",
20+
"eslint": "^9.31.0",
2121
"eslint-config-react": "^1.1.7",
2222
"eslint-define-config": "^2.1.0",
23-
"eslint-plugin-import": "^2.31.0",
23+
"eslint-plugin-import": "^2.32.0",
2424
"eslint-plugin-react": "^7.37.5",
2525
"eslint-plugin-react-hooks": "^5.2.0",
2626
"eslint-plugin-react-refresh": "^0.4.20",
2727
"eslint-plugin-unused-imports": "^4.1.4",
28-
"globals": "^16.0.0",
28+
"globals": "^16.3.0",
2929
"npm-check-updates": "^18.0.1",
3030
"vite": "^6.3.5",
3131
"vite-plugin-eslint": "^1.8.1"

src/platform/sdl/editor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ void Runtime::editSource(String loadPath, bool restoreOnExit) {
232232
if (_keypad != nullptr) {
233233
_output->addInput(_keypad);
234234
} else {
235-
_keypad = new KeypadInput(false, true, charWidth, charHeight, 0);
235+
_keypad = new KeypadInput(false, true, charWidth, charHeight);
236236
_output->addInput(_keypad);
237237
}
238238

src/ui/inputs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ struct FormInput : public Shape {
120120
virtual bool hasHover() { return false; }
121121
virtual void setFocus(bool focus);
122122
virtual void layout(int x, int y, int w, int h) {}
123+
virtual int layoutHeight(int screenHeight) { return 0; }
123124
virtual bool floatBottom() { return false; }
124125
virtual bool floatTop() { return false; }
125126
virtual void drawHover(int dx, int dy, bool selected);

src/ui/keypad.cpp

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ constexpr int SPACE_COLS = 3;
2323
constexpr int IMAGE_SIZE = 30;
2424
constexpr int MIN_DENSITY = 280;
2525
constexpr double PI = 3.14159;
26+
constexpr double PADDING_FACTOR = 0.95;
2627

2728
// https://materialui.co/colors
2829
KeypadTheme MODERN_DARK_THEME = {
@@ -128,7 +129,7 @@ KeypadDrawContext::KeypadDrawContext(int charWidth, int charHeight, int padding)
128129
_charWidth(charWidth),
129130
_charHeight(charHeight),
130131
_keySet(kLower) {
131-
const int imageSize = static_cast<int>(MAX(padding * 1.5, charHeight) * 1.2);
132+
const int imageSize = static_cast<int>(MAX(padding * 1.4, charHeight) * 1.2);
132133

133134
if (!_cutImage.decode(img_cut, img_cut_len) ||
134135
!_copyImage.decode(img_copy, img_copy_len) ||
@@ -370,23 +371,19 @@ void Key::onClick(const KeypadDrawContext *context) const {
370371
//
371372
// Keypad
372373
//
373-
Keypad::Keypad(int charWidth, int charHeight, bool toolbar, int density)
374+
Keypad::Keypad(int charWidth, int charHeight, bool toolbar)
374375
: _posX(0),
375376
_posY(0),
376377
_width(0),
377378
_height(0),
378-
_padding(density < MIN_DENSITY ? PADDING : (PADDING * 2)),
379+
_padding(static_cast<int>(charHeight * PADDING_FACTOR)),
379380
_theme(&MODERN_DARK_THEME),
380381
_context(charWidth, charHeight, _padding),
381382
_toolbar(toolbar),
382383
_pressed(nullptr) {
383384
generateKeys();
384385
}
385386

386-
int Keypad::outerHeight(int charHeight) const {
387-
return (_toolbar ? 1 : MAX_ROWS) * ((_padding * 2) + charHeight);
388-
}
389-
390387
void Keypad::generateKeys() {
391388
_keys.clear();
392389

@@ -451,15 +448,31 @@ void Keypad::layout(int x, int y, int w, int h) {
451448
}
452449
}
453450

451+
int Keypad::layoutHeight(int screenHeight) {
452+
int charHeight = _context._charHeight;
453+
int maxHeight = static_cast<int>(screenHeight * 0.45);
454+
int padding = static_cast<int>(charHeight * PADDING_FACTOR);
455+
int rows = _toolbar ? 1 : MAX_ROWS;
456+
int height = rows * ((padding * 2) + charHeight);
457+
if (height > maxHeight) {
458+
// h = r(ch + 2p) -> p = (h - r * ch) / (2 * r)
459+
height = maxHeight;
460+
padding = ((height - (rows * charHeight)) / (rows * 2));
461+
}
462+
_padding = padding;
463+
return height;
464+
}
465+
454466
void Keypad::clicked(int x, int y, bool pressed) {
467+
Key *down = _pressed;
455468
_pressed = nullptr;
456469
for (const auto key : _keys) {
457470
if (key->inside(x, y)) {
458471
if (pressed) {
459472
_pressed = key;
460473
} else if (key->_key._lower == K_TOGGLE) {
461474
_context.toggle();
462-
} else {
475+
} else if (key == down) {
463476
key->onClick(&_context);
464477
}
465478
break;
@@ -478,11 +491,10 @@ void Keypad::draw() const {
478491
//
479492
// KeypadInput
480493
//
481-
KeypadInput::KeypadInput(bool floatTop, bool toolbar, int charWidth, int charHeight, int density) :
494+
KeypadInput::KeypadInput(bool floatTop, bool toolbar, int charWidth, int charHeight) :
482495
FormInput(0, 0, 0, charHeight * 2),
483496
_floatTop(floatTop) {
484-
_keypad = new Keypad(charWidth, charHeight, toolbar, density);
485-
_height = _keypad->outerHeight(charHeight);
497+
_keypad = new Keypad(charWidth, charHeight, toolbar);
486498
}
487499

488500
KeypadInput::~KeypadInput() {
@@ -504,3 +516,8 @@ void KeypadInput::layout(int x, int y, int w, int h) {
504516
_width = w;
505517
_keypad->layout(_x, _y, _width, _height);
506518
}
519+
520+
int KeypadInput::layoutHeight(int screenHeight) {
521+
_height = _keypad->layoutHeight(screenHeight);
522+
return _height;
523+
}

src/ui/keypad.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ using namespace strlib;
1818
#if defined(_SDL)
1919
// for cursor display
2020
#define HAS_HOVER true
21-
#define PADDING 6
2221
#else
2322
#define HAS_HOVER false
24-
#define PADDING 16
2523
#endif
2624

2725
struct KeypadTheme {
@@ -96,13 +94,13 @@ struct Key {
9694
};
9795

9896
struct Keypad {
99-
Keypad(int charWidth, int charHeight, bool toolbar, int density);
97+
Keypad(int charWidth, int charHeight, bool toolbar);
10098
~Keypad() = default;
10199

102-
int outerHeight(int charHeight) const;
103100
void clicked(int x, int y, bool pressed);
104101
void draw() const;
105102
void layout(int x, int y, int w, int h);
103+
int layoutHeight(int screenHeight);
106104

107105
private:
108106
void generateKeys();
@@ -120,14 +118,15 @@ struct Keypad {
120118
};
121119

122120
struct KeypadInput : public FormInput {
123-
KeypadInput(bool floatTop, bool toolbar, int charWidth, int charHeight, int density = -1);
121+
KeypadInput(bool floatTop, bool toolbar, int charWidth, int charHeight);
124122
~KeypadInput() override;
125123

126124
void clicked(int x, int y, bool pressed) override;
127125
void draw(int x, int y, int w, int h, int chw) override;
128126
bool floatTop() override { return _floatTop; }
129127
bool floatBottom() override { return !_floatTop; }
130128
void layout(int x, int y, int w, int h) override;
129+
int layoutHeight(int screenHeight) override;
131130
void drawHover(int dx, int dy, bool selected) override {};
132131
bool hasHover() override { return HAS_HOVER; }
133132

src/ui/screen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ void Screen::layoutInputs(int newWidth, int newHeight) {
302302
List_each(FormInput *, it, _inputs) {
303303
FormInput *input = (*it);
304304
if (input->floatBottom()) {
305-
offsBottom = input->_height;
305+
offsBottom = input->layoutHeight(newHeight);
306306
} else if (input->floatTop()) {
307-
offsTop = input->_height;
307+
offsTop = input->layoutHeight(newHeight);
308308
}
309309
}
310310
List_each(FormInput *, it, _inputs) {

0 commit comments

Comments
 (0)