Skip to content

Commit

Permalink
fix some issues, add LG aspect ratio/zoom actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Maassoft committed Jul 16, 2022
1 parent 5836f88 commit a1121c1
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 86 deletions.
16 changes: 8 additions & 8 deletions ColorControl/ColorControl.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<TargetFramework>net6.0-windows10.0.20348.0</TargetFramework>
<OutputType>WinExe</OutputType>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
Expand All @@ -18,8 +18,8 @@
<PublisherName>Maassoft</PublisherName>
<Company>Maassoft</Company>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>7.0.0.0</ApplicationVersion>
<Version>7.0.0.0</Version>
<ApplicationVersion>7.1.0.0</ApplicationVersion>
<Version>7.1.0.0</Version>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>false</BootstrapperEnabled>
Expand All @@ -28,7 +28,7 @@
<UseWPF>false</UseWPF>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<ApplicationIcon>pngbarn.ico</ApplicationIcon>
<SupportedOSPlatformVersion>10.0.17763.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion>10.0.20348.0</SupportedOSPlatformVersion>
<Copyright>2020-2022 Maassoft</Copyright>
<Authors>Maassoft</Authors>
<ManifestCertificateThumbprint>CF0BC153EA664CD97AC6F234B91F45D34630958F</ManifestCertificateThumbprint>
Expand Down Expand Up @@ -89,11 +89,11 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NLog" Version="4.7.13" />
<PackageReference Include="NStandard" Version="0.8.28" />
<PackageReference Include="NLog" Version="5.0.1" />
<PackageReference Include="NStandard" Version="0.9.13" />
<PackageReference Include="NvAPIWrapper.Net" Version="0.8.1.101" />
<PackageReference Include="NWin32" Version="1.2.0" />
<PackageReference Include="TaskScheduler" Version="2.9.3" />
<PackageReference Include="NWin32" Version="1.2.3" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nspector\Nspector.csproj" />
Expand Down
5 changes: 5 additions & 0 deletions ColorControl/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -814,5 +814,10 @@ public static FileInfo SelectFile(string ext = "*.exe", string filter = "Applica

return null;
}

public static bool NormEquals(this string str1, string str2)
{
return str1.Trim().Replace(" ", string.Empty).Equals(str2.Trim().Replace(" ", string.Empty), StringComparison.OrdinalIgnoreCase);
}
}
}
35 changes: 28 additions & 7 deletions ColorControl/Forms/FormUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,28 @@ public static void SetNotifyIconText(NotifyIcon ni, string text)
t.GetMethod("UpdateIcon", hidden).Invoke(ni, new object[] { true });
}

public static ToolStripMenuItem BuildDropDownMenuEx(ContextMenuStrip mnuParent, string name, Type enumType, EventHandler clickEvent, object tag = null, int min = 0, int max = 0)
public static ToolStripMenuItem BuildDropDownMenuEx(ToolStripItemCollection items, string parentName, string name, Type enumType, EventHandler clickEvent, object tag = null, int min = 0, int max = 0, bool noSubItems = false)
{
var subMenuName = $"{mnuParent.Name}_{name}";
var subMenuItems = mnuParent.Items.Find(subMenuName, false);
var subMenuName = $"{parentName}_{name}";
var subMenuItems = items.Find(subMenuName, false);

if (subMenuItems.Length > 0)
{
return subMenuItems[0] as ToolStripMenuItem;
}

ToolStripMenuItem subMenuItem;
subMenuItem = (ToolStripMenuItem)mnuParent.Items.Add(name);
subMenuItem = (ToolStripMenuItem)items.Add(name);
subMenuItem.Name = subMenuName;

if (noSubItems)
{
subMenuItem.Tag = tag;
subMenuItem.Click += clickEvent;

return subMenuItem;
}

if (enumType != null)
{
foreach (var enumValue in Enum.GetValues(enumType))
Expand All @@ -81,12 +89,25 @@ public static ToolStripMenuItem BuildDropDownMenuEx(ContextMenuStrip mnuParent,
item.Click += clickEvent;
}
}
else if (min >= 0 && max > min)
else if (max > min)
{
for (var i = 0; i <= 10; i++)
List<int> range;

if (max - min <= 20)
{
var value = i * (max / 10);
range = Enumerable.Range(min, (max - min) + 1).ToList();
}
else
{
range = new List<int>();
for (var i = 0; i <= 10; i++)
{
range.Add(i * (max / 10));
}
}

foreach (var value in range)
{
var subSubItemName = $"{subMenuName}_{value}";

var item = subMenuItem.DropDownItems.Add(value.ToString());
Expand Down
137 changes: 106 additions & 31 deletions ColorControl/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ private bool HandleExternalServiceForLgDevice(string serviceName, string[] param
{
return _nvService.ApplyPreset(parameters[0]);
}
if (_nvService != null && serviceName.Equals("GsyncEnabled", StringComparison.OrdinalIgnoreCase))
{
return _nvService.IsGsyncEnabled();
}
if (_amdService != null && serviceName.Equals("AmdPreset", StringComparison.OrdinalIgnoreCase))
{
return _amdService.ApplyPreset(parameters[0]);
Expand Down Expand Up @@ -2055,10 +2059,29 @@ private void LoadLog()
{
var filename = Path.Combine(_dataDir, "LogFile.txt");

var lines = new[] { "No log file found" };
if (File.Exists(filename))
var lines = new List<string> { "No log file found" };

try
{
if (File.Exists(filename))
{
using var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using var reader = new StreamReader(fs);

String line;

while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}

//lines = File.ReadAllLines(filename);
}
}
catch (Exception ex)
{
lines = File.ReadAllLines(filename);
lines = new List<string> { $"Cannot load log file: {ex.Message}" };
}
var reversedLines = lines.ToList();
var builder = new StringBuilder();
Expand Down Expand Up @@ -2139,6 +2162,18 @@ private void miLgAddAction_Click(object sender, EventArgs e)
var item = sender as ToolStripItem;
var action = item.Tag as LgDevice.InvokableAction;

var value = ShowLgActionForm(action);

if (!string.IsNullOrEmpty(value))
{
var text = $"{action.Name}({value})";

FormUtils.AddStepToTextBox(edtStepsLg, text);
}
}

private string ShowLgActionForm(LgDevice.InvokableAction action)
{
var text = action.Name;
var title = action.Title ?? action.Name;

Expand All @@ -2154,7 +2189,7 @@ private void miLgAddAction_Click(object sender, EventArgs e)
{
fields.Add(new MessageForms.FieldDefinition
{
Label = "Enter desired " + item.Text,
Label = "Enter desired " + title,
FieldType = MessageForms.FieldType.Numeric,
MinValue = action.MinValue,
MaxValue = action.MaxValue,
Expand All @@ -2173,11 +2208,11 @@ private void miLgAddAction_Click(object sender, EventArgs e)
}));
}

var values = MessageForms.ShowDialog("Enter value", fields);
var values = MessageForms.ShowDialog($"Enter value for {title}", fields);

if (!values.Any())
{
return;
return null;
}

if (values.Count > 1)
Expand Down Expand Up @@ -2210,7 +2245,7 @@ private void miLgAddAction_Click(object sender, EventArgs e)

if (!values.Any())
{
return;
return null;
}

value = values.First().Value.ToString();
Expand All @@ -2221,12 +2256,7 @@ private void miLgAddAction_Click(object sender, EventArgs e)
}
}

if (!string.IsNullOrEmpty(value))
{
text += $"({value})";
}

FormUtils.AddStepToTextBox(edtStepsLg, text);
return value;
}

private void miLgAddNvPreset_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -3112,31 +3142,40 @@ private void mnuLgExpert_Opening(object sender, CancelEventArgs e)

const string gameBarName = "miGameBar";

foreach (var action in actions.Where(a => !a.Name.Contains("uhd", StringComparison.OrdinalIgnoreCase) &&
var expertActions = actions.Where(a => !a.Name.Contains("uhd", StringComparison.OrdinalIgnoreCase) &&
!a.Name.Contains("gameOpt", StringComparison.OrdinalIgnoreCase) &&
!a.Name.Contains("hdmiPc", StringComparison.OrdinalIgnoreCase) &&
(a.EnumType != null || a.MinValue >= 0 && a.MaxValue > a.MinValue)))
(a.EnumType != null || a.MaxValue > a.MinValue)).ToList();

var categories = expertActions.Select(a => a.Category ?? "misc").Where(c => !string.IsNullOrEmpty(c)).Distinct();

foreach (var category in categories)
{
var menu = FormUtils.BuildDropDownMenuEx(mnuLgExpert, action.Title, action.EnumType, btnLgExpertColorGamut_Click, action, (int)action.MinValue, (int)action.MaxValue);
var catMenuItem = FormUtils.BuildDropDownMenuEx(mnuLgExpert.Items, mnuLgExpert.Name, Utils.FirstCharUpperCase(category), null, null, category);

if (!gameBarActions.Contains(action))
foreach (var action in expertActions.Where(a => (a.Category ?? "misc") == category))
{
continue;
}
var menu = FormUtils.BuildDropDownMenuEx(catMenuItem.DropDownItems, catMenuItem.Name, action.Title, action.EnumType, btnLgExpertColorGamut_Click, action, (int)action.MinValue, (int)action.MaxValue, action.NumberOfValues > 1);

var itemName = $"{menu.Name}_{gameBarName}";
var gameBarItem = (ToolStripMenuItem)menu.DropDownItems.Find(itemName, false).FirstOrDefault();
if (!gameBarActions.Contains(action))
{
continue;
}

if (gameBarItem == null)
{
var separator = new ToolStripSeparator();
menu.DropDownItems.Add(separator);
var itemName = $"{menu.Name}_{gameBarName}";
var gameBarItem = (ToolStripMenuItem)menu.DropDownItems.Find(itemName, false).FirstOrDefault();

gameBarItem = (ToolStripMenuItem)menu.DropDownItems.Add("Show in Game Bar", null, miLgGameBarToggle_Click);
gameBarItem.CheckOnClick = true;
gameBarItem.Checked = activatedGameBarActions.Contains(action);
gameBarItem.Name = itemName;
gameBarItem.Tag = action.Name;
if (gameBarItem == null)
{
var separator = new ToolStripSeparator();
menu.DropDownItems.Add(separator);

gameBarItem = (ToolStripMenuItem)menu.DropDownItems.Add("Show in Game Bar", null, miLgGameBarToggle_Click);
gameBarItem.CheckOnClick = true;
gameBarItem.Checked = activatedGameBarActions.Contains(action);
gameBarItem.Name = itemName;
gameBarItem.Tag = action.Name;
}
}
}

Expand All @@ -3157,7 +3196,7 @@ private void mnuLgExpert_Opening(object sender, CancelEventArgs e)

foreach (var presetAction in presetActions)
{
var menu = FormUtils.BuildDropDownMenuEx(mnuLgExpert, presetAction.Name, null, btnLgExpertPresetAction_Click, presetAction.Preset);
var menu = FormUtils.BuildDropDownMenuEx(mnuLgExpert.Items, mnuLgExpert.Name, presetAction.Name, null, btnLgExpertPresetAction_Click, presetAction.Preset);
}
}

Expand Down Expand Up @@ -3192,9 +3231,29 @@ private void btnLgExpertColorGamut_Click(object sender, EventArgs e)
var action = item.Tag as LgDevice.InvokableAction;
var value = item.AccessibleName ?? item.Text;

if (action.NumberOfValues > 1)
{
ApplyLgExpertValueRange(action);
return;
}

_lgService.SelectedDevice?.ExecuteAction(action, new[] { value });
}

private void ApplyLgExpertValueRange(LgDevice.InvokableAction action)
{
var value = ShowLgActionForm(action);

if (value == null)
{
return;
}

var values = value.Split(";");

_lgService.SelectedDevice?.ExecuteAction(action, values);
}

private void btnLgExpertPresetAction_Click(object sender, EventArgs e)
{
var item = sender as ToolStripItem;
Expand Down Expand Up @@ -3850,6 +3909,14 @@ private void ListViewItemChecked<T>(ListView listView, ItemCheckedEventArgs e) w
return;
}

var point = listView.PointToClient(Cursor.Position);

if (point.X >= 20)
{
e.Item.Checked = !e.Item.Checked;
return;
}

checkedPreset.ShowInQuickAccess = e.Item.Checked;

var preset = listView.GetSelectedItemTag<T>();
Expand Down Expand Up @@ -3879,6 +3946,14 @@ private void lvNvPresets_ItemCheck(object sender, ItemCheckEventArgs e)
return;
}


var point = listView.PointToClient(Cursor.Position);

if (point.X >= 20)
{
return;
}

var listItem = listView.Items[e.Index];

if (listItem.Tag == null)
Expand Down
15 changes: 11 additions & 4 deletions ColorControl/Services/Common/PresetBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ enum PresetConditionType
FullScreen = 4,
[Description("Notifications Disabled")]
NotificationsDisabled = 8,
[Description("G-SYNC Disabled")]
GsyncDisabled = 16,
[Description("G-SYNC Enabled")]
GsyncEnabled = 32
}

class PresetTrigger
Expand All @@ -59,7 +63,9 @@ public PresetTrigger()

public bool TriggerActive(PresetTriggerContext context)
{
var active = Conditions == PresetConditionType.None || (Conditions.HasFlag(PresetConditionType.SDR) && !context.IsHDRActive) || (Conditions.HasFlag(PresetConditionType.HDR) && context.IsHDRActive);
var active = Conditions == PresetConditionType.None ||
(Conditions.HasFlag(PresetConditionType.SDR) ? !context.IsHDRActive : true) && (Conditions.HasFlag(PresetConditionType.HDR) ? context.IsHDRActive : true) &&
(Conditions.HasFlag(PresetConditionType.GsyncDisabled) ? !context.IsGsyncActive : true) && (Conditions.HasFlag(PresetConditionType.GsyncEnabled) ? context.IsGsyncActive : true);
var allProcesses = IncludedProcesses.Contains("*");

if (Trigger == PresetTriggerType.ProcessSwitch)
Expand All @@ -68,12 +74,12 @@ public bool TriggerActive(PresetTriggerContext context)

if (active)
{
var included = allProcesses || context.ChangedProcesses.Any(cp => IncludedProcesses.Any(ip => cp.ProcessName.Equals(ip, StringComparison.OrdinalIgnoreCase)));
var excluded = context.ChangedProcesses.Any(cp => ExcludedProcesses.Any(ep => cp.ProcessName.Equals(ep, StringComparison.OrdinalIgnoreCase)));
var included = allProcesses || context.ChangedProcesses.Any(cp => IncludedProcesses.Any(ip => cp.ProcessName.NormEquals(ip)));
var excluded = context.ChangedProcesses.Any(cp => ExcludedProcesses.Any(ep => cp.ProcessName.NormEquals(ep)));

var screenSizeCheck = (!Conditions.HasFlag(PresetConditionType.FullScreen) && !context.ForegroundProcessIsFullScreen) ||
(context.ForegroundProcess != null && context.ForegroundProcessIsFullScreen &&
(allProcesses || IncludedProcesses.Any(ip => context.ForegroundProcess.ProcessName.Equals(ip, StringComparison.OrdinalIgnoreCase))));
(allProcesses || IncludedProcesses.Any(ip => context.ForegroundProcess.ProcessName.NormEquals(ip))));

var notificationsDisabledCheck = !Conditions.HasFlag(PresetConditionType.NotificationsDisabled) || context.IsNotificationDisabled;

Expand Down Expand Up @@ -103,6 +109,7 @@ public static string DisplayProcesses(IEnumerable<string> processes)
class PresetTriggerContext
{
public bool IsHDRActive { get; set; }
public bool IsGsyncActive { get; set; }
public Process ForegroundProcess { get; set; }
public bool ForegroundProcessIsFullScreen { get; set; }
public List<Process> ChangedProcesses { get; set; }
Expand Down
Loading

0 comments on commit a1121c1

Please sign in to comment.