Skip to content

Commit

Permalink
add shrinkLimit to Notification args; add kNotifAdHoc
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Aug 8, 2024
1 parent 2030db2 commit 295aff8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ using Gdiplus::SolidBrush;
Kind kNotifCursorPos = "cursorPosHelper";
Kind kNotifActionResponse = "responseToAction";
Kind kNotifPageInfo = "pageInfoHelper";
// can have multiple of those
Kind kNotifAdHoc = "notifAdHoc";

constexpr int kPadding = 6;
constexpr int kTopLeftMargin = 8;
Expand Down Expand Up @@ -88,7 +90,7 @@ Vec<NotificationWnd*> gNotifs;

static void GetForHwnd(HWND hwnd, Vec<NotificationWnd*>& v) {
for (auto* wnd : gNotifs) {
HWND parent = GetParent(wnd->hwnd);
HWND parent = HwndGetParent(wnd->hwnd);
if (parent == hwnd) {
v.Append(wnd);
}
Expand Down Expand Up @@ -175,16 +177,16 @@ HWND NotificationWnd::Create(const NotificationCreateArgs& args) {
}

highlight = args.warning;
shrinkLimit = args.shrinkLimit;
if (shrinkLimit < 0.2f) {
ReportIf(shrinkLimit < 0.2f);
shrinkLimit = 1.f;
}
if (args.onRemoved.IsValid()) {
wndRemovedCb = args.onRemoved;
} else {
wndRemovedCb = MkFunc1Void(NotifsRemoveNotification);
}
// TODO: make shrinkLimit an arg
if (kNotifCursorPos == args.groupId) {
shrinkLimit = 0.7f;
}

timeoutMs = args.timeoutMs;

CreateCustomArgs cargs;
Expand Down Expand Up @@ -469,7 +471,8 @@ static int NotifsRemoveForGroup(Vec<NotificationWnd*>& wnds, Kind groupId) {
}

static void NotifsAdd(Vec<NotificationWnd*>& wnds, NotificationWnd* wnd, Kind groupId) {
if (groupId != nullptr) {
bool skipRemove = (groupId == nullptr) || (groupId == kNotifAdHoc);
if (!skipRemove) {
NotifsRemoveForGroup(wnds, groupId);
}
wnd->groupId = groupId;
Expand Down
3 changes: 3 additions & 0 deletions src/Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ struct NotificationWnd;
extern Kind kNotifCursorPos;
extern Kind kNotifActionResponse;
extern Kind kNotifPageInfo;
extern Kind kNotifAdHoc;

using NotificationWndRemoved = Func1<NotificationWnd*>;

Expand All @@ -18,6 +19,7 @@ struct NotificationCreateArgs {
Kind groupId = kNotifActionResponse;
bool warning = false;
int timeoutMs = 0; // if 0 => persists until closed manually
float shrinkLimit = 1.0f;
const char* msg = nullptr;
const char* progressMsg = nullptr;
NotificationWndRemoved onRemoved;
Expand All @@ -26,6 +28,7 @@ struct NotificationCreateArgs {
void NotificationUpdateMessage(NotificationWnd* wnd, const char* msg, int timeoutInMS = 0, bool highlight = false);
void RemoveNotification(NotificationWnd*);
bool RemoveNotificationsForGroup(HWND hwnd, Kind);
bool RemoveNotificationsForHwnd(HWND hwnd);
NotificationWnd* GetNotificationForGroup(HWND hwnd, Kind);
bool UpdateNotificationProgress(NotificationWnd*, int curr, int total);
bool NotificationExists(NotificationWnd*);
Expand Down
5 changes: 4 additions & 1 deletion src/SumatraPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4389,6 +4389,7 @@ static void ToggleCursorPositionInDoc(MainWindow* win) {
NotificationCreateArgs args;
args.hwndParent = win->hwndCanvas;
args.groupId = kNotifCursorPos;
args.shrinkLimit = 0.7f;
args.timeoutMs = 0;
notif = ShowNotification(args);
cursorPosUnit = MeasurementUnit::pt;
Expand Down Expand Up @@ -5758,6 +5759,7 @@ static LRESULT FrameOnCommand(MainWindow* win, HWND hwnd, UINT msg, WPARAM wp, L
NotificationCreateArgs args;
args.hwndParent = win->hwndCanvas;
args.msg = "This is a notification";
args.groupId = kNotifAdHoc;
args.warning = true;
args.timeoutMs = 0;
ShowNotification(args);
Expand All @@ -5766,7 +5768,8 @@ static LRESULT FrameOnCommand(MainWindow* win, HWND hwnd, UINT msg, WPARAM wp, L
{
NotificationCreateArgs args;
args.hwndParent = win->hwndCanvas;
args.msg = "";
args.groupId = kNotifAdHoc;
args.progressMsg = "Progress: %d out of %d";
args.warning = false;
args.timeoutMs = 0;
auto wnd = ShowNotification(args);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/WinUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,10 @@ bool IsCursorOverWindow(HWND hwnd) {
return rcWnd.Contains({pt.x, pt.y});
}

HWND HwndGetParent(HWND hwnd) {
return ::GetParent(hwnd);
}

TempStr HwndGetClassName(HWND hwnd) {
WCHAR buf[512] = {0};
int n = GetClassNameW(hwnd, buf, dimof(buf));
Expand Down
1 change: 1 addition & 0 deletions src/utils/WinUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ HWND HwndSetFocus(HWND hwnd);
bool HwndIsFocused(HWND);
bool IsCursorOverWindow(HWND);

HWND HwndGetParent(HWND hwnd);
TempStr HwndGetClassName(HWND hwnd);
Point HwndGetCursorPos(HWND hwnd);
int MapWindowPoints(HWND, HWND, Point*, int);
Expand Down

0 comments on commit 295aff8

Please sign in to comment.