Copilot suggestions

This commit is contained in:
Jaylyn Barbee
2025-11-07 13:36:42 -05:00
parent 48ddffa2e8
commit 89d58aa038
7 changed files with 64 additions and 30 deletions

View File

@@ -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)

View File

@@ -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<int, int> 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);
}

View File

@@ -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;

View File

@@ -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";
}
}
}

View File

@@ -18,7 +18,8 @@
mc:Ignorable="d">
<Page.Resources>
<converters:TimeSpanToFriendlyTimeConverter x:Key="TimeSpanToFriendlyTimeConverter" />
</Page.Resources>
<converters:StringToDoubleConverter x:Key="StringToDoubleConverter" />
</Page.Resources>
<Grid>
<controls:SettingsPageControl
x:Uid="LightSwitch"
@@ -93,9 +94,9 @@
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
Text="{x:Bind ViewModel.SyncButtonInformation, Mode=OneWay}" />
<Button
x:Uid="LightSwitch_SetLocationButton"
AutomationProperties.AutomationId="SetLocationButton_LightSwitch"
Click="SyncLocationButton_Click"
Content="Set location" />
Click="SyncLocationButton_Click" />
</StackPanel>
</tkcontrols:SettingsCard>
@@ -203,6 +204,7 @@
IsPrimaryButtonEnabled="False"
IsSecondaryButtonEnabled="True"
Opened="LocationDialog_Opened"
Closed="LocationDialog_Closed"
PrimaryButtonClick="LocationDialog_PrimaryButtonClick"
PrimaryButtonStyle="{StaticResource AccentButtonStyle}">
<Grid RowSpacing="16">
@@ -257,7 +259,7 @@
x:Uid="LightSwitch_LatitudeBox"
Maximum="90"
Minimum="-90"
Text="{x:Bind ViewModel.Latitude, Mode=TwoWay}"
Value="{x:Bind ViewModel.Latitude, Mode=TwoWay, Converter={StaticResource StringToDoubleConverter}}"
ValueChanged="LatLonBox_ValueChanged" />
<NumberBox
x:Name="LongitudeBox"
@@ -265,7 +267,7 @@
Grid.Column="1"
Maximum="180"
Minimum="-180"
Text="{x:Bind ViewModel.Longitude, Mode=TwoWay}"
Value="{x:Bind ViewModel.Longitude, Mode=TwoWay, Converter={StaticResource StringToDoubleConverter}}"
ValueChanged="LatLonBox_ValueChanged" />
<Button
x:Name="SyncButton"

View File

@@ -167,7 +167,7 @@ namespace Microsoft.PowerToys.Settings.UI.Views
double viewModelLatitude = double.TryParse(ViewModel.Latitude, out var lat) ? lat : 0.0;
double viewModelLongitude = double.TryParse(ViewModel.Longitude, out var lon) ? lon : 0.0;
if (latitude == viewModelLatitude && longitude == viewModelLongitude)
if (Math.Abs(latitude - viewModelLatitude) < 0.0001 && Math.Abs(longitude - viewModelLongitude) < 0.0001)
{
return;
}
@@ -210,13 +210,19 @@ namespace Microsoft.PowerToys.Settings.UI.Views
LongitudeBox.Loaded += LatLonBox_Loaded;
}
private void LocationDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
{
LatitudeBox.Loaded -= LatLonBox_Loaded;
LongitudeBox.Loaded -= LatLonBox_Loaded;
}
private void LatLonBox_Loaded(object sender, RoutedEventArgs e)
{
if (sender as NumberBox == LatitudeBox)
if (sender is NumberBox box && box == LatitudeBox)
{
_latLoaded = true;
}
else if (sender as NumberBox == LongitudeBox)
else if (sender is NumberBox box2 && box2 == LongitudeBox)
{
_lonLoaded = true;
}

View File

@@ -2696,8 +2696,6 @@ From there, simply click on one of the supported files in the File Explorer and
<value>Use a keyboard shortcut to highlight left and right mouse clicks.</value>
<comment>Mouse as in the hardware peripheral.</comment>
</data>
<!-- CursorWrap Module -->
<data name="MouseUtils_Enable_CursorWrap.Header" xml:space="preserve">
<value>Enable CursorWrap</value>
</data>
@@ -2707,8 +2705,6 @@ From there, simply click on one of the supported files in the File Explorer and
<data name="MouseUtils_CursorWrap.Description" xml:space="preserve">
<value>Wrap the mouse cursor between monitor edges</value>
</data>
<!-- Activation Shortcut -->
<data name="MouseUtils_CursorWrap_ActivationShortcut.Header" xml:space="preserve">
<value>Activation shortcut</value>
</data>
@@ -2718,19 +2714,15 @@ From there, simply click on one of the supported files in the File Explorer and
<data name="MouseUtils_CursorWrap_ActivationShortcut_Button.Content" xml:space="preserve">
<value>Set shortcut</value>
</data>
<data name="MouseUtils_CursorWrap_DisableWrapDuringDrag.Content" xml:space="preserve">
<value>Disable wrapping while dragging</value>
</data>
<!-- Auto-activate -->
<data name="MouseUtils_CursorWrap_AutoActivate.Header" xml:space="preserve">
<value>Auto-activate on startup</value>
</data>
<data name="MouseUtils_CursorWrap_AutoActivate.Content" xml:space="preserve">
<value>Automatically activate on utility startup</value>
</data>
<data name="Oobe_MouseUtils_MousePointerCrosshairs.Text" xml:space="preserve">
<value>Mouse Pointer Crosshairs</value>
<comment>Mouse as in the hardware peripheral.</comment>
@@ -5486,7 +5478,7 @@ To record a specific window, enter the hotkey with the Alt key in the opposite m
<value>Cancel</value>
</data>
<data name="LightSwitch_LocationDialog_Description.Text" xml:space="preserve">
<value>Enter your location manually or detect it automatically so Light Switch can calculate accurate sunrise and sunset times.</value>
<value>Detect your location automatically or enter it manually to calculate sunrise and sunset times.</value>
</data>
<data name="LightSwitch_SunriseText.Text" xml:space="preserve">
<value>Sunrise</value>
@@ -5588,7 +5580,7 @@ To record a specific window, enter the hotkey with the Alt key in the opposite m
</data>
<data name="ShortcutConflictControl_Automation.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
<value>Shortcut conflicts</value>
</data>
</data>
<data name="ShortcutConflictControl_NoConflictsFound" xml:space="preserve">
<value>No conflicts found</value>
</data>
@@ -5699,4 +5691,7 @@ To record a specific window, enter the hotkey with the Alt key in the opposite m
<data name="Hosts_Backup_CountInput_Header" xml:space="preserve">
<value>Backup count</value>
</data>
<data name="LightSwitch_SetLocationButton.Content" xml:space="preserve">
<value>Set Location</value>
</data>
</root>