|
20 | 20 | #include "ApplicationState.h" |
21 | 21 | #include "DefaultTerminal.h" |
22 | 22 | #include "FileUtils.h" |
| 23 | +#include "JsonManager.h" |
23 | 24 |
|
24 | 25 | #include "ProfileEntry.h" |
25 | 26 | #include "FolderEntry.h" |
@@ -1284,13 +1285,23 @@ try |
1284 | 1285 | // settings string back to the file. |
1285 | 1286 | if (mustWriteToDisk) |
1286 | 1287 | { |
1287 | | - settings->WriteSettingsToDisk(); |
| 1288 | + // JsonManager::WriteSettings (GH#12424) force-writes and returns the new |
| 1289 | + // hash (empty on failure); capture it so reload-detection has an accurate |
| 1290 | + // baseline. |
| 1291 | + const auto hash = JsonManager::WriteSettings(settings.as<Model::CascadiaSettings>()); |
| 1292 | + if (hash.empty()) |
| 1293 | + { |
| 1294 | + settings->_warnings.Append(SettingsLoadWarnings::FailedToWriteToSettings); |
| 1295 | + } |
| 1296 | + else |
| 1297 | + { |
| 1298 | + settings->_hash = hash; |
| 1299 | + } |
1288 | 1300 | } |
1289 | 1301 | else |
1290 | 1302 | { |
1291 | 1303 | // lastWriteTime is only valid if mustWriteToDisk is false. |
1292 | | - // Additionally WriteSettingsToDisk() updates the _hash for us already. |
1293 | | - settings->_hash = _calculateHash(settingsString, lastWriteTime); |
| 1304 | + settings->_hash = CalculateSettingsHash(settingsString, lastWriteTime); |
1294 | 1305 | } |
1295 | 1306 |
|
1296 | 1307 | settings->_researchOnLoad(); |
@@ -1496,7 +1507,7 @@ CascadiaSettings::CascadiaSettings(SettingsLoader&& loader) : |
1496 | 1507 | // and the defaults fallback never auto-save. |
1497 | 1508 | void CascadiaSettings::_installWriteSink() |
1498 | 1509 | { |
1499 | | - _writeSink = std::make_shared<std::function<void()>>(); |
| 1510 | + _writeSink = std::make_shared<implementation::SettingsWriteNotifier>(); |
1500 | 1511 |
|
1501 | 1512 | const auto installForProfile = [&](implementation::Profile* prof) { |
1502 | 1513 | if (!prof) |
@@ -1532,13 +1543,22 @@ void CascadiaSettings::_installWriteSink() |
1532 | 1543 | { |
1533 | 1544 | installForProfile(winrt::get_self<implementation::Profile>(p)); |
1534 | 1545 | } |
| 1546 | + |
| 1547 | + // TODO CARLOS: Known gaps (GH#12424): ColorScheme and Theme are not IInheritable / not |
| 1548 | + // JSON-backed, so per-property in-place edits (e.g. SetColorTableEntry, |
| 1549 | + // individual theme colors) do NOT trigger auto-save. ActionMap is IInheritable |
| 1550 | + // but not JSON-backed, so individual action edits don't either. Add/remove of |
| 1551 | + // color schemes and themes IS covered via the GlobalAppSettings collection |
| 1552 | + // mutators (AddColorScheme/RemoveColorScheme/DuplicateColorScheme/AddTheme). |
| 1553 | + // Full per-property coverage waits for those types becoming JSON-backed and is |
| 1554 | + // only required once the settings editor relies on auto-save. |
1535 | 1555 | } |
1536 | 1556 |
|
1537 | 1557 | void CascadiaSettings::SetWriteHandler(std::function<void()> handler) |
1538 | 1558 | { |
1539 | 1559 | if (_writeSink) |
1540 | 1560 | { |
1541 | | - *_writeSink = std::move(handler); |
| 1561 | + _writeSink->SetHandler(std::move(handler)); |
1542 | 1562 | } |
1543 | 1563 | } |
1544 | 1564 |
|
@@ -1566,12 +1586,6 @@ const std::filesystem::path& CascadiaSettings::_releaseSettingsPath() |
1566 | 1586 | return path; |
1567 | 1587 | } |
1568 | 1588 |
|
1569 | | -// Returns a has (approximately) uniquely identifying the settings.json contents on disk. |
1570 | | -winrt::hstring CascadiaSettings::_calculateHash(std::string_view settings, const FILETIME& lastWriteTime) |
1571 | | -{ |
1572 | | - return CalculateSettingsHash(settings, lastWriteTime); |
1573 | | -} |
1574 | | - |
1575 | 1589 | // This returns something akin to %LOCALAPPDATA%\Packages\WindowsTerminalDev_8wekyb3d8bbwe\LocalState |
1576 | 1590 | // just like SettingsPath(), but without the trailing \settings.json. |
1577 | 1591 | winrt::hstring CascadiaSettings::SettingsDirectory() |
@@ -1628,58 +1642,6 @@ void CascadiaSettings::ResetApplicationState() const |
1628 | 1642 | state.Flush(); |
1629 | 1643 | } |
1630 | 1644 |
|
1631 | | -void CascadiaSettings::ResetToDefaultSettings() |
1632 | | -{ |
1633 | | - ApplicationState::SharedInstance().Reset(); |
1634 | | - _writeSettingsToDisk(LoadStringResource(IDR_USER_DEFAULTS)); |
1635 | | -} |
1636 | | - |
1637 | | -// Method Description: |
1638 | | -// - Write the current state of CascadiaSettings to our settings file |
1639 | | -// - Create a backup file with the current contents, if one does not exist |
1640 | | -// - Persists the default terminal handler choice to the registry |
1641 | | -// Arguments: |
1642 | | -// - <none> |
1643 | | -// Return Value: |
1644 | | -// - <none> |
1645 | | -bool CascadiaSettings::WriteSettingsToDisk() |
1646 | | -{ |
1647 | | - // write current settings to current settings file |
1648 | | - Json::StreamWriterBuilder wbuilder; |
1649 | | - wbuilder.settings_["enableYAMLCompatibility"] = true; // suppress spaces around colons |
1650 | | - wbuilder.settings_["indentation"] = " "; |
1651 | | - wbuilder.settings_["precision"] = 6; // prevent values like 1.1000000000000001 |
1652 | | - |
1653 | | - try |
1654 | | - { |
1655 | | - _writeSettingsToDisk(Json::writeString(wbuilder, ToJson())); |
1656 | | - } |
1657 | | - catch (...) |
1658 | | - { |
1659 | | - LOG_CAUGHT_EXCEPTION(); |
1660 | | - _warnings.Append(SettingsLoadWarnings::FailedToWriteToSettings); |
1661 | | - return false; |
1662 | | - } |
1663 | | - return true; |
1664 | | -} |
1665 | | - |
1666 | | -void CascadiaSettings::_writeSettingsToDisk(std::string_view contents) |
1667 | | -{ |
1668 | | - FILETIME lastWriteTime{}; |
1669 | | - // Auto-save (GH#12424): write atomically via a process-unique temp file and |
1670 | | - // keep a best-effort "settings.json.bak" backup of the prior contents. |
1671 | | - WriteSettingsFile(_settingsPath(), contents, /*makeBackup*/ true, &lastWriteTime); |
1672 | | - |
1673 | | - _hash = _calculateHash(contents, lastWriteTime); |
1674 | | - |
1675 | | - // Persists the default terminal choice |
1676 | | - // GH#10003 - Only do this if _currentDefaultTerminal was actually initialized. |
1677 | | - if (_currentDefaultTerminal) |
1678 | | - { |
1679 | | - DefaultTerminal::Current(_currentDefaultTerminal); |
1680 | | - } |
1681 | | -} |
1682 | | - |
1683 | 1645 | #ifndef NDEBUG |
1684 | 1646 | [[maybe_unused]] static std::string _getDevPathToSchema() |
1685 | 1647 | { |
|
0 commit comments