Skip to content

Commit af34394

Browse files
MposKeyboard: switch textarea font for emoji input
otherwise the user types an emoji but the textarea doesnt show it because the font doesnt support it.
1 parent 971f070 commit af34394

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

internal_filesystem/lib/mpos/ui/keyboard.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class MposKeyboard:
108108
_saved_scroll_y = 0
109109
# Store textarea reference (we DON'T pass it to LVGL to avoid double-typing)
110110
_textarea = None
111+
_textarea_emoji_font_applied = False
111112

112113
def __init__(self, parent):
113114
# Create underlying LVGL keyboard widget
@@ -207,6 +208,7 @@ def _handle_events(self, event):
207208
else:
208209
# Regular character
209210
new_text = current_text + text
211+
self._ensure_textarea_emoji_font(ta, text)
210212

211213
# Update textarea
212214
ta.set_text(new_text)
@@ -224,10 +226,63 @@ def set_textarea(self, textarea):
224226
textarea: The lv.textarea widget to type into, or None to disconnect
225227
"""
226228
self._textarea = textarea
229+
self._textarea_emoji_font_applied = False
227230
# NOTE: We deliberately DO NOT call self._keyboard.set_textarea()
228231
# to avoid LVGL's automatic character insertion
229232
self._textarea.add_event_cb(lambda *args: self.show_keyboard(), lv.EVENT.CLICKED, None)
230233

234+
def _ensure_textarea_emoji_font(self, textarea, text):
235+
if self._textarea_emoji_font_applied:
236+
return
237+
if not self._contains_emoji(text):
238+
return
239+
240+
current_font = None
241+
try:
242+
current_font = textarea.get_style_text_font(lv.PART.MAIN)
243+
except Exception:
244+
pass
245+
246+
family = None
247+
size = 12
248+
if current_font is not None:
249+
base_font = current_font
250+
try:
251+
fallback_font = current_font.fallback
252+
if fallback_font is not None:
253+
base_font = fallback_font
254+
except Exception:
255+
pass
256+
257+
for record in FontManager._get_builtin_font_records():
258+
if record["font"] is base_font:
259+
family = record["family"]
260+
size = record["size"]
261+
break
262+
263+
if family is None:
264+
try:
265+
size = max(1, int(base_font.get_line_height()))
266+
except Exception:
267+
pass
268+
269+
emoji_font = FontManager.getFont(size=size, family=family, emoji=True)
270+
textarea.set_style_text_font(emoji_font, lv.PART.MAIN)
271+
self._textarea_emoji_font_applied = True
272+
273+
def _contains_emoji(self, text):
274+
if not text:
275+
return False
276+
277+
emoji_codepoints = FontManager.getEmojiCodepoints()
278+
if not emoji_codepoints:
279+
return False
280+
281+
for char in text:
282+
if ord(char) in emoji_codepoints:
283+
return True
284+
return False
285+
231286
def get_textarea(self):
232287
"""
233288
Get the textarea that this keyboard types into.

0 commit comments

Comments
 (0)