Skip to content

Commit

Permalink
feat(monitor): add smoother usage bar changes
Browse files Browse the repository at this point in the history
  • Loading branch information
leafspark committed Aug 17, 2024
1 parent adc8f4b commit b62e8fc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/GPUMonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
AMD_GPU_NOT_SUPPORTED,
)

from ui_update import animate_bar


class SimpleGraph(QGraphicsView):
def __init__(self, title, parent=None):
Expand Down Expand Up @@ -143,7 +145,7 @@ def update_gpu_info(self):
gpu_usage = utilization.gpu
vram_usage = (memory.used / memory.total) * 100

self.gpu_bar.setValue(int(vram_usage))
animate_bar(self, self.gpu_bar, int(vram_usage))
self.gpu_label.setText(
GPU_USAGE_FORMAT.format(
gpu_usage,
Expand Down
34 changes: 32 additions & 2 deletions src/ui_update.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from PySide6.QtCore import QTimer

from Localizations import *
import psutil
from error_handling import show_error
Expand All @@ -11,14 +13,42 @@ def update_model_info(logger, self, model_info):
def update_system_info(self):
ram = psutil.virtual_memory()
cpu = psutil.cpu_percent()
self.ram_bar.setValue(int(ram.percent))

# Smooth transition for RAM bar
animate_bar(self, self.ram_bar, ram.percent)

# Smooth transition for CPU bar
animate_bar(self, self.cpu_bar, cpu)

self.ram_bar.setFormat(
RAM_USAGE_FORMAT.format(
ram.percent, ram.used // 1024 // 1024, ram.total // 1024 // 1024
)
)
self.cpu_label.setText(CPU_USAGE_FORMAT.format(cpu))
self.cpu_bar.setValue(int(cpu))

def animate_bar(self, bar, target_value):
current_value = bar.value()
difference = target_value - current_value

if abs(difference) <= 1: # Avoid animation for small changes
bar.setValue(target_value)
return

step = 1 if difference > 0 else -1 # Increment or decrement based on difference
timer = QTimer(self)
timer.timeout.connect(lambda: _animate_step(bar, target_value, step, timer))
timer.start(10) # Adjust the interval for animation speed

def _animate_step(bar, target_value, step, timer):
current_value = bar.value()
new_value = current_value + step

if (step > 0 and new_value > target_value) or (step < 0 and new_value < target_value):
bar.setValue(target_value)
timer.stop()
else:
bar.setValue(new_value)


def update_download_progress(self, progress):
Expand Down

0 comments on commit b62e8fc

Please sign in to comment.