diff --git a/CMakeLists.txt b/CMakeLists.txt index 69fee6d..993de90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -292,6 +292,8 @@ add_test(NAME clipboard-history-layout COMMAND KSwitcherInputTests --history-lay set_tests_properties(clipboard-history-layout PROPERTIES RUN_SERIAL TRUE TIMEOUT 20) add_test(NAME tray-input-pause COMMAND KSwitcherInputTests --input-pause) add_test(NAME screenshot-ui-regressions COMMAND KSwitcherScreenshotUITests) +add_test(NAME screenshot-editor-layout COMMAND KSwitcherScreenshotUITests --layout-only) +set_tests_properties(screenshot-editor-layout PROPERTIES RUN_SERIAL TRUE TIMEOUT 20) add_test(NAME screenshot-save-regressions COMMAND KSwitcherScreenshotUITests --save-only) add_test(NAME updater-regressions COMMAND powershell.exe -NoProfile -ExecutionPolicy Bypass -File "${CMAKE_CURRENT_SOURCE_DIR}/tests/update.ps1" -Executable "$") set_tests_properties(native-regressions PROPERTIES WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/src/screenshot.cpp b/src/screenshot.cpp index fc54d56..579942b 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -1,6 +1,7 @@ #include "screenshot.h" #include "stroke_improvement.h" #include "ocr.h" +#include "ui_palette.h" #include "../resources/resource.h" #include #include @@ -55,6 +56,9 @@ bool confirmScreenshotClose(HWND owner) return SUCCEEDED(result) && button==IDYES; } constexpr int ToolIds[]{27,10,11,22,23,24,25,36,37,38,21,19,26,20,12,18,13}; +constexpr int EditorStatus = 50, EditorZoom = 51; +constexpr int EditorOptionsTop = 44, EditorPanelHeight = 118, EditorFooterHeight = 30; +constexpr int EditorToolDividers[]{50, 357, 464, 502}; struct Surface { HDC dc = CreateCompatibleDC(nullptr); @@ -209,7 +213,8 @@ struct Capture int zoomWheelDelta=0; std::vector marks; ScreenshotHistory history; - HWND editor{}, inlineText{}; + HWND editor{}, inlineText{}, tooltips{}; + WindowPalette palette; HFONT inlineFont{}; Mark textDraft{}; int textIndex=-1; @@ -222,7 +227,7 @@ struct Capture std::vector beforeDrag; HFONT font{}; HICON toolIcons[3][15]{}; - int iconSize{}; + int iconSize{}, fontDpi{}; HCURSOR pencilCursor{}; HCURSOR drawingCursors[std::size(CursorTools)]{}; int pencilCursorSize{}; @@ -268,10 +273,22 @@ struct Capture EnableWindow(GetDlgItem(editor,18),history.canRedo()); } }; +void setEditorStatus(Capture &c, std::wstring text) +{ + if (text == c.statusText) return; + c.statusText = std::move(text); + const auto status = GetDlgItem(c.editor, EditorStatus); + SetWindowTextW(status, c.statusText.c_str()); + if (c.tooltips && status) + { + TOOLINFOW tool{sizeof(tool), TTF_IDISHWND, c.editor, reinterpret_cast(status)}; + tool.lpszText = c.statusText.data(); + SendMessageW(c.tooltips, TTM_UPDATETIPTEXTW, 0, reinterpret_cast(&tool)); + } +} void updateStatus(Capture &c) { - auto status=GetDlgItem(c.editor,50); - if(!status) return; + if (!GetDlgItem(c.editor, EditorStatus)) return; std::wstring text; auto part=c.dragging?c.part:c.hoveredPart; if(c.hoveredTool && !c.dragging) { @@ -301,13 +318,11 @@ void updateStatus(Capture &c) else if(c.shape==ScreenshotShape::Arc) text=L"Arc (U): drag to draw a half ellipse | Handles = resize / rotate | V = pointer"; else if(c.arrow) text=L"Drag to draw | Ctrl+drag over objects = linked connector | V = pointer"; else text=L"Number (N): click to add | V = select and edit | Ctrl+C = copy screenshot"; - text=L"Zoom: "+std::to_wstring(int(std::lround(100.0*(c.view.right-c.view.left)/c.image->width)))+ - L"% (Ctrl +/- / wheel; Ctrl+0: fit; middle drag: pan) | "+text; - if(text!=c.statusText) { - c.statusText=std::move(text); - SendMessageW(status,SB_SETTEXTW,0,(LPARAM)c.statusText.c_str()); - SendMessageW(status,SB_SETTIPTEXTW,0,(LPARAM)c.statusText.c_str()); - } + const auto zoom = L"Zoom " + std::to_wstring(MulDiv(c.view.right - c.view.left, 100, c.image->width)) + L"%"; + wchar_t current[32]{}; + GetDlgItemTextW(c.editor, EditorZoom, current, std::size(current)); + if (zoom != current) SetDlgItemTextW(c.editor, EditorZoom, zoom.c_str()); + setEditorStatus(c, std::move(text)); } RECT region(POINT a, POINT b) { @@ -781,39 +796,48 @@ void layout(HWND w, Capture &c) c.toolIcons[variant][i]=(HICON)LoadImageW(c.app.instance,MAKEINTRESOURCEW(i<9?300+variant*10+i:400+variant*10+i-9),IMAGE_ICON,iconSize,iconSize,0); } } - if (c.font) DeleteObject(c.font); - c.font = CreateFontW(-MulDiv(14,dpi,96),0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,DEFAULT_CHARSET, - OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY,DEFAULT_PITCH,L"Segoe UI"); - c.unit = std::max(1, MulDiv(44, dpi, 96)); - auto status=GetDlgItem(w,50); - SendMessageW(status,WM_SETFONT,(WPARAM)c.font,TRUE); - SendMessageW(status,WM_SIZE,0,0); - RECT statusRect{}; GetWindowRect(status,&statusRect); - c.statusHeight=statusRect.bottom-statusRect.top; - r.bottom-=c.statusHeight; - const int positions[]{8,44,80,116,152,188,224,260,296,332,368,404,440,476,524,560,608}; - static_assert(std::size(positions)==std::size(ToolIds)); - for (size_t i = 0; i < std::size(ToolIds); ++i) + HFONT previousFont{}; + const bool fontChanged = dpi != c.fontDpi || !c.font; + if (fontChanged) { - SendMessageW(GetDlgItem(w,ToolIds[i]),WM_SETFONT,(WPARAM)c.font,TRUE); - MoveWindow(GetDlgItem(w,ToolIds[i]),MulDiv(positions[i],dpi,96),MulDiv(6,dpi,96), - MulDiv(32,dpi,96),MulDiv(32,dpi,96),TRUE); + c.fontDpi = dpi; + previousFont = c.font; + c.font = CreateFontW(-MulDiv(14,dpi,96),0,0,0,FW_NORMAL,FALSE,FALSE,FALSE,DEFAULT_CHARSET, + OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY,DEFAULT_PITCH,L"Segoe UI"); } - SendDlgItemMessageW(w,28,WM_SETFONT,(WPARAM)c.font,TRUE); - MoveWindow(GetDlgItem(w,28),MulDiv(648,dpi,96),MulDiv(6,dpi,96), - MulDiv(78,dpi,96),MulDiv(32,dpi,96),TRUE); - c.panelHeight=c.unit+MulDiv(80,dpi,96); - for(int id:{30,31,32,33,34,35,41,42,43,44}) SendDlgItemMessageW(w,id,WM_SETFONT,(WPARAM)c.font,TRUE); - MoveWindow(GetDlgItem(w,42),MulDiv(12,dpi,96),c.unit+MulDiv(44,dpi,96),MulDiv(128,dpi,96),MulDiv(24,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,41),MulDiv(142,dpi,96),c.unit+MulDiv(40,dpi,96),MulDiv(200,dpi,96),MulDiv(30,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,43),MulDiv(350,dpi,96),c.unit+MulDiv(44,dpi,96),MulDiv(150,dpi,96),MulDiv(24,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,44),MulDiv(516,dpi,96),c.unit+MulDiv(40,dpi,96),MulDiv(210,dpi,96),MulDiv(30,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,32),MulDiv(12,dpi,96),c.unit+MulDiv(6,dpi,96),MulDiv(64,dpi,96),MulDiv(24,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,30),MulDiv(80,dpi,96),c.unit+MulDiv(2,dpi,96),MulDiv(150,dpi,96),MulDiv(180,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,33),MulDiv(248,dpi,96),c.unit+MulDiv(6,dpi,96),MulDiv(40,dpi,96),MulDiv(24,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,31),MulDiv(294,dpi,96),c.unit+MulDiv(2,dpi,96),MulDiv(170,dpi,96),MulDiv(180,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,34),MulDiv(480,dpi,96),c.unit+MulDiv(2,dpi,96),MulDiv(28,dpi,96),MulDiv(28,dpi,96),TRUE); - MoveWindow(GetDlgItem(w,35),MulDiv(516,dpi,96),c.unit+MulDiv(2,dpi,96),MulDiv(28,dpi,96),MulDiv(28,dpi,96),TRUE); + const auto dip = [dpi](int n) { return MulDiv(n, dpi, 96); }; + if (fontChanged) SendMessageW(c.tooltips, TTM_SETMAXTIPWIDTH, 0, dip(530)); + const auto place = [&](int id, int x, int y, int width, int height) + { + const auto child = GetDlgItem(w, id); + if (fontChanged || reinterpret_cast(SendMessageW(child, WM_GETFONT, 0, 0)) != c.font) + SendMessageW(child, WM_SETFONT, reinterpret_cast(c.font), TRUE); + MoveWindow(child, x, y, width, height, TRUE); + }; + c.unit = std::max(1, dip(44)); + c.statusHeight = dip(EditorFooterHeight); + r.bottom -= c.statusHeight; + place(EditorZoom, dip(16), r.bottom + 1, dip(89), c.statusHeight - 1); + place(EditorStatus, dip(120), r.bottom + 1, std::max(0, static_cast(r.right) - dip(136)), c.statusHeight - 1); + const int positions[]{14,52,86,120,154,188,222,256,290,324,362,396,430,468,506,540}; + static_assert(std::size(positions) + 1 == std::size(ToolIds)); + for (size_t i = 0; i < std::size(positions); ++i) + place(ToolIds[i], dip(positions[i]), dip(6), dip(32), dip(32)); + const int copyLeft = std::max(dip(580), static_cast(r.right) - dip(168)); + place(13, copyLeft, dip(6), dip(66), dip(32)); + place(28, copyLeft + dip(76), dip(6), dip(76), dip(32)); + c.panelHeight = dip(EditorPanelHeight); + place(32, dip(16), dip(53), dip(62), dip(26)); + place(30, dip(80), dip(51), dip(150), dip(180)); + place(33, dip(246), dip(53), dip(40), dip(26)); + place(31, dip(288), dip(51), dip(170), dip(180)); + place(34, dip(470), dip(50), dip(28), dip(28)); + place(35, dip(506), dip(50), dip(28), dip(28)); + place(42, dip(16), dip(84), dip(122), dip(26)); + place(41, dip(140), dip(82), dip(198), dip(30)); + place(43, dip(350), dip(84), dip(154), dip(26)); + place(44, dip(514), dip(82), dip(208), dip(29)); + if (previousFont) DeleteObject(previousFont); syncLineOptions(w,c); c.canvas={0,c.panelHeight,r.right,std::max(LONG(c.panelHeight+1),r.bottom)}; double ratio = c.zoom*std::min({1.0, double(std::max(1L,r.right-16))/c.image->width, @@ -1015,8 +1039,11 @@ void drawTool(const DRAWITEMSTRUCT &item, const Capture &c) POINT mouse; GetCursorPos(&mouse); ScreenToClient(item.hwndItem,&mouse); bool hover=PtInRect(&r,mouse) && !disabled; bool pressed=item.itemState&ODS_SELECTED; - FillRect(dc,&r,static_cast(GetStockObject(WHITE_BRUSH))); - COLORREF fill=pressed?RGB(219,234,254):active?RGB(239,246,255):hover?RGB(241,245,249):RGB(255,255,255); + const bool option = item.CtlID == 34 || item.CtlID == 35; + FillRect(dc, &r, option ? c.palette.brush() : GetSysColorBrush(COLOR_WINDOW)); + const COLORREF fill = c.palette.highContrast ? GetSysColor(active || pressed ? COLOR_HIGHLIGHT : COLOR_WINDOW) : + pressed ? RGB(219,234,254) : active ? RGB(232,242,254) : hover ? RGB(241,245,249) : + option ? c.palette.color() : GetSysColor(COLOR_WINDOW); auto brush=CreateSolidBrush(fill); auto pen=CreatePen(PS_SOLID,1,fill); auto oldPen=SelectObject(dc,pen),oldBrush=SelectObject(dc,brush); @@ -1028,14 +1055,36 @@ void drawTool(const DRAWITEMSTRUCT &item, const Capture &c) if(item.CtlID==36 || item.CtlID==37 || item.CtlID==38) { int cx=(r.left+r.right)/2,cy=(r.top+r.bottom)/2,s=c.iconSize/2; auto trianglePen=CreatePen(PS_SOLID,std::max(1,MulDiv(2,GetDpiForWindow(item.hwndItem),96)), - disabled?RGB(148,163,184):active?RGB(37,99,235):RGB(51,65,85)); + c.palette.highContrast ? GetSysColor(disabled ? COLOR_GRAYTEXT : + active || pressed ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT) : + disabled ? RGB(148,163,184) : active ? RGB(37,99,235) : RGB(51,65,85)); auto previous=SelectObject(dc,trianglePen); if(item.CtlID==38) Arc(dc,cx-s,cy-s,cx+s,cy+3*s,cx+s,cy+s,cx-s,cy+s); else if(item.CtlID==36) { POINT vertices[]{{cx,cy-s},{cx+s,cy+s},{cx-s,cy+s},{cx,cy-s}}; Polyline(dc,vertices,4); } else { POINT vertices[]{{cx-s/2,cy-s},{cx+s/2,cy-s},{cx+s,cy+s},{cx-s,cy+s},{cx-s/2,cy-s}}; Polyline(dc,vertices,5); } SelectObject(dc,previous); DeleteObject(trianglePen); } - if(icon) DrawIconEx(dc,(r.right-c.iconSize)/2,(r.bottom-c.iconSize)/2,icon,c.iconSize,c.iconSize,0,nullptr,DI_NORMAL); + const auto foreground = GetSysColor(disabled ? COLOR_GRAYTEXT : c.palette.highContrast && (active || pressed) ? + COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT); + const auto dpi = GetDpiForWindow(item.hwndItem); + const int iconX = item.CtlID == 13 ? MulDiv(7, dpi, 96) : (r.right - c.iconSize) / 2; + const int iconY = (r.bottom - c.iconSize) / 2; + if (icon && c.palette.highContrast && item.CtlID != 27) + { + const auto brush = CreateSolidBrush(foreground); + DrawStateW(dc, brush, nullptr, reinterpret_cast(icon), 0, iconX, iconY, c.iconSize, c.iconSize, DST_ICON | DSS_MONO); + DeleteObject(brush); + } + else if (icon) DrawIconEx(dc, iconX, iconY, icon, c.iconSize, c.iconSize, 0, nullptr, DI_NORMAL); + if (item.CtlID == 13) + { + const SavedDc saved(dc); + SelectObject(dc, c.font); + SetBkMode(dc, TRANSPARENT); + SetTextColor(dc, foreground); + RECT label{MulDiv(30, dpi, 96), r.top, r.right, r.bottom}; + DrawTextW(dc, L"Copy", -1, &label, DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX); + } if(item.CtlID==20) { auto color=c.selected>=0?c.marks[c.selected].color:c.currentColor; auto swatch=CreateSolidBrush(color); @@ -1096,6 +1145,7 @@ void save(HWND w, Capture &c, bool saveAs=false, { if(!saveAs && !c.savedPath.empty()) { saveToFile(w,c,c.savedPath); + setEditorStatus(c, L"Saved: " + c.savedPath.wstring()); return; } wchar_t filename[32768]{}; @@ -1125,8 +1175,7 @@ void save(HWND w, Capture &c, bool saveAs=false, if(lower(std::filesystem::path(filename).extension().wstring())!=L".png") throw std::runtime_error("Choose a filename ending in .png for the screenshot"); saveToFile(w,c,filename); - c.statusText=L"Saved: "+c.savedPath.wstring(); - SendDlgItemMessageW(w,50,SB_SETTEXTW,0,(LPARAM)c.statusText.c_str()); + setEditorStatus(c, L"Saved: " + c.savedPath.wstring()); } void edit(HWND w, Capture &c) { @@ -1151,10 +1200,12 @@ void edit(HWND w, Capture &c) SetWindowLongPtrW(w,GWL_STYLE,WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN); auto title=L"KSwitcher — Screenshot #"+std::to_wstring(nextEditorIndex++); SetWindowTextW(w,localBuildTitle(title).c_str()); - INITCOMMONCONTROLSEX statusControls{sizeof(statusControls),ICC_BAR_CLASSES}; - InitCommonControlsEx(&statusControls); - CreateWindowExW(0,STATUSCLASSNAMEW,L"",WS_CHILD|WS_VISIBLE|SBARS_TOOLTIPS, - 0,0,0,0,w,(HMENU)50,c.app.instance,nullptr); + c.palette.refresh(); + for (int id : {EditorStatus, EditorZoom}) + CreateWindowW(L"STATIC", L"", WS_CHILD | WS_VISIBLE | SS_CENTERIMAGE | SS_ENDELLIPSIS | SS_NOTIFY, + 0, 0, 0, 0, w, reinterpret_cast(static_cast(id)), c.app.instance, nullptr); + INITCOMMONCONTROLSEX bars{sizeof(bars), ICC_BAR_CLASSES}; + InitCommonControlsEx(&bars); const wchar_t *labels[]{L"Pointer (V): select and edit objects", L"Number (N)", L"Arrow (A); Ctrl+drag between objects to link", L"Straight line (L); Ctrl+drag between objects to link", L"Rectangle (R)", L"Circle (O)", L"Ellipse (E)", L"Triangle (G): drag to draw; drag each vertex to reshape", L"Trapezoid (B): drag to draw", L"Arc (U): drag to draw a half ellipse; use handles to resize or rotate", L"Freehand line (F); Shift for 45-degree steps; move >50 px off line to turn; Ctrl to draw over objects", L"Highlight (H); Ctrl+drag to paint over an object", L"Text (T); double-click or F2 to edit", L"Color — selected object and new objects", L"Undo (Ctrl+Z)", L"Redo (Ctrl+Y / Ctrl+Shift+Z)", L"Copy (Ctrl+C)"}; static_assert(std::size(labels)==std::size(ToolIds)); @@ -1163,11 +1214,19 @@ void edit(HWND w, Capture &c) SetWindowSubclass(saveButton,toolHover,1,0); auto tips=CreateWindowExW(WS_EX_TOPMOST,TOOLTIPS_CLASSW,nullptr,WS_POPUP|TTS_ALWAYSTIP|TTS_NOPREFIX, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,w,nullptr,c.app.instance,nullptr); + c.tooltips = tips; + for (int id : {EditorStatus, EditorZoom}) + { + TOOLINFOW tool{sizeof(tool), TTF_IDISHWND | TTF_SUBCLASS, w, + reinterpret_cast(GetDlgItem(w, id))}; + tool.lpszText = const_cast(id == EditorZoom ? + L"Ctrl+wheel or Ctrl+plus/minus to zoom. Ctrl+0 to fit. Middle-drag to pan." : L""); + SendMessageW(tips, TTM_ADDTOOLW, 0, reinterpret_cast(&tool)); + } for (size_t i=0;i(L"Arrow label (F2 / double-click); drag label to reposition"); SendMessageW(tips,TTM_ADDTOOLW,0,(LPARAM)&fontTip); - INITCOMMONCONTROLSEX sliders{sizeof(sliders),ICC_BAR_CLASSES}; InitCommonControlsEx(&sliders); CreateWindowW(TRACKBAR_CLASSW,L"Pencil smoothing",WS_CHILD|WS_VISIBLE|WS_TABSTOP|TBS_NOTICKS, 0,0,0,0,w,(HMENU)41,c.app.instance,nullptr); SendDlgItemMessageW(w,41,TBM_SETRANGE,TRUE,MAKELPARAM(0,100)); SendDlgItemMessageW(w,41,TBM_SETPAGESIZE,0,10); - for(int id:{42,43}) CreateWindowW(L"STATIC",L"",WS_CHILD|WS_VISIBLE,0,0,0,0,w,(HMENU)(INT_PTR)id,c.app.instance,nullptr); + for(int id:{42,43}) CreateWindowW(L"STATIC",L"",WS_CHILD|WS_VISIBLE|SS_CENTERIMAGE,0,0,0,0,w,(HMENU)(INT_PTR)id,c.app.instance,nullptr); CreateWindowW(L"BUTTON",L"Apply to all strokes",WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON, 0,0,0,0,w,(HMENU)44,c.app.instance,nullptr); c.syncHistory(); @@ -1261,8 +1319,20 @@ LRESULT CALLBACK captureProc(HWND w, UINT msg, WPARAM wp, LPARAM lp) if((wp>=10 && wp<=13) || (wp>=18 && wp<=27) || wp==34 || wp==35 || wp==36 || wp==37 || wp==38) { drawTool(*reinterpret_cast(lp),*c); return TRUE; } break; case WM_CTLCOLORSTATIC: - SetBkColor((HDC)wp,RGB(255,255,255)); SetTextColor((HDC)wp,RGB(51,65,85)); - return (LRESULT)GetStockObject(WHITE_BRUSH); + { + const auto dc = reinterpret_cast(wp); + const int id = GetDlgCtrlID(reinterpret_cast(lp)); + SetBkColor(dc, c->palette.color()); + SetTextColor(dc, id == EditorZoom ? GetSysColor(COLOR_WINDOWTEXT) : c->palette.secondaryText()); + return reinterpret_cast(c->palette.brush()); + } + case WM_SYSCOLORCHANGE: case WM_SETTINGCHANGE: case WM_DWMCOLORIZATIONCOLORCHANGED: + if (c->editing) + { + c->palette.refresh(msg == WM_DWMCOLORIZATIONCOLORCHANGED ? std::optional(wp) : std::nullopt); + RedrawWindow(w, nullptr, nullptr, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN); + } + break; case WM_ERASEBKGND: return 1; case WM_SETCURSOR: if ((HWND)wp==w && LOWORD(lp)==HTCLIENT) { captureCursor(w,*c); return TRUE; } @@ -1840,18 +1910,22 @@ LRESULT CALLBACK captureProc(HWND w, UINT msg, WPARAM wp, LPARAM lp) const SavedDc savedFrame(buffer); const auto &dirty=ps.rcPaint; IntersectClipRect(buffer,dirty.left,dirty.top,dirty.right,dirty.bottom); - FillRect(buffer,&client,GetSysColorBrush(COLOR_APPWORKSPACE)); - RECT toolbar{0,0,client.right,c->panelHeight}; - FillRect(buffer,&toolbar,static_cast(GetStockObject(WHITE_BRUSH))); - auto divider=CreatePen(PS_SOLID,1,RGB(226,232,240)); - auto previous=SelectObject(buffer,divider); - MoveToEx(buffer,0,c->panelHeight-1,nullptr); LineTo(buffer,client.right,c->panelHeight-1); - int dpi=GetDpiForWindow(w); - for(int x:{440,524}) { - MoveToEx(buffer,MulDiv(x,dpi,96),MulDiv(13,dpi,96),nullptr); - LineTo(buffer,MulDiv(x,dpi,96),MulDiv(31,dpi,96)); - } - SelectObject(buffer,previous); DeleteObject(divider); + const auto dpi = GetDpiForWindow(w); + const auto dip = [dpi](int n) { return MulDiv(n, dpi, 96); }; + const RECT toolbar{0, 0, client.right, dip(EditorOptionsTop)}; + const RECT options{0, toolbar.bottom, client.right, c->panelHeight}; + const RECT footer{0, c->canvas.bottom, client.right, client.bottom}; + FillRect(buffer, &toolbar, GetSysColorBrush(COLOR_WINDOW)); + FillRect(buffer, &options, c->palette.brush()); + FillRect(buffer, &footer, c->palette.brush()); + fillSurface(buffer, c->canvas, c->palette.highContrast ? GetSysColor(COLOR_APPWORKSPACE) : RGB(224, 232, 239)); + const auto edge = c->palette.edge(); + fillSurface(buffer, {0, options.top, client.right, options.top + 1}, edge); + fillSurface(buffer, {0, options.bottom - 1, client.right, options.bottom}, edge); + fillSurface(buffer, {0, footer.top, client.right, footer.top + 1}, edge); + fillSurface(buffer, {dip(108), footer.top + dip(7), dip(108) + 1, footer.bottom - dip(7)}, edge); + for (int x : EditorToolDividers) + fillSurface(buffer, {dip(x), dip(13), dip(x) + 1, dip(31)}, edge); const auto preview=shapePreview(*c); const auto *previewMark=c->dragging && c->part==MarkPart::None? ((c->highlight || c->freehand)?&c->stroke:&preview):nullptr; @@ -1898,6 +1972,10 @@ LRESULT CALLBACK captureProc(HWND w, UINT msg, WPARAM wp, LPARAM lp) { const SavedDc savedCanvas(buffer); IntersectClipRect(buffer,c->canvas.left,c->canvas.top,c->canvas.right,c->canvas.bottom); + auto frame = c->view; + InflateRect(&frame, 1, 1); + SetDCBrushColor(buffer, c->palette.highContrast ? GetSysColor(COLOR_WINDOWTEXT) : RGB(155, 174, 191)); + FrameRect(buffer, &frame, reinterpret_cast(GetStockObject(DC_BRUSH))); StretchBlt(buffer,c->view.left,c->view.top,c->view.right-c->view.left,c->view.bottom-c->view.top, source->dc,0,0,source->width,source->height,SRCCOPY); drawHandles(buffer,*c); diff --git a/src/ui.cpp b/src/ui.cpp index bef3a93..a192f09 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -16,7 +16,7 @@ #include #include #include -#include +#include "ui_palette.h" #include #include #include @@ -45,53 +45,6 @@ HFONT headingFont(HWND w) { return font(w, 27, FW_SEMIBOLD); } -void fillSurface(HDC dc, const RECT &bounds, COLORREF color) -{ - SetDCBrushColor(dc, color); - FillRect(dc, &bounds, reinterpret_cast(GetStockObject(DC_BRUSH))); -} -class WindowPalette -{ -public: - WindowPalette() = default; - WindowPalette(const WindowPalette &) = delete; - WindowPalette &operator=(const WindowPalette &) = delete; - ~WindowPalette() { if (brush_) DeleteObject(brush_); } - - void refresh(std::optional colorization = std::nullopt) - { - HIGHCONTRASTW contrast{sizeof(contrast)}; - highContrast = SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof(contrast), &contrast, 0) && - (contrast.dwFlags & HCF_HIGHCONTRASTON); - const auto background = GetSysColor(COLOR_WINDOW); - if (brush_) DeleteObject(brush_); - brush_ = nullptr; - if (highContrast) return; - - DWORD argb{}; - BOOL opaque{}; - if (!colorization && SUCCEEDED(DwmGetColorizationColor(&argb, &opaque))) - colorization = argb; - const auto accent = colorization ? RGB(*colorization >> 16, *colorization >> 8, *colorization) - : GetSysColor(COLOR_HIGHLIGHT); - // Keep system window text readable even with a saturated accent. - const auto tint = [](int base, int accentChannel) { return (base * 92 + accentChannel * 8 + 50) / 100; }; - color_ = RGB(tint(GetRValue(background), GetRValue(accent)), - tint(GetGValue(background), GetGValue(accent)), - tint(GetBValue(background), GetBValue(accent))); - brush_ = CreateSolidBrush(color_); - } - - COLORREF color() const { return brush_ ? color_ : GetSysColor(COLOR_WINDOW); } - HBRUSH brush() const { return brush_ ? brush_ : GetSysColorBrush(COLOR_WINDOW); } - COLORREF edge() const { return highContrast ? GetSysColor(COLOR_WINDOWTEXT) : RGB(200, 213, 222); } - COLORREF secondaryText() const { return highContrast ? GetSysColor(COLOR_WINDOWTEXT) : RGB(70, 91, 112); } - bool highContrast{}; - -private: - COLORREF color_{}; - HBRUSH brush_{}; -}; #ifndef KS_CONFIG_APP constexpr int HistoryHeading = 119, HistoryDescription = 120, HistoryHeaderHeight = 73; constexpr int HistoryWidth = 760, HistoryHeight = 530, HistoryMargin = 24, HistoryFooterTop = 482; diff --git a/src/ui_palette.h b/src/ui_palette.h new file mode 100644 index 0000000..030ad11 --- /dev/null +++ b/src/ui_palette.h @@ -0,0 +1,55 @@ +#pragma once +#include +#include +#include + +namespace ks +{ +inline void fillSurface(HDC dc, const RECT &bounds, COLORREF color) +{ + SetDCBrushColor(dc, color); + FillRect(dc, &bounds, reinterpret_cast(GetStockObject(DC_BRUSH))); +} +class WindowPalette +{ +public: + WindowPalette() = default; + WindowPalette(const WindowPalette &) = delete; + WindowPalette &operator=(const WindowPalette &) = delete; + ~WindowPalette() { if (brush_) DeleteObject(brush_); } + + void refresh(std::optional colorization = std::nullopt) + { + HIGHCONTRASTW contrast{sizeof(contrast)}; + highContrast = SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof(contrast), &contrast, 0) && + (contrast.dwFlags & HCF_HIGHCONTRASTON); + const auto background = GetSysColor(COLOR_WINDOW); + if (brush_) DeleteObject(brush_); + brush_ = nullptr; + if (highContrast) return; + + DWORD argb{}; + BOOL opaque{}; + if (!colorization && SUCCEEDED(DwmGetColorizationColor(&argb, &opaque))) + colorization = argb; + const auto accent = colorization ? RGB(*colorization >> 16, *colorization >> 8, *colorization) + : GetSysColor(COLOR_HIGHLIGHT); + // Keep system window text readable even with a saturated accent. + const auto tint = [](int base, int accentChannel) { return (base * 92 + accentChannel * 8 + 50) / 100; }; + color_ = RGB(tint(GetRValue(background), GetRValue(accent)), + tint(GetGValue(background), GetGValue(accent)), + tint(GetBValue(background), GetBValue(accent))); + brush_ = CreateSolidBrush(color_); + } + + COLORREF color() const { return brush_ ? color_ : GetSysColor(COLOR_WINDOW); } + HBRUSH brush() const { return brush_ ? brush_ : GetSysColorBrush(COLOR_WINDOW); } + COLORREF edge() const { return highContrast ? GetSysColor(COLOR_WINDOWTEXT) : RGB(200, 213, 222); } + COLORREF secondaryText() const { return highContrast ? GetSysColor(COLOR_WINDOWTEXT) : RGB(70, 91, 112); } + bool highContrast{}; + +private: + COLORREF color_{}; + HBRUSH brush_{}; +}; +} // namespace ks diff --git a/tests/screenshot_ui.cpp b/tests/screenshot_ui.cpp index 1a886b7..93f42e9 100644 --- a/tests/screenshot_ui.cpp +++ b/tests/screenshot_ui.cpp @@ -87,7 +87,84 @@ int main(int argc, char **argv) auto window=CreateWindowW(cls.lpszClassName,L"Editor test",WS_POPUP,0,0,320,240,app.window,nullptr,app.instance,&c); auto require=[](bool ok,const char *message){ if(!ok) throw std::runtime_error(message); }; require(window!=nullptr,"editor creation"); - ks::edit(window,c); ShowWindow(window,SW_HIDE); + ks::edit(window,c); + if (argc > 1 && std::string_view(argv[1]) == "--layout-only") + { + const auto dip = [window](int n) { return MulDiv(n, GetDpiForWindow(window), 96); }; + const auto bounds = [&](int id) { + RECT result{}; + GetWindowRect(GetDlgItem(window, id), &result); + MapWindowPoints(nullptr, window, reinterpret_cast(&result), 2); + return result; + }; + MINMAXINFO limits{}; + SendMessageW(window, WM_GETMINMAXINFO, 0, reinterpret_cast(&limits)); + const auto checkLayout = [&] { + RECT client{}; + GetClientRect(window, &client); + const auto copy = bounds(13), save = bounds(28), undo = bounds(18); + const auto zoom = bounds(ks::EditorZoom), hint = bounds(ks::EditorStatus); + require(undo.right < copy.left && copy.right < save.left && + save.right <= client.right - dip(15) && copy.top == save.top, + "Copy and Save stay together at the right edge of the toolbar"); + require(bounds(44).bottom < c.canvas.top && c.canvas.bottom < zoom.top && + zoom.right < hint.left && hint.right <= client.right - dip(15), + "options, canvas, zoom and contextual hint have distinct bounds"); + }; + const UINT flags = SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE; + SetWindowPos(window, nullptr, 0, 0, limits.ptMinTrackSize.x, dip(610), flags); + checkLayout(); + const auto narrowSave = bounds(28); + SetWindowPos(window, nullptr, 0, 0, dip(1000), dip(660), flags); + checkLayout(); + require(bounds(28).left > narrowSave.left, "primary actions follow the right edge while resizing"); + c.image = std::make_unique(640, 360); + ks::fillSurface(c.image->dc, {0, 0, 640, 360}, RGB(255, 255, 255)); + ks::fillSurface(c.image->dc, {0, 0, 155, 360}, RGB(232, 241, 248)); + ks::fillSurface(c.image->dc, {155, 0, 640, 62}, RGB(246, 249, 252)); + { + const ks::SavedDc saved(c.image->dc); + SelectObject(c.image->dc, c.font); + SetBkMode(c.image->dc, TRANSPARENT); + SetTextColor(c.image->dc, RGB(28, 52, 76)); + RECT header{175, 20, 630, 50}; + DrawTextW(c.image->dc, L"Clipboard history", -1, &header, DT_SINGLELINE | DT_NOPREFIX); + } + for (int y : {92, 156, 220, 284}) + { + ks::fillSurface(c.image->dc, {175, y, 222, y + 40}, RGB(224, 235, 246)); + ks::fillSurface(c.image->dc, {239, y + 5, 459, y + 10}, RGB(193, 207, 220)); + ks::fillSurface(c.image->dc, {239, y + 22, 380, y + 27}, RGB(226, 233, 240)); + } + ks::Mark mark{{165, 78}, {613, 145}, 0}; + mark.color = RGB(26, 106, 207); + mark.shape = ks::ScreenshotShape::Rectangle; + c.marks = {mark}; + ks::layout(window, c); + RedrawWindow(window, nullptr, nullptr, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN); + RECT client{}; + GetClientRect(window, &client); + const auto dc = GetDC(window); + const auto preview = ks::screenshotClip(dc, client.right, client.bottom, {}); + const auto footerColor = GetPixel(dc, dip(5), c.canvas.bottom + dip(5)); + const auto canvasColor = GetPixel(dc, dip(5), c.canvas.top + dip(5)); + ReleaseDC(window, dc); + require(footerColor == c.palette.color(), "footer uses the Settings surface color"); + if (!c.palette.highContrast) + require(canvasColor != footerColor, "image canvas and footer are visually distinct"); + wchar_t zoomText[32]{}; + GetDlgItemTextW(window, ks::EditorZoom, zoomText, std::size(zoomText)); + require(std::wstring_view(zoomText).starts_with(L"Zoom ") && !c.statusText.empty(), + "zoom is visible separately from the contextual hint"); + ks::atomicWrite(std::filesystem::path(ks::executable()).parent_path() / L"screenshot-editor.png", + ks::encodePng(ks::decodeImage(preview))); + DestroyWindow(window); + DestroyWindow(app.window); + CoUninitialize(); + std::cout << "Passed screenshot editor layout checks\n"; + return 0; + } + ShowWindow(window,SW_HIDE); require(GetWindow(window,GW_OWNER)==nullptr && (GetWindowLongPtrW(window,GWL_EXSTYLE)&WS_EX_APPWINDOW), "editor is an independent Alt+Tab application window"); require(GetDlgItem(window,50)!=nullptr && c.statusHeight>0,"status bar is created");