Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提高开发者选项的门槛 #744

Merged
merged 10 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Magpie.App/AboutPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

namespace winrt::Magpie::App::implementation {

void AboutPage::VersionTextBlock_DoubleTapped(IInspectable const&, Input::DoubleTappedRoutedEventArgs const&) {
if (!_viewModel.IsDeveloperMode() && (GetAsyncKeyState(VK_MENU) & 0x8000)) {
_viewModel.IsDeveloperMode(true);

hstring message = ResourceLoader::GetForCurrentView().GetString(L"About_DeveloperModeEnabled");
Application::Current().as<App>().MainPage().ShowToast(message);
}
}

void AboutPage::BugReportButton_Click(IInspectable const&, RoutedEventArgs const&) {
Win32Utils::ShellOpen(L"https://github.com/Blinue/Magpie/issues/new?assignees=&labels=bug&template=01_bug.yaml");
}
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie.App/AboutPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ struct AboutPage : AboutPageT<AboutPage> {
return _viewModel;
}

void VersionTextBlock_DoubleTapped(IInspectable const&, Input::DoubleTappedRoutedEventArgs const&);

void BugReportButton_Click(IInspectable const&, RoutedEventArgs const&);
void FeatureRequestButton_Click(IInspectable const&, RoutedEventArgs const&);
void DiscussionsButton_Click(IInspectable const&, RoutedEventArgs const&);
Expand Down
3 changes: 2 additions & 1 deletion src/Magpie.App/AboutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
Spacing="4">
<TextBlock FontSize="24"
Text="Magpie" />
<TextBlock FontSize="{StaticResource SecondaryTextFontSize}"
<TextBlock DoubleTapped="VersionTextBlock_DoubleTapped"
FontSize="{StaticResource SecondaryTextFontSize}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind ViewModel.Version, Mode=OneTime}" />
</StackPanel>
Expand Down
8 changes: 8 additions & 0 deletions src/Magpie.App/AboutViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ hstring AboutViewModel::Version() const noexcept {
));
}

bool AboutViewModel::IsDeveloperMode() const noexcept {
return AppSettings::Get().IsDeveloperMode();
}

void AboutViewModel::IsDeveloperMode(bool value) {
AppSettings::Get().IsDeveloperMode(value);
}

fire_and_forget AboutViewModel::CheckForUpdates() {
return UpdateService::Get().CheckForUpdatesAsync(false);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Magpie.App/AboutViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct AboutViewModel : AboutViewModelT<AboutViewModel> {

hstring Version() const noexcept;

bool IsDeveloperMode() const noexcept;
void IsDeveloperMode(bool value);

fire_and_forget CheckForUpdates();

bool IsCheckForPreviewUpdates() const noexcept;
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie.App/AboutViewModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Magpie.App {

String Version { get; };

Boolean IsDeveloperMode;

void CheckForUpdates();
Boolean IsCheckingForUpdates { get; };
Boolean IsCheckForUpdatesButtonEnabled { get; };
Expand Down
17 changes: 17 additions & 0 deletions src/Magpie.App/AppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,20 @@ void AppSettings::CountdownSeconds(uint32_t value) noexcept {
SaveAsync();
}

void AppSettings::IsDeveloperMode(bool value) noexcept {
_isDeveloperMode = value;
if (!value) {
// 关闭开发者模式则禁用所有开发者选项
_isDebugMode = false;
_isDisableEffectCache = false;
_isDisableFontCache = false;
_isSaveEffectSources = false;
_isWarningsAreErrors = false;
}

SaveAsync();
}

void AppSettings::IsAlwaysRunAsAdmin(bool value) noexcept {
if (_isAlwaysRunAsAdmin == value) {
return;
Expand Down Expand Up @@ -494,6 +508,8 @@ bool AppSettings::_Save(const _AppSettingsData& data) noexcept {
writer.Bool(data._isAutoRestore);
writer.Key("countdownSeconds");
writer.Uint(data._countdownSeconds);
writer.Key("developerMode");
writer.Bool(data._isDeveloperMode);
writer.Key("debugMode");
writer.Bool(data._isDebugMode);
writer.Key("disableEffectCache");
Expand Down Expand Up @@ -640,6 +656,7 @@ void AppSettings::_LoadSettings(const rapidjson::GenericObject<true, rapidjson::
if (_countdownSeconds == 0 || _countdownSeconds > 5) {
_countdownSeconds = 3;
}
JsonHelper::ReadBool(root, "developerMode", _isDeveloperMode);
JsonHelper::ReadBool(root, "debugMode", _isDebugMode);
JsonHelper::ReadBool(root, "disableEffectCache", _isDisableEffectCache);
JsonHelper::ReadBool(root, "disableFontCache", _isDisableFontCache);
Expand Down
7 changes: 7 additions & 0 deletions src/Magpie.App/AppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ struct _AppSettingsData {

bool _isPortableMode = false;
bool _isAlwaysRunAsAdmin = false;
bool _isDeveloperMode = false;
bool _isDebugMode = false;
bool _isDisableEffectCache = false;
bool _isDisableFontCache = false;
Expand Down Expand Up @@ -185,6 +186,12 @@ class AppSettings : private _AppSettingsData {
_countdownSecondsChangedEvent.remove(token);
}

bool IsDeveloperMode() const noexcept {
return _isDeveloperMode;
}

void IsDeveloperMode(bool value) noexcept;

bool IsDebugMode() const noexcept {
return _isDebugMode;
}
Expand Down
58 changes: 57 additions & 1 deletion src/Magpie.App/MainPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void MainPage::NavigationView_DisplayModeChanged(MUXC::NavigationView const& nv,
nv.IsPaneOpen(true);
}

// HACK!
// !!! HACK !!!
// 使导航栏的可滚动区域不会覆盖标题栏
FrameworkElement menuItemsScrollViewer = nv.GetTemplateChild(L"MenuItemsScrollViewer").as<FrameworkElement>();
menuItemsScrollViewer.Margin({ 0,isExpanded ? TitleBar().ActualHeight() : 0.0,0,0});
Expand Down Expand Up @@ -210,6 +210,62 @@ void MainPage::NavigateToAboutPage() {
nv.SelectedItem(nv.FooterMenuItems().GetAt(0));
}

fire_and_forget MainPage::ShowToast(const hstring& message) {
// !!! HACK !!!
// 重用 TeachingTip 有一个 bug:前一个 Toast 正在消失时新的 Toast 不会显示。为了
// 规避它,我们每次都创建新的 TeachingTip,但要保留旧对象的引用,因为播放动画时销毁
// 会导致崩溃。oldToastTeachingTip 的生存期可确保动画播放完毕。
MUXC::TeachingTip oldToastTeachingTip = ToastTeachingTip();
if (oldToastTeachingTip) {
UnloadObject(oldToastTeachingTip);
}

weak_ref<MUXC::TeachingTip> weakTeachingTip;
{
// 创建新的 TeachingTip
MUXC::TeachingTip newTeachingTip = FindName(L"ToastTeachingTip").as<MUXC::TeachingTip>();
ToastTextBlock().Text(message);
newTeachingTip.IsOpen(true);

// !!! HACK !!!
// 我们不想要 IsLightDismissEnabled,因为它会阻止用户和其他控件交互,但我们也不想要关闭按钮,于是
// 手动隐藏它。我们必须在模板加载完成后再做这些,但 TeachingTip 没有 Opening 事件,于是有了又一个
// workaround:监听 ToastTextBlock 的 LayoutUpdated 事件,它在 TeachingTip 显示前必然会被引发。
ToastTextBlock().LayoutUpdated([weak(weak_ref(newTeachingTip))](IInspectable const&, IInspectable const&) {
auto toastTeachingTip = weak.get();
if (!toastTeachingTip) {
return;
}

// 隐藏关闭按钮
if (DependencyObject closeButton = toastTeachingTip.GetTemplateChild(L"AlternateCloseButton")) {
closeButton.as<FrameworkElement>().Visibility(Visibility::Collapsed);
}

// 减小 Flyout 尺寸
if (DependencyObject container = toastTeachingTip.GetTemplateChild(L"TailOcclusionGrid")) {
container.as<FrameworkElement>().MinWidth(0.0);
}
});

weakTeachingTip = newTeachingTip;
}

auto weakThis = get_weak();
CoreDispatcher dispatcher = Dispatcher();
// 显示时长固定 2 秒
co_await 2s;
co_await dispatcher;

if (weakThis.get()) {
MUXC::TeachingTip curTeachingTip = ToastTeachingTip();
if (curTeachingTip == weakTeachingTip.get()) {
// 如果已经显示新的 Toast 则无需关闭,因为 newTeachingTip 已被卸载(但仍在生存期内)
curTeachingTip.IsOpen(false);
}
}
}

static Color Win32ColorToWinRTColor(COLORREF color) {
return { 255, GetRValue(color), GetGValue(color), GetBValue(color) };
}
Expand Down
2 changes: 2 additions & 0 deletions src/Magpie.App/MainPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct MainPage : MainPageT<MainPage> {

void NavigateToAboutPage();

fire_and_forget ShowToast(const hstring& message);

private:
void _UpdateTheme(bool updateIcons = true);

Expand Down
2 changes: 2 additions & 0 deletions src/Magpie.App/MainPage.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ namespace Magpie.App {
NewProfileViewModel NewProfileViewModel { get; };

void NavigateToAboutPage();

void ShowToast(String message);
}
}
7 changes: 7 additions & 0 deletions src/Magpie.App/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
Loaded="Loaded"
mc:Ignorable="d">
<Grid>
<muxc:TeachingTip x:Name="ToastTeachingTip"
x:Load="False">
<!-- Title 属性有主题错误 -->
<TextBlock x:Name="ToastTextBlock" />
</muxc:TeachingTip>

<local:TitleBarControl x:Name="TitleBar"
Canvas.ZIndex="1" />

<muxc:NavigationView Name="RootNavigationView"
Canvas.ZIndex="0"
CompactModeThresholdWidth="0"
Expand Down
3 changes: 3 additions & 0 deletions src/Magpie.App/Resources.language-en-US.resw
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,7 @@
<data name="About_Version_CommitId" xml:space="preserve">
<value>Commit</value>
</data>
<data name="About_DeveloperModeEnabled" xml:space="preserve">
<value>Developer mode is enabled.</value>
</data>
</root>
5 changes: 4 additions & 1 deletion src/Magpie.App/Resources.language-zh-Hans.resw
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root">
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
Expand Down Expand Up @@ -847,4 +847,7 @@
<data name="About_Version_CommitId" xml:space="preserve">
<value>提交</value>
</data>
<data name="About_DeveloperModeEnabled" xml:space="preserve">
<value>开发者模式已启用。</value>
</data>
</root>
78 changes: 42 additions & 36 deletions src/Magpie.App/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,43 +141,49 @@
IsOn="{x:Bind ViewModel.IsInlineParams, Mode=TwoWay}" />
</local:SettingsCard.ActionContent>
</local:SettingsCard>
<muxc:Expander HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Style="{StaticResource SettingExpanderStyle}">
<muxc:Expander.Header>
<local:SettingsCard x:Uid="Settings_DeveloperOptions"
Style="{StaticResource ExpanderHeaderSettingStyle}">
<local:SettingsCard.Icon>
<FontIcon Glyph="&#xEC7A;" />
</local:SettingsCard.Icon>
</local:SettingsCard>
</muxc:Expander.Header>
<muxc:Expander.Content>
<StackPanel>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_DebugMode"
IsChecked="{x:Bind ViewModel.IsDebugMode, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_DisableEffectCache"
IsChecked="{x:Bind ViewModel.IsDisableEffectCache, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_DisableFontCache"
IsChecked="{x:Bind ViewModel.IsDisableFontCache, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_SaveEffectSources"
IsChecked="{x:Bind ViewModel.IsSaveEffectSources, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_WarningsAreErrors"
IsChecked="{x:Bind ViewModel.IsWarningsAreErrors, Mode=TwoWay}" />
</local:SettingsCard>
</StackPanel>
</muxc:Expander.Content>
</muxc:Expander>
</local:SettingsGroup>
<muxc:Expander Margin="0,0,0,2"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
IsExpanded="{x:Bind ViewModel.IsDeveloperMode, Mode=OneTime}"
Style="{StaticResource SettingExpanderStyle}"
Visibility="{x:Bind ViewModel.IsDeveloperMode, Mode=OneWay}">
<muxc:Expander.Header>
<local:SettingsCard x:Uid="Settings_DeveloperOptions"
Style="{StaticResource ExpanderHeaderSettingStyle}">
<local:SettingsCard.Icon>
<FontIcon Glyph="&#xEC7A;" />
</local:SettingsCard.Icon>
<local:SettingsCard.ActionContent>
<ToggleSwitch IsOn="{x:Bind ViewModel.IsDeveloperMode, Mode=TwoWay}" />
</local:SettingsCard.ActionContent>
</local:SettingsCard>
</muxc:Expander.Header>
<muxc:Expander.Content>
<StackPanel>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_DebugMode"
IsChecked="{x:Bind ViewModel.IsDebugMode, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_DisableEffectCache"
IsChecked="{x:Bind ViewModel.IsDisableEffectCache, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_DisableFontCache"
IsChecked="{x:Bind ViewModel.IsDisableFontCache, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_SaveEffectSources"
IsChecked="{x:Bind ViewModel.IsSaveEffectSources, Mode=TwoWay}" />
</local:SettingsCard>
<local:SettingsCard Style="{StaticResource ExpanderContentSettingStyle}">
<CheckBox x:Uid="Settings_DeveloperOptions_WarningsAreErrors"
IsChecked="{x:Bind ViewModel.IsWarningsAreErrors, Mode=TwoWay}" />
</local:SettingsCard>
</StackPanel>
</muxc:Expander.Content>
</muxc:Expander>
</StackPanel>
</local:PageFrame>
</Page>
15 changes: 15 additions & 0 deletions src/Magpie.App/SettingsViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,21 @@ void SettingsViewModel::IsInlineParams(bool value) noexcept {
_propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsInlineParams"));
}

bool SettingsViewModel::IsDeveloperMode() const noexcept {
return AppSettings::Get().IsDeveloperMode();
}

void SettingsViewModel::IsDeveloperMode(bool value) noexcept {
AppSettings& settings = AppSettings::Get();

if (settings.IsDeveloperMode() == value) {
return;
}

settings.IsDeveloperMode(value);
_propertyChangedEvent(*this, PropertyChangedEventArgs(L"IsDeveloperMode"));
}

bool SettingsViewModel::IsDebugMode() const noexcept {
return AppSettings::Get().IsDebugMode();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Magpie.App/SettingsViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ struct SettingsViewModel : SettingsViewModelT<SettingsViewModel> {
bool IsInlineParams() const noexcept;
void IsInlineParams(bool value) noexcept;

bool IsDeveloperMode() const noexcept;
void IsDeveloperMode(bool value) noexcept;

bool IsDebugMode() const noexcept;
void IsDebugMode(bool value) noexcept;

Expand Down
2 changes: 2 additions & 0 deletions src/Magpie.App/SettingsViewModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Magpie.App {
Boolean IsAllowScalingMaximized;
Boolean IsSimulateExclusiveFullscreen;
Boolean IsInlineParams;

Boolean IsDeveloperMode;
Boolean IsDebugMode;
Boolean IsDisableEffectCache;
Boolean IsDisableFontCache;
Expand Down