#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.UI.h>
#ifdef DAY_WINUI
#define DAY_XAML_NS winrt::Microsoft::UI::Xaml
#include <winrt/Microsoft.UI.Xaml.h>
#include <winrt/Microsoft.UI.Xaml.Automation.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
#include <winrt/Microsoft.UI.Xaml.Controls.Primitives.h>
#include <winrt/Microsoft.UI.Xaml.Markup.h>
#include <winrt/Microsoft.UI.Xaml.Media.h>
#else
#define DAY_XAML_NS winrt::Windows::UI::Xaml
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Automation.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.UI.Xaml.Controls.Primitives.h>
#include <winrt/Windows.UI.Xaml.Markup.h>
#include <winrt/Windows.UI.Xaml.Media.h>
#endif
#include <windows.h>
#include <cmath>
#include <cstdint>
#include <limits>
#include <string>
#include <vector>
using namespace winrt;
namespace WF = winrt::Windows::Foundation;
namespace WUX = DAY_XAML_NS;
namespace WUXC = DAY_XAML_NS::Controls;
namespace WUXM = DAY_XAML_NS::Media;
namespace WUXMk = DAY_XAML_NS::Markup;
namespace WUXA = DAY_XAML_NS::Automation;
extern "C" void *day_xaml_box(void *iinspectable_abi);
extern "C" void *day_xaml_unbox(void *handle);
extern "C" int day_xaml_is_dark();
static constexpr double kPad = 16.0;
static constexpr double kRadius = 4.0;
static constexpr wchar_t kXamlNs[] = L"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
static winrt::hstring hs(const char *s) {
if (!s || !*s)
return winrt::hstring{};
int len = MultiByteToWideChar(CP_UTF8, 0, s, -1, nullptr, 0);
if (len <= 1)
return winrt::hstring{};
std::wstring w(static_cast<size_t>(len - 1), L'\0');
MultiByteToWideChar(CP_UTF8, 0, s, -1, w.data(), len);
return winrt::hstring{w};
}
static winrt::Windows::UI::Color color_argb(unsigned int argb) {
winrt::Windows::UI::Color c;
c.A = static_cast<uint8_t>((argb >> 24) & 0xFF);
c.R = static_cast<uint8_t>((argb >> 16) & 0xFF);
c.G = static_cast<uint8_t>((argb >> 8) & 0xFF);
c.B = static_cast<uint8_t>(argb & 0xFF);
return c;
}
static WUXM::SolidColorBrush solid(unsigned int argb) {
return WUXM::SolidColorBrush(color_argb(argb));
}
static WUX::Style theme_style(wchar_t const *target, std::wstring const &setters) {
try {
std::wstring xaml = L"<Style xmlns=\"";
xaml += kXamlNs;
xaml += L"\" TargetType=\"";
xaml += target;
xaml += L"\">";
xaml += setters;
xaml += L"</Style>";
return WUXMk::XamlReader::Load(winrt::hstring{xaml}).as<WUX::Style>();
} catch (...) {
return nullptr;
}
}
static std::wstring setter(wchar_t const *property, wchar_t const *key) {
std::wstring s = L"<Setter Property=\"";
s += property;
s += L"\" Value=\"{ThemeResource ";
s += key;
s += L"}\"/>";
return s;
}
static void apply_chrome(WUXC::Border const &border) {
static WUX::Style style = theme_style(
L"Border", setter(L"Background", L"CardBackgroundFillColorDefaultBrush") +
setter(L"BorderBrush", L"CardStrokeColorDefaultBrush"));
if (style) {
border.Style(style);
return;
}
auto paint = [](WUXC::Border const &b) {
bool dark = day_xaml_is_dark() != 0;
b.Background(solid(dark ? 0x0F'FFFFFFu : 0xB3'FFFFFFu));
b.BorderBrush(solid(dark ? 0x19'000000u : 0x0F'000000u));
};
paint(border);
border.ActualThemeChanged([paint](WUX::FrameworkElement const &sender, WF::IInspectable const &) mutable {
if (auto b = sender.try_as<WUXC::Border>())
paint(b);
});
}
static void apply_secondary(WUXC::TextBlock const &tb) {
static WUX::Style style = theme_style(L"TextBlock",
#ifdef DAY_WINUI
setter(L"Foreground", L"TextFillColorSecondaryBrush")
#else
setter(L"Foreground", L"SystemControlForegroundBaseMediumBrush")
#endif
);
if (style)
tb.Style(style);
else
tb.Foreground(solid(day_xaml_is_dark() != 0 ? 0xC5'FFFFFFu : 0x9E'000000u));
}
static void apply_destructive(WUXC::TextBlock const &tb) {
static WUX::Style style = theme_style(L"TextBlock", setter(L"Foreground", L"SystemFillColorCriticalBrush"));
if (style)
tb.Style(style);
else
tb.Foreground(solid(day_xaml_is_dark() != 0 ? 0xFF'FF99A4u : 0xFF'C42B1Cu));
}
static void apply_glyph_color(WUXC::FontIcon const &icon) {
static WUX::Style style = theme_style(L"FontIcon",
#ifdef DAY_WINUI
setter(L"Foreground", L"TextFillColorSecondaryBrush")
#else
setter(L"Foreground", L"SystemControlForegroundBaseMediumBrush")
#endif
);
if (style)
icon.Style(style);
else
icon.Foreground(solid(day_xaml_is_dark() != 0 ? 0xC5'FFFFFFu : 0x9E'000000u));
}
static void apply_card_button(WUXC::Button const &button) {
button.HorizontalContentAlignment(WUX::HorizontalAlignment::Stretch);
button.VerticalContentAlignment(WUX::VerticalAlignment::Stretch);
button.Padding(WUX::Thickness{0, 0, 0, 0});
button.BorderThickness(WUX::Thickness{1, 1, 1, 1});
try {
button.CornerRadius(WUX::CornerRadius{kRadius, kRadius, kRadius, kRadius});
} catch (...) {
}
static WUX::Style style = theme_style(
L"Button", setter(L"Background", L"CardBackgroundFillColorDefaultBrush") +
setter(L"BorderBrush", L"CardStrokeColorDefaultBrush"));
if (style) {
button.Style(style);
return;
}
auto paint = [](WUXC::Button const &b) {
bool dark = day_xaml_is_dark() != 0;
b.Background(solid(dark ? 0x0F'FFFFFFu : 0xB3'FFFFFFu));
b.BorderBrush(solid(dark ? 0x19'000000u : 0x0F'000000u));
};
paint(button);
button.ActualThemeChanged([paint](WUX::FrameworkElement const &sender, WF::IInspectable const &) mutable {
if (auto b = sender.try_as<WUXC::Button>())
paint(b);
});
}
static void name_card_button(WUXC::Button const &button, winrt::hstring const &header,
winrt::hstring const &description) {
WUXA::AutomationProperties::SetName(button, header.empty() ? description : header);
WUXA::AutomationProperties::SetHelpText(button, header.empty() ? winrt::hstring{} : description);
}
struct Card {
WUXC::Canvas root{nullptr};
WUXC::Button button{nullptr};
WUXC::Border chrome{nullptr};
WUXC::Grid text{nullptr};
};
static Card card_of(void *handle) {
Card c;
try {
WUX::UIElement e{nullptr};
winrt::copy_from_abi(e, day_xaml_unbox(handle));
if (auto b = e.try_as<WUXC::Button>()) {
c.button = b;
c.text = b.Content().try_as<WUXC::Grid>();
return c;
}
c.root = e.try_as<WUXC::Canvas>();
if (!c.root)
return c;
auto kids = c.root.Children();
if (kids.Size() >= 2) {
c.chrome = kids.GetAt(0).try_as<WUXC::Border>();
c.text = kids.GetAt(1).try_as<WUXC::Grid>();
}
} catch (...) {
}
return c;
}
static WUXC::TextBlock text_block(WUXC::Grid const &text, uint32_t index) {
if (!text || text.Children().Size() == 0)
return nullptr;
auto stack = text.Children().GetAt(0).try_as<WUXC::StackPanel>();
if (!stack || stack.Children().Size() <= index)
return nullptr;
return stack.Children().GetAt(index).try_as<WUXC::TextBlock>();
}
static WUXC::Grid build_text(const char *header, const char *description, unsigned glyph,
int destructive) {
WUXC::Grid text;
WUXC::ColumnDefinition col_text;
col_text.Width(WUX::GridLength{1, WUX::GridUnitType::Star});
WUXC::ColumnDefinition col_glyph;
col_glyph.Width(WUX::GridLength{0, WUX::GridUnitType::Auto});
text.ColumnDefinitions().Append(col_text);
text.ColumnDefinitions().Append(col_glyph);
text.IsHitTestVisible(false);
WUXC::StackPanel stack;
stack.Orientation(WUXC::Orientation::Vertical);
stack.VerticalAlignment(WUX::VerticalAlignment::Center);
WUXC::Grid::SetColumn(stack, 0);
WUXC::TextBlock head;
head.Text(hs(header));
head.TextWrapping(WUX::TextWrapping::Wrap);
if (destructive)
apply_destructive(head);
head.Visibility((header && *header) ? WUX::Visibility::Visible : WUX::Visibility::Collapsed);
stack.Children().Append(head);
WUXC::TextBlock desc;
desc.Text(hs(description));
desc.FontSize(12.0); desc.TextWrapping(WUX::TextWrapping::Wrap);
apply_secondary(desc);
desc.Visibility((description && *description) ? WUX::Visibility::Visible : WUX::Visibility::Collapsed);
stack.Children().Append(desc);
text.Children().Append(stack);
if (glyph != 0) {
WUXC::FontIcon icon;
wchar_t g[2] = {static_cast<wchar_t>(glyph), L'\0'};
icon.Glyph(winrt::hstring{g});
icon.FontSize(glyph == 0xE974u ? 12.0 : 16.0);
icon.VerticalAlignment(WUX::VerticalAlignment::Center);
icon.Margin(WUX::Thickness{kPad, 0, 0, 0});
apply_glyph_color(icon);
WUXC::Grid::SetColumn(icon, 1);
text.Children().Append(icon);
}
return text;
}
extern "C" {
void day_settings_xaml_sidebar_symbols(void *abi, const char *paths) {
try {
WUX::UIElement element{nullptr};
winrt::copy_from_abi(element, abi);
auto nav = element.try_as<WUXC::NavigationView>();
if (!nav) return;
std::vector<std::string> outlines;
std::string joined = paths ? paths : "";
size_t start = 0;
while (true) {
auto end = joined.find('\x1f', start);
outlines.push_back(joined.substr(start, end == std::string::npos ? end : end - start));
if (end == std::string::npos) break;
start = end + 1;
}
auto paint = [outlines](auto const& items) {
for (uint32_t i = 0; i < items.Size() && i < outlines.size(); ++i) {
if (outlines[i].empty()) continue;
auto item = items.GetAt(i).template try_as<WUXC::NavigationViewItem>();
if (!item || item.Icon()) continue;
auto xaml = std::string("<PathIcon xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Data=\"F0 ")
+ outlines[i] + "\"/>";
auto icon = WUXMk::XamlReader::Load(hs(xaml.c_str())).as<WUXC::PathIcon>();
WUXM::ScaleTransform scale;
scale.ScaleX(16.0 / 24.0);
scale.ScaleY(16.0 / 24.0);
icon.Data().Transform(scale);
item.Icon(icon);
}
};
paint(nav.MenuItems());
auto observable = nav.MenuItems().as<WF::Collections::IObservableVector<WF::IInspectable>>();
observable.VectorChanged([paint](auto const& items, auto const&) { paint(items); });
} catch (...) {
}
}
void *day_settings_xaml_card_new(const char *header, const char *description, unsigned glyph,
int clickable, int destructive, uint64_t id,
void (*cb)(uint64_t)) {
WUXC::Grid text = build_text(header, description, glyph, destructive);
if (clickable) {
WUXC::Button button;
apply_card_button(button);
text.Margin(WUX::Thickness{kPad - 1, 0, kPad - 1, 0});
text.HorizontalAlignment(WUX::HorizontalAlignment::Stretch);
text.VerticalAlignment(WUX::VerticalAlignment::Stretch);
button.Content(text);
name_card_button(button, hs(header), hs(description));
if (cb)
button.Click([id, cb](WF::IInspectable const &, WUX::RoutedEventArgs const &) { cb(id); });
return day_xaml_box(winrt::get_abi(button));
}
WUXC::Canvas root;
WUXC::Border chrome;
chrome.BorderThickness(WUX::Thickness{1, 1, 1, 1});
chrome.CornerRadius(WUX::CornerRadius{kRadius, kRadius, kRadius, kRadius});
WUXC::Canvas::SetLeft(chrome, 0);
WUXC::Canvas::SetTop(chrome, 0);
chrome.Width(0);
chrome.Height(0);
apply_chrome(chrome);
root.Children().Append(chrome);
WUXC::Canvas::SetLeft(text, kPad);
WUXC::Canvas::SetTop(text, 0);
root.Children().Append(text);
root.SizeChanged([chrome](WF::IInspectable const &, WUX::SizeChangedEventArgs const &e) mutable {
chrome.Width(e.NewSize().Width);
chrome.Height(e.NewSize().Height);
});
return day_xaml_box(winrt::get_abi(root));
}
void day_settings_xaml_card_set_text(void *handle, const char *header, const char *description) {
try {
auto card = card_of(handle);
auto h = hs(header);
auto d = hs(description);
if (auto head = text_block(card.text, 0)) {
if (head.Text() != h)
head.Text(h);
head.Visibility(h.empty() ? WUX::Visibility::Collapsed : WUX::Visibility::Visible);
}
if (auto desc = text_block(card.text, 1)) {
if (desc.Text() != d)
desc.Text(d);
desc.Visibility(d.empty() ? WUX::Visibility::Collapsed : WUX::Visibility::Visible);
}
if (card.button)
name_card_button(card.button, h, d);
} catch (...) {
}
}
void day_settings_xaml_card_measure(void *handle, double text_w, double *out_w, double *out_h) {
*out_w = 0;
*out_h = 0;
try {
auto card = card_of(handle);
if (!card.text)
return;
const double kAuto = std::numeric_limits<double>::quiet_NaN();
double saved_h = card.text.Height();
card.text.Width(kAuto);
card.text.Height(kAuto);
float fw = text_w < 0 ? std::numeric_limits<float>::infinity() : static_cast<float>(text_w);
card.text.Measure(WF::Size{fw, std::numeric_limits<float>::infinity()});
auto d = card.text.DesiredSize();
*out_w = d.Width;
*out_h = d.Height;
if (text_w >= 0 && card.root)
card.text.Width(text_w);
if (!std::isnan(saved_h))
card.text.Height(saved_h);
} catch (...) {
}
}
void day_settings_xaml_card_place_text(void *handle, double x, double y, double width, double height) {
if (!handle) return;
try {
auto card = card_of(handle);
if (!card.root || !card.text) return; WUXC::Canvas::SetLeft(card.text, x);
WUXC::Canvas::SetTop(card.text, y);
card.text.Width(width);
card.text.Height(height);
} catch (...) {
}
}
}