Change login button behavior to flip-flop

This commit is contained in:
DevilXD committed 2022-11-04 14:47:54 +01:00
1 parent 73a2a6145f
commit 2f78d476e0
2 files changed
+24 -30

No files matched your search

+21 -18
View File
@@ -453,6 +453,13 @@ class LoginForm:
async def ask_login(self) -> str: async def ask_login(self) -> str:
self.update(_("gui", "login", "required"), None) self.update(_("gui", "login", "required"), None)
self._manager.print(_("gui", "login", "request")) self._manager.print(_("gui", "login", "request"))
# the user needs to press on the login button to open the browser
self._confirm.clear()
try:
self._button.config(state="normal")
await self._confirm.wait()
finally:
self._button.config(state="disabled")
self._confirm.clear() self._confirm.clear()
# open the chrome browser on the Twitch's login page # open the chrome browser on the Twitch's login page
@@ -480,15 +487,7 @@ class LoginForm:
service_creationflags=CREATE_NO_WINDOW, service_creationflags=CREATE_NO_WINDOW,
) )
) )
done, pending = await asyncio.wait( driver = await self._manager.coro_unless_closed(driver_coro)
[driver_coro, self._manager.wait_until_closed()],
return_when=asyncio.FIRST_COMPLETED
)
for task in pending:
task.cancel()
if self._manager.close_requested:
raise ExitRequest()
driver = next(iter(done)).result()
break break
except WebDriverException as exc: except WebDriverException as exc:
message = exc.msg message = exc.msg
@@ -514,14 +513,7 @@ class LoginForm:
assert driver is not None assert driver is not None
driver.request_interceptor = self.interceptor driver.request_interceptor = self.interceptor
page_coro = loop.run_in_executor(None, driver.get, "https://twitch.tv/login") page_coro = loop.run_in_executor(None, driver.get, "https://twitch.tv/login")
done, pending = await asyncio.wait( await self._manager.coro_unless_closed(page_coro)
[page_coro, self._manager.wait_until_closed()],
return_when=asyncio.FIRST_COMPLETED
)
for task in pending:
task.cancel()
if self._manager.close_requested:
raise ExitRequest()
# enable the login button once the page finishes opening # enable the login button once the page finishes opening
self._button.config(state="normal") self._button.config(state="normal")
@@ -537,7 +529,7 @@ class LoginForm:
# inputmode="numeric" pattern="[0-9]*" value=""> # inputmode="numeric" pattern="[0-9]*" value="">
# wait for the user to navigate away from the URL, indicating successful login # wait for the user to navigate away from the URL, indicating successful login
# alternatively, they can press on the login button # alternatively, they can press on the login button again
while ( while (
driver.current_url != "https://www.twitch.tv/?no-reload=true" driver.current_url != "https://www.twitch.tv/?no-reload=true"
and not self._confirm.is_set() and not self._confirm.is_set()
@@ -1930,6 +1922,17 @@ class GUIManager:
# wait until the user closes the window # wait until the user closes the window
await self._close_requested.wait() await self._close_requested.wait()
async def coro_unless_closed(self, coro: abc.Awaitable[_T]) -> _T:
done, pending = await asyncio.wait(
[coro, self._close_requested.wait()],
return_when=asyncio.FIRST_COMPLETED,
)
for task in pending:
task.cancel()
if self._close_requested.is_set():
raise ExitRequest()
return next(iter(done)).result()
def prevent_close(self): def prevent_close(self):
self._close_requested.clear() self._close_requested.clear()
+3 -12
View File
@@ -937,19 +937,10 @@ class Twitch:
raise RequestInvalid() raise RequestInvalid()
try: try:
response: aiohttp.ClientResponse | None = None response: aiohttp.ClientResponse | None = None
done, pending = await asyncio.wait( response = await self.gui.coro_unless_closed(
[session.request(method, url, **kwargs), self.gui.wait_until_closed()], session.request(method, url, **kwargs)
return_when=asyncio.FIRST_COMPLETED,
) )
for task in pending: assert response is not None
task.cancel()
if self.gui.close_requested:
raise ExitRequest()
for task in done:
response = task.result()
break
if response is None:
raise RuntimeError("Close request leak")
logger.debug(f"Response: {response.status}: {response}") logger.debug(f"Response: {response.status}: {response}")
if response.status < 500: if response.status < 500:
# pre-read the response to avoid getting errors outside of the context manager # pre-read the response to avoid getting errors outside of the context manager