-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TSE: Replace TSM.Profile with a ProfileViewModel (#8552)
It's generally accepted that in a graphical application you'll follow a design pattern. The one this moves us towards is "MVVM" -- Model, View, View Model. In an ideal world, _all of our display smarts_ would be in the ViewModel and the View would literally just be XAML. This moves us closer to that reality. The only thing the Profile page ViewModel does right now is proxy requests through to the Profile behind it while adding some basic invalidation logic and detection of a setting being set to its current value. We have to do this because (1) MVVM is better and (2) separation of concerns dictates that TSM::Profile should **not** have a Windows.UI.Xaml dependency (in IPropertyChangedNotifier). It therefore **cannot** conform to Xaml's notion of what a ViewModel should be. I've done this by introducing a heap of code-generating macros and a template that work together to make the process of adding more settings easier (if not "easy"). We are eventually going to need to do this for all of the existing TSM model objects. In an ideal world, even our navigation would be driven through a viewmodel that supports a notion of an "active page". All I've done so far is break ProfilesPage's dependency on Profile entirely. Weird how that works. Fixes "browse buttons do not actually update the profile" Fixes "we need to have convoluted local overrides that update the visibility of things like AcrylicOpacity" Fixes "we'll eventually need a way to update a preview TermControl" Fixes "we have no idea how we're going to update the Discard/Apply buttons or the unsaved changes label" Having a PVM that notifies on icon/name change will also make it easier for the MainPage to update its NavViewItems.
- Loading branch information
Showing
7 changed files
with
207 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include "../inc/cppwinrt_utils.h" | ||
|
||
template<typename T> | ||
struct ViewModelHelper | ||
{ | ||
public: | ||
winrt::event_token PropertyChanged(::winrt::Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler) | ||
{ | ||
return _propertyChangedHandlers.add(handler); | ||
} | ||
|
||
void PropertyChanged(winrt::event_token const& token) | ||
{ | ||
_propertyChangedHandlers.remove(token); | ||
} | ||
|
||
protected: | ||
void _NotifyChangeCore(const std::wstring_view name) | ||
{ | ||
_propertyChangedHandlers(*static_cast<T*>(this), ::winrt::Windows::UI::Xaml::Data::PropertyChangedEventArgs{ name }); | ||
} | ||
|
||
// template recursion base case: single dispatch | ||
void _NotifyChanges(const std::wstring_view name) { _NotifyChangeCore(name); } | ||
|
||
template<typename... Args> | ||
void _NotifyChanges(const std::wstring_view name, Args&&... more) | ||
{ | ||
_NotifyChangeCore(name); | ||
_NotifyChanges(std::forward<Args>(more)...); | ||
} | ||
|
||
private: | ||
winrt::event<::winrt::Windows::UI::Xaml::Data::PropertyChangedEventHandler> _propertyChangedHandlers; | ||
}; | ||
|
||
#define _BASE_OBSERVABLE_PROJECTED_SETTING(target, name) \ | ||
public: \ | ||
auto name() const noexcept { return target.name(); }; \ | ||
template<typename T> \ | ||
void name(const T& value) \ | ||
{ \ | ||
if (name() != value) \ | ||
{ \ | ||
target.name(value); \ | ||
_NotifyChanges(L"Has" #name, L#name); \ | ||
} \ | ||
} \ | ||
bool Has##name() { return target.Has##name(); } | ||
|
||
// Defines a setting that reflects another object's same-named | ||
// setting. | ||
#define OBSERVABLE_PROJECTED_SETTING(target, name) \ | ||
_BASE_OBSERVABLE_PROJECTED_SETTING(target, name) \ | ||
void Clear##name() \ | ||
{ \ | ||
const auto hadValue{ target.Has##Name() }; \ | ||
target.Clear##name(); \ | ||
if (hadValue) \ | ||
{ \ | ||
_NotifyChanges(L"Has" #name, L#name); \ | ||
} \ | ||
} | ||
|
||
// Defines a setting that reflects another object's same-named | ||
// setting, but which cannot be erased. | ||
#define PERMANENT_OBSERVABLE_PROJECTED_SETTING(target, name) \ | ||
_BASE_OBSERVABLE_PROJECTED_SETTING(target, name) \ | ||
void Clear##name() {} |
13 changes: 13 additions & 0 deletions
13
src/cascadia/TerminalSettingsEditor/ViewModelHelpers.idl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#define OBSERVABLE_PROJECTED_SETTING(Type, Name) \ | ||
Type Name \ | ||
{ \ | ||
get; \ | ||
set; \ | ||
}; \ | ||
Boolean Has##Name { get; }; \ | ||
void Clear##Name() |