Skip to content

Commit

Permalink
Support searching the devices as user types (#13)
Browse files Browse the repository at this point in the history
* Remove unused values

* Remove shadowed variable

* Use more explicit function: `string.IsNullOrWhiteSpace`

* `.gitignore` the `.idea` folder

* Remove unused values

* Fix imports

* Fix warnings and style

* Support searching the devices

* Version bump

* Fix warnings

* Dictionary

---------

Co-authored-by: TheBestPessimist <cristian+gitpush@tbp.land>
  • Loading branch information
TheBestPessimist and TheBestPessimist committed Apr 25, 2023
1 parent 18aec08 commit d3e6edb
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 86 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,6 @@ FodyWeavers.xsd
*.msp

# JetBrains Rider
*.sln.iml
*.sln.iml

.idea/
2 changes: 2 additions & 0 deletions src/Flow.Launcher.Plugin.AudioDeviceSelector.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=audiodeviceselector/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
184 changes: 100 additions & 84 deletions src/Flow.Launcher.Plugin.AudioDeviceSelector/Main.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
using Flow.Launcher.Plugin.AudioDeviceSelector.Audio;
using Flow.Launcher.Plugin.AudioDeviceSelector.Audio.Interop;
using Flow.Launcher.Plugin.AudioDeviceSelector.Components;
using Flow.Launcher.Plugin.AudioDeviceSelector.Views;
using NAudio.CoreAudioApi;
using System;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Navigation;
using Flow.Launcher.Plugin.AudioDeviceSelector.Audio;
using Flow.Launcher.Plugin.AudioDeviceSelector.Components;
using Flow.Launcher.Plugin.AudioDeviceSelector.Views;

namespace Flow.Launcher.Plugin.AudioDeviceSelector
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
public class Main : IPlugin, IPluginI18n, ISettingProvider
{
internal PluginInitContext Context;

private DateTime lastDeviceUpdateTimeStamp = DateTime.Now;
private int updateIntervalSeconds = 5;
private MMDeviceEnumerator deviceEnumerator = new MMDeviceEnumerator();
private PluginInitContext Context;

private const string imagePath = "Images/speaker.png";

private SettingsUserControl SettingWindow;
private Settings settings;
private bool disposedValue;

private AudioDevicesManager audioDevicesManager;

Expand All @@ -33,7 +27,7 @@ public Control CreateSettingPanel()
return SettingWindow;
}

public TitleTypeSettings GetTitleTypeSettings(Settings settings)
private TitleTypeSettings GetTitleTypeSettings()
{
if (settings.DisplayFriendlyName)
return TitleTypeSettings.FriendlyName;
Expand All @@ -49,65 +43,25 @@ public List<Result> Query(Query query)
{
try
{
audioDevicesManager.UpdateDevices();
var results = new List<Result>();

var titleType = GetTitleTypeSettings(settings);
var allDevices = CreateAllDevicesResultsList();

foreach (var device in audioDevicesManager.Devices)
var result = new List<Result>();
if (!string.IsNullOrWhiteSpace(query.Search))
{
string title = string.Empty;
string subTitle = string.Empty;
switch (titleType)
{
case TitleTypeSettings.FriendlyName:
title = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.FriendlyName);
break;
case TitleTypeSettings.DeviceName:
title = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceName);
subTitle = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceDescription);
break;
case TitleTypeSettings.DeviceDescription:
title = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceDescription);
subTitle = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceName);
break;
}

if (string.IsNullOrEmpty(subTitle) || (subTitle != null && subTitle.Trim().Length == 0))
{
subTitle = GetTranslatedPluginTitle();
}

var result = new Result
{
Title = title,
SubTitle = subTitle,
Action = c =>
{
try
{
if (!audioDevicesManager.SetDevice(device.FriendlyName))
{
// Show Notification Message if device is not found
// Can happen in situations where since FlowLauncher was shown, the device went offline
Context.API.ShowMsg(GetTranslatedPluginTitle(),
GetTranslatedDeviceNotFoundError(device.FriendlyName));
}
}
catch (Exception)
{
Context.API.ShowMsg(GetTranslatedPluginTitle(),
GetTranslatedChangingDeviceError());
}
return true;
},
IcoPath = imagePath
};
// ReSharper disable once ConvertToLocalFunction
Func<Result, bool> resultContainsQuery = r =>
r.Title.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase) ||
r.SubTitle.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase);
result = allDevices.Where(resultContainsQuery).ToList();
}

results.Add(result);
// be lenient to the user: if the query has a typo, show allDevices
if (result.Count == 0)
{
result = allDevices;
}

return results;
return result;
}
catch (Exception e)
{
Expand All @@ -119,22 +73,84 @@ public List<Result> Query(Query query)
}
}

private List<Result> CreateAllDevicesResultsList()
{
audioDevicesManager.UpdateDevices();
var results = new List<Result>();

var titleType = GetTitleTypeSettings();

foreach (var device in audioDevicesManager.Devices)
{
string title = string.Empty;
string subTitle = string.Empty;
switch (titleType)
{
case TitleTypeSettings.FriendlyName:
title = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.FriendlyName);
break;
case TitleTypeSettings.DeviceName:
title = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceName);
subTitle = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceDescription);
break;
case TitleTypeSettings.DeviceDescription:
title = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceDescription);
subTitle = audioDevicesManager.GetDeviceTitle(device.FriendlyName, TitleTypeSettings.DeviceName);
break;
}

if (string.IsNullOrWhiteSpace(subTitle))
{
subTitle = GetTranslatedPluginTitle();
}

var result = new Result
{
Title = title,
SubTitle = subTitle,
Action = _ =>
{
try
{
if (!audioDevicesManager.SetDevice(device.FriendlyName))
{
// Show Notification Message if device is not found
// Can happen in situations where since FlowLauncher was shown, the device went offline
Context.API.ShowMsg(GetTranslatedPluginTitle(), GetTranslatedDeviceNotFoundError(device.FriendlyName));
}
}
catch (Exception)
{
Context.API.ShowMsg(GetTranslatedPluginTitle(), GetTranslatedChangingDeviceError());
}
return true;
},
IcoPath = imagePath
};

results.Add(result);
}

return results;
}

// Returns a list with a single result
private static List<Result> SingleResult(string title, string subtitle = "", Action action = default, bool hideAfterAction = true) =>
new()
{
new Result
{
new Result()
Title = title,
SubTitle = subtitle,
IcoPath = imagePath,
Action = _ =>
{
Title = title,
SubTitle = subtitle,
IcoPath = imagePath ,
Action = _ =>
{
action?.Invoke();
return hideAfterAction;
}
action?.Invoke();
return hideAfterAction;
}
};
}
};

public void Init(PluginInitContext context)
{
Expand All @@ -146,7 +162,7 @@ public void Init(PluginInitContext context)
audioDevicesManager = new AudioDevicesManager();
}

public string GetTranslatedDeviceNotFoundError(string deviceName)
private string GetTranslatedDeviceNotFoundError(string deviceName)
{
var message = Context.API.GetTranslation("plugin_audiodeviceselector_device_not_found");
if (string.IsNullOrEmpty(message))
Expand All @@ -156,10 +172,10 @@ public string GetTranslatedDeviceNotFoundError(string deviceName)

return string.Format(message, deviceName);
}
public string GetTranslatedChangingDeviceError()

private string GetTranslatedChangingDeviceError()
{
return Context.API.GetTranslation("plugin_audiodeviceselector_error_while_changing_device"); ;
return Context.API.GetTranslation("plugin_audiodeviceselector_error_while_changing_device");
}

public string GetTranslatedPluginTitle()
Expand All @@ -172,4 +188,4 @@ public string GetTranslatedPluginDescription()
return Context.API.GetTranslation("plugin_audiodeviceselector_plugin_description");
}
}
}
}
2 changes: 1 addition & 1 deletion src/Flow.Launcher.Plugin.AudioDeviceSelector/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Name": "Audio Device Selector",
"Description": "Easy change your playback device",
"Author": "AttilaKapostyak",
"Version": "1.0.2",
"Version": "1.0.3",
"Language": "csharp",
"Website": "https://github.com/attilakapostyak/Flow.Launcher.Plugin.AudioDeviceSelector",
"ExecuteFileName": "Flow.Launcher.Plugin.AudioDeviceSelector.dll",
Expand Down

0 comments on commit d3e6edb

Please sign in to comment.