Skip to content

Commit

Permalink
Add screen selector for OSD notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Stas committed Oct 6, 2022
1 parent cb83d16 commit 181cae3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 18 deletions.
8 changes: 8 additions & 0 deletions Volumey/DataProvider/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ public bool ReactToAllVolumeChanges
}
}

[OptionalField]
private int selectedScreenIndex;
public int SelectedScreenIndex
{
get => selectedScreenIndex;
set => selectedScreenIndex = value;
}

[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

Expand Down
5 changes: 5 additions & 0 deletions Volumey/Helper/NotificationManagerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ internal static void CloseNotificationExample()
}
}

internal static void SetWindowWorkArea(double left, double top, double width, double height)
{
_notificationManager.SetWorkArea(left, top, width, height);
}

internal static void ChangePosition(NotificationPosition newPosition)
{
_notificationManager.ChangeNotificationAreaPosition(newPosition);
Expand Down
Binary file modified Volumey/Resources/Notification.Wpf.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion Volumey/Resources/Resources.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,6 @@
<value>Отображать при любых изменениях звука</value>
</data>
<data name="Settings_SelectedScreen" xml:space="preserve">
<value>Дисплей для отображения окна</value>
<value>Дисплей для отображения</value>
</data>
</root>
18 changes: 8 additions & 10 deletions Volumey/View/SettingsPage/AppBehaviorPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@

<TextBlock Text="{lc:Localization Settings_AlwaysTop}"/>
<ui:ToggleSwitch IsOn="{Binding AlwaysOnTop}"/>

<TextBlock
Text="{lc:Localization Settings_SelectedScreen}"
Width="{StaticResource TextBlockWidth}"/>
<ComboBox
SelectedItem="{Binding SelectedScreen}"
ItemsSource="{Binding AllScreens}"
IsSynchronizedWithCurrentItem="False"
Style="{StaticResource SettingsComboBoxStyle}"/>


<TextBlock Text="{lc:Localization Settings_SelectedScreen}"
Width="{StaticResource TextBlockWidth}"/>
<ComboBox SelectedItem="{Binding SelectedScreen}"
ItemsSource="{Binding AllScreens}"
IsSynchronizedWithCurrentItem="False"
Style="{StaticResource SettingsComboBoxStyle}"/>

</StackPanel>
</StackPanel>
</ui:Page>
12 changes: 12 additions & 0 deletions Volumey/View/SettingsPage/NotificationsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
EventName="Unloaded">
<behaviors:InvokeCommandAction Command="{Binding UnloadedCommand}"/>
</behaviors:EventTrigger>

<behaviors:EventTrigger
EventName="Loaded">
<behaviors:InvokeCommandAction Command="{Binding LoadedCommand}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>

<StackPanel>
Expand All @@ -43,6 +48,13 @@
<TextBlock Text="{lc:Localization Notifications_Position}"/>
<ComboBox ItemsSource="{Binding Positions}"
SelectedItem="{Binding SelectedPosition}"/>

<TextBlock Text="{lc:Localization Settings_SelectedScreen}"
Width="{StaticResource TextBlockWidth}"/>
<ComboBox SelectedItem="{Binding SelectedScreen}"
ItemsSource="{Binding AllScreens}"
IsSynchronizedWithCurrentItem="False"
Style="{StaticResource SettingsComboBoxStyle}"/>

<TextBlock Text="{lc:Localization Notifications_Time}"/>
<controls:NumberBox Value="{Binding DisplayTime}"
Expand Down
3 changes: 1 addition & 2 deletions Volumey/ViewModel/AppBehaviorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public ScreenInfo SelectedScreen
if(SettingsProvider.Settings.SelectedScreenIndex != index)
{
SettingsProvider.Settings.SelectedScreenIndex = index;
SettingsProvider.SaveSettings();
_ = SettingsProvider.SaveSettings();
}
}
}
Expand Down Expand Up @@ -327,7 +327,6 @@ private void CheckIfSelectedScreenIsAvailable()
});
}

//Hide the window if the selected screen is not available so the user would have to open it again using the available screen
if(!screens.Contains(SelectedScreen))
{
SelectedScreen = screenInfoProvider.GetPrimaryScreenInfo();
Expand Down
84 changes: 79 additions & 5 deletions Volumey/ViewModel/Settings/NotificationViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Input;
using log4net;
using Microsoft.Xaml.Behaviors.Core;
using Notification.Wpf.Controls;
using Volumey.Helper;
Expand Down Expand Up @@ -164,10 +167,41 @@ public int DisplayTime
public int MinDisplayTime { get; } = 1;

public ICommand UnloadedCommand { get; }
public ICommand LoadedCommand { get; }

public static int MinIndent => NotificationManagerHelper.MinIndent;
public static int MaxIndent => NotificationManagerHelper.MaxIndent;

private ScreenInfo selectedScreen;
public ScreenInfo SelectedScreen
{
get => selectedScreen;
set
{
selectedScreen = value;
OnPropertyChanged();

NotificationManagerHelper.SetWindowWorkArea(value.AbsoluteLeft, value.AbsoluteTop, value.Width, value.Height);

int index = AllScreens.IndexOf(value);
if(SettingsProvider.NotificationsSettings.SelectedScreenIndex != index)
{
SettingsProvider.NotificationsSettings.SelectedScreenIndex = index;
_ = SettingsProvider.SaveSettings();
}
}
}

public ObservableCollection<ScreenInfo> AllScreens { get; set; } = new ObservableCollection<ScreenInfo>();

private IScreenInfoProvider _screenInfoProvider = new ScreenInfoProvider();
private IDeviceProvider _deviceProvider;
private OutputDeviceModel _currentDefaultDevice;
private AudioProcessStateNotificationMediator StateMediator = new AudioProcessStateNotificationMediator();

private static ILog _logger;
private static ILog Logger => _logger ??= LogManager.GetLogger(typeof(NotificationViewModel));

public NotificationViewModel()
{
this.NotificationsEnabled = SettingsProvider.NotificationsSettings.Enabled;
Expand All @@ -177,13 +211,53 @@ public NotificationViewModel()
this.ReactToAllVolumeChanges = SettingsProvider.NotificationsSettings.ReactToAllVolumeChanges;

this.UnloadedCommand = new ActionCommand(() => this.PreviewIsOn = false);
this.LoadedCommand = new ActionCommand(this.CheckIfSelectedScreenIsAvailable);

this.DisplayTime = SettingsProvider.NotificationsSettings.DisplayTimeInSeconds;

SetSelectedScreen();
}

private void SetSelectedScreen()
{
foreach(var screen in _screenInfoProvider.GetAllScreensInfo())
AllScreens.Add(screen);

int selectedScreenIndex = SettingsProvider.NotificationsSettings.SelectedScreenIndex;
if(AllScreens.Count - 1 < selectedScreenIndex)
SelectedScreen = _screenInfoProvider.GetPrimaryScreenInfo();
else
SelectedScreen = AllScreens[selectedScreenIndex];
}

private void CheckIfSelectedScreenIsAvailable()
{
Task.Run(() =>
{
var screens = _screenInfoProvider.GetAllScreensInfo().ToList();

if(screens.Count != AllScreens.Count || screens.Except(AllScreens).Any())
{
App.Current.Dispatcher.Invoke(() =>
{
AllScreens.Clear();
foreach(var screen in screens)
AllScreens.Add(screen);
});
}

if(!screens.Contains(SelectedScreen))
{
SelectedScreen = _screenInfoProvider.GetPrimaryScreenInfo();
}
}).ContinueWith(task =>
{
if(task.Exception != null)
{
Logger.Error("Failed to check the screen for availability", task.Exception.Flatten());
}
}, TaskContinuationOptions.OnlyOnFaulted);
}

private IDeviceProvider _deviceProvider;
private OutputDeviceModel _currentDefaultDevice;
private AudioProcessStateNotificationMediator StateMediator = new AudioProcessStateNotificationMediator();

private void SetStateMediatorForDefaultDeviceProcesses()
{
Expand Down

0 comments on commit 181cae3

Please sign in to comment.