Open QR login in window when possible
This commit is contained in:
@@ -3,6 +3,8 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import webbrowser
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
|
||||
@@ -145,7 +147,7 @@ class ClientWrapper:
|
||||
except Exception:
|
||||
pass
|
||||
if qr_open and saved_path:
|
||||
self._open_file(saved_path)
|
||||
self._open_qr_window(saved_path)
|
||||
|
||||
def _save_qr_image(self, url: str, qr_path: str) -> Optional[Path]:
|
||||
try:
|
||||
@@ -160,6 +162,34 @@ class ClientWrapper:
|
||||
logger.info("QR image saved to %s", path)
|
||||
return path
|
||||
|
||||
def _open_qr_window(self, path: Path) -> None:
|
||||
if self._try_tk_window(path):
|
||||
return
|
||||
self._open_file(path)
|
||||
|
||||
def _try_tk_window(self, path: Path) -> bool:
|
||||
try:
|
||||
import tkinter as tk
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def run_window() -> None:
|
||||
try:
|
||||
root = tk.Tk()
|
||||
root.title("OverUB QR Login")
|
||||
root.attributes("-topmost", True)
|
||||
image = tk.PhotoImage(file=str(path))
|
||||
label = tk.Label(root, image=image)
|
||||
label.image = image
|
||||
label.pack()
|
||||
root.mainloop()
|
||||
except Exception:
|
||||
return
|
||||
|
||||
thread = threading.Thread(target=run_window, daemon=True)
|
||||
thread.start()
|
||||
return True
|
||||
|
||||
def _open_file(self, path: Path) -> None:
|
||||
try:
|
||||
if sys.platform.startswith("darwin"):
|
||||
@@ -169,7 +199,10 @@ class ClientWrapper:
|
||||
else:
|
||||
subprocess.run(["xdg-open", str(path)], check=False)
|
||||
except Exception:
|
||||
logger.warning("Failed to open QR image automatically")
|
||||
try:
|
||||
webbrowser.open(path.as_uri())
|
||||
except Exception:
|
||||
logger.warning("Failed to open QR image automatically")
|
||||
|
||||
async def _prompt(self, prompt: str) -> str:
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
Reference in New Issue
Block a user