Skip to content

Commit 2c14567

Browse files
committed
Fix text wrapping
1 parent 866d546 commit 2c14567

2 files changed

Lines changed: 53 additions & 27 deletions

File tree

menus/main.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from utils.preload import *
55
from utils.constants import button_style, slider_style, audio_extensions, discord_presence_id
6-
from utils.utils import FakePyPresence, UIFocusTextureButton, Card, extract_metadata, get_audio_thumbnail_texture, truncate_end
6+
from utils.utils import FakePyPresence, UIFocusTextureButton, Card, extract_metadata, get_audio_thumbnail_texture, truncate_end, get_wrapped_text
77

88
from math import ceil
99
from thefuzz import process, fuzz
@@ -56,7 +56,9 @@ def __init__(self, pypresence_client: None | FakePyPresence | pypresence.Presenc
5656

5757
self.tab_options = self.settings_dict.get("tab_options", [os.path.join("~", "Music"), os.path.join("~", "Downloads")])
5858
self.tab_content = {}
59+
self.tab_wrapped_text = {}
5960
self.playlist_content = {}
61+
self.playlist_wrapped_text = {}
6062
self.thumbnails = {}
6163
self.tab_buttons = {}
6264
self.music_buttons = {}
@@ -105,7 +107,7 @@ def create_ui(self):
105107
self.scrollbar.size_hint = (0.02, 1)
106108
self.scroll_box.add(self.scrollbar)
107109

108-
self.music_grid = arcade.gui.UIGridLayout(horizontal_spacing=30, vertical_spacing=30, row_count=100, column_count=5)
110+
self.music_grid = arcade.gui.UIGridLayout(horizontal_spacing=30, vertical_spacing=30, row_count=100, column_count=8)
109111
self.scroll_area.add(self.music_grid)
110112

111113
# Utility
@@ -259,20 +261,21 @@ def show_content(self, tab):
259261
self.highest_score_file = f"{self.current_tab}/{matches[0][0]}"
260262
for n, match in enumerate(matches):
261263
music_filename = match[0]
262-
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[f"{tab}/{music_filename}"], font_name="Roboto", font_size=13, text=music_filename, width=self.window.width / 7, height=self.window.height / 7), row=0, column=n)
264+
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[f"{tab}/{music_filename}"], font_name="Roboto", font_size=13, text=music_filename, width=self.window.width / 11, height=self.window.height / 11), row=0, column=n)
263265
self.music_buttons[music_filename].button.on_click = lambda event, tab=tab, music_filename=music_filename: self.queue.append(f"{tab}/{music_filename}")
264266
else:
265-
self.music_grid.row_count = ceil(len(self.tab_content[tab]) / 5)
267+
self.music_grid.row_count = ceil(len(self.tab_content[tab]) / 8)
266268
self.music_grid._update_size_hints()
267269

268270
self.highest_score_file = ""
269271

270272
self.no_music_label.visible = not self.tab_content[tab]
273+
271274
for n, music_filename in enumerate(self.tab_content[tab]):
272-
row = n // 5
273-
col = n % 5
275+
row = n // 8
276+
col = n % 8
274277

275-
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[f"{tab}/{music_filename}"], font_name="Roboto", font_size=13, text=music_filename, width=self.window.width / 7, height=self.window.height / 7), row=row, column=col)
278+
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[f"{tab}/{music_filename}"], font_name="Roboto", font_size=13, text=self.tab_wrapped_text[tab][n], width=self.window.width / 11, height=self.window.height / 11), row=row, column=col)
276279
self.music_buttons[music_filename].button.on_click = lambda event, tab=tab, music_filename=music_filename: self.queue.append(f"{tab}/{music_filename}")
277280

278281
elif self.current_mode == "playlist":
@@ -284,46 +287,51 @@ def show_content(self, tab):
284287
self.highest_score_file = matches[0][0]
285288
for n, match in enumerate(matches):
286289
music_filename = match[0]
287-
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[music_filename], font_name="Roboto", font_size=13, text=music_filename, width=self.window.width / 6, height=self.window.height / 5), row=0, column=n)
290+
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[music_filename], font_name="Roboto", font_size=13, text=music_filename, width=self.window.width / 11, height=self.window.height / 11), row=0, column=n)
288291
self.music_buttons[music_filename].button.on_click = lambda event, tab=tab, music_filename=music_filename: self.queue.append(music_filename)
289292

290293
else:
291-
self.music_grid.row_count = ceil((len(self.playlist_content[tab]) + 1) / 5)
294+
self.music_grid.row_count = ceil((len(self.playlist_content[tab]) + 1) / 8)
292295
self.music_grid._update_size_hints()
293296

294297
self.highest_score_file = ""
295298

296299
self.no_music_label.visible = not self.playlist_content[tab]
297300

298301
for n, music_filename in enumerate(self.playlist_content[tab]):
299-
row = n // 5
300-
col = n % 5
302+
row = n // 8
303+
col = n % 8
301304

302-
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[music_filename], font_name="Roboto", font_size=13, text=music_filename, width=self.window.width / 6, height=self.window.height / 5), row=row, column=col)
305+
self.music_buttons[music_filename] = self.music_grid.add(Card(card_texture=self.thumbnails[music_filename], font_name="Roboto", font_size=13, text=self.playlist_wrapped_text[tab][n], width=self.window.width / 11, height=self.window.height / 11), row=row, column=col)
303306
self.music_buttons[music_filename].button.on_click = lambda event, tab=tab, music_filename=music_filename: self.queue.append(music_filename)
304-
row = (n + 1) // 5
305-
col = (n + 1) % 5
307+
row = (n + 1) // 8
308+
col = (n + 1) % 8
306309

307-
self.music_buttons["add_music"] = self.music_grid.add(Card(card_texture=music_icon, font_name="Roboto", font_size=13, text="Add Music", width=self.window.width / 6, height=self.window.height / 5), row=row, column=col)
310+
self.music_buttons["add_music"] = self.music_grid.add(Card(card_texture=music_icon, font_name="Roboto", font_size=13, text="Add Music", width=self.window.width / 11, height=self.window.height / 11), row=row, column=col)
308311
self.music_buttons["add_music"].button.on_click = lambda event: self.add_music()
309312

310313
self.update_buttons()
311314

312315
def load_content(self):
313316
for tab in self.tab_options:
314-
self.tab_content[os.path.expanduser(tab)] = []
315-
for filename in os.listdir(os.path.expanduser(tab)):
317+
expanded_tab = os.path.expanduser(tab)
318+
self.tab_content[expanded_tab] = []
319+
320+
for filename in os.listdir(expanded_tab):
316321
if filename.split(".")[-1] in audio_extensions:
317-
if f"{os.path.expanduser(tab)}/{filename}" not in self.thumbnails:
318-
self.thumbnails[f"{os.path.expanduser(tab)}/{filename}"] = get_audio_thumbnail_texture(f"{os.path.expanduser(tab)}/{filename}", self.window.size)
319-
self.tab_content[os.path.expanduser(tab)].append(filename)
322+
if f"{expanded_tab}/{filename}" not in self.thumbnails:
323+
self.thumbnails[f"{expanded_tab}/{filename}"] = get_audio_thumbnail_texture(f"{expanded_tab}/{filename}", self.window.size)
324+
self.tab_content[expanded_tab].append(filename)
325+
326+
self.tab_wrapped_text[expanded_tab] = get_wrapped_text(self.tab_content[expanded_tab], self.window.width // 11, 14)
320327

321-
for content in self.settings_dict.get("playlists", {}).values():
328+
for playlist, content in self.settings_dict.get("playlists", {}).items():
322329
for file in content:
323330
if file not in self.thumbnails:
324331
self.thumbnails[file] = get_audio_thumbnail_texture(file, self.window.size)
325332

326-
self.playlist_content = self.settings_dict.get("playlists", {})
333+
self.playlist_content[playlist] = content
334+
self.playlist_wrapped_text[playlist] = get_wrapped_text(content, self.window.width // 11, 14)
327335

328336
def load_tabs(self):
329337
self.tab_box.clear()

utils/utils.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def on_hover(self):
8080
self.resize(width=self.width / 1.1, height=self.height / 1.1)
8181

8282
class Card(arcade.gui.UIBoxLayout):
83-
def __init__(self, width: int, height: int, font_name: str, font_size: int, text: str, card_texture: arcade.Texture, padding=10):
83+
def __init__(self, width: int, height: int, font_name: str, font_size: int, text: str, card_texture: arcade.Texture, padding=10, new_lines=3):
8484
super().__init__(width=width, height=height, space_between=padding, align="top")
8585

8686
self.button = self.add(arcade.gui.UITextureButton(
@@ -92,18 +92,36 @@ def __init__(self, width: int, height: int, font_name: str, font_size: int, text
9292
height=height,
9393
))
9494

95-
wrapped_lines = textwrap.wrap(text, width=int(width / (font_size * 0.6)))
96-
wrapped_text = "\n".join(wrapped_lines)
97-
9895
self.label = self.add(arcade.gui.UILabel(
99-
text=wrapped_text,
96+
text=text,
10097
font_name=font_name,
10198
font_size=font_size,
10299
width=width,
103100
height=height * 0.1,
104101
multiline=True,
105102
))
106103

104+
def get_wrapped_text(text_list: list[str], width: int, font_size: int):
105+
max_lines = 0
106+
wrapped_line_list = []
107+
wrapped_text_list = []
108+
109+
for text in text_list: # get max lines and wrap text
110+
wrapped_lines = textwrap.wrap(text, width=int(width / (font_size * 0.6)))
111+
if len(wrapped_lines) > max_lines:
112+
max_lines = len(wrapped_lines)
113+
114+
wrapped_line_list.append(wrapped_lines)
115+
116+
for wrapped_lines in wrapped_line_list:
117+
if len(wrapped_lines) < max_lines: # adjust text to maximum lines
118+
for i in range(max_lines - len(wrapped_lines)):
119+
wrapped_lines.append("")
120+
121+
wrapped_text_list.append("\n".join(wrapped_lines))
122+
123+
return wrapped_text_list
124+
107125
def on_exception(*exc_info):
108126
logging.error(f"Unhandled exception:\n{''.join(traceback.format_exception(exc_info[1], limit=None))}")
109127

0 commit comments

Comments
 (0)