Skip to content

Commit

Permalink
Merge pull request #897 from reupen/direct-write-status-bar
Browse files Browse the repository at this point in the history
Use DirectWrite to render text in the status bar
  • Loading branch information
reupen authored Jun 20, 2024
2 parents e884384 + 961ab9c commit f2fa4c7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

### Features

- Text in the status bar is now rendered using DirectWrite.
[[#897](https://github.com/reupen/columns_ui/pull/897)]

This includes colour font support on Windows 8.1 and newer (allowing the use
of, for example, colour emojis).

- Ctrl+Tab and Shift+Ctrl+Tab can now be used in Tab stack and Playlist tabs to
switch to the next and previous tab respectively.
[[#817](https://github.com/reupen/columns_ui/pull/817)]
Expand Down
8 changes: 4 additions & 4 deletions foo_ui_columns/foo_ui_columns.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>DebugFastLink</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down Expand Up @@ -160,7 +160,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>DebugFastLink</GenerateDebugInformation>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down Expand Up @@ -198,7 +198,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>Debug</GenerateDebugInformation>
<DataExecutionPrevention>
</DataExecutionPrevention>
Expand Down Expand Up @@ -233,7 +233,7 @@
<Culture>0x0809</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Urlmon.lib;comctl32.lib;shell32.lib;shlwapi.lib;gdiplus.lib;../foobar2000/shared/shared-$(Platform).lib;uxtheme.lib;Dwmapi.lib;usp10.lib;Windowscodecs.lib;Dwrite.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>Debug</GenerateDebugInformation>
<DataExecutionPrevention>
</DataExecutionPrevention>
Expand Down
62 changes: 46 additions & 16 deletions foo_ui_columns/status_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct StatusBarState {
std::string track_count_text;
std::string volume_text;
wil::unique_hfont font;
std::optional<uih::direct_write::Context> direct_write_ctx;
std::optional<uih::direct_write::TextFormat> direct_write_text_format;
wil::unique_hbitmap lock_bitmap;
wil::unique_hicon lock_icon;
std::unique_ptr<colours::dark_mode_notifier> dark_mode_notifier;
Expand All @@ -37,7 +39,25 @@ void on_status_font_change()

state->lock_icon.reset();
state->lock_bitmap.reset();
state->font.reset(fb2k::std_api_get<fonts::manager>()->get_font(font_client_status_guid));

LOGFONT log_font{};
fb2k::std_api_get<fonts::manager>()->get_font(font_client_status_guid, log_font);

const LOGFONT log_font_unscaled = fb2k::std_api_get<fonts::manager_v2>()->get_client_font(font_client_status_guid);

state->font.reset(CreateFontIndirect(&log_font));
state->direct_write_text_format.reset();

try {
if (!state->direct_write_ctx)
state->direct_write_ctx = std::make_optional<uih::direct_write::Context>();
}
CATCH_LOG();

if (state->direct_write_ctx) {
state->direct_write_text_format = state->direct_write_ctx->create_text_format_with_fallback(
log_font_unscaled, gsl::narrow_cast<float>(-log_font_unscaled.lfHeight));
}

SetWindowFont(g_status, state->font.get(), TRUE);

Expand Down Expand Up @@ -172,25 +192,36 @@ std::string get_playlist_lock_text()

int calculate_volume_size(const char* p_text)
{
return win32_helpers::status_bar_get_text_width(g_status, p_text);
if (!state->direct_write_text_format)
return 0;

return state->direct_write_text_format->measure_text_width(p_text);
}

int calculate_selected_length_size(const char* p_text)
{
return std::max(win32_helpers::status_bar_get_text_width(g_status, p_text),
win32_helpers::status_bar_get_text_width(g_status, "0d 00:00:00"));
if (!state->direct_write_text_format)
return 0;

return std::max(state->direct_write_text_format->measure_text_width(p_text),
state->direct_write_text_format->measure_text_width(L"0d 00:00:00"));
}

int calculate_selected_count_size(const char* p_text)
{
return std::max(win32_helpers::status_bar_get_text_width(g_status, p_text),
win32_helpers::status_bar_get_text_width(g_status, "0,000 tracks"));
if (!state->direct_write_text_format)
return 0;

return std::max(state->direct_write_text_format->measure_text_width(p_text),
state->direct_write_text_format->measure_text_width(L"0,000 tracks"));
}

int calculate_playback_lock_size(const char* p_text)
{
return uih::get_font_height(state->font.get()) - 2_spx + 2_spx
+ win32_helpers::status_bar_get_text_width(g_status, p_text);
const auto icon_width = uih::get_font_height(state->font.get()) - 2_spx + 2_spx;

return icon_width
+ (state->direct_write_text_format ? state->direct_write_text_format->measure_text_width(p_text) : 0);
}

std::string get_selected_length_text(unsigned dp = 0)
Expand Down Expand Up @@ -402,15 +433,15 @@ void draw_item_content(const HDC dc, const StatusBarPartID part_id, const std::s
const auto text_colour = get_colour(dark::ColourID::StatusBarText, colours::is_dark_mode_active());

if (part_id == StatusBarPartID::PlaybackInformation) {
text_out_colours_tab(dc, text.data(), gsl::narrow<int>(text.size()), 0, 0, &rc, FALSE, text_colour, true, false,
uih::ALIGN_LEFT);
if (state->direct_write_text_format)
uih::direct_write::text_out_columns_and_colours(
*state->direct_write_text_format, dc, text, 0, 0, rc, false, text_colour, true, true, uih::ALIGN_LEFT);
return;
}

const auto font_height = uih::get_dc_font_height(dc);
int x = rc.left;
const int y = rc.top + (wil::rect_height(rc) - font_height) / 2;
const auto icon_size = font_height - 2_spx;
int x = rc.left;

if (part_id == StatusBarPartID::PlaylistLock) {
const auto icon_y = rc.top + (wil::rect_height(rc) - icon_size) / 2;
Expand All @@ -436,10 +467,9 @@ void draw_item_content(const HDC dc, const StatusBarPartID part_id, const std::s
x += icon_size + 2_spx;
}

const auto utf16_text = pfc::stringcvt::string_wide_from_utf8(text.data(), text.size());
SetTextColor(dc, text_colour);
SetBkMode(dc, TRANSPARENT);
ExtTextOutW(dc, x, y, ETO_CLIPPED, &rc, utf16_text, gsl::narrow<UINT>(utf16_text.length()), nullptr);
if (state->direct_write_text_format)
uih::direct_write::text_out_columns_and_colours(
*state->direct_write_text_format, dc, text, x - rc.left, 0, rc, false, text_colour, false, false);
}

} // namespace
Expand Down

0 comments on commit f2fa4c7

Please sign in to comment.