diff --git a/LunaTranslator/LunaTranslator/LunaTranslator.py b/LunaTranslator/LunaTranslator/LunaTranslator.py index 32da25db..85b06804 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator.py @@ -1248,7 +1248,10 @@ class MAINUI: self.__count += 1 elif msg == 1: if bool(param): - windows.BringWindowToTop(int(self.translation_ui.winid)) + self.translation_ui.settop() + else: + if not globalconfig['keepontop']: + self.translation_ui.canceltop() elif msg == 2: self.translation_ui.closesignal.emit() diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index f8695a6c..9b88a8b2 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -702,29 +702,16 @@ class QUnFrameWindow(resizableframeless): self.setontopthread() def canceltop(self): - windows.SetWindowPos( - self.winid, - windows.HWND_NOTOPMOST, - 0, - 0, - 0, - 0, - windows.SWP_NOACTIVATE | windows.SWP_NOSIZE | windows.SWP_NOMOVE, - ) - HWNDStyleEx = windows.GetWindowLong(self.winid, windows.GWL_EXSTYLE) - windows.SetWindowLong( - self.winid, windows.GWL_EXSTYLE, HWNDStyleEx & ~windows.WS_EX_TOPMOST - ) - - windows.SetWindowPos( - self.winid, - windows.GetForegroundWindow(), - 0, - 0, - 0, - 0, - windows.SWP_NOACTIVATE | windows.SWP_NOSIZE | windows.SWP_NOMOVE, - ) + if self.istopmost(): + windows.SetWindowPos( + self.winid, + windows.HWND_NOTOPMOST, + 0, + 0, + 0, + 0, + windows.SWP_NOACTIVATE | windows.SWP_NOSIZE | windows.SWP_NOMOVE, + ) def istopmost(self): return bool( @@ -733,12 +720,6 @@ class QUnFrameWindow(resizableframeless): ) def settop(self): - if not self.istopmost(): - self.canceltop() - HWNDStyleEx = windows.GetWindowLong(self.winid, windows.GWL_EXSTYLE) - windows.SetWindowLong( - self.winid, windows.GWL_EXSTYLE, HWNDStyleEx | windows.WS_EX_TOPMOST - ) windows.SetWindowPos( self.winid, windows.HWND_TOPMOST, @@ -751,22 +732,25 @@ class QUnFrameWindow(resizableframeless): @threader def setontopthread(self): - self.settop() - while globalconfig["keepontop"]: - try: - hwnd = windows.GetForegroundWindow() - pid = windows.GetWindowThreadProcessId(hwnd) - if pid == os.getpid(): - pass - elif globalconfig["focusnotop"] and self.thistimenotsetop: - pass - else: - self.settop() - except: - print_exc() - time.sleep(0.5) - self.canceltop() + with self.setontopthread_lock: + if not globalconfig["keepontop"]: + return self.canceltop() + self.settop() + while globalconfig["keepontop"]: + + try: + hwnd = windows.GetForegroundWindow() + pid = windows.GetWindowThreadProcessId(hwnd) + if pid == os.getpid(): + pass + elif globalconfig["focusnotop"] and self.thistimenotsetop: + pass + else: + self.settop() + except: + print_exc() + time.sleep(0.5) def seteffect(self): if globalconfig["WindowEffect"] == 0: @@ -792,6 +776,7 @@ class QUnFrameWindow(resizableframeless): self.processismuteed = False self.thistimenotsetop = False self.isbindedwindow = False + self.setontopthread_lock = threading.Lock() def displayglobaltooltip_f(self, string): QToolTip.showText(QCursor.pos(), string, self) diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index 603c60fd..efff6b11 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -430,25 +430,35 @@ def minmaxmoveobservefunc(self): return _focusp = windows.GetWindowThreadProcessId(hwnd) - if event == windows.EVENT_SYSTEM_FOREGROUND: - if globalconfig["keepontop"] and globalconfig["focusnotop"]: - if _focusp == os.getpid(): - pass - else: - hwndmagpie = windows.FindWindow( - "Window_Magpie_967EB565-6F73-4E94-AE53-00CC42592A22", None - ) - if ( - len(gobject.baseobject.textsource.pids) == 0 - or _focusp in gobject.baseobject.textsource.pids - or hwnd == hwndmagpie - ): - gobject.baseobject.translation_ui.thistimenotsetop = False - gobject.baseobject.translation_ui.settop() - else: - gobject.baseobject.translation_ui.thistimenotsetop = True - if gobject.baseobject.translation_ui.istopmost(): - gobject.baseobject.translation_ui.canceltop() + if event != windows.EVENT_SYSTEM_FOREGROUND: + return + if not (globalconfig["keepontop"] and globalconfig["focusnotop"]): + return + if _focusp == os.getpid(): + return + + if windows.FindWindow( + "Window_Magpie_967EB565-6F73-4E94-AE53-00CC42592A22", None + ): + return + if ( + len(gobject.baseobject.textsource.pids) == 0 + or _focusp in gobject.baseobject.textsource.pids + ): + gobject.baseobject.translation_ui.thistimenotsetop = False + gobject.baseobject.translation_ui.settop() + else: + gobject.baseobject.translation_ui.thistimenotsetop = True + gobject.baseobject.translation_ui.canceltop() + windows.SetWindowPos( + hwnd, + windows.HWND_TOP, + 0, + 0, + 0, + 0, + windows.SWP_NOACTIVATE | windows.SWP_NOSIZE | windows.SWP_NOMOVE, + ) except: print_exc() diff --git a/LunaTranslator/LunaTranslator/translator/sakura.py b/LunaTranslator/LunaTranslator/translator/sakura.py index cd5cb327..82add5ee 100644 --- a/LunaTranslator/LunaTranslator/translator/sakura.py +++ b/LunaTranslator/LunaTranslator/translator/sakura.py @@ -113,7 +113,7 @@ class TS(basetrans): content += ( "将下面的日文文本根据上述术语表的对应关系和备注翻译成中文:" + query ) - print(content) + # print(content) messages.append({"role": "user", "content": content}) return messages @@ -185,12 +185,15 @@ class TS(basetrans): for o in output.iter_lines(): try: res = o.decode("utf-8").strip()[6:] # .replace("data: ", "") - print(res) - if res != "": - yield json.loads(res) + # print(res) + if res == "": + continue + res = json.loads(res) except: raise Exception(o) + yield res + def translate(self, query): query = json.loads(query) gpt_dict = query["gpt_dict"] @@ -223,13 +226,13 @@ class TS(basetrans): if bool(self.config["fix_degeneration"]): cnt = 0 - print(completion_tokens) + # print(completion_tokens) while completion_tokens == int(self.config["max_new_token"]): # detect degeneration, fixing frequency_penalty += 0.1 yield "\0" yield "[检测到退化,重试中]" - print("------------------清零------------------") + # print("------------------清零------------------") if bool(self.config["流式输出"]) == True: output = self.send_request_stream( query, @@ -307,12 +310,12 @@ class TS(basetrans): if bool(self.config["fix_degeneration"]): cnt = 0 - print(completion_tokens) + # print(completion_tokens) while completion_tokens == int(self.config["max_new_token"]): frequency_penalty += 0.1 yield "\0" yield "[检测到退化,重试中]" - print("------------------清零------------------") + # print("------------------清零------------------") if bool(self.config["流式输出"]) == True: output = self.send_request_stream( query, diff --git a/LunaTranslator/LunaTranslator/windows.py b/LunaTranslator/LunaTranslator/windows.py index 92bb3a38..5a34ad39 100644 --- a/LunaTranslator/LunaTranslator/windows.py +++ b/LunaTranslator/LunaTranslator/windows.py @@ -87,6 +87,7 @@ SW_MAX = 11 WS_MINIMIZE = 536870912 HWND_TOPMOST = -1 HWND_NOTOPMOST = -2 +HWND_TOP = 0 SW_HIDE = 0 SWP_NOACTIVATE = 16 SWP_NOMOVE = 2