Skip to content

Commit

Permalink
Call UpdateJumplist only if the settings changed
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Aug 5, 2022
1 parent 74cdffe commit e279408
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 17 deletions.
22 changes: 20 additions & 2 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,27 @@ namespace winrt::TerminalApp::implementation

// Register for directory change notification.
_RegisterSettingsChange();
_ProcessLazySettingsChanges();
}

// Call this function after loading your _settings.
// It handles any updates which should only occur if the settings file changed.
void AppLogic::_ProcessLazySettingsChanges()
{
const auto hash = _settings.Hash();
const auto applicationState = ApplicationState::SharedInstance();
const auto cachedHash = applicationState.SettingsHash();

// The hash might be empty if LoadAll() failed and we're dealing with the defaults settings object.
// In that case we can just wait until the user fixed their settings or CascadiaSettings fixed
// itself and either will soon trigger a settings reload.
if (hash.empty() || hash == cachedHash)
{
return;
}

Jumplist::UpdateJumplist(_settings);
applicationState.SettingsHash(hash);
}

// Method Description:
Expand Down Expand Up @@ -1058,8 +1077,7 @@ namespace winrt::TerminalApp::implementation
_ApplyLanguageSettingChange();
_RefreshThemeRoutine();
_ApplyStartupTaskStateChange();

Jumplist::UpdateJumplist(_settings);
_ProcessLazySettingsChanges();

_SettingsChangedHandlers(*this, nullptr);
}
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ namespace winrt::TerminalApp::implementation
void _OnLoaded(const IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& eventArgs);

[[nodiscard]] HRESULT _TryLoadSettings() noexcept;
void _ProcessLazySettingsChanges();
void _RegisterSettingsChange();
fire_and_forget _DispatchReloadSettings();
void _ReloadSettings();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ApplicationState.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// It provides X with the following arguments:
// (source, type, function name, JSON key, ...variadic construction arguments)
#define MTSM_APPLICATION_STATE_FIELDS(X) \
X(FileSource::Shared, winrt::hstring, SettingsHash, "settingsHash") \
X(FileSource::Shared, std::unordered_set<winrt::guid>, GeneratedProfiles, "generatedProfiles") \
X(FileSource::Local, Windows::Foundation::Collections::IVector<Model::WindowLayout>, PersistedWindowLayouts, "persistedWindowLayouts") \
X(FileSource::Shared, Windows::Foundation::Collections::IVector<hstring>, RecentCommands, "recentCommands") \
Expand Down
13 changes: 5 additions & 8 deletions src/cascadia/TerminalSettingsModel/ApplicationState.idl
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ namespace Microsoft.Terminal.Settings.Model

Boolean IsStatePath(String filename);

Windows.Foundation.Collections.IVector<WindowLayout> PersistedWindowLayouts { get; set; };

Windows.Foundation.Collections.IVector<String> RecentCommands { get; set; };

Windows.Foundation.Collections.IVector<InfoBarMessage> DismissedMessages { get; set; };

Windows.Foundation.Collections.IVector<String> AllowedCommandlines { get; set; };

String SettingsHash;
Windows.Foundation.Collections.IVector<WindowLayout> PersistedWindowLayouts;
Windows.Foundation.Collections.IVector<String> RecentCommands;
Windows.Foundation.Collections.IVector<InfoBarMessage> DismissedMessages;
Windows.Foundation.Collections.IVector<String> AllowedCommandlines;
}
}
5 changes: 5 additions & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ winrt::com_ptr<Profile> Model::implementation::CreateChild(const winrt::com_ptr<
return profile;
}

winrt::hstring CascadiaSettings::Hash() const noexcept
{
return _hash;
}

Model::CascadiaSettings CascadiaSettings::Copy() const
{
const auto settings{ winrt::make_self<CascadiaSettings>() };
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
explicit CascadiaSettings(SettingsLoader&& loader);

// user settings
winrt::hstring Hash() const noexcept;
Model::CascadiaSettings Copy() const;
Model::GlobalAppSettings GlobalSettings() const;
winrt::Windows::Foundation::Collections::IObservableVector<Model::Profile> AllProfiles() const noexcept;
Expand Down Expand Up @@ -162,6 +163,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
void _validateThemeExists();

// user settings
winrt::hstring _hash;
winrt::com_ptr<implementation::GlobalAppSettings> _globals = winrt::make_self<implementation::GlobalAppSettings>();
winrt::com_ptr<implementation::Profile> _baseLayerProfile = winrt::make_self<implementation::Profile>();
winrt::Windows::Foundation::Collections::IObservableVector<Model::Profile> _allProfiles = winrt::single_threaded_observable_vector<Model::Profile>();
Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/TerminalSettingsModel/CascadiaSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ namespace Microsoft.Terminal.Settings.Model
static String ApplicationVersion { get; };

static void ExportFile(String path, String content);

CascadiaSettings(String userJSON, String inboxJSON);

CascadiaSettings Copy();
void WriteSettingsToDisk();

String Hash { get; };

GlobalAppSettings GlobalSettings { get; };

Profile ProfileDefaults { get; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,8 @@ void SettingsLoader::_executeGenerator(const IDynamicProfileGenerator& generator
Model::CascadiaSettings CascadiaSettings::LoadAll()
try
{
auto settingsString = ReadUTF8FileIfExists(_settingsPath()).value_or(std::string{});
FILETIME lastWriteTime{};
auto settingsString = ReadUTF8FileIfExists(_settingsPath(), false, &lastWriteTime).value_or(std::string{});
auto firstTimeSetup = settingsString.empty();

// If it's the firstTimeSetup and a preview build, then try to
Expand Down Expand Up @@ -874,6 +875,16 @@ try
settings->_warnings.Append(SettingsLoadWarnings::FailedToWriteToSettings);
}
}
else
{
// There's no need to calculate a hash if mustWriteToDisk is true, because that will update
// settings.json on disk, which will trigger a file write event and cause AppLogic to call
// LoadAll() again. We'll then reach this point, because mustWriteToDisk will be false.
const ULARGE_INTEGER fileTime{ lastWriteTime.dwLowDateTime, lastWriteTime.dwHighDateTime };
const auto fileHash = til::hash(settingsString);
const auto hash = fmt::format(L"{:016x}-{:016x}", fileHash, fileTime.QuadPart);
settings->_hash = winrt::hstring{ hash };
}

return *settings;
}
Expand Down
11 changes: 8 additions & 3 deletions src/cascadia/TerminalSettingsModel/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model
}
// Tries to read a file somewhat atomically without locking it.
// Strips the UTF8 BOM if it exists.
std::string ReadUTF8File(const std::filesystem::path& path, const bool elevatedOnly)
std::string ReadUTF8File(const std::filesystem::path& path, const bool elevatedOnly, FILETIME* lastWriteTime)
{
// From some casual observations we can determine that:
// * ReadFile() always returns the requested amount of data (unless the file is smaller)
Expand Down Expand Up @@ -179,18 +179,23 @@ namespace winrt::Microsoft::Terminal::Settings::Model
buffer.erase(0, Utf8Bom.size());
}

if (lastWriteTime)
{
THROW_IF_WIN32_BOOL_FALSE(GetFileTime(file.get(), nullptr, nullptr, lastWriteTime));
}

return buffer;
}

THROW_WIN32_MSG(ERROR_READ_FAULT, "file size changed while reading");
}

// Same as ReadUTF8File, but returns an empty optional, if the file couldn't be opened.
std::optional<std::string> ReadUTF8FileIfExists(const std::filesystem::path& path, const bool elevatedOnly)
std::optional<std::string> ReadUTF8FileIfExists(const std::filesystem::path& path, const bool elevatedOnly, FILETIME* lastWriteTime)
{
try
{
return { ReadUTF8File(path, elevatedOnly) };
return { ReadUTF8File(path, elevatedOnly, lastWriteTime) };
}
catch (const wil::ResultException& exception)
{
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalSettingsModel/FileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model
{
std::filesystem::path GetBaseSettingsPath();
std::filesystem::path GetReleaseSettingsPath();
std::string ReadUTF8File(const std::filesystem::path& path, const bool elevatedOnly = false);
std::optional<std::string> ReadUTF8FileIfExists(const std::filesystem::path& path, const bool elevatedOnly = false);
std::string ReadUTF8File(const std::filesystem::path& path, const bool elevatedOnly = false, FILETIME* lastWriteTime = nullptr);
std::optional<std::string> ReadUTF8FileIfExists(const std::filesystem::path& path, const bool elevatedOnly = false, FILETIME* lastWriteTime = nullptr);
void WriteUTF8File(const std::filesystem::path& path, const std::string_view& content, const bool elevatedOnly = false);
void WriteUTF8FileAtomic(const std::filesystem::path& path, const std::string_view& content);
}

0 comments on commit e279408

Please sign in to comment.