Skip to content

Commit

Permalink
simplify Notifications API
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Aug 8, 2024
1 parent 295aff8 commit 76d2100
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
52 changes: 16 additions & 36 deletions src/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,21 @@ static void GetForHwnd(HWND hwnd, Vec<NotificationWnd*>& v) {
}
}

// notification can be removed due to a timeout or manual closing
bool IsNotificationValid(NotificationWnd* wnd) {
bool exists = gNotifs.Contains(wnd);
return exists;
}

static void GetForSameHwnd(NotificationWnd* wnd, Vec<NotificationWnd*>& v) {
HWND parent = GetParent(wnd->hwnd);
GetForHwnd(parent, v);
}

// TODO: better name
bool NotifsContains(NotificationWnd* wnd) {
return gNotifs.Contains(wnd);
}

static void NotifsRelayout(Vec<NotificationWnd*>& wnds) {
void RelayoutNotifications(HWND hwnd) {
Vec<NotificationWnd*> wnds;
HWND parent = HwndGetParent(hwnd);
GetForHwnd(parent, wnds);
if (wnds.IsEmpty()) {
return;
}
Expand All @@ -118,7 +122,7 @@ static void NotifsRelayout(Vec<NotificationWnd*>& wnds) {
int topLeftMargin = DpiScale(hwndCanvas, kTopLeftMargin);
int dyPadding = DpiScale(hwndCanvas, kPadding);
int y = topLeftMargin;
for (auto* wnd : wnds) {
for (NotificationWnd* wnd : wnds) {
Rect rect = WindowRect(wnd->hwnd);
rect = MapRectToWindow(rect, HWND_DESKTOP, hwndCanvas);
if (IsUIRightToLeft()) {
Expand All @@ -133,31 +137,13 @@ static void NotifsRelayout(Vec<NotificationWnd*>& wnds) {
}
}

static void NotifsRelayout(NotificationWnd* wnd) {
Vec<NotificationWnd*> wnds;
GetForSameHwnd(wnd, wnds);
NotifsRelayout(wnds);
}

void NotifsRemove(Vec<NotificationWnd*>& wnds, NotificationWnd* wnd) {
static void NotifsRemoveNotification(NotificationWnd* wnd) {
int pos = gNotifs.Remove(wnd);
if (pos < 0) {
return;
}
NotifsRelayout(wnd);
}

static void NotifsRemoveNotification(Vec<NotificationWnd*>& wnds, NotificationWnd* wnd) {
if (wnds.Contains(wnd)) {
NotifsRemove(wnds, wnd);
delete wnd;
}
}

static void NotifsRemoveNotification(NotificationWnd* wnd) {
Vec<NotificationWnd*> wnds;
GetForSameHwnd(wnd, wnds);
NotifsRemoveNotification(wnds, wnd);
RelayoutNotifications(wnd->hwnd);
delete wnd;
}

int GetWndX(NotificationWnd* wnd) {
Expand Down Expand Up @@ -465,7 +451,7 @@ static int NotifsRemoveForGroup(Vec<NotificationWnd*>& wnds, Kind groupId) {
}
}
for (auto* wnd : toRemove) {
NotifsRemoveNotification(wnds, wnd);
NotifsRemoveNotification(wnd);
}
return toRemove.Size();
}
Expand All @@ -477,7 +463,7 @@ static void NotifsAdd(Vec<NotificationWnd*>& wnds, NotificationWnd* wnd, Kind gr
}
wnd->groupId = groupId;
gNotifs.Append(wnd);
NotifsRelayout(wnd);
RelayoutNotifications(wnd->hwnd);
}

static void NotifsAdd(NotificationWnd* wnd, Kind groupId) {
Expand Down Expand Up @@ -573,9 +559,3 @@ void AddNotification(NotificationWnd* wnd, Kind kind) {
bool NotificationExists(NotificationWnd* wnd) {
return gNotifs.Contains(wnd);
}

void RelayoutNotifications(HWND hwnd) {
Vec<NotificationWnd*> wnds;
GetForHwnd(hwnd, wnds);
NotifsRelayout(wnds);
}
1 change: 1 addition & 0 deletions src/Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ void RelayoutNotifications(HWND hwnd);
NotificationWnd* ShowNotification(const NotificationCreateArgs& args);
NotificationWnd* ShowTemporaryNotification(HWND hwnd, const char* msg, int timeoutMs = kNotifDefaultTimeOut);
NotificationWnd* ShowWarningNotification(HWND hwndParent, const char* msg, int timeoutMs);
bool IsNotificationValid(NotificationWnd*);

0 comments on commit 76d2100

Please sign in to comment.