Skip to content

Commit

Permalink
improve ui
Browse files Browse the repository at this point in the history
  • Loading branch information
wikm360 committed Oct 14, 2024
1 parent be8b2ea commit 7891849
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ core/linux/select.txt
gui/test3.py
GUI-ver/test.py
subs/3ircle
GUI-ver/subs/3ircle
GUI-ver/subs/3ircle
core/win/select.txt
38 changes: 24 additions & 14 deletions GUI-ver/backendflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,41 +103,51 @@ def delete_subscription(self, profile):

# self.log(f"Previous {profile} sub deleted")

def ping_config(self, profile, config_num , ping_type):
def ping_config(self, profile, config_num, ping_type):
config_path = f"./subs/{profile}/{config_num}.json"
if ping_type == "Tcping" :

if ping_type == "Tcping":
try:
with open(config_path, 'r') as f:
config = json.load(f)
address = config['outbounds'][0]['settings']['vnext'][0]['address']
ping_cmd = ['ping', '-n', '1', '-w', '1000', address] if self.os_sys == "win" else ['ping', '-c', '1', '-W', '1', address]
result = subprocess.run(ping_cmd, capture_output=True, text=True)

if os.name == 'nt':
result = subprocess.Popen(ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, creationflags=subprocess.CREATE_NO_WINDOW)
else:
result = subprocess.Popen(ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = result.communicate()
if result.returncode == 0:
time_p = result.stdout.split('time=')[1].split()[0]
return f"{time_p}ms"
time_p = stdout.split('time=')[1].split()[0]
return f"{time_p}"
else:
return "Timeout"

except Exception as e:
return f"Error: {str(e)}"
elif ping_type == "Real-delay" :

elif ping_type == "Real-delay":
if self.xray_process:
self.stop_xray()

self.run_xray(config_path)
time.sleep(2)
try :
try:
s_time = time.time()
response = requests.get('http://gstatic.com/generate_204', proxies={"http":f"http://127.0.0.1:1080"})
response = requests.get('http://gstatic.com/generate_204', proxies={"http": "http://127.0.0.1:1080"})
e_time = time.time()
if 200 <= response.status_code < 300:
delay_ms = (e_time - s_time) * 1000
return f"{delay_ms:.2f} ms"
else:
return "Timeout"
except :
self.stop_xray()
return "Timeout"
if response.status_code <300 and response.status_code > 199:
self.stop_xray()
return e_time - s_time
else:
return f"Timeout"
finally:
self.stop_xray()
return "Timeout"


def run_xray(self, config_path):
try:
Expand Down
88 changes: 43 additions & 45 deletions GUI-ver/flet-ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,23 @@ def create_config_tile_with_ping(self, config, profile):
config_name = ft.Text(config, size=16, color=ft.colors.WHITE if self.page.theme_mode == ft.ThemeMode.DARK else ft.colors.BLACK)
ping_text = ft.Text("Ping: -", size=16, color=ft.colors.WHITE if self.page.theme_mode == ft.ThemeMode.DARK else ft.colors.BLACK)

return ft.Container(
content=ft.Row(
[
ft.Container(
content=config_name,
padding=ft.padding.all(10),
bgcolor=ft.colors.LIGHT_BLUE if is_selected else ft.colors.TRANSPARENT,
expand=1,
border_radius=10
),
ft.Container(
content=ping_text,
padding=ft.padding.all(10),
expand=0,
border_radius=10
)
],
alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
return ft.ListTile(
title=ft.Container(
content=ft.Text(config, size=16),
bgcolor=ft.colors.LIGHT_BLUE if is_selected else ft.colors.TRANSPARENT,
padding=ft.padding.all(10),
animate=ft.animation.Animation(duration=300, curve=ft.AnimationCurve.EASE_IN_OUT),
border_radius=10
),
on_click=lambda _, c=config, p=profile: self.select_config(c, p),
on_hover=lambda e: self.on_hover_row(e, config),
data={'config_name': config_name, 'ping_text': ping_text}
trailing=ft.Container(
content=ping_text,
padding=ft.padding.all(10)
),
selected=is_selected,
on_click=lambda _, c=config, p=profile: self.select_config(c, p)
)


# "0" = cancel does not exist
# "1" = cancel showed
# "2" = camcel clicked
Expand All @@ -235,9 +228,9 @@ def ping_all_configs(self, profile, config_list):
def ping_worker():
self.cancel_real_delay_stat = "1"
for control in config_list.controls:
if isinstance(control, ft.Container):
config_name = control.data['config_name'].value
ping_text = control.data['ping_text']
if isinstance(control, ft.ListTile):
config_name = control.title.content.value
ping_text = control.trailing.content
config_num = config_name.split("-")[0].strip()
result = self.backend.ping_config(profile, config_num , self.ping_type)

Expand All @@ -255,19 +248,16 @@ def ping_worker():

threading.Thread(target=ping_worker, daemon=True).start()



def on_hover_row(self, e, config):
container = e.control
if e.data == "true":
container.bgcolor = ft.colors.BLUE_GREY
container.border_radius = 10
else:
container.bgcolor = ft.colors.LIGHT_BLUE if config == self.selected_config else ft.colors.TRANSPARENT
container.border_radius = 10
self.page.update()


# def on_hover_row(self, e, config):
# container = e.control
# if e.data == "true" and container.bgcolor != ft.colors.BLUE_GREY:
# container.bgcolor = ft.colors.BLUE_GREY
# container.border_radius = 10
# self.page.update()
# elif e.data == "false" and container.bgcolor != (ft.colors.LIGHT_BLUE if config == self.selected_config else ft.colors.TRANSPARENT):
# container.bgcolor = ft.colors.LIGHT_BLUE if config == self.selected_config else ft.colors.TRANSPARENT
# container.border_radius = 10
# self.page.update()

def select_config(self, config, profile):
self.selected_config = config
Expand All @@ -281,23 +271,34 @@ def select_config(self, config, profile):
self.refresh_profile_tab(profile)

def refresh_profile_tab(self, profile):
if profile == "all" :
if profile == "all":
profiles = self.backend.get_profiles()
for profile in profiles :
for profile in profiles:
for tab in self.tabs.tabs:
if tab.text == profile:
configs = self.backend.get_configs(profile)
config_list = tab.content.controls[1]
config_list.controls = [self.create_config_tile_with_ping(config, profile) for config in configs]

for tab in self.tabs.tabs:
if tab.text == profile:
configs = self.backend.get_configs(profile)
config_list = tab.content.controls[1]
config_list.controls = [self.create_config_tile_with_ping(config, profile) for config in configs]
for control in config_list.controls:
config_name = control.title.content.value
# change color to selcted
if config_name == self.selected_config:
control.title.content.bgcolor = ft.colors.LIGHT_BLUE
else:
control.title.content.bgcolor = ft.colors.TRANSPARENT
# just add new config
for config in configs:
if not any(control.title.content.value == config for control in config_list.controls):
config_list.controls.append(self.create_config_tile_with_ping(config, profile))

break
self.page.update()


def show_import_dialog(self, e):
def import_sub(e):
name = name_field.value
Expand Down Expand Up @@ -385,10 +386,7 @@ def update_subscription(self , profile) :
self.refresh_profile_tab(profile)
def delete_subscription(self , profile) :
self.backend.delete_subscription(profile)
for tab in self.tabs.tabs:
if tab.text == profile:
self.tabs.tabs.remove(tab)
break
self.tabs.tabs = [tab for tab in self.tabs.tabs if tab.text != profile]

def main(page: ft.Page):
XrayClientUI(page)
Expand Down

0 comments on commit 7891849

Please sign in to comment.