Skip to content

Commit

Permalink
feat(widget): add image_icon_size option to ApplicationsWidget for cu…
Browse files Browse the repository at this point in the history
…stomizable icon scaling

- Introduced 'image_icon_size' parameter to allow dynamic scaling of application icons.
- Updated ApplicationsWidget to utilize the new parameter for rendering icons.
- Enhanced user interaction with a blinking effect on label click for better feedback.
  • Loading branch information
amnweb committed Dec 29, 2024
1 parent d5b4c4c commit eec1e27
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/core/validation/widgets/yasb/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
'required': False,
'default': ""
},
'image_icon_size': {
'type': 'integer',
'required': False,
'default': 14
},
'app_list': {
'type': 'list',
'schema': {
Expand Down
55 changes: 48 additions & 7 deletions src/core/widgets/yasb/applications.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import os
from core.widgets.base import BaseWidget
from core.validation.widgets.yasb.applications import VALIDATION_SCHEMA
from PyQt6.QtWidgets import QLabel, QHBoxLayout, QWidget
from PyQt6.QtGui import QCursor
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QLabel, QHBoxLayout, QWidget, QGraphicsOpacityEffect
from PyQt6.QtGui import QCursor, QPixmap
from PyQt6.QtCore import Qt, QTimer
import subprocess
import logging
from core.utils.win32.system_function import function_map

class ApplicationsWidget(BaseWidget):
validation_schema = VALIDATION_SCHEMA

def __init__(self, label: str, class_name: str,app_list: list[str, dict[str]],container_padding: dict):
def __init__(self, label: str, class_name: str,app_list: list[str, dict[str]], image_icon_size: int, container_padding: dict):
super().__init__(class_name=f"apps-widget {class_name}")
self._label = label
self._apps = app_list
self._padding = container_padding
self._image_icon_size = image_icon_size
# Construct container
self._widget_container_layout: QHBoxLayout = QHBoxLayout()
self._widget_container_layout.setSpacing(0)
Expand All @@ -34,7 +36,12 @@ def _update_label(self):
label = ClickableLabel(self)
label.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
label.setProperty("class", "label")
label.setText(app_data['icon'])
icon = app_data['icon']
if os.path.isfile(icon):
pixmap = QPixmap(icon).scaled(self._image_icon_size, self._image_icon_size)
label.setPixmap(pixmap)
else:
label.setText(icon)
label.data = app_data['launch']
self._widget_container_layout.addWidget(label)
else:
Expand All @@ -53,13 +60,47 @@ def execute_code(self, data):
except Exception as e:
logging.error(f"Exception occurred: {str(e)} \"{data}\"")


class ClickableLabel(QLabel):
def __init__(self, parent=None):
super().__init__(parent)
self.parent_widget = parent
self.data = None
self._opacity_effect = None
self._blink_timer = None

def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton and self.data:
self.parent_widget.execute_code(self.data)
self._blink_on_click()
self.parent_widget.execute_code(self.data)

def _blink_on_click(self, duration=200):
if hasattr(self, '_opacity_effect') and self._opacity_effect is not None:
self._opacity_effect.setOpacity(1.0)
if self._blink_timer.isActive():
self._blink_timer.stop()

self._opacity_effect = QGraphicsOpacityEffect()
self.setGraphicsEffect(self._opacity_effect)
self._opacity_effect.setOpacity(0.4)

self._blink_timer = QTimer()
step = 0
steps = 20
increment = 0.5 / steps

def animate():
nonlocal step
new_opacity = self._opacity_effect.opacity() + increment
if new_opacity >= 1.0:
new_opacity = 1.0
self._opacity_effect.setOpacity(new_opacity)
self._blink_timer.stop()
self._opacity_effect = None
return
self._opacity_effect.setOpacity(new_opacity)
step += 1

self._blink_timer.timeout.connect(animate)
self._blink_timer.start(duration // steps)

0 comments on commit eec1e27

Please sign in to comment.