| 51 | } |
| 52 | |
| 53 | std::optional<std::wstring> Window::title() const |
| 54 | { |
| 55 | SetLastError(NO_ERROR); |
| 56 | const int titleSize = GetWindowTextLength(m_WindowHandle); |
| 57 | if (!titleSize) |
| 58 | { |
| 59 | if (const DWORD lastErr = GetLastError(); lastErr != NO_ERROR) |
| 60 | { |
| 61 | HresultHandle(HRESULT_FROM_WIN32(lastErr), spdlog::level::info, L"Getting size of title of a window failed."); |
| 62 | return std::nullopt; |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | return std::make_optional<std::wstring>(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // We're assuming that a window won't change title between the previous call and this. |
| 71 | // But it very well could. It'll either be smaller and waste a bit of RAM, or have |
| 72 | // GetWindowText trim it. |
| 73 | std::wstring windowTitle; |
| 74 | bool failed = false; |
| 75 | windowTitle.resize_and_overwrite(titleSize, [hwnd = m_WindowHandle, &failed](wchar_t* data, std::size_t count) |
| 76 | { |
| 77 | SetLastError(NO_ERROR); |
| 78 | const int copiedChars = GetWindowText(hwnd, data, static_cast<int>(count) + 1); |
| 79 | if (!copiedChars) |
| 80 | { |
| 81 | if (const DWORD lastErr = GetLastError(); lastErr != NO_ERROR) |
| 82 | { |
| 83 | HresultHandle(HRESULT_FROM_WIN32(lastErr), spdlog::level::info, L"Getting title of a window failed."); |
| 84 | failed = true; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return copiedChars; |
| 89 | }); |
| 90 | |
| 91 | if (!failed) |
| 92 | { |
| 93 | return { std::move(windowTitle) }; |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | return std::nullopt; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | std::optional<std::wstring> Window::classname() const |
| 102 | { |
no outgoing calls
no test coverage detected