Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update flashlight.py #4586

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions sdk/python/packages/flet/src/flet/core/flashlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,40 @@ def __init__(
def _get_control_name(self):
return "flashlight"

def _toggle_state(self, sr: str, state: bool=True) -> bool:
if ("1" == sr):
self.turned_on = state
return self.turned_on

def turn_on(self, wait_timeout: Optional[int] = 5) -> bool:
sr = self.invoke_method("on", wait_for_result=True, wait_timeout=wait_timeout)
return self._toggle_state(sr)

if int(sr) == 1:
self.turned_on = True
return self.turned_on

async def turn_on_async(self, wait_timeout: Optional[int] = 5) -> bool:
sr = await self.invoke_method_async(
"on", wait_for_result=True, wait_timeout=wait_timeout
)
if int(sr) == 1:
self.turned_on = True
return self.turned_on
return self._toggle_state(sr)


def turn_off(self, wait_timeout: Optional[int] = 5) -> bool:
sr = self.invoke_method("off", wait_for_result=True, wait_timeout=wait_timeout)

if int(sr) == 1:
self.turned_on = False
return self.turned_on
return self._toggle_state(sr, False)


async def turn_off_async(self, wait_timeout: Optional[int] = 5) -> bool:
sr = await self.invoke_method_async(
"off", wait_for_result=True, wait_timeout=wait_timeout
)
if int(sr) == 1:
self.turned_on = False
return self.turned_on
return self._toggle_state(sr, False)


def toggle(self, wait_timeout: Optional[int] = 5) -> bool:
if self.turned_on:
return self.turn_off(wait_timeout)
return self.turn_on(wait_timeout)
_func = self.turn_off if self.turned_on else self.turn_on
return _func(wait_timeout)


async def toggle_async(self, wait_timeout: Optional[int] = 5) -> bool:
if self.turned_on:
return await self.turn_off_async(wait_timeout)
Expand Down