Skip to content

Commit

Permalink
fix: crash rash while asleep
Browse files Browse the repository at this point in the history
  • Loading branch information
Maasea committed Jun 19, 2023
1 parent 4f08b98 commit 88277fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
20 changes: 17 additions & 3 deletions src/drawtray.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
from PySide2.QtCore import Qt, QRect, QRectF, QSize, QPointF
from PySide2.QtGui import QPixmap, QPainter, QFont, QColor, QPen, QFontMetrics
from PySide2.QtWidgets import QApplication
from enum import Enum
import darkdetect


class IconStyle(Enum):
NUM = 0
RECT = 1


class TrayIcon:

def __init__(self, IconType='Tray'):
self.painter = None
self.bgWidth = 128
self.bgHeight = 128
self.pixmap = QPixmap(self.bgWidth, self.bgHeight)
self.pixmap.fill(Qt.transparent)
self.painter = QPainter(self.pixmap)
if IconType == 'Tray':
self._color = Qt.white if darkdetect.isDark() else Qt.black
else:
Expand Down Expand Up @@ -48,13 +54,17 @@ def drawNumIcon(self, battery, isCharge):
self.drawChargeIcon(26)

default_size = 62
min_size = 30
ft = QFont("Microsoft YaHei", default_size)

while True:
ft.setPointSize(default_size)
ft_rect = QFontMetrics(ft).boundingRect(str(battery))

if ft_rect.width() <= self.pixmap.width() and ft_rect.height() <= self.pixmap.height():
break
if default_size < min_size:
break
else:
default_size -= 3

Expand Down Expand Up @@ -86,9 +96,13 @@ def drawRectIcon(self, battery, isCharge):
self.drawChargeIcon()
self.painter.end()

def draw(self, battery, isCharge, style=0):
if style == 0:
def draw(self, battery, isCharge, style=IconStyle.NUM):
self.painter = QPainter(self.pixmap)

if style == IconStyle.NUM:
self.drawNumIcon(battery, isCharge)
else:
self.drawRectIcon(battery, isCharge)

del self.painter
return self.pixmap
11 changes: 6 additions & 5 deletions src/ui.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import time
import resource
from drawtray import TrayIcon
from drawtray import TrayIcon, IconStyle
from storage import DEVICE
from controller import Rebt
from PySide2.QtUiTools import QUiLoader
Expand Down Expand Up @@ -143,12 +143,13 @@ def generateIcon(self, battery, isCharge):
widget = TrayIcon('Widget')

if battery == -1:
return self.defaultIcon, widget.draw(0, isCharge, 1)
return self.defaultIcon, widget.draw(0, isCharge, IconStyle.RECT)

widgetIcon = widget.draw(battery, isCharge, 1)
widgetIcon = widget.draw(battery, isCharge, IconStyle.RECT)

tray_style = self.rebt.config.getDefault("trayStyle")
trayIcon = tray.draw(battery, isCharge, tray_style)
icon_style = IconStyle(self.rebt.config.getDefault("trayStyle"))

trayIcon = tray.draw(battery, isCharge, icon_style)
return trayIcon, widgetIcon

def updateBatteryInfo(self):
Expand Down

0 comments on commit 88277fa

Please sign in to comment.