Magpie/src/Magpie.App/KeyVisual.cpp
Xu 490a4ea16d
移植 SettingsControls (#771)
* feat: 移植 SettingsCard 1

* feat: 移植 SettingsCard 2

* feat: 移植 SettingsCard 3

* feat: 移植 SettingsCard 4

* feat: 移植 SettingsCard 5

* feat: 更多设置项迁移到新版 SettingsCard

* feat: 添加 IsEqualStateTrigger 和 IsNullStateTrigger

* feat: 移植 ControlSizeTrigger

* fix: 修复 SettinsCard 在程序启动时错误展示动画的问题

* fix: 修复内存泄露问题

* feat: SettingsCard 不再支持宽度较窄时切换到垂直布局

* Revert "feat: SettingsCard 不再支持宽度较窄时切换到垂直布局"

This reverts commit a5fadae06b.

* feat: 只对特定SettingsCard启用自动切换布局的功能

* UI: PageFrame 在宽度较小时减小边距

* UI: 使 InfoBar 和 SettingsCard 对齐

* feat: 添加 SettingsExpander

* feat: 移植 SettingsExpander 1

* feat: 移植 SettingsExpander 2

* feat: 移植 SettingsExpander 3

* feat: 移植 SettingsExpander 4

* feat: 删除旧版控件

* fix: 修复缩放配置页面崩溃

* feat: SettingsGroup.Title 改名为 Header

* UI: 修复裁剪选项禁用状态文字颜色

* refactor: 优化缩放配置页面布局
使用 SettingsExpander 中的 ListView

* UI: 修复裁剪选项

* UI: 简化启动参数选项 UI

* UI: 简化启动参数选项 UI

* fix: 修复 SettingsExcpander.Items 中的 SettingsCard 对于鼠标点击会错误设置焦点的问题

* chore: 清理
2023-12-12 18:03:41 +08:00

166 lines
4.6 KiB
C++

#include "pch.h"
#include "KeyVisual.h"
#if __has_include("KeyVisual.g.cpp")
#include "KeyVisual.g.cpp"
#endif
#include "StrUtils.h"
#include "Win32Utils.h"
using namespace winrt;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Markup;
namespace winrt::Magpie::App::implementation {
// uint8_t 不起作用
const DependencyProperty KeyVisual::_keyProperty = DependencyProperty::Register(
L"Key",
xaml_typename<int>(),
xaml_typename<class_type>(),
PropertyMetadata(box_value<int>(0), &KeyVisual::_OnPropertyChanged)
);
const DependencyProperty KeyVisual::_visualTypeProperty = DependencyProperty::Register(
L"VisualTypeProperty",
xaml_typename<IInspectable>(),
xaml_typename<class_type>(),
PropertyMetadata(box_value(Magpie::App::VisualType{}), &KeyVisual::_OnPropertyChanged)
);
const DependencyProperty KeyVisual::_isErrorProperty = DependencyProperty::Register(
L"IsError",
xaml_typename<bool>(),
xaml_typename<class_type>(),
PropertyMetadata(box_value(false), &KeyVisual::_OnIsErrorChanged)
);
KeyVisual::KeyVisual() {
DefaultStyleKey(box_value(GetRuntimeClassName()));
Style(_GetStyleSize(L"TextKeyVisualStyle"));
}
void KeyVisual::OnApplyTemplate() {
base_type::OnApplyTemplate();
_isEnabledChangedRevoker.revoke();
_keyPresenter = GetTemplateChild(L"KeyPresenter").as<ContentPresenter>();
_Update();
_SetEnabledState();
_SetErrorState();
_isEnabledChangedRevoker = IsEnabledChanged(auto_revoke, { this, &KeyVisual::_IsEnabledChanged });
}
void KeyVisual::_OnPropertyChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&) {
get_self<KeyVisual>(sender.as<class_type>())->_Update();
}
void KeyVisual::_OnIsErrorChanged(DependencyObject const& sender, DependencyPropertyChangedEventArgs const&) {
get_self<KeyVisual>(sender.as<class_type>())->_SetErrorState();
}
void KeyVisual::_IsEnabledChanged(IInspectable const&, DependencyPropertyChangedEventArgs const&) {
_SetEnabledState();
}
void KeyVisual::_Update() {
if (!_keyPresenter) {
return;
}
uint8_t key = (uint8_t)Key();
switch (key) {
case VK_UP:
Style(_GetStyleSize(L"IconKeyVisualStyle"));
_keyPresenter.Content(box_value(L"\uE96D"));
break;
case VK_DOWN:
Style(_GetStyleSize(L"IconKeyVisualStyle"));
_keyPresenter.Content(box_value(L"\uE96E"));
break;
case VK_LEFT:
Style(_GetStyleSize(L"IconKeyVisualStyle"));
_keyPresenter.Content(box_value(L"\uE96F"));
break;
case VK_RIGHT:
Style(_GetStyleSize(L"IconKeyVisualStyle"));
_keyPresenter.Content(box_value(L"\uE970"));
break;
case VK_LWIN:
{
Style(_GetStyleSize(L"IconKeyVisualStyle"));
PathIcon winIcon = XamlReader::Load(LR"(<PathIcon xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Data="M9,17V9h8v8ZM0,17V9H8v8ZM9,8V0h8V8ZM0,8V0H8V8Z" />)").as<PathIcon>();
Viewbox winIconContainer;
winIconContainer.Child(winIcon);
winIconContainer.HorizontalAlignment(HorizontalAlignment::Center);
winIconContainer.VerticalAlignment(VerticalAlignment::Center);
double iconDimensions = _GetIconSize();
winIconContainer.Height(iconDimensions);
winIconContainer.Width(iconDimensions);
_keyPresenter.Content(winIconContainer);
break;
}
case VK_LCONTROL:
Style(_GetStyleSize(L"TextKeyVisualStyle"));
_keyPresenter.Content(box_value(L"Ctrl"));
break;
case VK_LMENU:
Style(_GetStyleSize(L"TextKeyVisualStyle"));
_keyPresenter.Content(box_value(L"Alt"));
break;
case VK_LSHIFT:
Style(_GetStyleSize(L"TextKeyVisualStyle"));
_keyPresenter.Content(box_value(L"Shift"));
break;
default:
Style(_GetStyleSize(L"TextKeyVisualStyle"));
_keyPresenter.Content(box_value(Win32Utils::GetKeyName(key)));
break;
}
}
Style KeyVisual::_GetStyleSize(std::wstring_view styleName) const {
const wchar_t* prefix = nullptr;
Magpie::App::VisualType vt = VisualType();
if (vt == VisualType::Small) {
prefix = L"Small";
} else if (vt == VisualType::SmallOutline) {
prefix = L"SmallOutline";
} else {
prefix = L"Default";
}
return Application::Current().Resources()
.Lookup(box_value(StrUtils::Concat(prefix, styleName)))
.as<Windows::UI::Xaml::Style>();
}
double KeyVisual::_GetIconSize() const {
const wchar_t* key = nullptr;
Magpie::App::VisualType vt = VisualType();
if (vt == VisualType::Small || vt == VisualType::SmallOutline) {
key = L"SmallIconSize";
} else {
key = L"DefaultIconSize";
}
return Application::Current().Resources()
.Lookup(box_value(key))
.as<double>();
}
void KeyVisual::_SetErrorState() {
VisualStateManager::GoToState(*this, IsError() ? L"Error" : L"Default", true);
}
void KeyVisual::_SetEnabledState() {
VisualStateManager::GoToState(*this, IsEnabled() ? L"Normal" : L"Disabled", true);
}
}