Skip to content

Commit

Permalink
feat(update-check): enhance update check functionality with reload an…
Browse files Browse the repository at this point in the history
…d event handling

Added methods for reloading Windows and Winget updates in the UpdateCheckService. Updated the UpdateCheckWidget to handle update signals more efficiently by consolidating signal handlers. Introduced threading for update reloads and improved mouse event handling for user interactions, allowing users to reload updates or open relevant settings via mouse clicks. This enhances user experience by providing more responsive and interactive update management.
  • Loading branch information
amnweb committed Oct 27, 2024
1 parent 03f516f commit 7f71e5c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/core/utils/widgets/update_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ def winget_update_timer_callback(self):
update_info = self.get_winget_update()
self.emit_event('winget_update', update_info)
threading.Timer(self.winget_update_interval, self.winget_update_timer_callback).start()

def windows_update_reload(self):
update_info = self.get_windows_update()
self.emit_event('windows_update', update_info)

def winget_update_reload(self):
update_info = self.get_winget_update()
self.emit_event('winget_update', update_info)

def emit_event(self, event_name, data):
self.event_service.emit_event(event_name, data)

Expand Down
94 changes: 65 additions & 29 deletions src/core/widgets/yasb/update_check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import re
import shutil
import subprocess
import threading
from core.widgets.base import BaseWidget
from core.validation.widgets.yasb.update_check import VALIDATION_SCHEMA
from PyQt6.QtWidgets import QLabel, QHBoxLayout, QWidget
Expand All @@ -18,11 +21,9 @@ class UpdateCheckWidget(BaseWidget):
windows_update_signal = pyqtSignal(object)
winget_update_signal = pyqtSignal(object)

def __init__(
self,
windows_update: dict[str, str],
winget_update: dict[str, str]
):
TOOLTIP_STYLE = """QToolTip { padding:4px;color: #cdd6f4;font-size:12px; background-color: #1e1e2e; border: 1px solid #313244;border-radius: 8px; }"""

def __init__(self, windows_update: dict[str, str], winget_update: dict[str, str]):
super().__init__(class_name="update-check-widget")
self._event_service = EventService()

Expand All @@ -34,26 +35,24 @@ def __init__(

self._winget_update_enabled = self._winget_update['enabled']
self._winget_update_label = self._winget_update['label']

self.windows_update_data = 0
self.winget_update_data = 0

# Use the main layout directly
self._create_dynamically_label(self._winget_update_label, self._windows_update_label)

self.windows_update_signal.connect(self._on_windows_update_signal)
self.windows_update_signal.connect(self._on_update_signal('windows'))
self._event_service.register_event("windows_update", self.windows_update_signal)

self.winget_update_signal.connect(self._on_winget_update_signal)
self.winget_update_signal.connect(self._on_update_signal('winget'))
self._event_service.register_event("winget_update", self.winget_update_signal)

self.check_and_hide()

self._update_label('winget', 0, [])
self._update_label('windows', 0, [])
self.check_and_hide_widget()

def _create_dynamically_label(self, windows_label: str, winget_label: str):
def process_content(label_text, label_type):
# Create a new container for each set of labels
container = QWidget()
container_layout = QHBoxLayout()
container_layout.setSpacing(0)
Expand All @@ -62,9 +61,7 @@ def process_content(label_text, label_type):
class_name = "windows" if label_type == "windows" else "winget"
container.setProperty("class", f"widget-container {class_name}")
self.widget_layout.addWidget(container)
# Initially hide the container
container.hide()
# Split label text into parts, separating only span tags
label_parts = re.split(r'(<span.*?>.*?</span>)', label_text)
label_parts = [part for part in label_parts if part]
widgets = []
Expand All @@ -86,7 +83,7 @@ def process_content(label_text, label_type):
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
container_layout.addWidget(label)
widgets.append(label)

label.mousePressEvent = self.handle_mouse_events(label_type)
return container, widgets

if self._winget_update_enabled:
Expand Down Expand Up @@ -117,28 +114,67 @@ def _update_label(self, widget_type, data, names):
part = part.strip()
if part and widget_index < len(active_widgets) and isinstance(active_widgets[widget_index], QLabel):
if '<span' in part and '</span>' in part:
# This part is an icon
icon = re.sub(r'<span.*?>|</span>', '', part).strip()
active_widgets[widget_index].setText(icon)
else:
# This part is text, replace {count} with actual data
formatted_text = part.format(count=data)
active_widgets[widget_index].setText(formatted_text)
active_widgets[widget_index].setToolTip("\n".join(names))
active_widgets[widget_index].setStyleSheet("""QToolTip { padding:4px;color: #cdd6f4;font-size:12px; background-color: #1e1e2e; border: 1px solid #313244;border-radius: 8px; }""")
active_widgets[widget_index].setStyleSheet(self.TOOLTIP_STYLE)
widget_index += 1

def _on_windows_update_signal(self, data):
self.windows_update_data = data['count']
self._update_label('windows', self.windows_update_data, data['names'])
self.check_and_hide()

def _on_winget_update_signal(self, data):
self.winget_update_data = data['count']
self._update_label('winget', self.winget_update_data, data['names'])
self.check_and_hide()

def check_and_hide(self):
def _on_update_signal(self, widget_type):
def handler(data):
if widget_type == 'windows':
self.windows_update_data = data['count']
self._update_label('windows', self.windows_update_data, data['names'])
elif widget_type == 'winget':
self.winget_update_data = data['count']
self._update_label('winget', self.winget_update_data, data['names'])
self.check_and_hide_widget()
return handler

def reload_widget(self, widget_type, event=None):
self.hide_container(widget_type)
def run_update():
if widget_type == 'windows':
UpdateCheckService().windows_update_reload()
elif widget_type == 'winget':
UpdateCheckService().winget_update_reload()
update_thread = threading.Thread(target=run_update)
update_thread.start()

def open_console(self, event=None):
powershell_path = shutil.which('pwsh') or shutil.which('powershell') or 'powershell.exe'
command = f'start "Winget Upgrade" "{powershell_path}" -NoExit -Command "winget upgrade --all"'
subprocess.Popen(command, shell=True)
self.hide_container('winget')

def open_windows_update(self, event=None):
subprocess.Popen('start ms-settings:windowsupdate', shell=True)
self.hide_container('windows')

def handle_mouse_events(self, label_type):
def event_handler(event):
if event.button() == Qt.MouseButton.LeftButton:
if label_type == 'windows':
self.open_windows_update(event)
elif label_type == 'winget':
self.open_console(event)
elif event.button() == Qt.MouseButton.RightButton:
self.reload_widget(label_type, event)
return event_handler

def hide_container(self, container):
if container == 'windows':
self.windows_update_data = 0
self._update_label('windows', 0, [])
elif container == 'winget':
self.winget_update_data = 0
self._update_label('winget', 0, [])
self.check_and_hide_widget()

def check_and_hide_widget(self):
if self.windows_update_data == 0 and self.winget_update_data == 0:
self.hide()
else:
Expand Down

0 comments on commit 7f71e5c

Please sign in to comment.