Repository: microsoft/PowerToys
File: src/modules/awake/Awake/Core/Manager.cs (C#)

Code before:

            foreach (var handle in windowHandles)
            {
                StringBuilder className = new(256);
                int classQueryResult = Bridge.GetClassName(handle, className, className.Capacity);
                if (classQueryResult != 0 && className.ToString().StartsWith(targetClass, StringComparison.InvariantCultureIgnoreCase))
                {
                    return handle;
                }
            }

            return IntPtr.Zero;
        }

        public static Dictionary<string, int> GetDefaultTrayOptions()
        {
            Dictionary<string, int> optionsList = new Dictionary<string, int>
            {
                { string.Format(CultureInfo.InvariantCulture, AwakeMinutes, 30), 1800 },
                { Resources.AWAKE_1_HOUR, 3600 },
                { string.Format(CultureInfo.InvariantCulture, AwakeHours, 2), 7200 },
            };
            return optionsList;
        }

        private static void ResetAwakeMode()
        {
            try
            {
                SettingsUtils settingsUtils = new SettingsUtils();
                string settingsPath = settingsUtils.GetSettingsFilePath(Core.Constants.AppName);
                if (settingsPath != null)
                {
                    AwakeSettings settings = new AwakeSettings();
                    settings.Properties.Mode = AwakeMode.PASSIVE;
                    settingsUtils.SaveSettings(JsonSerializer.Serialize(settings), Core.Constants.AppName);

                    SetNoKeepAwake();
                }
            }
            catch (Exception ex)
            {
                string? errorString = $"Failed to reset Awake mode: {ex.Message}";
                Logger.LogError(errorString);
            }
        }
    }
}


Review comment (suggestion):
Instead of creating a method for this, why not use the code that already exists in PassiveKeepAwakeCommandHandler ? Feels like we're duplicating code here.

Code after:

            foreach (var handle in windowHandles)
            {
                StringBuilder className = new(256);
                int classQueryResult = Bridge.GetClassName(handle, className, className.Capacity);
                if (classQueryResult != 0 && className.ToString().StartsWith(targetClass, StringComparison.InvariantCultureIgnoreCase))
                {
                    return handle;
                }
            }

            return IntPtr.Zero;
        }

        public static Dictionary<string, int> GetDefaultTrayOptions()
        {
            Dictionary<string, int> optionsList = new Dictionary<string, int>
            {
                { string.Format(CultureInfo.InvariantCulture, AwakeMinutes, 30), 1800 },
                { Resources.AWAKE_1_HOUR, 3600 },
                { string.Format(CultureInfo.InvariantCulture, AwakeHours, 2), 7200 },
            };
            return optionsList;
        }

        public static void SetPassiveKeepAwakeMode(string moduleName)
        {
            try
            {
                SettingsUtils settingsUtils = new SettingsUtils();
                AwakeSettings settings = new AwakeSettings();

                settings.Properties.Mode = AwakeMode.PASSIVE;
                settingsUtils.SaveSettings(JsonSerializer.Serialize(settings), moduleName);
            }
            catch (Exception ex)
            {
                string? errorString = $"Failed to reset Awake mode: {ex.Message}";
                Logger.LogError(errorString);
            }
        }
    }
}