Example #1
0
        private static string GetCurrentThemeName()
        {
            string currentTheme = ThemeHandler.GetCurrentThemeName();

            Logger.Debug($"active windows theme on startup: {currentTheme}");
            return(currentTheme);
        }
        private void HandleThemeMonitorEvent()
        {
            Logger.Debug("theme switch detected");
            Thread thread = new(() =>
            {
                try
                {
                    CurrentWindowsThemeName = ThemeHandler.GetCurrentThemeName();
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, $"could not update theme name");
                }
            })
            {
                Name = "COMThemeManagerThread"
            };

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            try
            {
                thread.Join();
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "error while waiting for thread to stop:");
            }
            ThemeManager.RequestSwitch(AdmConfigBuilder.Instance(), new(SwitchSource.ExternalThemeSwitch));
        }
Example #3
0
 protected GlobalState()
 {
     CurrentWallpaperTheme   = Theme.Unknown;
     CurrentWindowsThemeName = ThemeHandler.GetCurrentThemeName();
     ForcedTheme             = Theme.Unknown;
     PostponeSwitch          = false;
 }
Example #4
0
        public override void Fire()
        {
            Thread thread = new Thread(() =>
            {
                try
                {
                    RuntimeConfigInstance.CurrentWindowsThemeName = ThemeHandler.GetCurrentThemeName();
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, $"couldn't refresh currently active theme");
                }
            })
            {
                Name = "COMThemeManagerThread"
            };

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
        protected RuntimeConfig()
        {
            CurrentAppsTheme   = RegistryHandler.AppsUseLightTheme() ? Theme.Light : Theme.Dark;
            CurrentSystemTheme = RegistryHandler.SystemUsesLightTheme() ? Theme.Light : Theme.Dark;

            try
            {
                CurrentEdgeTheme = RegistryHandler.EdgeUsesLightTheme() ? Theme.Light : Theme.Dark;
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "could not retrieve edge theme key value");
                CurrentEdgeTheme = Theme.Undefined;
            }

            CurrentColorPrevalence  = RegistryHandler.IsColorPrevalence();
            CurrentWallpaperTheme   = Theme.Undefined;
            CurrentWindowsThemeName = ThemeHandler.GetCurrentThemeName();
            CurrentOfficeTheme      = Theme.Undefined;
            ForcedTheme             = Theme.Undefined;
            PostponeSwitch          = false;
        }
        static void Main(string[] args)
        {
            try
            {
                //Set up Logger
                var config    = new NLog.Config.LoggingConfiguration();
                var configDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AutoDarkMode");

                // Targets where to log to: File and Console
                var logfile = new NLog.Targets.FileTarget("logfile")
                {
                    FileName = Path.Combine(configDir, "service.log"),
                    Layout   = @"${date:format=yyyy-MM-dd HH\:mm\:ss} | ${level} | " +
                               "${callsite:includeNamespace=False:" +
                               "cleanNamesOfAnonymousDelegates=true:" +
                               "cleanNamesOfAsyncContinuations=true}: ${message} ${exception:separator=|}"
                };
                var logconsole = new NLog.Targets.ColoredConsoleTarget("logconsole")
                {
                    Layout = @"${date:format=yyyy-MM-dd HH\:mm\:ss} | ${level} | " +
                             "${callsite:includeNamespace=False:" +
                             "cleanNamesOfAnonymousDelegates=true:" +
                             "cleanNamesOfAsyncContinuations=true}: ${message} ${exception:separator=|}"
                };

                List <string> argsList;
                if (args.Length > 0)
                {
                    argsList = new List <string>(args);
                }
                else
                {
                    argsList = new List <string>();
                }

                // Rules for mapping loggers to targets
                config.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);
                if (argsList.Contains("/debug"))
                {
                    config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
                }
                else
                {
                    config.AddRule(LogLevel.Info, LogLevel.Fatal, logfile);
                }
                // Apply config
                LogManager.Configuration = config;

                try
                {
                    Directory.CreateDirectory(configDir);
                }
                catch (Exception e)
                {
                    Logger.Fatal(e, "could not create config directory");
                }

                try
                {
                    if (!mutex.WaitOne(TimeSpan.FromSeconds(2), false))
                    {
                        Logger.Debug("app instance already open");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "failed getting mutex, " + ex.Message);
                    return;
                }

                //Instantiate Runtime config
                GlobalState.Instance();

                //Populate configuration
                AdmConfigBuilder Builder = AdmConfigBuilder.Instance();
                try
                {
                    Builder.Load();
                    Builder.LoadLocationData();
                    Logger.Debug("config builder instantiated and configuration loaded");
                }
                catch (Exception e)
                {
                    Logger.Fatal(e, "could not read config file. shutting down application!");
                    LogManager.Shutdown();
                    Environment.Exit(-1);
                }

                if (Builder.Config.Tunable.Debug && !argsList.Contains("/debug"))
                {
                    config = new NLog.Config.LoggingConfiguration();
                    config.AddRule(LogLevel.Debug, LogLevel.Fatal, logconsole);
                    config.AddRule(LogLevel.Debug, LogLevel.Fatal, logfile);
                    LogManager.Configuration = config;
                }

                Logger.Debug("config file loaded");

                //if a path is set to null, set it to the currently actvie theme for convenience reasons
                bool configUpdateNeeded = false;
                if (!Builder.Config.ClassicMode)
                {
                    if (!File.Exists(Builder.Config.DarkThemePath) || Builder.Config.DarkThemePath == null)
                    {
                        Builder.Config.DarkThemePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
                                                                    + @"\Microsoft\Windows\Themes", ThemeHandler.GetCurrentThemeName() + ".theme");
                        configUpdateNeeded = true;
                    }
                    if (!File.Exists(Builder.Config.DarkThemePath) || Builder.Config.LightThemePath == null)
                    {
                        Builder.Config.LightThemePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
                                                                     + @"\Microsoft\Windows\Themes", ThemeHandler.GetCurrentThemeName() + ".theme");
                        configUpdateNeeded = true;
                    }
                    if (configUpdateNeeded)
                    {
                        Logger.Warn("one or more theme paths not set at program start, reinstantiation needed");
                        try
                        {
                            Builder.Save();
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex, "couldn't save configuration file");
                        }
                    }
                }

                int timerMillis = 0;
                if (args.Length != 0)
                {
                    int.TryParse(args[0], out timerMillis);
                }
                timerMillis = (timerMillis == 0) ? TimerFrequency.Short : timerMillis;
                Application.SetHighDpiMode(HighDpiMode.SystemAware);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Service = new Service(timerMillis);
                Application.Run(Service);
            }
            finally
            {
                //clean shutdown
                if (Service != null)
                {
                    Service.Cleanup();
                }
                mutex.Dispose();
            }
        }