Skip to content

Commit

Permalink
Add icon and minimize to tray functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
thomazmoura committed May 9, 2023
2 parents 4d5ca7d + 5949f53 commit 437f2ca
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 8 deletions.
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net6.0-windows/win-x64/SpotlightDimmer.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/SpotlightDimmer.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SpotlightDimmer.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/SpotlightDimmer.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
5 changes: 4 additions & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
<StackPanel Orientation="Horizontal" Margin="10,10,10,10" Grid.ColumnSpan="3">
<StackPanel Orientation="Vertical" Margin="10" Name="SettingsPanel">
<Label Content="Settings:" VerticalAlignment="Center"/>
<CheckBox Content="TopMost" IsChecked="{Binding Topmost, Mode=TwoWay}" Margin="5"></CheckBox>
<StackPanel Orientation="Horizontal">
<CheckBox Content="TopMost" IsChecked="{Binding Topmost, Mode=TwoWay}" Margin="5"></CheckBox>
<CheckBox Content="Minimize to Tray" IsChecked="{Binding MinimizeToTray, Mode=TwoWay}" Margin="5"></CheckBox>
</StackPanel>
<wpf:ColorCanvas x:Name="colorPicker"
SelectedColor="{Binding SelectedColor, Mode=TwoWay}"
UsingAlphaChannel="True"/>
Expand Down
67 changes: 62 additions & 5 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Windows.Media;
using SpotlightDimmer.Models;
using System;
using System.Windows.Interop;
using System.Windows.Media.Imaging;

namespace SpotlightDimmer
{
Expand All @@ -14,13 +16,15 @@ public partial class MainWindow : Window
public WindowsEventsManager _dimmerStateManager;
public DimmerState _state;
private List<Window> _dimmerWindows;
private NotifyIcon _notifyIcon;

public MainWindow()
{
InitializeComponent();

SetApplicationIcon();
BuildTheViewModel();

SetMinimizeToTrayOptions();
CreateTheDimmerWindows();
Closing += OnClosing;
}
Expand All @@ -44,27 +48,80 @@ protected void CreateTheDimmerWindows()
}
}

private void saveSettingsButton_Click(object sender, RoutedEventArgs e)
private void SetApplicationIcon()
{
var icon = GetSpotlightDimmerIcon();
var handle = icon.Handle;
this.Icon = Imaging.CreateBitmapSourceFromHIcon(handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

private void SetMinimizeToTrayOptions()
{
// Create the NotifyIcon object
_notifyIcon = new NotifyIcon();
_notifyIcon.Icon = GetSpotlightDimmerIcon();
_notifyIcon.Text = "Spotlight Dimmer";

_notifyIcon.BalloonTipText = "The application is still running on the system tray. Click here to open it again";
_notifyIcon.BalloonTipTitle = "Spotlight Dimmer";
_notifyIcon.BalloonTipIcon = ToolTipIcon.Info;

_notifyIcon.Visible = true;

// Handle the StateChanged event of the Window
this.StateChanged += MainWindow_StateChanged;

// Handle the DoubleClick event of the NotifyIcon
_notifyIcon.Click += NotifyIcon_Click;
}

private void saveSettingsButton_Click(object? sender, RoutedEventArgs e)
{
_dimmerSettings.SaveSettings();
}

private void Window_Activated(object sender, EventArgs e)
private void Window_Activated(object? sender, EventArgs e)
{
this.Activate();
this.Focus();
}


private void MainWindow_StateChanged(object? sender, System.EventArgs e)
{
if (WindowState == WindowState.Minimized && _state.MinimizeToTray)
{
Hide();
_notifyIcon.ShowBalloonTip((int)TimeSpan.FromSeconds(5).TotalMilliseconds);
}
}

private void NotifyIcon_Click(object? sender, System.EventArgs e)
{
Show();
this.Activate();
this.Focus();
WindowState = WindowState.Normal;
}

private void DebugInfoTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
private void DebugInfoTextBox_TextChanged(object? sender, System.Windows.Controls.TextChangedEventArgs e)
{
DebugInfoTextBox.ScrollToEnd();
}

private void OnClosing(object sender, CancelEventArgs e)
private void OnClosing(object? sender, CancelEventArgs e)
{
foreach (var childWindow in _dimmerWindows)
childWindow.Close();
_dimmerStateManager.Dispose();

_notifyIcon.Dispose();
}

private System.Drawing.Icon GetSpotlightDimmerIcon()
{
using var stream = typeof(MainWindow).Assembly.GetManifestResourceStream("SpotlightDimmer.ico");
return new System.Drawing.Icon(stream);
}
}
}
25 changes: 24 additions & 1 deletion Models/DimmerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public DimmerSettings(DimmerState state)
_configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
_state.SelectedColor = GetColorFromSettings();
_state.Topmost = GetTopmostFromSettings();
_state.MinimizeToTray = GetMinimizeToTrayFromSettings();
_state.DebugInfo = $"Saved Settings: \r\n{GetSavedSettings()}";
}

Expand Down Expand Up @@ -78,6 +79,23 @@ public bool GetTopmostFromSettings()
}
}

public bool GetMinimizeToTrayFromSettings()
{
var fallbackValue = true;
try
{
string? minimizeToTray = _configuration.AppSettings?.Settings["MinimizeToTray"]?.Value;
minimizeToTray ??= fallbackValue.ToString();

return bool.Parse(minimizeToTray);
}
catch (Exception ex)
{
_state.DebugInfo = ex.ToString();
return fallbackValue;
}
}

public string CurrentSavedColor => _configuration.AppSettings.Settings["BackgroundHex"] != null?
$"#{_configuration.AppSettings.Settings["BackgroundHex"].Value}":
"No saved configuration found";
Expand All @@ -98,10 +116,15 @@ public void SaveSettings()
else
_configuration.AppSettings.Settings["Topmost"].Value = _state.Topmost.ToString();

if (_configuration.AppSettings.Settings["MinimizeToTray"] == null)
_configuration.AppSettings.Settings.Add("MinimizeToTray", _state.MinimizeToTray.ToString());
else
_configuration.AppSettings.Settings["MinimizeToTray"].Value = _state.MinimizeToTray.ToString();

_configuration.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("appSettings");

_state.DebugInfo = $"Settings saved successfuly.\r\nSaved color: {_state.SelectedColor}\r\nTopmost: {_state.Topmost}";
_state.DebugInfo = $"Settings saved successfuly.\r\nSaved color: {_state.SelectedColor}\r\nTopmost: {_state.Topmost}\r\nMinimizeToTray: {_state.MinimizeToTray}";
}
catch (Exception ex)
{
Expand Down
11 changes: 11 additions & 0 deletions Models/DimmerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public bool Topmost
}
}

private bool _minimizeToTray = false;
public bool MinimizeToTray
{
get { return _minimizeToTray; }
set
{
_minimizeToTray = value;
OnPropertyChanged(nameof(MinimizeToTray));
}
}

private Screen _focusedScreen = Screen.PrimaryScreen;
public Screen FocusedScreen
{
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ Feature List (✔ is working, 📅 is planned and ❓is wish that might eventual
✔ - Support for different sized monitors
✔ - Color and transparency customization
✔ - Optional persistence of chosen colors and transparency
✔ - Minimizing to tray
📅 - Support for changing number of monitors
📅 - Minimizing to tray
📅 - Pipeline for updating releases automatically
❓ - Installation from winget
❓ - Option to temporarily follow mouse position on cursor movement instead of focused window (would help with realizing where the mouse is when you have to resort to it)

Icon credits

"Light Bulb or Idea Flat Icon Vector" by VideoPlasty is licensed under CC BY-SA 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-sa/4.0/?ref=openverse.
4 changes: 4 additions & 0 deletions SpotlightDimmer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@
<PackageReference Include="Extended.WPF.Toolkit" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="icon.ico" LogicalName="SpotlightDimmer.ico" />
</ItemGroup>

</Project>
Binary file added icon.ico
Binary file not shown.

0 comments on commit 437f2ca

Please sign in to comment.