Skip to content

Commit 8c99200

Browse files
committed
color schemes: fix BringIntoView
1 parent 14bab6c commit 8c99200

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

src/cascadia/TerminalSettingsEditor/ColorSchemesPageViewModel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
3737

3838
WINRT_OBSERVABLE_PROPERTY(ColorSchemesSubPage, CurrentPage, _propertyChangedHandlers, ColorSchemesSubPage::Base);
3939
WINRT_OBSERVABLE_PROPERTY(Windows::Foundation::Collections::IObservableVector<Editor::ColorSchemeViewModel>, AllColorSchemes, _propertyChangedHandlers, nullptr);
40+
WINRT_PROPERTY(hstring, ElementToFocus);
4041

4142
private:
4243
Editor::ColorSchemeViewModel _CurrentScheme{ nullptr };

src/cascadia/TerminalSettingsEditor/EditColorScheme.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
4040

4141
void EditColorScheme::OnNavigatedTo(const NavigationEventArgs& e)
4242
{
43-
_ViewModel = e.Parameter().as<Editor::ColorSchemeViewModel>();
43+
const auto args = e.Parameter().as<Editor::NavigateToEditColorSchemeArgs>();
44+
_ViewModel = args.ViewModel();
45+
46+
BringIntoViewWhenLoaded(args.ElementToFocus());
4447

4548
const auto schemeName = _ViewModel.Name();
4649
NameBox().Text(schemeName);

src/cascadia/TerminalSettingsEditor/EditColorScheme.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@
33

44
#pragma once
55

6+
#include "NavigateToEditColorSchemeArgs.g.h"
67
#include "EditColorScheme.g.h"
78
#include "ColorSchemeViewModel.h"
89
#include "Utils.h"
910

1011
namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
1112
{
13+
struct NavigateToEditColorSchemeArgs : NavigateToEditColorSchemeArgsT<NavigateToEditColorSchemeArgs>
14+
{
15+
NavigateToEditColorSchemeArgs(const Editor::ColorSchemeViewModel& vm, const hstring& elementToFocus = {}) :
16+
_ViewModel(vm),
17+
_ElementToFocus(elementToFocus) {}
18+
19+
Editor::ColorSchemeViewModel ViewModel() const noexcept { return _ViewModel; }
20+
hstring ElementToFocus() const noexcept { return _ElementToFocus; }
21+
22+
private:
23+
Editor::ColorSchemeViewModel _ViewModel{ nullptr };
24+
hstring _ElementToFocus{};
25+
};
26+
1227
struct EditColorScheme : public HasScrollViewer<EditColorScheme>, EditColorSchemeT<EditColorScheme>
1328
{
1429
EditColorScheme();

src/cascadia/TerminalSettingsEditor/EditColorScheme.idl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import "ColorSchemeViewModel.idl";
55

66
namespace Microsoft.Terminal.Settings.Editor
77
{
8+
runtimeclass NavigateToEditColorSchemeArgs
9+
{
10+
ColorSchemeViewModel ViewModel { get; };
11+
String ElementToFocus { get; };
12+
}
13+
814
[default_interface] runtimeclass EditColorScheme : Windows.UI.Xaml.Controls.Page, Windows.UI.Xaml.Data.INotifyPropertyChanged
915
{
1016
EditColorScheme();

src/cascadia/TerminalSettingsEditor/MainPage.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "GlobalAppearance.h"
1616
#include "GlobalAppearanceViewModel.h"
1717
#include "ColorSchemes.h"
18+
#include "EditColorScheme.h"
1819
#include "AddProfile.h"
1920
#include "InteractionViewModel.h"
2021
#include "LaunchViewModel.h"
@@ -293,16 +294,21 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
293294
const auto settingName{ args.PropertyName() };
294295
if (settingName == L"CurrentPage")
295296
{
297+
// extract ElementToFocus and clear it; we only want to use it once
298+
auto vmImpl = get_self<ColorSchemesPageViewModel>(_colorSchemesPageVM);
299+
const auto elementToFocus = vmImpl->ElementToFocus();
300+
vmImpl->ElementToFocus(L"");
301+
296302
const auto currentScheme = _colorSchemesPageVM.CurrentScheme();
297303
if (_colorSchemesPageVM.CurrentPage() == ColorSchemesSubPage::EditColorScheme && currentScheme)
298304
{
299-
contentFrame().Navigate(xaml_typename<Editor::EditColorScheme>(), currentScheme);
305+
contentFrame().Navigate(xaml_typename<Editor::EditColorScheme>(), winrt::make<NavigateToEditColorSchemeArgs>(currentScheme, elementToFocus));
300306
const auto crumb = winrt::make<Breadcrumb>(box_value(colorSchemesTag), currentScheme.Name(), BreadcrumbSubPage::ColorSchemes_Edit);
301307
_breadcrumbs.Append(crumb);
302308
}
303309
else if (_colorSchemesPageVM.CurrentPage() == ColorSchemesSubPage::Base)
304310
{
305-
_Navigate(winrt::hstring{ colorSchemesTag }, BreadcrumbSubPage::None);
311+
_Navigate(winrt::hstring{ colorSchemesTag }, BreadcrumbSubPage::None, elementToFocus);
306312
}
307313
}
308314
else if (settingName == L"CurrentSchemeName")
@@ -862,16 +868,17 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
862868
{
863869
_PreNavigateHelper();
864870

865-
// TODO CARLOS:
866-
// - should navigate to EditColorScheme, not ColorSchemes
867-
// - EditColorScheme::OnNavigatedTo needs to accept NavigateToColorSchemesArgs (or similar)
868-
// - EditColorScheme::OnNavigatedTo needs BringIntoViewWhenLoaded(args.ElementToFocus())
869871
const auto crumb = winrt::make<Breadcrumb>(box_value(colorSchemesTag), RS_(L"Nav_ColorSchemes/Content"), BreadcrumbSubPage::None);
870872
_breadcrumbs.Append(crumb);
871873
contentFrame().Navigate(xaml_typename<Editor::ColorSchemes>(), winrt::make<NavigateToColorSchemesArgs>(_colorSchemesPageVM, elementToFocus));
872874
SettingsNav().SelectedItem(ColorSchemesNavItem());
873875

876+
// Pass along the element to focus to the ColorSchemesPageViewModel.
877+
// This will work as a staging area before we navigate to EditColorScheme
878+
get_self<ColorSchemesPageViewModel>(_colorSchemesPageVM)->ElementToFocus(elementToFocus);
879+
874880
// Set CurrentScheme BEFORE the CurrentPage!
881+
// Doing so triggers the PropertyChanged event which performs the navigation to EditColorScheme
875882
if (subPage == BreadcrumbSubPage::None)
876883
{
877884
_colorSchemesPageVM.CurrentScheme(nullptr);

src/cascadia/TerminalSettingsEditor/Utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ struct HasScrollViewer
131131
_loadedRevoker = pThis->Loaded(winrt::auto_revoke, [weakThis{ pThis->get_weak() }, elementName](auto&&, auto&&) {
132132
if (auto page{ weakThis.get() })
133133
{
134-
if (const auto& elementToFocus{ page->FindName(elementName).try_as<winrt::Microsoft::Terminal::Settings::Editor::SettingContainer>() })
134+
if (const auto& controlToFocus{ page->FindName(elementName).try_as<winrt::Windows::UI::Xaml::Controls::Control>() })
135135
{
136136
// We need to wait for the page to be loaded
137137
// or else the call to StartBringIntoView()
138138
// will end up doing nothing
139-
elementToFocus.StartBringIntoView();
139+
controlToFocus.StartBringIntoView();
140140

141141
// TODO CARLOS: ensure this works in all scenarios (easiest to test when navigating to page by keyboard)
142-
elementToFocus.Focus(winrt::Windows::UI::Xaml::FocusState::Programmatic);
142+
controlToFocus.Focus(winrt::Windows::UI::Xaml::FocusState::Programmatic);
143143
}
144144
page->_loadedRevoker.revoke();
145145
}

0 commit comments

Comments
 (0)