diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp index b52add4d85..6d5371c321 100644 --- a/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp +++ b/src/modules/LightSwitch/LightSwitchService/LightSwitchService.cpp @@ -19,8 +19,6 @@ SERVICE_STATUS_HANDLE g_StatusHandle = nullptr; HANDLE g_ServiceStopEvent = nullptr; extern int g_lastUpdatedDay = -1; static ScheduleMode prevMode = ScheduleMode::Off; -static std::wstring prevLat = L"0"; -static std::wstring prevLon = L"0"; static int prevMinutes = -1; static bool lastOverrideStatus = false; @@ -153,7 +151,7 @@ void ApplyTheme(bool shouldBeLight) } } -static void DetectAndHandleExternalThemeChange(LightSwitchStateManager stateManager) +static void DetectAndHandleExternalThemeChange(LightSwitchStateManager& stateManager) { const auto& s = LightSwitchSettings::settings(); if (s.scheduleMode == ScheduleMode::Off) diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchStateManager.cpp b/src/modules/LightSwitch/LightSwitchService/LightSwitchStateManager.cpp index 5ede2f8965..5a1aa18c37 100644 --- a/src/modules/LightSwitch/LightSwitchService/LightSwitchStateManager.cpp +++ b/src/modules/LightSwitch/LightSwitchService/LightSwitchStateManager.cpp @@ -61,7 +61,7 @@ bool LightSwitchStateManager::CoordinatesAreValid(const std::wstring& lat, const { double latVal = std::stod(lat); double lonVal = std::stod(lon); - return !(latVal == 0 && lonVal == 0); + return !(latVal == 0 && lonVal == 0) && (latVal >= -90.0 && latVal <= 90.0) && (lonVal >= -180.0 && lonVal <= 180.0); } catch (...) { @@ -93,7 +93,8 @@ static std::pair update_sun_times(auto& settings) } catch (const std::exception& e) { - std::wstring wmsg(e.what(), e.what() + strlen(e.what())); + std::string msg = e.what(); + std::wstring wmsg(msg.begin(), msg.end()); Logger::error(L"[LightSwitchService] Exception during sun time update: {}", wmsg); } diff --git a/src/modules/LightSwitch/LightSwitchService/LightSwitchUtils.h b/src/modules/LightSwitch/LightSwitchService/LightSwitchUtils.h index 10b5bbdab0..0f4462bb65 100644 --- a/src/modules/LightSwitch/LightSwitchService/LightSwitchUtils.h +++ b/src/modules/LightSwitch/LightSwitchService/LightSwitchUtils.h @@ -4,19 +4,18 @@ constexpr bool ShouldBeLight(int nowMinutes, int lightTime, int darkTime) { // Normalize values into [0, 1439] - lightTime = (lightTime % 1440 + 1440) % 1440; - darkTime = (darkTime % 1440 + 1440) % 1440; - nowMinutes = (nowMinutes % 1440 + 1440) % 1440; + int normalizedLightTime = (lightTime % 1440 + 1440) % 1440; + int normalizedDarkTime = (darkTime % 1440 + 1440) % 1440; + int normalizedNowMinutes = (nowMinutes % 1440 + 1440) % 1440; // Case 1: Normal range, e.g. light mode comes before dark mode in the same day - if (lightTime < darkTime) - return nowMinutes >= lightTime && nowMinutes < darkTime; + if (normalizedLightTime < normalizedDarkTime) + return normalizedNowMinutes >= normalizedLightTime && normalizedNowMinutes < normalizedDarkTime; // Case 2: Wrap-around range, e.g. light mode starts in the evening and dark mode starts in the morning - return nowMinutes >= lightTime || nowMinutes < darkTime; + return normalizedNowMinutes >= normalizedLightTime || normalizedNowMinutes < normalizedDarkTime; } - inline int GetNowMinutes() { SYSTEMTIME st; diff --git a/src/settings-ui/Settings.UI/Converters/StringToDouble.cs b/src/settings-ui/Settings.UI/Converters/StringToDouble.cs new file mode 100644 index 0000000000..fae0618467 --- /dev/null +++ b/src/settings-ui/Settings.UI/Converters/StringToDouble.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft Corporation +// The Microsoft Corporation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Globalization; +using Microsoft.UI.Xaml.Data; + +namespace Microsoft.PowerToys.Settings.UI.Converters +{ + public partial class StringToDoubleConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, string language) + { + if (value is string s && double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out double result)) + { + return result; + } + + return 0.0; + } + + public object ConvertBack(object value, Type targetType, object parameter, string language) + { + if (value is double d) + { + return d.ToString(CultureInfo.InvariantCulture); + } + + return "0"; + } + } +} diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml index 66d2c37109..bbeb33996b 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/LightSwitchPage.xaml @@ -18,7 +18,8 @@ mc:Ignorable="d"> - + +