Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
Logging implemented
  • Loading branch information
Codectory committed Jan 11, 2021
1 parent 8563499 commit 14a6df5
Show file tree
Hide file tree
Showing 18 changed files with 232 additions and 57 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ mono_crash.*
[Rr]eleases/
x64/
x86/
Debug_x64/
Debug_x86/
Release_x64/
Release_x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
Expand Down Expand Up @@ -351,3 +355,5 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/
Source/Debug_x64/de/HDRProfile.resources.dll
Source/Debug_x64/HDRProfile.exe
Binary file modified Source/Debug_x64/HDRProfile.exe
Binary file not shown.
Binary file modified Source/Debug_x64/de/HDRProfile.resources.dll
Binary file not shown.
Binary file added Source/Externals/CodectoryCore.Logging.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Source/HDRProfile/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<system:String x:Key="DonateLink">https://paypal.me/pools/c/8vksshrMln</system:String>
<system:String x:Key="InfoLink">https://sourceforge.net/projects/hdr-profile</system:String>
<system:String x:Key="Version">1.1.0</system:String>
<system:String x:Key="Version">1.2.0</system:String>
<SolidColorBrush x:Key="ButtonBackground" Color="#FF0086F5"/>
<SolidColorBrush x:Key="AccentColor" Color="#FFFF581A"/>
<SolidColorBrush x:Key="AccentColor2" Color="#4C87B3"/>
Expand Down
1 change: 1 addition & 0 deletions Source/HDRProfile/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override void OnStartup(StartupEventArgs e)
if (mutex.WaitOne(TimeSpan.Zero, true))
{
mutex.ReleaseMutex();

}
else
{
Expand Down
4 changes: 4 additions & 0 deletions Source/HDRProfile/HDRProfile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="CodectoryCore.Logging">
<HintPath>..\Externals\CodectoryCore.Logging.dll</HintPath>
</Reference>
<Reference Include="CodectoryCore.UI.Wpf, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Release\CodectoryCore.UI.Wpf.dll</HintPath>
Expand Down Expand Up @@ -87,6 +90,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System.ServiceModel" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Windows.Interactivity">
<HintPath>..\Externals\System.Windows.Interactivity.dll</HintPath>
</Reference>
Expand Down
178 changes: 133 additions & 45 deletions Source/HDRProfile/HDRProfileHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CodectoryCore.UI.Wpf;
using CodectoryCore.Logging;
using CodectoryCore.UI.Wpf;
using Hardcodet.Wpf.TaskbarNotification;
using Microsoft.Win32;
using System;
Expand All @@ -23,9 +24,12 @@ namespace HDRProfile
{
public class HDRProfileHandler : BaseViewModel
{

public static Logs Logs = new Logs($"{System.AppDomain.CurrentDomain.BaseDirectory}HDRProfile.log", "HDRPProfile", true);

Dictionary<ApplicationItem, bool> _lastApplicationStates = new Dictionary<ApplicationItem, bool>();

private string SettingsPath => $"{System.AppDomain.CurrentDomain.BaseDirectory}\\HDRProfile_Settings.xml";
private string SettingsPath => $"{System.AppDomain.CurrentDomain.BaseDirectory}HDRProfile_Settings.xml";
TaskbarIcon TrayMenu;
readonly object _accessLock = new object();

Expand Down Expand Up @@ -77,7 +81,7 @@ private void Initialize()
{
if (Initialized)
return;

Logs.Add("Initializing...", false);
ProcessWatcher = new ProcessWatcher();
HDRSwitcherHandler = new HDRController();
LoadSettings();
Expand All @@ -86,53 +90,89 @@ private void Initialize()
SwitchTrayIcon(Settings.StartMinimizedToTray);
ShowView = !Settings.StartMinimizedToTray;
Initialized = true;
Logs.Add("Initialized", false);

}
}

private void LoadSettings()
{
Logs.Add("Loading settings..", false);
try
{
Settings = HDRProfileSettings.ReadSettings(SettingsPath);
}
catch (Exception)
catch (Exception ex)
{
Logs.Add("Failed to load settings", false);
Logs.AddException(ex);
Settings = new HDRProfileSettings();
Settings.SaveSettings(SettingsPath);
SaveSettings();
Logs.Add("Created new settings file", false);

}

Settings.ApplicationItems.CollectionChanged += ApplicationItems_CollectionChanged;
settings.PropertyChanged += Settings_PropertyChanged;
Logs.LoggingEnabled = Settings.Logging;
foreach (var application in Settings.ApplicationItems)
{
ProcessWatcher.AddProcess(application);
application.PropertyChanged += ApplicationItem_PropertyChanged;
}

Logs.Add("Settings loaded", false);
}

private void InitializeTrayMenu()
{
TrayMenu = new TaskbarIcon();
TrayMenu.Visibility = Visibility.Hidden;
TrayMenu.ToolTipText = Locale_Texts.HDRProfile;
TrayMenu.Icon = Locale_Texts.Logo;
ContextMenu contextMenu = new ContextMenu();
MenuItem close = new MenuItem()
Logs.Add("Initializing tray menu", false);
try
{
Header = Locale_Texts.Shutdown
};
close.Click += (o, e) => Shutdown();
TrayMenu = new TaskbarIcon();
TrayMenu.Visibility = Visibility.Hidden;
TrayMenu.ToolTipText = Locale_Texts.HDRProfile;
TrayMenu.Icon = Locale_Texts.Logo;
ContextMenu contextMenu = new ContextMenu();
MenuItem close = new MenuItem()
{
Header = Locale_Texts.Shutdown
};
close.Click += (o, e) => Shutdown();

MenuItem open = new MenuItem()
{
Header = Locale_Texts.Open
};
open.Click += (o, e) => SwitchTrayIcon(false);
MenuItem open = new MenuItem()
{
Header = Locale_Texts.Open
};
open.Click += (o, e) => SwitchTrayIcon(false);


MenuItem activateHDR = new MenuItem()
{
Header = Locale_Texts.ActivateHDR
};
activateHDR.Click += (o,e) => HDRController.SetHDR(true);

MenuItem deactivateHDR = new MenuItem()
{
Header = Locale_Texts.DeactivateHDR
};
deactivateHDR.Click += (o, e) => HDRController.SetHDR(false);

contextMenu.Items.Add(open);
contextMenu.Items.Add(close);
TrayMenu.ContextMenu = contextMenu;
TrayMenu.TrayLeftMouseDown += TrayMenu_TrayLeftMouseDown;
contextMenu.Items.Add(open);
contextMenu.Items.Add(activateHDR);
contextMenu.Items.Add(deactivateHDR);
contextMenu.Items.Add(close);

TrayMenu.ContextMenu = contextMenu;
TrayMenu.TrayLeftMouseDown += TrayMenu_TrayLeftMouseDown;
Logs.Add("Tray menu initialized", false);

}
catch (Exception ex)
{
Logs.AddException(ex);
throw ex;
}
}

private void CreateRelayCommands()
Expand All @@ -156,14 +196,16 @@ private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
Tools.SetAutoStart(Locale_Texts.HDRProfile, System.Reflection.Assembly.GetEntryAssembly().Location, settings.AutoStart);
if (e.PropertyName.Equals(nameof(Settings.HDRMode)))
UpdateHDRMode();
Settings.SaveSettings(SettingsPath);
Logs.LoggingEnabled = Settings.Logging;
SaveSettings();
}
}



private void TrayMenu_TrayLeftMouseDown(object sender, RoutedEventArgs e)
{
Logs.Add("Open app from Tray", false);
SwitchTrayIcon(false);
ShowView = true;

Expand All @@ -172,14 +214,23 @@ private void TrayMenu_TrayLeftMouseDown(object sender, RoutedEventArgs e)

private void StartApplication(ApplicationItem application)
{
HDRController.SetHDR(true);
System.Threading.Thread.Sleep(3000);
Process process = new Process();
process.StartInfo = new ProcessStartInfo(application.ApplicationFilePath);
process.Start();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
System.Threading.Thread.Sleep(3000);
Logs.Add($"Start application {application.ApplicationName}", false);
try
{
HDRController.SetHDR(true);
System.Threading.Thread.Sleep(3000);
Process process = new Process();
process.StartInfo = new ProcessStartInfo(application.ApplicationFilePath);
process.Start();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
System.Threading.Thread.Sleep(3000);
}
catch (Exception ex)
{
Logs.AddException(ex);
throw ex;
}
}


Expand All @@ -191,10 +242,12 @@ private void Closing()
{
if (Settings.CloseToTray)
{
Logs.Add($"Minimizing to tray...", false);
SwitchTrayIcon(true);
}
else
{
Logs.Add($"Shutting down...", false);
Shutdown();
}
}
Expand All @@ -208,31 +261,36 @@ private void Shutdown()

public void Start()
{
if (Started)
return;
lock (_accessLock)
{
if (Started)
return;
Logs.Add($"Starting process watcher...", false);
ProcessWatcher.OneProcessIsRunningChanged += ProcessWatcher_RunningOrFocusedChanged;
ProcessWatcher.OneProcessIsFocusedChanged += ProcessWatcher_RunningOrFocusedChanged;

Started = true;
ProcessWatcher.Start();
Logs.Add($"Process watcher started", false);

}
}

public void Stop()
{
if (!Started)
return;
lock (_accessLock)
{
if (!Started)
return;
Logs.Add($"Stopping process watcher...", false);

ProcessWatcher.OneProcessIsRunningChanged -= ProcessWatcher_RunningOrFocusedChanged;
ProcessWatcher.OneProcessIsFocusedChanged -= ProcessWatcher_RunningOrFocusedChanged;

ProcessWatcher.Stop();
HDRSwitcherHandler.DeactivateHDR();
Started = false;
Logs.Add($"Process watcher stopped", false);

}

}
Expand All @@ -253,6 +311,7 @@ private void ApplicationItems_CollectionChanged(object sender, NotifyCollectionC
case NotifyCollectionChangedAction.Add:
foreach (var applicationItem in e.NewItems)
{
Logs.Add($"Application added: {((ApplicationItem)applicationItem).ApplicationName}", false);
ProcessWatcher.AddProcess(((ApplicationItem)applicationItem));
((ApplicationItem)applicationItem).PropertyChanged += ApplicationItem_PropertyChanged;
}
Expand All @@ -261,20 +320,37 @@ private void ApplicationItems_CollectionChanged(object sender, NotifyCollectionC
case NotifyCollectionChangedAction.Remove:
foreach (var applicationItem in e.OldItems)
{
Logs.Add($"Application removed: {((ApplicationItem)applicationItem).ApplicationName}", false);
ProcessWatcher.RemoveProcess(((ApplicationItem)applicationItem));
((ApplicationItem)applicationItem).PropertyChanged -= ApplicationItem_PropertyChanged;

}
break;

}

SaveSettings();
}
}

private void SaveSettings()
{
Logs.Add("Saving settings..", false);
try
{
Settings.SaveSettings(SettingsPath);
Logs.Add("Settings saved", false);

}
catch (Exception ex)
{
Logs.AddException(ex);
}
}

private void ApplicationItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Settings.SaveSettings(SettingsPath);
SaveSettings();
}

private void AddAplication()
Expand Down Expand Up @@ -306,13 +382,25 @@ private void UpdateHDRMode()
{
lock (_accessLock)
{
if ((Settings.HDRMode == HDRMode.Running && ProcessWatcher.OneProcessIsRunning) || Settings.HDRMode == HDRMode.Focused && ProcessWatcher.OneProcessIsFocused)
try
{
Logs.Add($"Updating HDR mode to {Settings.HDRMode}...", false);

if ((Settings.HDRMode == HDRMode.Running && ProcessWatcher.OneProcessIsRunning) || Settings.HDRMode == HDRMode.Focused && ProcessWatcher.OneProcessIsFocused)
{
HDRSwitcherHandler.ActivateHDR();
CheckIfRestartIsNecessary((IDictionary<ApplicationItem, bool>)ProcessWatcher.Applications);
}
else if (Settings.HDRMode != HDRMode.None)
HDRSwitcherHandler.DeactivateHDR();
Logs.Add($"HDR mode updated to {Settings.HDRMode}", false);

}
catch (Exception ex)
{
HDRSwitcherHandler.ActivateHDR();
CheckIfRestartIsNecessary((IDictionary<ApplicationItem, bool>)ProcessWatcher.Applications);
Logs.AddException(ex);
throw ex;
}
else if (Settings.HDRMode != HDRMode.None)
HDRSwitcherHandler.DeactivateHDR();
}
}

Expand All @@ -330,13 +418,13 @@ private void CheckIfRestartIsNecessary(IDictionary<ApplicationItem, bool> applic
else if (_lastApplicationStates.ContainsKey(applicationState.Key) && applicationState.Value && !_lastApplicationStates[applicationState.Key])
RestartProcess(applicationState.Key);
}

_lastApplicationStates.Clear();
_lastApplicationStates = newLastStates;
}

private void RestartProcess(ApplicationItem application)
{
Logs.Add($"Restarting application {application.ApplicationName}", false);
Process.GetProcessesByName(application.ApplicationName).ToList().ForEach(p => p.Kill());
Process proc = new Process();
StartApplication(application);
Expand Down
2 changes: 1 addition & 1 deletion Source/HDRProfile/HDRProfileHandlerView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<RowDefinition />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<local:HDRProfileSettingsView Grid.Row="0" DataContext="{Binding ElementName=MainWindow , Path=DataContext.Settings}" Margin="10,10,10,0" Grid.Column="0" Grid.ColumnSpan="4" VerticalAlignment="Top" Height="107"/>
<local:HDRProfileSettingsView Grid.Row="0" DataContext="{Binding ElementName=MainWindow , Path=DataContext.Settings}" Margin="10" Grid.Column="0" Grid.ColumnSpan="4"/>

<Button Grid.Column="3" Grid.Row="1" Content="{x:Static local:Locale_Texts.Shutdown}" Command="{Binding ShutdownCommand}" Margin="5,5,10,5" RenderTransformOrigin="0.914,0.566" HorizontalAlignment="Right" Width="113" Height="20" VerticalAlignment="Bottom"/>
<TextBlock Text="{Binding Mode=OneWay, Source={StaticResource Version}, StringFormat= V\{0\} }" Width="Auto" Margin="10,5,5,5" Grid.Row="1" Grid.Column="0" Height="13" HorizontalAlignment="Right" />
Expand Down
Loading

0 comments on commit 14a6df5

Please sign in to comment.