private void OnChangedConfig(object source, FileSystemEventArgs e) { _ = state.ConfigIsUpdatingWaitHandle.Reset(); state.ConfigIsUpdating = true; if (state.SkipConfigFileReload) { state.SkipConfigFileReload = false; Logger.Debug("skipping config file reload, update source internal"); return; } try { AdmConfig oldConfig = builder.Config; builder.Load(); componentManager.UpdateSettings(); UpdateEventStates(); builder.OnConfigUpdated(oldConfig); // fire warden ro register/unregister enabled/disabled modules if (warden != null) { warden.Fire(); } Logger.Debug("updated configuration file"); } catch (Exception ex) { Logger.Debug(ex, "config file load failed:"); } state.ConfigIsUpdating = false; if (!state.ConfigIsUpdatingWaitHandle.Set()) { Logger.Fatal("could not trigger reset event"); } }
async public Task <ApiResult> _Edit([FromForm] string Value, [FromForm] string Remark, [FromForm] string Id, [FromForm] DateTime CreateTime, [FromForm] DateTime UpdateTime, [FromForm] bool IsDeleted, [FromForm] int Sort) { var item = new AdmConfig(); item.Id = Id; using (var ctx = fsql.CreateDbContext()) { ctx.Attach(item); item.Value = Value; item.Remark = Remark; item.CreateTime = CreateTime; item.UpdateTime = UpdateTime; item.IsDeleted = IsDeleted; item.Sort = Sort; await ctx.UpdateAsync(item); var affrows = await ctx.SaveChangesAsync(); if (affrows > 0) { return(ApiResult.Success.SetMessage($"更新成功,影响行数:{affrows}")); } } return(ApiResult.Failed); }
public override void Fire() { AdmConfig config = ConfigBuilder.Config; AutoManageModule(typeof(TimeSwitchModule).Name, typeof(TimeSwitchModule), false, config.AutoThemeSwitchingEnabled && !Rtc.PostponeSwitch); AutoManageModule(typeof(GeopositionUpdateModule).Name, typeof(GeopositionUpdateModule), true, config.Location.Enabled); AutoManageModule(typeof(ThemeUpdateModule).Name, typeof(ThemeUpdateModule), true, !config.ClassicMode); AutoManageModule(typeof(GPUMonitorModule).Name, typeof(GPUMonitorModule), true, config.GPUMonitoring.Enabled); }
/// <summary> /// Registers all modules enabled in the AutoDarkMode Configuration /// </summary> public override void Fire() { AdmConfig config = ConfigBuilder.Config; AutoManageModule(typeof(GeopositionUpdateModule), true, config.Location.Enabled); AutoManageModule(typeof(TimeSwitchModule), true, config.AutoThemeSwitchingEnabled && !State.PostponeSwitch); //AutoManageModule(typeof(ThemeUpdateModule), true, config.WindowsThemeMode.Enabled && config.WindowsThemeMode.MonitorActiveTheme); AutoManageModule(typeof(GPUMonitorModule), true, config.GPUMonitoring.Enabled); AutoManageModule(typeof(UpdaterModule), true, config.Updater.Enabled); }
/// <summary> /// checks if any theme needs an update because the runtimeconfig differs from the actual configured state /// </summary> /// <param name="config">AutoDarkModeConfig instance</param> /// <param name="newTheme">new theme that is requested</param> /// <returns></returns> private static bool ThemeOptionsNeedUpdate(AdmConfig config, Theme newTheme) { RuntimeConfig rtc = RuntimeConfig.Instance(); if (config.Wallpaper.Enabled) { if (rtc.CurrentWallpaperTheme == Theme.Undefined || rtc.CurrentWallpaperTheme != newTheme) { return(true); } } if (config.SystemTheme != Mode.LightOnly) { if (config.AccentColorTaskbarEnabled && rtc.CurrentColorPrevalence && newTheme == Theme.Light) { return(true); } else if (config.AccentColorTaskbarEnabled && !rtc.CurrentColorPrevalence) { return(true); } else if (!config.AccentColorTaskbarEnabled && rtc.CurrentColorPrevalence) { return(true); } } if (ComponentNeedsUpdate(config.SystemTheme, rtc.CurrentSystemTheme, newTheme)) { return(true); } if (ComponentNeedsUpdate(config.AppsTheme, rtc.CurrentAppsTheme, newTheme)) { return(true); } if (ComponentNeedsUpdate(config.EdgeTheme, rtc.CurrentEdgeTheme, newTheme)) { return(true); } if (config.Office.Enabled && ComponentNeedsUpdate(config.Office.Mode, rtc.CurrentOfficeTheme, newTheme)) { return(true); } if (config.ColorFilterEnabled && ColorFilterNeedsUpdate(config.ColorFilterEnabled, newTheme)) { return(true); } return(false); }
public static void SwitchTheme(AdmConfig config, Theme newTheme, bool automatic = false, DateTime sunset = new DateTime(), DateTime sunrise = new DateTime()) { if (config.ClassicMode) { ApplyThemeOptions(config, newTheme, automatic, sunset, sunrise); } else { ApplyTheme(config, newTheme, automatic, sunset, sunrise); } }
private static void ApplyTheme(AdmConfig config, Theme newTheme, bool automatic, DateTime sunset, DateTime sunrise) { RuntimeConfig rtc = RuntimeConfig.Instance(); if (config.DarkThemePath == null || config.LightThemePath == null) { Logger.Error("dark or light theme path empty"); return; } if (!File.Exists(config.DarkThemePath)) { Logger.Error($"invalid dark theme path: {config.DarkThemePath}"); return; } if (!File.Exists(config.LightThemePath)) { Logger.Error($"invalid light theme path : {config.LightThemePath}"); return; } if (!config.DarkThemePath.EndsWith(".theme") || !config.DarkThemePath.EndsWith(".theme")) { Logger.Error("both theme paths must have a .theme extension"); return; } if (Path.GetFileNameWithoutExtension(config.DarkThemePath) != rtc.CurrentWindowsThemeName && newTheme == Theme.Dark) { if (automatic) { Logger.Info($"automatic dark theme switch pending, sunset: {sunset.ToString("HH:mm:ss")}"); } else { Logger.Info("switching to dark theme"); } SetColorFilter(config.ColorFilterEnabled, newTheme); SetOfficeTheme(config.Office.Mode, newTheme, rtc, config.Office.LightTheme, config.Office.DarkTheme, config.Office.Enabled); ThemeHandler.Apply(config.DarkThemePath); } else if (Path.GetFileNameWithoutExtension(config.LightThemePath) != rtc.CurrentWindowsThemeName && newTheme == Theme.Light) { if (automatic) { Logger.Info($"automatic light theme switch pending, sunrise: {sunrise.ToString("HH:mm:ss")}"); } else { Logger.Info("switching to light theme"); } SetColorFilter(config.ColorFilterEnabled, newTheme); SetOfficeTheme(config.Office.Mode, newTheme, rtc, config.Office.LightTheme, config.Office.DarkTheme, config.Office.Enabled); ThemeHandler.Apply(config.LightThemePath); } }
public static void RestoreEnergySaver(AdmConfig config) { if (!config.Tunable.DisableEnergySaverOnThemeSwitch) { return; } if (PowerManager.BatteryStatus != BatteryStatus.NotPresent && allowRestore) { ChangeBatterySlider(config.Tunable.BatterySliderDefaultValue); allowRestore = false; } }
private void SwitchThemeNow(object sender, EventArgs e) { AdmConfig config = AdmConfigBuilder.Instance().Config; Logger.Info("ui signal received: switching theme"); if (RegistryHandler.AppsUseLightTheme()) { ThemeManager.SwitchTheme(config, Theme.Dark); } else { ThemeManager.SwitchTheme(config, Theme.Light); } }
public static void DisableEnergySaver(AdmConfig config) { if (!config.Tunable.DisableEnergySaverOnThemeSwitch) { Logger.Debug($"energy saver mitigation disabled"); return; } if (PowerManager.BatteryStatus != BatteryStatus.NotPresent && PowerManager.EnergySaverStatus == EnergySaverStatus.On) { ChangeBatterySlider(0); allowRestore = true; } }
private static void ApplyThemeOptions(AdmConfig config, Theme newTheme, bool automatic, DateTime sunset, DateTime sunrise) { GlobalState state = GlobalState.Instance(); if (!ThemeOptionsNeedUpdate(config, newTheme)) { return; } PowerHandler.DisableEnergySaver(config); var oldsys = state.CurrentSystemTheme; var oldapp = state.CurrentAppsTheme; var oldedg = state.CurrentEdgeTheme; var oldwal = state.CurrentWallpaperTheme; var oldoff = state.CurrentOfficeTheme; var oldcol = state.ColorFilterEnabled; SetColorFilter(config.ColorFilterEnabled, newTheme); SetAppsTheme(config.AppsTheme, newTheme, state); SetEdgeTheme(config.EdgeTheme, newTheme, state); SetWallpaper(newTheme, state, config.Wallpaper.DarkThemeWallpapers, config.Wallpaper.LightThemeWallpapers, config.Wallpaper.Enabled); SetOfficeTheme(config.Office.Mode, newTheme, state, config.Office.LightTheme, config.Office.DarkTheme, config.Office.Enabled); //run async to delay at specific parts due to color prevalence not switching icons correctly int taskdelay = config.Tunable.AccentColorSwitchDelay; Task.Run(async() => { await SetSystemTheme(config.SystemTheme, newTheme, taskdelay, state, config); if (automatic) { Logger.Info($"theme switch invoked automatically. Sunrise: {sunrise.ToString("HH:mm:ss")}, Sunset: {sunset.ToString("HH:mm:ss")}"); } else { Logger.Info($"theme switch invoked manually"); } PowerHandler.RestoreEnergySaver(config); Logger.Info($"theme: {newTheme} with modes (s:{config.SystemTheme}, a:{config.AppsTheme}, e:{config.EdgeTheme}, w:{config.Wallpaper.Enabled}, o:{config.Office.Enabled}, c:{config.ColorFilterEnabled})"); Logger.Info($"was (s:{oldsys}, a:{oldapp}, e:{oldedg}, w:{oldwal}, o:{oldoff}, c:{oldcol})"); Logger.Info($"is (s:{state.CurrentSystemTheme}, a:{state.CurrentAppsTheme}, e:{state.CurrentEdgeTheme}, w:{state.CurrentWallpaperTheme}, o:{state.CurrentOfficeTheme}, c:{state.ColorFilterEnabled})"); }); }
public static void SwitchTheme(AdmConfig config, Theme newTheme, bool automatic = false, DateTime sunset = new DateTime(), DateTime sunrise = new DateTime()) { if (!automatic && state.ForcedTheme == Theme.Unknown) { Logger.Info($"theme switch invoked manually"); } bool themeModeSwitched = false; if (config.WindowsThemeMode.Enabled) { themeModeSwitched = ApplyTheme(config, newTheme); } // this is possibly necessary in the future if the config is internally updated and switchtheme is called before it is saved //cm.UpdateSettings(); List <ISwitchComponent> componentsToUpdate = cm.GetComponentsToUpdate(newTheme); if (componentsToUpdate.Count > 0) { //if a theme switch did not occur, run mitigations if (!themeModeSwitched) { PowerHandler.DisableEnergySaver(config); } cm.Run(componentsToUpdate, newTheme); } // disable mitigation after all components and theme switch have been executed if (componentsToUpdate.Count > 0 || themeModeSwitched) { if (newTheme == Theme.Light && automatic) { Logger.Info($"automatic light theme switch performed, sunrise: {sunrise:HH:mm:ss}"); } else if (newTheme == Theme.Dark && automatic) { Logger.Info($"automatic dark theme switch performed, sunset: {sunset:HH:mm:ss}"); } PowerHandler.RestoreEnergySaver(config); } }
async public Task <ApiResult> _Add([FromForm] string Value, [FromForm] string Remark, [FromForm] string Id, [FromForm] DateTime CreateTime, [FromForm] DateTime UpdateTime, [FromForm] bool IsDeleted, [FromForm] int Sort) { var item = new AdmConfig(); item.Value = Value; item.Remark = Remark; item.Id = Id; item.CreateTime = CreateTime; item.UpdateTime = UpdateTime; item.IsDeleted = IsDeleted; item.Sort = Sort; using (var ctx = fsql.CreateDbContext()) { await ctx.AddAsync(item); await ctx.SaveChangesAsync(); } return(ApiResult <object> .Success.SetData(item)); }
/// <summary> /// Applies the theme using the KAWAII Theme switcher logic for windows theme files /// </summary> /// <param name="config"></param> /// <param name="newTheme"></param> /// <param name="automatic"></param> /// <param name="sunset"></param> /// <param name="sunrise"></param> /// <returns>true if an update was performed; false otherwise</returns> private static bool ApplyTheme(AdmConfig config, Theme newTheme) { GlobalState state = GlobalState.Instance(); if (config.WindowsThemeMode.DarkThemePath == null || config.WindowsThemeMode.LightThemePath == null) { Logger.Error("dark or light theme path empty"); return(false); } if (!File.Exists(config.WindowsThemeMode.DarkThemePath)) { Logger.Error($"invalid dark theme path: {config.WindowsThemeMode.DarkThemePath}"); return(false); } if (!File.Exists(config.WindowsThemeMode.LightThemePath)) { Logger.Error($"invalid light theme path: {config.WindowsThemeMode.LightThemePath}"); return(false); } if (!config.WindowsThemeMode.DarkThemePath.EndsWith(".theme") || !config.WindowsThemeMode.DarkThemePath.EndsWith(".theme")) { Logger.Error("both theme paths must have a .theme extension"); return(false); } // TODO change tracking when having active theme monitor disabled if (newTheme == Theme.Dark && !state.CurrentWindowsThemeName.Equals(Path.GetFileNameWithoutExtension(config.WindowsThemeMode.DarkThemePath), StringComparison.Ordinal)) { PowerHandler.DisableEnergySaver(config); ThemeHandler.Apply(config.WindowsThemeMode.DarkThemePath); return(true); } else if (newTheme == Theme.Light && !state.CurrentWindowsThemeName.Equals(Path.GetFileNameWithoutExtension(config.WindowsThemeMode.LightThemePath), StringComparison.Ordinal)) { PowerHandler.DisableEnergySaver(config); ThemeHandler.Apply(config.WindowsThemeMode.LightThemePath); return(true); } return(false); }
public void ForceMode(object sender, EventArgs e) { ToolStripMenuItem mi = sender as ToolStripMenuItem; if (mi.Checked) { Logger.Info("ui signal received: stop forcing specific theme"); GlobalState rtc = GlobalState.Instance(); rtc.ForcedTheme = Theme.Undefined; ThemeManager.TimedSwitch(AdmConfigBuilder.Instance()); mi.Checked = false; } else { foreach (var item in NotifyIcon.ContextMenuStrip.Items) { if (item is ToolStripMenuItem) { (item as ToolStripMenuItem).Checked = false; } } AdmConfig config = AdmConfigBuilder.Instance().Config; GlobalState rtc = GlobalState.Instance(); if (mi.Name == "forceLight") { Logger.Info("ui signal received: forcing light theme"); rtc.ForcedTheme = Theme.Light; ThemeManager.SwitchTheme(config, Theme.Light); } else if (mi.Name == "forceDark") { Logger.Info("ui signal received: forcing dark theme"); rtc.ForcedTheme = Theme.Dark; ThemeManager.SwitchTheme(config, Theme.Dark); } mi.Checked = true; } }
/// <summary> /// checks if any theme needs an update because the runtimeconfig differs from the actual configured state /// </summary> /// <param name="config">AutoDarkModeConfig instance</param> /// <param name="newTheme">new theme that is requested</param> /// <returns></returns> private static bool ThemeOptionsNeedUpdate(AdmConfig config, Theme newTheme) { RuntimeConfig rtc = RuntimeConfig.Instance(); if (config.Wallpaper.Enabled) { if (rtc.CurrentWallpaperTheme == Theme.Undefined || rtc.CurrentWallpaperTheme != newTheme) { return(true); } } if (config.SystemTheme != Mode.LightOnly) { // if accent color is enabled in config, accent color is enabled in windows // and the target theme is light we need to update if (config.AccentColorTaskbarEnabled && rtc.CurrentColorPrevalence && newTheme == Theme.Light) { return(true); } // if accent color is enabled in config, but it's not currently active, update else if (config.AccentColorTaskbarEnabled && !rtc.CurrentColorPrevalence) { return(true); } // if accent color is disabled in config but still active, we need to disable it else if (!config.AccentColorTaskbarEnabled && rtc.CurrentColorPrevalence) { return(true); } } if (ComponentNeedsUpdate(config.SystemTheme, rtc.CurrentSystemTheme, newTheme)) { return(true); } if (ComponentNeedsUpdate(config.AppsTheme, rtc.CurrentAppsTheme, newTheme)) { return(true); } if (ComponentNeedsUpdate(config.EdgeTheme, rtc.CurrentEdgeTheme, newTheme)) { return(true); } if (WallpaperNeedsUpdate(config.Wallpaper.Enabled, rtc.CurrentWallpaperPath, config.Wallpaper.LightThemeWallpapers, config.Wallpaper.DarkThemeWallpapers, rtc.CurrentWallpaperTheme, newTheme)) { return(true); } if (config.Office.Enabled && ComponentNeedsUpdate(config.Office.Mode, rtc.CurrentOfficeTheme, newTheme)) { return(true); } if (config.ColorFilterEnabled && ColorFilterNeedsUpdate(config.ColorFilterEnabled, rtc.ColorFilterEnabled, newTheme)) { return(true); } return(false); }
public static void ApplyManagedTheme(AdmConfig config, string path) { PowerHandler.RequestDisableEnergySaver(config); Apply(path); }
private async static Task SetSystemTheme(Mode mode, Theme newTheme, int taskdelay, GlobalState state, AdmConfig config) { try { // Set system theme if (mode == Mode.DarkOnly) { if (state.CurrentSystemTheme != Theme.Dark) { RegistryHandler.SetSystemTheme((int)Theme.Dark); } else { taskdelay = 0; } state.CurrentSystemTheme = Theme.Dark; await Task.Delay(taskdelay); if (config.AccentColorTaskbarEnabled) { RegistryHandler.SetColorPrevalence(1); state.CurrentColorPrevalence = true; } else if (!config.AccentColorTaskbarEnabled && state.CurrentColorPrevalence == true) { RegistryHandler.SetColorPrevalence(0); state.CurrentColorPrevalence = false; } } else if (config.SystemTheme == Mode.LightOnly) { RegistryHandler.SetColorPrevalence(0); state.CurrentColorPrevalence = false; await Task.Delay(taskdelay); RegistryHandler.SetSystemTheme((int)Theme.Light); state.CurrentSystemTheme = Theme.Light; } else { if (newTheme == Theme.Light) { RegistryHandler.SetColorPrevalence(0); state.CurrentColorPrevalence = false; await Task.Delay(taskdelay); RegistryHandler.SetSystemTheme((int)newTheme); } else if (newTheme == Theme.Dark) { if (state.CurrentSystemTheme == Theme.Dark) { taskdelay = 0; } RegistryHandler.SetSystemTheme((int)newTheme); if (config.AccentColorTaskbarEnabled) { await Task.Delay(taskdelay); RegistryHandler.SetColorPrevalence(1); state.CurrentColorPrevalence = true; } else { await Task.Delay(taskdelay); RegistryHandler.SetColorPrevalence(0); state.CurrentColorPrevalence = false; } } state.CurrentSystemTheme = newTheme; } } catch (Exception ex) { Logger.Error(ex, "could not set system theme"); } }
/// <summary> /// checks if any theme needs an update because the runtimeconfig differs from the actual configured state /// </summary> /// <param name="config">AutoDarkModeConfig instance</param> /// <param name="newTheme">new theme that is requested</param> /// <returns></returns> private static bool ThemeOptionsNeedUpdate(AdmConfig config, Theme newTheme) { GlobalState state = GlobalState.Instance(); if (config.Wallpaper.Enabled) { if (state.CurrentWallpaperTheme == Theme.Undefined || state.CurrentWallpaperTheme != newTheme) { return(true); } } if (config.SystemTheme == Mode.Switch) { if (config.AccentColorTaskbarEnabled && state.CurrentColorPrevalence && newTheme == Theme.Light) { return(true); } else if ((!config.AccentColorTaskbarEnabled && state.CurrentColorPrevalence) || (config.AccentColorTaskbarEnabled && !state.CurrentColorPrevalence && newTheme == Theme.Dark)) { return(true); } } else if (config.SystemTheme == Mode.DarkOnly) { if ((!config.AccentColorTaskbarEnabled && state.CurrentColorPrevalence) || (config.AccentColorTaskbarEnabled && !state.CurrentColorPrevalence)) { return(true); } } if (ComponentNeedsUpdate(config.SystemTheme, state.CurrentSystemTheme, newTheme)) { return(true); } if (ComponentNeedsUpdate(config.AppsTheme, state.CurrentAppsTheme, newTheme)) { return(true); } if (ComponentNeedsUpdate(config.EdgeTheme, state.CurrentEdgeTheme, newTheme)) { return(true); } if (WallpaperNeedsUpdate(config.Wallpaper.Enabled, state.CurrentWallpaperPath, config.Wallpaper.LightThemeWallpapers, config.Wallpaper.DarkThemeWallpapers, state.CurrentWallpaperTheme, newTheme)) { return(true); } if (config.Office.Enabled && ComponentNeedsUpdate(config.Office.Mode, state.CurrentOfficeTheme, newTheme)) { return(true); } if (config.ColorFilterEnabled && ColorFilterNeedsUpdate(config.ColorFilterEnabled, state.ColorFilterEnabled, newTheme)) { return(true); } return(false); }
private async static Task SetSystemTheme(Mode mode, Theme newTheme, int taskdelay, RuntimeConfig rtc, AdmConfig config) { // Set system theme if (mode == Mode.DarkOnly) { RegistryHandler.SetSystemTheme((int)Theme.Dark); rtc.CurrentSystemTheme = Theme.Dark; await Task.Delay(taskdelay); if (config.AccentColorTaskbarEnabled) { RegistryHandler.SetColorPrevalence(1); rtc.CurrentColorPrevalence = true; } else if (!config.AccentColorTaskbarEnabled && rtc.CurrentColorPrevalence == true) { RegistryHandler.SetColorPrevalence(0); rtc.CurrentColorPrevalence = false; } } else if (config.SystemTheme == Mode.LightOnly) { RegistryHandler.SetColorPrevalence(0); rtc.CurrentColorPrevalence = false; await Task.Delay(taskdelay); RegistryHandler.SetSystemTheme((int)Theme.Light); rtc.CurrentSystemTheme = Theme.Light; } else { if (newTheme == Theme.Light) { RegistryHandler.SetColorPrevalence(0); rtc.CurrentColorPrevalence = false; await Task.Delay(taskdelay); } RegistryHandler.SetSystemTheme((int)newTheme); if (config.AccentColorTaskbarEnabled) { if (newTheme == Theme.Dark) { await Task.Delay(taskdelay); RegistryHandler.SetColorPrevalence(1); rtc.CurrentColorPrevalence = true; } } rtc.CurrentSystemTheme = newTheme; } }
public static void UpdateTheme(AdmConfig config, Theme newTheme, SwitchEventArgs e, DateTime switchTime = new())