Skip to content

Commit 56799f3

Browse files
committed
refactor(notification_banner): remove defensive patterns
1 parent 808d434 commit 56799f3

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

bec_widgets/widgets/containers/main_window/addons/notification_center/notification_banner.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,14 @@ def __init__(
150150
body_lbl.setWordWrap(True)
151151

152152
self.time_lbl = QtWidgets.QLabel()
153+
self._showing_absolute = False
153154
self._update_relative_time()
154155
# enable absolute timestamp on hover
155156
self.time_lbl.setCursor(QtCore.Qt.PointingHandCursor)
156157
self.time_lbl.installEventFilter(self)
157-
self._showing_absolute = False
158+
159+
# shared ID assigned by NotificationCentre.add_notification
160+
self.notification_id: str | None = None
158161

159162
self.close_btn = QtWidgets.QPushButton("✕")
160163
self.close_btn.setObjectName("toastCloseBtn")
@@ -247,15 +250,14 @@ def __init__(
247250
# lifetime progress animation
248251
self._lifetime = max(0, lifetime_ms) # 0 → never expire
249252
self._progress_anim: QtCore.QPropertyAnimation | None = None
253+
# flag to indicate this toast has fully expired (progress bar finished)
254+
self._expired = False
250255

251256
if self._lifetime > 0:
252257
self._start_progress_animation()
253258
else:
254259
self.progress.hide()
255260

256-
# flag to indicate this toast has fully expired (progress bar finished)
257-
self._expired = False
258-
259261
# ------------------------------------------------------------------
260262
def _connect_to_theme_change(self):
261263
"""Connect this toast to the global theme‑updated signal."""
@@ -337,9 +339,6 @@ def kind(self, value):
337339
}}
338340
""")
339341
self.apply_theme(self._theme)
340-
# keep injected gradient in sync
341-
if getattr(self, "_hg_enabled", False):
342-
self._hg_cols[0] = self._accent_color
343342

344343
@SafeProperty(str)
345344
def traceback(self):
@@ -456,7 +455,7 @@ def apply_theme(self, theme: str | None = None):
456455
########################################
457456

458457
def _update_relative_time(self) -> None:
459-
if getattr(self, "_showing_absolute", False):
458+
if self._showing_absolute:
460459
return # don't overwrite while user is viewing absolute time
461460
seconds = int((datetime.now() - self.created).total_seconds())
462461
if seconds < 10:
@@ -497,7 +496,7 @@ def enterEvent(self, event):
497496
Pause the countdown while the cursor is over the toast, and reset the
498497
elapsed time and progress bar to full width.
499498
"""
500-
if getattr(self, "_expired", False):
499+
if self._expired:
501500
return super().enterEvent(event)
502501
self._hover = True
503502
if self._progress_anim is not None:
@@ -511,10 +510,10 @@ def leaveEvent(self, event):
511510
Resume the countdown when the cursor leaves, continuing from the
512511
paused progress rather than restarting.
513512
"""
514-
if getattr(self, "_expired", False):
513+
if self._expired:
515514
return super().leaveEvent(event)
516515
self._hover = False
517-
if self._lifetime > 0 and not self._expired:
516+
if self._lifetime > 0:
518517
self._start_progress_animation()
519518
super().leaveEvent(event)
520519

@@ -530,13 +529,11 @@ def paintEvent(self, event):
530529
painter.fillPath(path, self._base_color)
531530

532531
# accent gradient, fades to transparent
533-
grad = QtGui.QLinearGradient(
534-
0, 0, self.width() * getattr(self, "_gradient_width_factor", 0.70), 0
535-
)
532+
grad = QtGui.QLinearGradient(0, 0, self.width() * self._gradient_width_factor, 0)
536533
accent = QtGui.QColor(self._accent_color)
537-
if getattr(self, "_theme", "dark") == "light":
534+
if self._theme == "light":
538535
accent = accent.darker(115)
539-
accent.setAlpha(getattr(self, "_accent_alpha", 50))
536+
accent.setAlpha(self._accent_alpha)
540537
grad.setColorAt(0.0, accent)
541538
fade = QtGui.QColor(self._accent_color)
542539
fade.setAlpha(0)
@@ -590,8 +587,7 @@ def _emit_counts(self):
590587
def __init__(self, parent=None, *, fixed_width: int = 420, margin: int = 16):
591588
super().__init__(parent=parent)
592589
self.setObjectName("NotificationCentre")
593-
app = QApplication.instance()
594-
self._theme = getattr(getattr(app, "theme", None), "theme", "dark").lower()
590+
self._theme = get_theme_name()
595591

596592
self.setWidgetResizable(True)
597593
# transparent background so only the toast cards are visible
@@ -756,7 +752,7 @@ def add_notification(
756752
def remove_notification(self, notification_id: str) -> None:
757753
"""Close a specific notification in this centre if present."""
758754
for toast in list(self.toasts):
759-
if getattr(toast, "notification_id", None) == notification_id:
755+
if toast.notification_id == notification_id:
760756
self._hide_notification(toast)
761757

762758
# ------------------------------------------------------------------

0 commit comments

Comments
 (0)