Skip to content

Commit 86d9c38

Browse files
Introduce DisplayMetrics framework
1 parent 4efe22f commit 86d9c38

File tree

19 files changed

+163
-121
lines changed

19 files changed

+163
-121
lines changed

internal_filesystem/apps/com.micropythonos.imageview/assets/imageview.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gc
22
import os
33

4-
from mpos import Activity, smooth_show, smooth_hide, pct_of_display_width, pct_of_display_height
4+
from mpos import Activity, smooth_show, smooth_hide, DisplayMetrics
55

66
class ImageView(Activity):
77

@@ -271,8 +271,8 @@ def scale_image(self):
271271
pct = 100
272272
else:
273273
pct = 70
274-
lvgl_w = pct_of_display_width(pct)
275-
lvgl_h = pct_of_display_height(pct)
274+
lvgl_w = DisplayMetrics.pct_of_width(pct)
275+
lvgl_h = DisplayMetrics.pct_of_height(pct)
276276
print(f"scaling to size: {lvgl_w}x{lvgl_h}")
277277
header = lv.image_header_t()
278278
self.image.decoder_get_info(self.image.get_src(), header)

internal_filesystem/apps/com.micropythonos.nostr/assets/fullscreen_qr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import lvgl as lv
22

3-
from mpos import Activity, min_resolution
3+
from mpos import Activity, DisplayMetrics
44

55
class FullscreenQR(Activity):
66
# No __init__() so super.__init__() will be called automatically
@@ -28,7 +28,7 @@ def onCreate(self):
2828
qr_screen.set_scroll_dir(lv.DIR.NONE)
2929
qr_screen.add_event_cb(lambda e: self.finish(),lv.EVENT.CLICKED,None)
3030
big_receive_qr = lv.qrcode(qr_screen)
31-
big_receive_qr.set_size(min_resolution())
31+
big_receive_qr.set_size(DisplayMetrics.min_dimension())
3232
big_receive_qr.set_dark_color(lv.color_black())
3333
big_receive_qr.set_light_color(lv.color_white())
3434
big_receive_qr.center()

internal_filesystem/apps/com.micropythonos.nostr/assets/nostr_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import lvgl as lv
22

3-
from mpos import Activity, Intent, ConnectivityManager, pct_of_display_width, pct_of_display_height, SharedPreferences, SettingsActivity
3+
from mpos import Activity, Intent, ConnectivityManager, DisplayMetrics, SharedPreferences, SettingsActivity
44
from fullscreen_qr import FullscreenQR
55

66
class ShowNpubQRActivity(Activity):
@@ -82,13 +82,13 @@ def onCreate(self):
8282
self.balance_label.align(lv.ALIGN.TOP_LEFT, 0, 0)
8383
self.balance_label.set_style_text_font(lv.font_montserrat_24, 0)
8484
self.balance_label.add_flag(lv.obj.FLAG.CLICKABLE)
85-
self.balance_label.set_width(pct_of_display_width(100))
85+
self.balance_label.set_width(DisplayMetrics.pct_of_width(100))
8686
# Events label
8787
self.events_label = lv.label(self.main_screen)
8888
self.events_label.set_text("")
8989
self.events_label.align_to(header_line,lv.ALIGN.OUT_BOTTOM_LEFT,0,10)
9090
self.update_events_label_font()
91-
self.events_label.set_width(pct_of_display_width(100))
91+
self.events_label.set_width(DisplayMetrics.pct_of_width(100))
9292
self.events_label.add_flag(lv.obj.FLAG.CLICKABLE)
9393
self.events_label.add_event_cb(self.events_label_clicked,lv.EVENT.CLICKED,None)
9494
settings_button = lv.button(self.main_screen)

internal_filesystem/builtin/apps/com.micropythonos.about/assets/about.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mpos import Activity, pct_of_display_width, get_display_width, get_display_height, get_dpi
1+
from mpos import Activity, DisplayMetrics
22

33
import mpos.info
44
import sys
@@ -38,7 +38,7 @@ def onCreate(self):
3838
screen = lv.obj()
3939
screen.set_style_border_width(0, 0)
4040
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
41-
screen.set_style_pad_all(pct_of_display_width(2), 0)
41+
screen.set_style_pad_all(DisplayMetrics.pct_of_width(2), 0)
4242
# Make the screen focusable so it can be scrolled with the arrow keys
4343
focusgroup = lv.group_get_default()
4444
if focusgroup:
@@ -147,10 +147,10 @@ def onCreate(self):
147147
# Display info
148148
try:
149149
self._add_label(screen, f"{lv.SYMBOL.IMAGE} Display", is_header=True)
150-
hor_res = get_display_width()
151-
ver_res = get_display_height()
150+
hor_res = DisplayMetrics.width()
151+
ver_res = DisplayMetrics.height()
152152
self._add_label(screen, f"Resolution: {hor_res}x{ver_res}")
153-
dpi = get_dpi()
153+
dpi = DisplayMetrics.dpi()
154154
self._add_label(screen, f"Dots Per Inch (dpi): {dpi}")
155155
except Exception as e:
156156
print(f"Could not get display info: {e}")

internal_filesystem/builtin/apps/com.micropythonos.launcher/assets/launcher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# Most of this time is actually spent reading and parsing manifests.
1111
import lvgl as lv
1212
import mpos.apps
13-
from mpos import NOTIFICATION_BAR_HEIGHT, PackageManager, Activity, pct_of_display_width
13+
from mpos import NOTIFICATION_BAR_HEIGHT, PackageManager, Activity, DisplayMetrics
1414
import time
1515
import uhashlib
1616
import ubinascii
@@ -29,7 +29,7 @@ def onCreate(self):
2929
main_screen.set_style_border_width(0, lv.PART.MAIN)
3030
main_screen.set_style_radius(0, 0)
3131
main_screen.set_pos(0, NOTIFICATION_BAR_HEIGHT)
32-
main_screen.set_style_pad_hor(pct_of_display_width(2), 0)
32+
main_screen.set_style_pad_hor(DisplayMetrics.pct_of_width(2), 0)
3333
main_screen.set_style_pad_ver(NOTIFICATION_BAR_HEIGHT, 0)
3434
main_screen.set_flex_flow(lv.FLEX_FLOW.ROW_WRAP)
3535
self.setContentView(main_screen)

internal_filesystem/builtin/apps/com.micropythonos.osupdate/assets/osupdate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import ujson
33
import time
44

5-
from mpos import Activity, PackageManager, ConnectivityManager, TaskManager, DownloadManager, pct_of_display_width, pct_of_display_height
5+
from mpos import Activity, PackageManager, ConnectivityManager, TaskManager, DownloadManager, DisplayMetrics
66
import mpos.info
77

88
class OSUpdate(Activity):
@@ -39,7 +39,7 @@ def set_state(self, new_state):
3939

4040
def onCreate(self):
4141
self.main_screen = lv.obj()
42-
self.main_screen.set_style_pad_all(pct_of_display_width(2), 0)
42+
self.main_screen.set_style_pad_all(DisplayMetrics.pct_of_width(2), 0)
4343

4444
# Make the screen focusable so it can be scrolled with the arrow keys
4545
if focusgroup := lv.group_get_default():
@@ -51,7 +51,7 @@ def onCreate(self):
5151
self.force_update = lv.checkbox(self.main_screen)
5252
self.force_update.set_text("Force Update")
5353
self.force_update.add_event_cb(lambda *args: self.force_update_clicked(), lv.EVENT.VALUE_CHANGED, None)
54-
self.force_update.align_to(self.current_version_label, lv.ALIGN.OUT_BOTTOM_LEFT, 0, pct_of_display_height(5))
54+
self.force_update.align_to(self.current_version_label, lv.ALIGN.OUT_BOTTOM_LEFT, 0, DisplayMetrics.pct_of_height(5))
5555
self.install_button = lv.button(self.main_screen)
5656
self.install_button.align(lv.ALIGN.TOP_RIGHT, 0, 0)
5757
self.install_button.add_state(lv.STATE.DISABLED) # button will be enabled if there is an update available
@@ -72,7 +72,7 @@ def onCreate(self):
7272
check_again_label.center()
7373

7474
self.status_label = lv.label(self.main_screen)
75-
self.status_label.align_to(self.force_update, lv.ALIGN.OUT_BOTTOM_LEFT, 0, pct_of_display_height(5))
75+
self.status_label.align_to(self.force_update, lv.ALIGN.OUT_BOTTOM_LEFT, 0, DisplayMetrics.pct_of_height(5))
7676
self.setContentView(self.main_screen)
7777

7878
def _update_ui_for_state(self):

internal_filesystem/builtin/apps/com.micropythonos.settings/assets/calibrate_imu.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import lvgl as lv
1111
import time
1212
import sys
13-
from mpos import Activity, SensorManager, wait_for_render, pct_of_display_width
13+
from mpos import Activity, SensorManager, wait_for_render, DisplayMetrics
1414

1515

1616
class CalibrationState:
@@ -40,7 +40,7 @@ def __init__(self):
4040

4141
def onCreate(self):
4242
screen = lv.obj()
43-
screen.set_style_pad_all(pct_of_display_width(3), 0)
43+
screen.set_style_pad_all(DisplayMetrics.pct_of_width(3), 0)
4444
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
4545
screen.set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.START, lv.FLEX_ALIGN.CENTER)
4646
focusgroup = lv.group_get_default()

internal_filesystem/builtin/apps/com.micropythonos.settings/assets/check_imu_calibration.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import lvgl as lv
88
import time
99
import sys
10-
from mpos import Activity, SensorManager, pct_of_display_width
10+
from mpos import Activity, SensorManager, DisplayMetrics
1111

1212

1313
class CheckIMUCalibrationActivity(Activity):
@@ -34,7 +34,7 @@ def __init__(self):
3434

3535
def onCreate(self):
3636
screen = lv.obj()
37-
screen.set_style_pad_all(pct_of_display_width(1), 0)
37+
screen.set_style_pad_all(DisplayMetrics.pct_of_width(1), 0)
3838
#screen.set_style_pad_all(0, 0)
3939
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
4040
focusgroup = lv.group_get_default()
@@ -96,7 +96,7 @@ def onResume(self, screen):
9696

9797
# Gyroscope section
9898
gyro_cont = lv.obj(data_cont)
99-
gyro_cont.set_width(pct_of_display_width(45))
99+
gyro_cont.set_width(DisplayMetrics.pct_of_width(45))
100100
gyro_cont.set_height(lv.SIZE_CONTENT)
101101
gyro_cont.set_style_border_width(0, 0)
102102
gyro_cont.set_style_pad_all(0, 0)

internal_filesystem/builtin/apps/com.micropythonos.wifi/assets/wifi.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import lvgl as lv
33
import _thread
44

5-
from mpos import Activity, Intent, MposKeyboard, WifiService, CameraActivity, pct_of_display_width, CameraManager
5+
from mpos import Activity, Intent, MposKeyboard, WifiService, CameraActivity, DisplayMetrics, CameraManager
66
import mpos.apps
77

88
class WiFi(Activity):
@@ -238,8 +238,8 @@ def onCreate(self):
238238
label.set_text(f"Network name:")
239239
self.ssid_ta = lv.textarea(password_page)
240240
self.ssid_ta.set_width(lv.pct(100))
241-
self.ssid_ta.set_style_margin_left(pct_of_display_width(2), lv.PART.MAIN)
242-
self.ssid_ta.set_style_margin_right(pct_of_display_width(2), lv.PART.MAIN)
241+
self.ssid_ta.set_style_margin_left(DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
242+
self.ssid_ta.set_style_margin_right(DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
243243
self.ssid_ta.set_one_line(True)
244244
self.ssid_ta.set_placeholder_text("Enter the SSID")
245245
self.keyboard = MposKeyboard(password_page)
@@ -254,8 +254,8 @@ def onCreate(self):
254254
label.set_text(f"Password for '{self.selected_ssid}':")
255255
self.password_ta = lv.textarea(password_page)
256256
self.password_ta.set_width(lv.pct(100))
257-
self.password_ta.set_style_margin_left(pct_of_display_width(2), lv.PART.MAIN)
258-
self.password_ta.set_style_margin_right(pct_of_display_width(2), lv.PART.MAIN)
257+
self.password_ta.set_style_margin_left(DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
258+
self.password_ta.set_style_margin_right(DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
259259
self.password_ta.set_one_line(True)
260260
if known_password:
261261
self.password_ta.set_text(known_password)
@@ -267,7 +267,7 @@ def onCreate(self):
267267
# Hidden network:
268268
self.hidden_cb = lv.checkbox(password_page)
269269
self.hidden_cb.set_text("Hidden network (always try connecting)")
270-
self.hidden_cb.set_style_margin_left(pct_of_display_width(2), lv.PART.MAIN)
270+
self.hidden_cb.set_style_margin_left(DisplayMetrics.pct_of_width(2), lv.PART.MAIN)
271271
if known_hidden:
272272
self.hidden_cb.set_state(lv.STATE.CHECKED, True)
273273

internal_filesystem/lib/mpos/__init__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131
)
3232

3333
# UI utility functions
34-
from .ui.display import (
35-
pct_of_display_width, pct_of_display_height,
36-
get_display_width, get_display_height, get_dpi,
37-
min_resolution, max_resolution, get_pointer_xy
38-
)
34+
from .ui.display_metrics import DisplayMetrics
3935
from .ui.event import get_event_name, print_event
4036
from .ui.view import setContentView, back_screen
4137
from .ui.theme import set_theme
@@ -73,10 +69,8 @@
7369
"SettingActivity", "SettingsActivity", "CameraActivity",
7470
# UI components
7571
"MposKeyboard",
76-
# UI utility functions
77-
"pct_of_display_width", "pct_of_display_height",
78-
"get_display_width", "get_display_height", "get_dpi",
79-
"min_resolution", "max_resolution", "get_pointer_xy",
72+
# UI utility - DisplayMetrics
73+
"DisplayMetrics",
8074
"get_event_name", "print_event",
8175
"setContentView", "back_screen",
8276
"set_theme",

0 commit comments

Comments
 (0)