Skip to content

Commit cf4c6c1

Browse files
committed
feat: Added keybinds for setting time and carousel movement
1 parent 8ba8a72 commit cf4c6c1

File tree

1 file changed

+59
-32
lines changed

1 file changed

+59
-32
lines changed

pyttml/screens/sync.py

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from components.carousel import Carousel, ScrollDirection, VerticalScroller
22
from components.playerbox import PlayerBox
33
from components.sidebar import Sidebar
4-
from ttml import Lyrics, process_lyrics
5-
from screens.edit import EditScreen
6-
4+
from ttml import Lyrics
75

86
from textual import events
97
from textual.app import ComposeResult
@@ -15,13 +13,32 @@
1513
class SyncScreen(Screen):
1614
lyrics_saved_state: Lyrics = None
1715
active_line_index: int = 0
16+
action_mapping:dict = {}
17+
18+
BINDINGS = [
19+
("a", "move_carousel_backward()", "Moves carousel backward"),
20+
("d", "move_carousel_forward()", "Moves carousel forward"),
21+
("f", "set_start_time()", "Sets the start time of active word as current timestamp"),
22+
("g", "set_end_move()", "Sets the end time of the active word and start time of the next word as current timestamp"),
23+
("h", "set_end_time()", "Sets the end time of the active word as current timestamp"),
24+
]
25+
26+
def __init__(self):
27+
self.action_mapping = {
28+
"next_word": self.action_move_carousel_forward,
29+
"prev_word": self.action_move_carousel_backward,
30+
"set_start_time": self.action_set_start_time,
31+
"set_end_time": self.action_set_end_time,
32+
"set_end_move": self.action_set_end_move
33+
}
34+
super().__init__()
1835

1936
def compose(self) -> ComposeResult:
2037
yield Static("No Lyrics Loaded", id="lyrics_label")
2138
yield Sidebar(id="sidebar")
2239
yield (Horizontal(
23-
Button("←", id="prev_word_button"),
24-
Button("→", id="next_word_button"),
40+
Button("←", id="prev_word"),
41+
Button("→", id="next_word"),
2542
Button("F", id="set_start_time",
2643
tooltip="Set timestamp as the startime of the word"),
2744
Button("G", id="set_end_move",
@@ -40,36 +57,46 @@ def update_scroller(self) -> None:
4057
vertical_scroller.scroll(ScrollDirection.backward)
4158
elif vertical_scroller.active_line_index < carousel_item_line_index:
4259
vertical_scroller.scroll(ScrollDirection.forward)
60+
61+
def action_move_carousel_forward(self):
62+
self.query_one(Carousel).move(ScrollDirection.forward)
63+
self.update_scroller()
64+
65+
def action_move_carousel_backward(self):
66+
self.query_one(Carousel).move(ScrollDirection.backward)
67+
self.update_scroller()
68+
69+
def action_set_start_time(self):
70+
carousel: Carousel = self.query_one(Carousel)
4371

44-
def on_button_pressed(self, event: Button.Pressed):
72+
active_word_index = self.app.CURR_LYRICS.get_element_map_index(carousel.active_item.element)
73+
timestamp = round(self.app.PLAYER.get_timestamp(), 3)
74+
active_item_index = carousel._nodes.index(carousel.active_item)
75+
self.app.CURR_LYRICS.element_map[active_word_index][0].start_time = timestamp
76+
carousel._nodes[active_item_index].update()
77+
78+
def action_set_end_time(self):
4579
carousel: Carousel = self.query_one(Carousel)
4680

47-
if event.button.id == "next_word_button":
48-
carousel.move(ScrollDirection.forward)
49-
self.update_scroller()
50-
51-
elif event.button.id == "prev_word_button":
52-
carousel.move(ScrollDirection.backward)
53-
self.update_scroller()
54-
55-
elif event.button.id in ["set_start_time", "set_end_time", "set_end_move"]:
56-
active_word_index = self.app.CURR_LYRICS.get_element_map_index(carousel.active_item.element)
57-
timestamp = round(self.app.PLAYER.get_timestamp(), 3)
58-
active_item_index = carousel._nodes.index(carousel.active_item)
59-
60-
if event.button.id == "set_start_time":
61-
self.app.CURR_LYRICS.element_map[active_word_index][0].start_time = timestamp
62-
63-
elif event.button.id in ["set_end_time", "set_end_move"]:
64-
self.app.CURR_LYRICS.element_map[active_word_index][0].end_time = timestamp
65-
66-
if event.button.id == "set_end_move":
67-
carousel.move(ScrollDirection.forward)
68-
self.update_scroller()
69-
self.app.CURR_LYRICS.element_map[active_word_index + 1][0].start_time = timestamp + 0.02
70-
carousel._nodes[active_item_index + 1].update()
71-
72-
carousel._nodes[active_item_index].update()
81+
active_word_index = self.app.CURR_LYRICS.get_element_map_index(carousel.active_item.element)
82+
timestamp = round(self.app.PLAYER.get_timestamp(), 3)
83+
active_item_index = carousel._nodes.index(carousel.active_item)
84+
self.app.CURR_LYRICS.element_map[active_word_index][0].end_time = timestamp
85+
carousel._nodes[active_item_index].update()
86+
87+
def action_set_end_move(self):
88+
carousel: Carousel = self.query_one(Carousel)
89+
self.action_set_end_time()
90+
carousel.move(ScrollDirection.forward)
91+
self.update_scroller()
92+
self.action_set_start_time()
93+
94+
def on_button_pressed(self, event: Button.Pressed):
95+
96+
try:
97+
self.action_mapping[event.button.id]()
98+
except KeyError:
99+
pass
73100

74101
def on_screen_resume(self, event: events.ScreenResume):
75102
label: Static = self.query_one("#lyrics_label", Static)

0 commit comments

Comments
 (0)