1313from random import SystemRandom
1414from typing import Literal
1515
16- try :
17- import psutil
18- except ImportError :
19- psutil = None
16+ import psutil
2017from PySide6 .QtCore import (
2118 QCoreApplication ,
2219 QEventLoop ,
@@ -76,6 +73,10 @@ def __init__(self, service: "LDDCService", instance_id: int, client_id: int, pid
7673 def handle_task (self , task : dict ) -> None :
7774 ...
7875
76+ @abstractmethod
77+ def init (self ) -> None :
78+ ...
79+
7980 def stop (self ) -> None :
8081 self .loop .quit ()
8182 logger .info ("Service instance %s stopped" , self .instance_id )
@@ -87,6 +88,7 @@ def run(self) -> None:
8788 logger .info ("Service instance %s started" , self .instance_id )
8889 self .signals .handle_task .connect (self .handle_task )
8990 self .loop = QEventLoop ()
91+ self .init ()
9092 self .loop .exec ()
9193
9294
@@ -95,8 +97,6 @@ def run(self) -> None:
9597
9698
9799def clean_dead_instance () -> bool :
98- if not psutil :
99- return False
100100 to_stop = []
101101 instance_dict_mutex .lock ()
102102 for instance_id , instance in instance_dict .items ():
@@ -238,8 +238,10 @@ def q_server_read_client(self) -> None:
238238 client_connection .flush ()
239239 client_connection .disconnectFromServer ()
240240 case "show" :
241- from view .main_window import main_window
242- in_main_thread (main_window .show_window )
241+ def show_main_window () -> None :
242+ from view .main_window import main_window
243+ main_window .show_window ()
244+ in_main_thread (show_main_window )
243245 client_connection .write (b"message_received" )
244246 client_connection .flush ()
245247 client_connection .disconnectFromServer ()
@@ -337,7 +339,7 @@ def __init__(self, service: LDDCService, instance_id: int, json_data: dict, clie
337339 # 任务ID 用于防止旧任务的结果覆盖新任务的结果
338340 self .taskid = 0
339341
340- def run (self ) -> None :
342+ def init (self ) -> None :
341343 # 初始化界面
342344 in_main_thread (self .init_widget )
343345 if "name" in self .client_info and "repo" in self .client_info and "ver" in self .client_info :
@@ -366,7 +368,6 @@ def run(self) -> None:
366368 self .widget .menu .action_unlink_lyrics .triggered .connect (self .unlink_lyrics )
367369
368370 cfg .desktop_lyrics_changed .connect (self .cfg_changed_slot )
369- super ().run ()
370371
371372 def init_widget (self ) -> None :
372373 """初始化界面(主线程)"""
@@ -426,9 +427,8 @@ def handle_task(self, task: dict) -> None:
426427 case "start" :
427428 # 开始播放
428429 logger .debug ("start" )
429- playback_time = self .get_playback_time (task )
430- if playback_time is not None :
431- self .start_time = int (time .time () * 1000 )
430+ if (playback_time := self .get_playback_time (task )) is not None :
431+ self .start_time = int (time .time () * 1000 ) - playback_time
432432 else :
433433 self .start_time = int (time .time () * 1000 ) - self .current_time
434434 if not self .timer .isActive ():
@@ -458,6 +458,9 @@ def handle_task(self, task: dict) -> None:
458458 logger .error ("task:chang_music, invalid data" )
459459 return
460460
461+ if (playback_time := self .get_playback_time (task )) is not None :
462+ self .start_time = int (time .time () * 1000 ) - playback_time
463+
461464 self .song_info = {"title" : title , "artist" : artist , "album" : album , "duration" : duration , "song_path" : song_path ,
462465 "track_number" : str (track ) if isinstance (track , int ) else track }
463466 # 先在关联数据库中查找
@@ -625,20 +628,20 @@ def set_inst(self) -> None:
625628 if self .song_info :
626629 self .config ["inst" ] = True
627630 self .widget .new_lyrics .emit ({"inst" : True })
628- self .show_artist_title (QCoreApplication .translate ("DesktopLyrics" , "纯音乐,请欣赏" ))
629- self .unlink_lyrics ()
631+ self .unlink_lyrics (QCoreApplication .translate ("DesktopLyrics" , "纯音乐,请欣赏" ))
630632
631633 def set_auto_search (self , is_disable : bool | None = None ) -> None :
632634 if self .song_info :
633635 self .config ["disable_auto_search" ] = is_disable if is_disable is not None else bool (not self .config .get ("disable_auto_search" ))
634636 self .update_db_data ()
635637
636- def unlink_lyrics (self ) -> None :
638+ def unlink_lyrics (self , msg : str = "" ) -> None :
637639 if self .song_info :
638640 self .lyrics_path = None
639641 self .lyrics = None
640642 self .offseted_lyrics = None
641643 self .update_db_data ()
644+ self .show_artist_title (msg )
642645
643646 def update_db_data (self ) -> None :
644647 local_song_lyrics .set_song (** self .song_info , lyrics_path = self .lyrics_path , config = {k : v for k , v in self .config .items ()
0 commit comments