Skip to content

Commit

Permalink
option to enable/disable media detection
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniachi committed Jul 9, 2023
1 parent aefa059 commit c7c3e6f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions Totoro.Core/Contracts/ISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public interface ISettings : INotifyPropertyChanged
bool AutoDownloadTorrents { get; set; }
string AnimeCardClickAction { get; set; }
int SmallSkipAmount { get; set; }
bool MediaDetectionEnabled { get; set; }
}

public class DefaultUrls : ReactiveObject
Expand Down
4 changes: 4 additions & 0 deletions Totoro.Core/Services/Initalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Totoro.Core.Services;

public class Initalizer : IInitializer
{
private readonly IPluginManager _pluginManager;
private readonly IKnownFolders _knownFolders;
private readonly ILocalSettingsService _localSettingsService;
private readonly IRssDownloader _rssDownloader;
private readonly PluginOptionsStorage _pluginOptionsStorage;
Expand All @@ -22,6 +24,8 @@ public Initalizer(IPluginManager pluginManager,
IRssDownloader rssDownloader,
PluginOptionsStorage pluginOptionsStorage)
{
_pluginManager = pluginManager;
_knownFolders = knownFolders;
_localSettingsService = localSettingsService;
_rssDownloader = rssDownloader;
_pluginOptionsStorage = pluginOptionsStorage;
Expand Down
1 change: 1 addition & 0 deletions Totoro.Core/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class Settings
public static Key<string> DefaultTorrentTrackerType { get; } = new("DefaultTorrentTrackerType", "nya");
public static Key<int> SmallSkipAmount { get; } = new("SmallSkipAmount", 5);
public static Key<string> DefaultMediaPlayer { get; } = new("DefaultMediaPlayer", "vlc");
public static Key<bool> MediaDetectionEnabled { get; } = new("MediaDetectionEnabled", false);

public static IEnumerable<string> GetObsoleteKeys()
{
Expand Down
1 change: 1 addition & 0 deletions Totoro.Core/ViewModels/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private void ObserveObject<T>(T target, Key<T> key)
[Reactive] public string DefaultTorrentTrackerType { get; set; }
[Reactive] public int SmallSkipAmount { get; set; }
[Reactive] public string DefaultMediaPlayer { get; set; }
[Reactive] public bool MediaDetectionEnabled { get; set; }
}


Expand Down
24 changes: 14 additions & 10 deletions Totoro.Plugins.MediaDetection/ProcessWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,31 @@ public class ProcessWatcher : IEnableLogger
private readonly Subject<int> _mediaPlayerClosed = new();
private readonly FlaUI.UIA3.UIA3Automation _automation = new();
private readonly AutomationElement _desktop;
private readonly UIA3AutomationEventHandler _automationEventHandler;

public ProcessWatcher()
{
_desktop = _automation.GetDesktop();
UIA3AutomationEventHandler automationEventHandler = new(_desktop.FrameworkAutomationElement,_automation.EventLibrary.Window.WindowOpenedEvent, OnWindowOpened);
_automation.NativeAutomation.AddAutomationEventHandler(_automation.EventLibrary.Window.WindowOpenedEvent.Id,
_desktop.ToNative(),
(Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
null,
automationEventHandler);


_automationEventHandler = new(_desktop.FrameworkAutomationElement,_automation.EventLibrary.Window.WindowOpenedEvent, OnWindowOpened);
DetectMediaProcess();
}

public void Start()
public void Enable()
{
foreach (var item in _desktop.FindAllChildren(cb => cb.ByControlType(ControlType.Window)).Select(x => x.AsWindow()))
{
OnWindowOpened(item, EventId.NotSupportedByFramework);
}
DetectMediaProcess();
_automation.NativeAutomation.AddAutomationEventHandler(_automation.EventLibrary.Window.WindowOpenedEvent.Id,
_desktop.ToNative(),
(Interop.UIAutomationClient.TreeScope)TreeScope.Descendants,
null,
_automationEventHandler);
}

public void Disable()
{
_automation.NativeAutomation.RemoveAutomationEventHandler(_automation.EventLibrary.Window.WindowOpenedEvent.Id, _desktop.ToNative(), _automationEventHandler);
}

public IObservable<INativeMediaPlayer> MediaPlayerDetected => _mediaPlayerDetected;
Expand Down
18 changes: 17 additions & 1 deletion Totoro.WinUI/Activation/DefaultActivationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ public DefaultActivationHandler(IWinUINavigationService navigationService,
_settings = settings;
_viewService = viewService;
_processWatcher = processWatcher;

settings.ObservableForProperty(x => x.MediaDetectionEnabled, x => x)
.Subscribe(isEnabled =>
{
if (isEnabled)
{
processWatcher.Enable();
}
else
{
processWatcher.Disable();
}
});

processWatcher
.MediaPlayerDetected
Expand Down Expand Up @@ -72,7 +85,10 @@ protected override Task HandleInternalAsync(LaunchActivatedEventArgs args)
_viewService.Information("Ops...", "There has been a breaking change, please wait till application downloads update");
}

_processWatcher.Start();
if(_settings.MediaDetectionEnabled)
{
_processWatcher.Enable();
}

return Task.CompletedTask;
}
Expand Down
9 changes: 9 additions & 0 deletions Totoro.WinUI/Views/SettingsSections/ExternalMediaSection.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
ItemsSource="{x:Bind MediaPlayers}"
SelectedItem="{x:Bind SelectedMediaPlayer, Mode=TwoWay}" />
</labs:SettingsCard>
<labs:SettingsCard Description="Automatical detect and track media playing in external media players" Header="Enable Media Detection">
<labs:SettingsCard.HeaderIcon>
<FontIcon Glyph="&#xE773;" />
</labs:SettingsCard.HeaderIcon>
<ToggleSwitch
IsOn="{x:Bind ViewModel.Settings.MediaDetectionEnabled, Mode=TwoWay}"
OffContent="Off"
OnContent="On" />
</labs:SettingsCard>

<TextBlock
Margin="0,20,0,0"
Expand Down

0 comments on commit c7c3e6f

Please sign in to comment.