Skip to content

Commit

Permalink
Hook up the Save and Reset buttons (#8348)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request
Adds an event handler for the Save and Reset buttons. "Save" writes the settings to disk using the API introduced by #8018. "Reset" simply overwrites the `settingsClone` (what the Settings UI reads from) with `settingsSource` (provided by TermApp on Settings UI initialization).

## References
#1564 - Settings UI

## Validation Steps Performed
The following scenarios were tested and are verified to work properly:
- Change default profile in SUI, then save/reset
- Hide a profile, then save/reset
- Modifying the settings.json directly propagates changes to SUI
- "Reset" updates the current page
  • Loading branch information
carlos-zamora authored Dec 1, 2020
1 parent 62aa0ce commit 2f747a7
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 12 deletions.
9 changes: 8 additions & 1 deletion src/cascadia/TerminalApp/SettingsTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Microsoft::Terminal::TerminalControl;
using namespace winrt::Microsoft::Terminal::Settings::Model;
using namespace winrt::Microsoft::Terminal::Settings::Editor;
using namespace winrt::Windows::System;

namespace winrt
Expand All @@ -22,7 +23,7 @@ namespace winrt

namespace winrt::TerminalApp::implementation
{
SettingsTab::SettingsTab(winrt::Microsoft::Terminal::Settings::Editor::MainPage settingsUI)
SettingsTab::SettingsTab(MainPage settingsUI)
{
Content(settingsUI);

Expand All @@ -31,6 +32,12 @@ namespace winrt::TerminalApp::implementation
_CreateIcon();
}

void SettingsTab::UpdateSettings(CascadiaSettings settings)
{
auto settingsUI{ Content().as<MainPage>() };
settingsUI.UpdateSettings(settings);
}

// Method Description:
// - Focus the settings UI
// Arguments:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/SettingsTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace winrt::TerminalApp::implementation
public:
SettingsTab(winrt::Microsoft::Terminal::Settings::Editor::MainPage settingsUI);

void UpdateSettings(Microsoft::Terminal::Settings::Model::CascadiaSettings settings);
void Focus(winrt::Windows::UI::Xaml::FocusState focusState) override;

private:
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/SettingsTab.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ namespace TerminalApp
{
[default_interface] runtimeclass SettingsTab : TabBase
{
void UpdateSettings(Microsoft.Terminal.Settings.Model.CascadiaSettings settings);
}
}
4 changes: 4 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,10 @@ namespace winrt::TerminalApp::implementation
// Force the TerminalTab to re-grab its currently active control's title.
terminalTab->UpdateTitle();
}
else if (auto settingsTab = tab.try_as<TerminalApp::SettingsTab>())
{
settingsTab.UpdateSettings(_settings);
}
}

auto weakThis{ get_weak() };
Expand Down
88 changes: 86 additions & 2 deletions src/cascadia/TerminalSettingsEditor/MainPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,87 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
{
MainPage::MainPage(const CascadiaSettings& settings) :
_settingsSource{ settings },
_settingsClone{ settings },
_settingsClone{ settings.Copy() },
_profileToNavItemMap{ winrt::single_threaded_map<Model::Profile, MUX::Controls::NavigationViewItem>() }
{
InitializeComponent();

_InitializeProfilesList();
}

fire_and_forget MainPage::UpdateSettings(Model::CascadiaSettings settings)
{
_settingsSource = settings;
_settingsClone = settings.Copy();

co_await winrt::resume_foreground(Dispatcher());

// reconstruct our list of profiles
auto menuItems{ SettingsNav().MenuItems() };
unsigned int i = 0;
while (i < menuItems.Size())
{
if (const auto navViewItem{ menuItems.GetAt(i).try_as<MUX::Controls::NavigationViewItem>() })
{
const auto tag{ navViewItem.Tag() };
if (tag.try_as<Model::Profile>())
{
// remove NavViewItem pointing to a Profile
menuItems.RemoveAt(i);
continue;
}
else if (const auto stringTag{ tag.try_as<hstring>() })
{
if (stringTag == addProfileTag)
{
// remove NavViewItem pointing to "Add Profile"
menuItems.RemoveAt(i);
continue;
}
}
}
++i;
}
_InitializeProfilesList();

_RefreshCurrentPage();
}

void MainPage::_RefreshCurrentPage()
{
auto navigationMenu{ SettingsNav() };
if (const auto selectedItem{ navigationMenu.SelectedItem() })
{
if (const auto tag{ selectedItem.as<MUX::Controls::NavigationViewItem>().Tag() })
{
if (const auto profile{ tag.try_as<Model::Profile>() })
{
// check if the profile still exists
if (_settingsClone.FindProfile(profile.Guid()))
{
// Navigate to the page with the given profile
contentFrame().Navigate(xaml_typename<Editor::Profiles>(), winrt::make<ProfilePageNavigationState>(profile, _settingsClone.GlobalSettings().ColorSchemes(), *this));
return;
}
}
else if (const auto stringTag{ tag.try_as<hstring>() })
{
// navigate to the page with this tag
_Navigate(*stringTag);
return;
}
}
}

// could not find the page we were on, fallback to first menu item
const auto firstItem{ navigationMenu.MenuItems().GetAt(0) };
navigationMenu.SelectedItem(firstItem);
if (const auto tag{ navigationMenu.SelectedItem().as<NavigationViewItem>().Tag() })
{
_Navigate(unbox_value<hstring>(tag));
}
}

void MainPage::SetHostingWindow(uint64_t hostingWindow) noexcept
{
_hostingHwnd.emplace(reinterpret_cast<HWND>(hostingWindow));
Expand Down Expand Up @@ -166,6 +239,17 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
}
}

void MainPage::SaveButton_Click(IInspectable const& /*sender*/, RoutedEventArgs const& /*args*/)
{
_settingsClone.WriteSettingsToDisk();
}

void MainPage::ResetButton_Click(IInspectable const& /*sender*/, RoutedEventArgs const& /*args*/)
{
_settingsClone = _settingsSource.Copy();
_RefreshCurrentPage();
}

void MainPage::_InitializeProfilesList()
{
// Manually create a NavigationViewItem for each profile
Expand All @@ -181,7 +265,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
// Top off (the end of the nav view) with the Add Profile item
MUX::Controls::NavigationViewItem addProfileItem;
addProfileItem.Content(box_value(RS_(L"Nav_AddNewProfile/Content")));
addProfileItem.Tag(box_value(L"AddProfile"));
addProfileItem.Tag(box_value(addProfileTag));
addProfileItem.SelectsOnInvoked(false);

FontIcon icon;
Expand Down
17 changes: 10 additions & 7 deletions src/cascadia/TerminalSettingsEditor/MainPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,33 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
MainPage() = delete;
MainPage(const Model::CascadiaSettings& settings);

fire_and_forget UpdateSettings(Model::CascadiaSettings settings);

void OpenJsonKeyDown(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& args);
void OpenJsonTapped(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs const& args);
void SettingsNav_Loaded(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
void SettingsNav_ItemInvoked(Microsoft::UI::Xaml::Controls::NavigationView const& sender, Microsoft::UI::Xaml::Controls::NavigationViewItemInvokedEventArgs const& args);
void SaveButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);
void ResetButton_Click(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::RoutedEventArgs const& args);

void SetHostingWindow(uint64_t hostingWindow) noexcept;
bool TryPropagateHostingWindow(IInspectable object) noexcept;

TYPED_EVENT(OpenJson, winrt::Windows::Foundation::IInspectable, winrt::Microsoft::Terminal::Settings::Model::SettingsTarget);
TYPED_EVENT(OpenJson, Windows::Foundation::IInspectable, Model::SettingsTarget);

private:
// XAML should data-bind to the _settingsClone
// When "save" is pressed, _settingsSource = _settingsClone
winrt::Microsoft::Terminal::Settings::Model::CascadiaSettings _settingsSource;
winrt::Microsoft::Terminal::Settings::Model::CascadiaSettings _settingsClone;
winrt::Windows::Foundation::Collections::IMap<winrt::Microsoft::Terminal::Settings::Model::Profile, winrt::Microsoft::UI::Xaml::Controls::NavigationViewItem> _profileToNavItemMap;
Model::CascadiaSettings _settingsSource;
Model::CascadiaSettings _settingsClone;
winrt::Windows::Foundation::Collections::IMap<Model::Profile, winrt::Microsoft::UI::Xaml::Controls::NavigationViewItem> _profileToNavItemMap;

std::optional<HWND> _hostingHwnd;

void _InitializeProfilesList();
void _CreateAndNavigateToNewProfile(const uint32_t index);
winrt::Microsoft::UI::Xaml::Controls::NavigationViewItem _CreateProfileNavViewItem(const winrt::Microsoft::Terminal::Settings::Model::Profile& profile);
winrt::Microsoft::UI::Xaml::Controls::NavigationViewItem _CreateProfileNavViewItem(const Model::Profile& profile);

void _Navigate(hstring clickedItemTag);
void _RefreshCurrentPage();
};
}

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsEditor/MainPage.idl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Terminal.Settings.Editor
{
MainPage(Microsoft.Terminal.Settings.Model.CascadiaSettings settings);

void UpdateSettings(Microsoft.Terminal.Settings.Model.CascadiaSettings settings);
event Windows.Foundation.TypedEventHandler<Object, Microsoft.Terminal.Settings.Model.SettingsTarget> OpenJson;

// Due to the aforementioned bug, we can't use IInitializeWithWindow _here_ either.
Expand Down
6 changes: 4 additions & 2 deletions src/cascadia/TerminalSettingsEditor/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ the MIT License. See LICENSE in the project root for license information. -->
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Right" Orientation="Horizontal" Margin="0,0,30,0">
<Button x:Uid="Settings_ResetSettingsButton"
FontSize="15"
ToolTipService.Placement="Mouse"/>
ToolTipService.Placement="Mouse"
Click="ResetButton_Click"/>
<Button x:Uid="Settings_SaveSettingsButton"
FontSize="15"
Style="{StaticResource AccentButtonStyle}"
Margin="10,0,0,0"/>
Margin="10,0,0,0"
Click="SaveButton_Click"/>
</StackPanel>
</Grid>
</Grid>
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ void GlobalAppSettings::DefaultProfile(const winrt::guid& defaultProfile) noexce
{
_validDefaultProfile = true;
_defaultProfile = defaultProfile;
_UnparsedDefaultProfile = Utils::GuidToString(defaultProfile);
}

winrt::guid GlobalAppSettings::DefaultProfile() const
Expand Down

0 comments on commit 2f747a7

Please sign in to comment.