Ejemplo n.º 1
0
        public Icon DrawIcon(BatteryData battery, int drawWidth)
        {
            // Format32bppArgb is required for transparancy
            using var bitmap   = new Bitmap(IconSettings.Width, IconSettings.Height, PixelFormat.Format32bppArgb);
            using var graphics = Graphics.FromImage(bitmap);

            DrawBackground(graphics, bitmap);
            DrawForeground(graphics);
            DrawChargeLevels(graphics, battery, drawWidth);
#if DEBUG
            bitmap.Save(@$ "c:\system\temp\batterymax-{drawWidth}.png");
Ejemplo n.º 2
0
        public int GetDrawingWidth(BatteryData battery)
        {
            var drawWidth = Convert.ToInt32(battery.CurrentCharge / IconSettings.PercentPerLevel);

            // Always draw something if charge > 0
            if (drawWidth == 0 && battery.CurrentCharge > 0)
            {
                drawWidth = 1;
            }

            return(drawWidth);
        }
Ejemplo n.º 3
0
        public static string FormatBatteryUpdateText(BatteryData battery)
        {
            var currentCharge = battery.CurrentCharge;

            var sb = new StringBuilder();

            //Handle the special case with no time
            if (battery.IsPluggedInNotCharging)
            {
                sb.AppendFormat(Resources.PluggedIn, currentCharge);
                sb.AppendLine();
                sb.Append(Resources.NotCharging);

                return(sb.ToString());
            }

            int targetCharge;

            if (battery.IsCharging)
            {
                sb.AppendFormat(Resources.Charging, currentCharge);
                targetCharge = Settings.MaximumCharge;

                if (battery.IsAboveMaximumCharge)
                {
                    sb.AppendLine();
                    sb.AppendFormat(Resources.AboveMaximumCharge, Settings.MaximumCharge);
                    targetCharge = 100;
                }
            }
            else // Not charging
            {
                sb.AppendFormat(Resources.Remaining, currentCharge);
                targetCharge = Settings.MinimumCharge;

                if (battery.IsBelowMinimumCharge)
                {
                    sb.AppendLine();
                    sb.AppendFormat(Resources.BelowMinimumCharge, Settings.MinimumCharge);
                    targetCharge = 0;
                }
            }

            if (battery.CurrentTime.TotalSeconds > 0)
            {
                sb.AppendLine();
                FormatTime(sb, battery, targetCharge);
            }

            return(sb.ToString());
        }
Ejemplo n.º 4
0
        private NotifyIcon notifyIcon;  // The icon that sits in the system tray

        public async Task InitializeContextAsync(BatteryData testBatteryData = null)
        {
            components = new Container();
            notifyIcon = new NotifyIcon(components)
            {
                ContextMenuStrip = new ContextMenuStrip(),
            };

            notifyIcon.DoubleClick += (s, e) => ShowDetailsForm();

            var detailsItem = new ToolStripMenuItem
            {
                Text = "Show &Details"
            };

            detailsItem.Click += (s, e) => ShowDetailsForm();
            notifyIcon.ContextMenuStrip.Items.Add(detailsItem);

            var restartItem = new ToolStripMenuItem
            {
                Text = "R&estart"
            };

            restartItem.Click += (s, e) => Restart();
            notifyIcon.ContextMenuStrip.Items.Add(restartItem);

            var exitItem = new ToolStripMenuItem
            {
                Text = "E&xit"
            };

            exitItem.Click += (s, e) => ExitThread();
            notifyIcon.ContextMenuStrip.Items.Add(exitItem);

            batteryIconManager = new BatteryIconManager();
            await batteryIconManager.InitializeDataAsync(testBatteryData);

            // Handle initial update here. Setting notifyicon visible from BatteryManager thread causes the contextmenu to hang.
            UpdateIcon();
            notifyIcon.Visible = true;
            // ShowBalloonTip only works when notifyicon is visble so any initial message (like battery not found) must run here.
            ShowBalloon();

            screenBounds = Screen.PrimaryScreen.Bounds;
            SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;

            batteryIconManager.BatteryChanged += (s, e) => UpdateIcon();
            batteryIconManager.Start();
        }
Ejemplo n.º 5
0
 private static void FormatTime(StringBuilder sb, BatteryData battery, int targetCharge)
 {
     if (battery.CurrentTime.Hours > 0 && battery.CurrentTime.Minutes > 0)
     {
         sb.AppendFormat(Resources.HourMin, battery.CurrentTime.Hours, battery.CurrentTime.Minutes, targetCharge);
     }
     else if (battery.CurrentTime.Hours > 0)
     {
         sb.AppendFormat(Resources.Hour, battery.CurrentTime.Hours, targetCharge);
     }
     else // This can return "0 min" and that's fine
     {
         sb.AppendFormat(Resources.Min, battery.CurrentTime.Minutes, targetCharge);
     }
 }
Ejemplo n.º 6
0
        public async Task InitializeDataAsync(BatteryData testBatteryData = null)
        {
            this.testBatteryData = testBatteryData;

            if (this.testBatteryData == null)
            {
                var device = new BatteryDevice();
                battery = await device.GetBatteryAsync();

                if (battery != null)
                {
                    // Based on observation this fires immediately when power status changes. But when unchanged it
                    // fires in 3-6 minutes intervals (which is useless of course).
                    battery.ReportUpdated += (s, e) => OnBatteryReportUpdated();
                }
            }

            InitializeTheme();
        }
Ejemplo n.º 7
0
        public Color GetColor(BatteryData battery)
        {
            if (battery.IsCriticalCharge)
            {
                return(Settings.CriticalColor);
            }

            if (battery.IsBelowMinimumCharge || battery.IsAboveMaximumCharge)
            {
                return(Settings.WarningColor);
            }

            if (battery.IsCharging || battery.IsPluggedInNotCharging)
            {
                return(Settings.ChargingColor);
            }

            return(Settings.DrainingColor);
        }
Ejemplo n.º 8
0
        private bool Update()
        {
            var currentBatteryData = testBatteryData != null?testBatteryData.GetNextTestData() : new BatteryData(battery);

            // These conditions all forces icon (re)drawing, even if there's no change in CurrentCharge or the change is too small
            // to cause the number of levels to update.
            var drawIcon = windowsTheme != currentWindowsTheme ||
                           batteryData == null ||
                           batteryData.IsCharging != currentBatteryData.IsCharging ||
                           batteryData.IsAboveMaximumCharge != currentBatteryData.IsAboveMaximumCharge ||
                           batteryData.IsBelowMinimumCharge != currentBatteryData.IsBelowMinimumCharge ||
                           batteryData.IsCriticalCharge != currentBatteryData.IsCriticalCharge;

            if (drawIcon ||
                batteryData.CurrentCharge != currentBatteryData.CurrentCharge ||
                batteryData.CurrentTime != currentBatteryData.CurrentTime ||
                batteryData.IsPluggedInNotCharging != currentBatteryData.IsPluggedInNotCharging)
            {
                if (currentBatteryData.IsNotAvailable)
                {
                    CreateBatteryUpdateText(Resources.BatteryNotFound);
                    CreateBatteryWarningText(Resources.BatteryNotFound);

                    CreateBatteryIcon(currentBatteryData, currentWindowsTheme, drawIcon);
                }
                else
                {
                    Log.Write("Battery: {0}", currentBatteryData);

                    var currentUpdateText = TextFormatter.FormatBatteryUpdateText(currentBatteryData);
                    CreateBatteryUpdateText(currentUpdateText);
                    WarningText = null;

                    CreateBatteryIcon(currentBatteryData, currentWindowsTheme, drawIcon);
                }

                batteryData  = currentBatteryData;
                windowsTheme = currentWindowsTheme;
                return(true);
            }
            return(false);
        }