Skip to content

Commit

Permalink
refactor(vscode): flatten vscode menu items into its main menu #102
Browse files Browse the repository at this point in the history
  • Loading branch information
sassanh committed May 10, 2024
1 parent 21693c6 commit 56e476c
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 306 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
in the service file so that user can easily add their custom services
- fix(voice): remove "clear access key" item when access key is not set #97
- fix(voice): update pvorca to 2.1.0 as they suddenly yanked 1.4.0 in pypi #103
- refactor(vscode): flatten vscode menu items into its main menu #102

## Version 0.14.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import asyncio
import json
import socket
import subprocess
from typing import Literal, TypedDict

Expand Down Expand Up @@ -121,3 +122,91 @@ async def check_status() -> None:
),
),
)


async def set_name() -> None:
try:
hostname = socket.gethostname()
process = await asyncio.create_subprocess_exec(
CODE_BINARY_PATH,
'tunnel',
'--accept-server-license-terms',
'rename',
hostname,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
await process.wait()
except subprocess.CalledProcessError:
dispatch(
NotificationsAddAction(
notification=Notification(
title='VSCode',
content='Failed to setup: renaming the tunnel',
display_type=NotificationDisplayType.STICKY,
color=DANGER_COLOR,
icon='󰜺',
chime=Chime.FAILURE,
),
),
)
finally:
await check_status()


async def install_service() -> None:
try:
process = await asyncio.create_subprocess_exec(
CODE_BINARY_PATH,
'tunnel',
'--accept-server-license-terms',
'service',
'install',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
await process.wait()
except subprocess.CalledProcessError:
dispatch(
NotificationsAddAction(
notification=Notification(
title='VSCode',
content='Failed to setup: installing service',
display_type=NotificationDisplayType.STICKY,
color=DANGER_COLOR,
icon='󰜺',
chime=Chime.FAILURE,
),
),
)
finally:
await check_status()


async def uninstall_service() -> None:
try:
process = await asyncio.create_subprocess_exec(
CODE_BINARY_PATH,
'tunnel',
'--accept-server-license-terms',
'service',
'uninstall',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
await process.wait()
except subprocess.CalledProcessError:
dispatch(
NotificationsAddAction(
notification=Notification(
title='VSCode',
content='Failed to setup: uninstalling service',
display_type=NotificationDisplayType.STICKY,
color=DANGER_COLOR,
icon='󰜺',
chime=Chime.FAILURE,
),
),
)
finally:
await check_status()
2 changes: 1 addition & 1 deletion ubo_app/services/050-vscode/login_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import subprocess

from checks import check_status
from commands import check_status
from constants import CODE_BINARY_PATH
from kivy.clock import mainthread
from kivy.lang.builder import Builder
Expand Down
110 changes: 75 additions & 35 deletions ubo_app/services/050-vscode/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import subprocess
from typing import TYPE_CHECKING

from checks import check_status
from commands import check_status, install_service, uninstall_service
from constants import CODE_BINARY_PATH, CODE_BINARY_URL, DOWNLOAD_PATH
from login_page import LoginPage
from setup_page import SetupPage
from ubo_gui.constants import DANGER_COLOR
from ubo_gui.menu.types import ActionItem, ApplicationItem, HeadedMenu
from vscode_qrcode_page import CODE_TUNNEL_URL_PREFIX, VSCodeQRCodePage

from ubo_app.constants import INSTALLATION_PATH
from ubo_app.store import autorun, dispatch
Expand All @@ -25,6 +25,7 @@
VSCodeDoneDownloadingAction,
VSCodeStartDownloadingAction,
VSCodeState,
VSCodeStatus,
)
from ubo_app.utils.async_ import create_task

Expand Down Expand Up @@ -112,41 +113,63 @@ async def act() -> None:
create_task(act())


@autorun(
lambda state: state.vscode,
comparator=lambda state: (
state.vscode.is_binary_installed,
state.vscode.is_logged_in,
state.vscode.is_downloading,
),
)
def status_based_actions(status: VSCodeStatus) -> list[ActionItem | ApplicationItem]:
actions = []
if status.is_running:
actions.append(
ApplicationItem(
label='Show URL',
icon='󰐲',
application=VSCodeQRCodePage(
url=f'{CODE_TUNNEL_URL_PREFIX}{status.name}',
),
),
)
actions.append(
ActionItem(
label='Uninstall Service'
if status.is_service_installed
else 'Install Service',
icon='󰫜' if status.is_service_installed else '󰫚',
action=(lambda: create_task(uninstall_service()) and None)
if status.is_service_installed
else (lambda: create_task(install_service()) and None),
),
)
return actions


def login_actions(*, is_logged_in: bool) -> list[ActionItem | ApplicationItem]:
actions = []
if is_logged_in:
actions.extend(
[
ActionItem(
label='Logout',
icon='󰍃',
action=logout,
),
],
)
else:
actions.append(
ApplicationItem(
label='Login',
icon='󰍂',
application=LoginPage,
),
)
return actions


@autorun(lambda state: state.vscode)
def vscode_menu(state: VSCodeState) -> HeadedMenu:
actions = []
if not state.is_downloading:
if state.is_binary_installed:
if state.is_logged_in:
actions.extend(
[
ApplicationItem(
label='Setup Tunnel',
icon='󰒔',
application=SetupPage,
),
ActionItem(
label='Logout',
icon='󰍃',
action=logout,
),
],
)
else:
actions.append(
ApplicationItem(
label='Login',
icon='󰍂',
application=LoginPage,
),
)
if state.status:
actions.extend(status_based_actions(state.status))
actions.extend(login_actions(is_logged_in=state.is_logged_in))

actions.append(
ActionItem(
Expand All @@ -158,10 +181,27 @@ def vscode_menu(state: VSCodeState) -> HeadedMenu:
),
)

status = ''
if state.status:
if state.status.is_running:
status = 'Service is running'
elif not state.status.is_service_installed:
status = 'Service not installed'
else:
status = 'Service installed but not running'
elif state.is_downloading:
status = 'Downloading...'
elif not state.is_binary_installed:
status = 'Code CLI not installed'
elif not state.is_logged_in:
status = 'Needs authentication'
else:
status = 'Unknown status'

return HeadedMenu(
title='󰨞VSCode',
heading='Setup VSCode Tunnel',
sub_heading='Downloading...' if state.is_downloading else '',
heading='VSCode Remote Tunnel',
sub_heading=status,
items=actions,
)

Expand Down
59 changes: 0 additions & 59 deletions ubo_app/services/050-vscode/setup_page.kv

This file was deleted.

Loading

0 comments on commit 56e476c

Please sign in to comment.