Skip to content

Commit 4004151

Browse files
committed
Added user interface scaling
1 parent 7885f39 commit 4004151

File tree

2 files changed

+152
-3
lines changed

2 files changed

+152
-3
lines changed

sbapp/main.py

+118-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,51 @@
2424
import threading
2525
import RNS.vendor.umsgpack as msgpack
2626

27+
app_ui_scaling_path = None
28+
def apply_ui_scale():
29+
global app_ui_scaling_path
30+
default_scale = os.environ["KIVY_METRICS_DENSITY"] if "KIVY_METRICS_DENSITY" in os.environ else "unknown"
31+
config_path = None
32+
ui_scale_path = None
33+
34+
try:
35+
if RNS.vendor.platformutils.is_android():
36+
import plyer
37+
ui_scale_path = plyer.storagepath.get_application_dir()+"/io.unsigned.sideband/files/app_storage/ui_scale"
38+
else:
39+
if config_path == None:
40+
import sbapp.plyer as plyer
41+
ui_scale_path = plyer.storagepath.get_home_dir()+"/.config/sideband/app_storage/ui_scale"
42+
if ui_scale_path.startswith("file://"):
43+
ui_scale_path = ui_scale_path.replace("file://", "")
44+
else:
45+
ui_scale_path = config_path+"/app_storage/ui_scale"
46+
47+
app_ui_scaling_path = ui_scale_path
48+
49+
except Exception as e:
50+
RNS.log(f"Error while locating UI scale file: {e}", RNS.LOG_ERROR)
51+
52+
if ui_scale_path != None:
53+
RNS.log("Default scaling factor on this platform is "+str(default_scale), RNS.LOG_NOTICE)
54+
try:
55+
RNS.log("Looking for scaling info in "+str(ui_scale_path))
56+
if os.path.isfile(ui_scale_path):
57+
scale_factor = None
58+
with open(ui_scale_path, "r") as sf:
59+
scale_factor = float(sf.readline())
60+
61+
if scale_factor != None:
62+
if scale_factor >= 0.3 and scale_factor <= 5.0:
63+
os.environ["KIVY_METRICS_DENSITY"] = str(scale_factor)
64+
RNS.log("UI scaling factor set to "+str(os.environ["KIVY_METRICS_DENSITY"]), RNS.LOG_NOTICE)
65+
elif scale_factor == 0.0:
66+
RNS.log("Using default UI scaling factor", RNS.LOG_NOTICE)
67+
68+
69+
except Exception as e:
70+
RNS.log(f"Error while reading UI scaling factor: {e}", RNS.LOG_ERROR)
71+
2772
if args.export_settings:
2873
from .sideband.core import SidebandCore
2974
sideband = SidebandCore(
@@ -143,9 +188,11 @@ class DaemonApp():
143188
NewConv = DaemonElement; Telemetry = DaemonElement; ObjectDetails = DaemonElement; Announces = DaemonElement;
144189
Messages = DaemonElement; ts_format = DaemonElement; messages_screen_kv = DaemonElement; plyer = DaemonElement; multilingual_markup = DaemonElement;
145190
ContentNavigationDrawer = DaemonElement; DrawerList = DaemonElement; IconListItem = DaemonElement; escape_markup = DaemonElement;
146-
SoundLoader = DaemonElement;
191+
SoundLoader = DaemonElement; BoxLayout = DaemonElement;
147192

148193
else:
194+
apply_ui_scale()
195+
149196
from kivymd.app import MDApp
150197
app_superclass = MDApp
151198
from kivy.core.window import Window
@@ -157,6 +204,7 @@ class DaemonApp():
157204
from kivy.effects.scroll import ScrollEffect
158205
from kivy.uix.screenmanager import ScreenManager
159206
from kivy.uix.screenmanager import FadeTransition, NoTransition, SlideTransition
207+
from kivy.uix.boxlayout import BoxLayout
160208
from kivymd.uix.list import OneLineIconListItem, IconLeftWidget
161209
from kivy.properties import StringProperty
162210
from kivymd.uix.button import BaseButton, MDIconButton
@@ -2729,6 +2777,72 @@ def settings_open(self, sender=None, direction="left", no_transition=False):
27292777
if no_transition:
27302778
self.root.ids.screen_manager.transition = self.slide_transition
27312779

2780+
def configure_ui_scaling_action(self, sender=None):
2781+
global app_ui_scaling_path
2782+
try:
2783+
cancel_button = MDRectangleFlatButton(text="Cancel",font_size=dp(18))
2784+
set_button = MDRectangleFlatButton(text="Set",font_size=dp(18), theme_text_color="Custom", line_color=self.color_accept, text_color=self.color_accept)
2785+
2786+
dialog_content = UIScaling()
2787+
dialog = MDDialog(
2788+
title="UI Scaling",
2789+
type="custom",
2790+
content_cls=dialog_content,
2791+
buttons=[ set_button, cancel_button ],
2792+
# elevation=0,
2793+
)
2794+
dialog.d_content = dialog_content
2795+
dialog.d_content.ids["scaling_factor"].text = os.environ["KIVY_METRICS_DENSITY"] if "KIVY_METRICS_DENSITY" in os.environ else "0.0"
2796+
def dl_yes(s):
2797+
new_sf = 1.0
2798+
scaling_ok = False
2799+
try:
2800+
si = dialog.d_content.ids["scaling_factor"].text
2801+
sf = float(si)
2802+
if (sf >= 0.3 and sf <= 5.0) or sf == 0.0:
2803+
new_sf = sf
2804+
scaling_ok = True
2805+
2806+
except Exception as e:
2807+
RNS.log("Error while getting scaling factor from user: "+str(e), RNS.LOG_ERROR)
2808+
2809+
if scaling_ok:
2810+
dialog.d_content.ids["scaling_factor"].helper_text = ""
2811+
dialog.d_content.ids["scaling_factor"].helper_text_mode = "on_focus"
2812+
dialog.d_content.ids["scaling_factor"].error = False
2813+
dialog.dismiss()
2814+
if app_ui_scaling_path == None:
2815+
RNS.log("No path to UI scaling factor file could be found, cannot save scaling factor", RNS.LOG_ERROR)
2816+
else:
2817+
try:
2818+
with open(app_ui_scaling_path, "w") as sfile:
2819+
sfile.write(str(new_sf))
2820+
RNS.log(f"Saved configured scaling factor {new_sf} to {app_ui_scaling_path}", RNS.LOG_DEBUG)
2821+
except Exception as e:
2822+
RNS.log(f"Error while saving scaling factor {new_sf} to {app_ui_scaling_path}: {e}", RNS.LOG_ERROR)
2823+
2824+
else:
2825+
dialog.d_content.ids["scaling_factor"].helper_text = "Invalid scale factor, check your input"
2826+
dialog.d_content.ids["scaling_factor"].helper_text_mode = "persistent"
2827+
dialog.d_content.ids["scaling_factor"].error = True
2828+
2829+
def dl_no(s):
2830+
dialog.dismiss()
2831+
2832+
def dl_ds(s):
2833+
self.dialog_open = False
2834+
2835+
set_button.bind(on_release=dl_yes)
2836+
cancel_button.bind(on_release=dl_no)
2837+
2838+
dialog.bind(on_dismiss=dl_ds)
2839+
dialog.open()
2840+
self.dialog_open = True
2841+
2842+
except Exception as e:
2843+
RNS.log("Error while creating UI scaling dialog: "+str(e), RNS.LOG_ERROR)
2844+
2845+
27322846
def settings_action(self, sender=None, direction="left"):
27332847
if self.settings_ready:
27342848
self.settings_open(direction=direction)
@@ -5966,6 +6080,9 @@ class DialogItem(OneLineIconListItem):
59666080
class MDMapIconButton(MDIconButton):
59676081
pass
59686082

6083+
class UIScaling(BoxLayout):
6084+
pass
6085+
59696086
if not args.daemon:
59706087
from kivy.base import ExceptionManager, ExceptionHandler
59716088
class SidebandExceptionHandler(ExceptionHandler):

sbapp/ui/layouts.py

+34-2
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,28 @@
12601260
"""
12611261

12621262
layout_settings_screen = """
1263+
<UIScaling>
1264+
orientation: "vertical"
1265+
spacing: "24dp"
1266+
size_hint_y: None
1267+
height: self.minimum_height+dp(0)
1268+
1269+
MDLabel:
1270+
id: scaling_info
1271+
markup: True
1272+
text: "You can scale the entire Sideband UI by specifying a scaling factor in the field below. After setting it, restart sideband for the scaling to take effect.\\n\\nSet to 0.0 to disable scaling adjustments."
1273+
size_hint_y: None
1274+
text_size: self.width, None
1275+
height: self.texture_size[1]
1276+
1277+
MDTextField:
1278+
id: scaling_factor
1279+
hint_text: "Scaling Factor"
1280+
helper_text: "From 0.3 to 5.0"
1281+
helper_text_mode: "on_focus"
1282+
text: ""
1283+
font_size: dp(24)
1284+
12631285
MDScreen:
12641286
name: "settings_screen"
12651287
@@ -1399,11 +1421,21 @@
13991421
size_hint_y: None
14001422
height: self.texture_size[1]
14011423
1424+
MDRectangleFlatIconButton:
1425+
id: appearance_ui_scaling
1426+
icon: "relative-scale"
1427+
text: "Configure UI Scaling"
1428+
padding: [dp(0), dp(14), dp(0), dp(14)]
1429+
icon_size: dp(24)
1430+
font_size: dp(16)
1431+
size_hint: [1.0, None]
1432+
on_release: root.app.configure_ui_scaling_action(self)
1433+
14021434
MDBoxLayout:
14031435
orientation: "horizontal"
14041436
size_hint_y: None
1405-
padding: [0,0,dp(24),dp(0)]
1406-
height: dp(48)
1437+
padding: [0,dp(14),dp(24),dp(0)]
1438+
height: dp(62)
14071439
14081440
MDLabel:
14091441
text: "Notifications"

0 commit comments

Comments
 (0)