diff --git a/src/Lively/Lively.Grpc.Client/IDesktopCoreClient.cs b/src/Lively/Lively.Grpc.Client/IDesktopCoreClient.cs index ce9d10f3..a09f5b92 100644 --- a/src/Lively/Lively.Grpc.Client/IDesktopCoreClient.cs +++ b/src/Lively/Lively.Grpc.Client/IDesktopCoreClient.cs @@ -24,6 +24,7 @@ public interface IDesktopCoreClient : IDisposable void SendMessageWallpaper(IDisplayMonitor display, ILibraryModel obj, IpcMessage msg); Task PreviewWallpaper(string livelyInfoPath); Task TakeScreenshot(string monitorId, string savePath); + Task SetWallpaperLoop(bool state, int intervall); event EventHandler WallpaperChanged; event EventHandler WallpaperError; diff --git a/src/Lively/Lively.Grpc.Client/UserSettingsClient.cs b/src/Lively/Lively.Grpc.Client/UserSettingsClient.cs index 87287007..c30a5171 100644 --- a/src/Lively/Lively.Grpc.Client/UserSettingsClient.cs +++ b/src/Lively/Lively.Grpc.Client/UserSettingsClient.cs @@ -254,6 +254,8 @@ private SettingsDataModel CreateGrpcSettings(ISettingsModel settings) ApplicationThemeBackground = (Common.Proto.Settings.AppThemeBackground)settings.ApplicationThemeBackground, ApplicationThemeBackgroundPath = settings.ApplicationThemeBackgroundPath, ThemeBundleVersion = settings.ThemeBundleVersion, + DoRandomWallpaper = settings.DoRandomWallpaper, + TimeToChangeWallpaper = settings.TimeToChangeWallpaper, }; } @@ -330,6 +332,8 @@ private ISettingsModel CreateSettingsFromGrpc(SettingsDataModel settings) ApplicationThemeBackground = (Lively.Common.AppThemeBackground)settings.ApplicationThemeBackground, ApplicationThemeBackgroundPath = settings.ApplicationThemeBackgroundPath, ThemeBundleVersion = settings.ThemeBundleVersion, + DoRandomWallpaper = settings.DoRandomWallpaper, + TimeToChangeWallpaper = settings.TimeToChangeWallpaper, }; } diff --git a/src/Lively/Lively.Grpc.Client/WinDesktopCoreClient.cs b/src/Lively/Lively.Grpc.Client/WinDesktopCoreClient.cs index a8100a43..a50d2899 100644 --- a/src/Lively/Lively.Grpc.Client/WinDesktopCoreClient.cs +++ b/src/Lively/Lively.Grpc.Client/WinDesktopCoreClient.cs @@ -279,6 +279,16 @@ private async Task SubscribeWallpaperUpdatedStream(CancellationToken token) } } + public async Task SetWallpaperLoop(bool state, int intervall) + { + var request = new SetWallpaperLoopRequest + { + State = state, + Intervall = intervall, + }; + _ = await client.SetWallpaperLoopAsync(request); + } + #region dispose protected virtual void Dispose(bool disposing) diff --git a/src/Lively/Lively.Grpc.Common/Proto/settings.proto b/src/Lively/Lively.Grpc.Common/Proto/settings.proto index 8771c98c..498c0e70 100644 --- a/src/Lively/Lively.Grpc.Common/Proto/settings.proto +++ b/src/Lively/Lively.Grpc.Common/Proto/settings.proto @@ -72,6 +72,8 @@ message SettingsDataModel { AppThemeBackground application_theme_background = 60; string application_theme_background_path = 61; int32 theme_bundle_version = 62; + bool do_random_wallpaper = 63; + int32 time_to_change_wallpaper = 64; } message AppRulesSettings { diff --git a/src/Lively/Lively.Grpc.Common/Proto/wallpaper.proto b/src/Lively/Lively.Grpc.Common/Proto/wallpaper.proto index bcb83ac2..1a90f5e1 100644 --- a/src/Lively/Lively.Grpc.Common/Proto/wallpaper.proto +++ b/src/Lively/Lively.Grpc.Common/Proto/wallpaper.proto @@ -16,6 +16,7 @@ service DesktopService { rpc SendMessageWallpaper(WallpaperMessageRequest) returns (google.protobuf.Empty); rpc GetCoreStats (google.protobuf.Empty) returns (GetCoreStatsResponse); rpc TakeScreenshot(WallpaperScreenshotRequest) returns (google.protobuf.Empty); + rpc SetWallpaperLoop (SetWallpaperLoopRequest) returns (google.protobuf.Empty); } message SetWallpaperRequest { @@ -111,6 +112,11 @@ message WallpaperErrorResponse { string error_msg = 2; } +message SetWallpaperLoopRequest { + bool state = 1; + int32 intervall = 2; +} + enum ErrorCategory { workerw = 0; wallpaper_not_found = 1; diff --git a/src/Lively/Lively.Models/ISettingsModel.cs b/src/Lively/Lively.Models/ISettingsModel.cs index beb15047..edf6a66d 100644 --- a/src/Lively/Lively.Models/ISettingsModel.cs +++ b/src/Lively/Lively.Models/ISettingsModel.cs @@ -70,5 +70,7 @@ public interface ISettingsModel string ApplicationThemeBackgroundPath { get; set; } AppThemeBackground ApplicationThemeBackground { get; set; } int ThemeBundleVersion { get; set; } + bool DoRandomWallpaper { get; set; } + int TimeToChangeWallpaper { get; set; } } } \ No newline at end of file diff --git a/src/Lively/Lively.Models/SettingsModel.cs b/src/Lively/Lively.Models/SettingsModel.cs index 936593fb..3aba3d39 100644 --- a/src/Lively/Lively.Models/SettingsModel.cs +++ b/src/Lively/Lively.Models/SettingsModel.cs @@ -119,6 +119,8 @@ public class SettingsModel : ISettingsModel public string ApplicationThemeBackgroundPath { get; set; } public AppThemeBackground ApplicationThemeBackground { get; set; } public int ThemeBundleVersion { get; set; } + public bool DoRandomWallpaper { get; set; } + public int TimeToChangeWallpaper { get; set; } public SettingsModel() { @@ -191,6 +193,8 @@ public SettingsModel() IsUpdated = false; ApplicationThemeBackgroundPath = null; ApplicationThemeBackground = AppThemeBackground.default_mica; + DoRandomWallpaper = false; + TimeToChangeWallpaper = 20; try { diff --git a/src/Lively/Lively.UI.WinUI/App.xaml.cs b/src/Lively/Lively.UI.WinUI/App.xaml.cs index 135cfb62..5d79c164 100644 --- a/src/Lively/Lively.UI.WinUI/App.xaml.cs +++ b/src/Lively/Lively.UI.WinUI/App.xaml.cs @@ -166,6 +166,7 @@ private IServiceProvider ConfigureServices() .AddTransient() .AddTransient() .AddTransient() + .AddTransient() //https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests .AddHttpClient() .BuildServiceProvider(); diff --git a/src/Lively/Lively.UI.WinUI/Lively.UI.WinUI.csproj b/src/Lively/Lively.UI.WinUI/Lively.UI.WinUI.csproj index ff7764ec..59c68b7d 100644 --- a/src/Lively/Lively.UI.WinUI/Lively.UI.WinUI.csproj +++ b/src/Lively/Lively.UI.WinUI/Lively.UI.WinUI.csproj @@ -82,6 +82,7 @@ + @@ -145,6 +146,9 @@ MSBuild:Compile + + $(DefaultXamlRuntime) + diff --git a/src/Lively/Lively.UI.WinUI/Strings/en-US/Resources.resw b/src/Lively/Lively.UI.WinUI/Strings/en-US/Resources.resw index dc004782..ff305821 100644 --- a/src/Lively/Lively.UI.WinUI/Strings/en-US/Resources.resw +++ b/src/Lively/Lively.UI.WinUI/Strings/en-US/Resources.resw @@ -1220,4 +1220,13 @@ If still not working, close & start Lively again/restart windows. Download + + Enable automatic wallpaper change + + + Shuffle settings + + + Time Intervall (in minutes) + \ No newline at end of file diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/ShuffleViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/ShuffleViewModel.cs new file mode 100644 index 00000000..21e8dc86 --- /dev/null +++ b/src/Lively/Lively.UI.WinUI/ViewModels/ShuffleViewModel.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using System.Timers; +using Lively.Common.Helpers.MVVM; +using Lively.Grpc.Client; +using Lively.Models; +using Lively.UI.WinUI.Views.Pages; +using Microsoft.UI.Dispatching; +using NLog; +using Windows.Devices.Printers; +using Windows.System.Profile; + +namespace Lively.UI.WinUI.ViewModels +{ + public class ShuffleViewModel : ObservableObject + { + + public event EventHandler OnRequestClose; + private readonly DispatcherQueue dispatcherQueue; + + private readonly IDisplayManagerClient displayManager; + private readonly IDesktopCoreClient desktopCore; + private readonly IUserSettingsClient userSettings; + + public ShuffleViewModel(IDesktopCoreClient desktopCore, IDisplayManagerClient displayManager, IUserSettingsClient userSettings) + { + this.userSettings = userSettings; + this.desktopCore = desktopCore; + EnableShuffle = userSettings.Settings.DoRandomWallpaper; + TimeToChangeWallpaper = userSettings.Settings.TimeToChangeWallpaper.ToString(); + dispatcherQueue = DispatcherQueue.GetForCurrentThread() ?? DispatcherQueueController.CreateOnCurrentThread().DispatcherQueue; + } + + private bool _enableShuffle; + public bool EnableShuffle + { + get { return _enableShuffle; } + set + { + _enableShuffle = value; + if (userSettings.Settings.DoRandomWallpaper != _enableShuffle) + { + userSettings.Settings.DoRandomWallpaper = _enableShuffle; + UpdateSettingsConfigFile(); + } + OnPropertyChanged(); + } + } + + private string _timeToChangeWallpaper; + public string TimeToChangeWallpaper + { + get { return _timeToChangeWallpaper; } + set + { + _timeToChangeWallpaper = value; + if (userSettings.Settings.TimeToChangeWallpaper.ToString() != _timeToChangeWallpaper) + { + try + { + userSettings.Settings.TimeToChangeWallpaper = Int32.Parse(_timeToChangeWallpaper); + UpdateSettingsConfigFile(); + } + catch { } + } + OnPropertyChanged(); + } + } + + private string _debugBoxText; + public string DebugBoxText + { + get { return _debugBoxText; } + set + { + _debugBoxText = value; + OnPropertyChanged(); + } + } + + public void UpdateSettingsConfigFile() + { + _ = dispatcherQueue.TryEnqueue(() => + { + userSettings.Save(); + }); + } + + public void PageUnloaded() + { + desktopCore.SetWallpaperLoop(userSettings.Settings.DoRandomWallpaper, userSettings.Settings.TimeToChangeWallpaper); + } + + } +} diff --git a/src/Lively/Lively.UI.WinUI/Views/MainWindow.xaml b/src/Lively/Lively.UI.WinUI/Views/MainWindow.xaml index 0a5f8048..eb197b09 100644 --- a/src/Lively/Lively.UI.WinUI/Views/MainWindow.xaml +++ b/src/Lively/Lively.UI.WinUI/Views/MainWindow.xaml @@ -224,6 +224,17 @@ + + + + + + + LinkHandler.OpenBrowser("https://rocksdanister.github.io/lively/coffee/"); + private void ToogleShuffleButton_Click(object sender, RoutedEventArgs e) + { + + _ = new ContentDialog() + { + Title = i18n.GetString("Shuffle/Label"), + Content = new ShuffleView(), + PrimaryButtonText = i18n.GetString("TextOK"), + DefaultButton = ContentDialogButton.Primary, + XamlRoot = this.Content.XamlRoot, + }.ShowAsyncQueue(); + + } + private void AppBarThemeButton_Click(object sender, RoutedEventArgs e) => dialogService.ShowThemeDialog(); private void AppBarHelpButton_Click(object sender, RoutedEventArgs e) diff --git a/src/Lively/Lively.UI.WinUI/Views/Pages/ShuffleView.xaml b/src/Lively/Lively.UI.WinUI/Views/Pages/ShuffleView.xaml new file mode 100644 index 00000000..d3dc7266 --- /dev/null +++ b/src/Lively/Lively.UI.WinUI/Views/Pages/ShuffleView.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + diff --git a/src/Lively/Lively.UI.WinUI/Views/Pages/ShuffleView.xaml.cs b/src/Lively/Lively.UI.WinUI/Views/Pages/ShuffleView.xaml.cs new file mode 100644 index 00000000..d3024ffd --- /dev/null +++ b/src/Lively/Lively.UI.WinUI/Views/Pages/ShuffleView.xaml.cs @@ -0,0 +1,29 @@ +using Lively.Common; +using Lively.UI.WinUI.ViewModels; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Navigation; +using NLog; +using System; + +namespace Lively.UI.WinUI.Views.Pages +{ + public sealed partial class ShuffleView : Page + { + private ShuffleViewModel _viewModel; + public ShuffleView() + { + this.InitializeComponent(); + this.Unloaded += ShuffleView_Unloaded; + + _viewModel = App.Services.GetRequiredService(); + this.DataContext = _viewModel; + } + + private void ShuffleView_Unloaded(object sender, RoutedEventArgs e) + { + _viewModel?.PageUnloaded(); + } + } +} \ No newline at end of file diff --git a/src/Lively/Lively/Core/IDesktopCore.cs b/src/Lively/Lively/Core/IDesktopCore.cs index a0b138c7..4bbe3785 100644 --- a/src/Lively/Lively/Core/IDesktopCore.cs +++ b/src/Lively/Lively/Core/IDesktopCore.cs @@ -24,6 +24,7 @@ public interface IDesktopCore : IDisposable void SendMessageWallpaper(string info_path, IpcMessage msg); void SendMessageWallpaper(IDisplayMonitor display, string info_path, IpcMessage msg); void SetWallpaper(ILibraryModel wallpaper, IDisplayMonitor display); + void SetRandomWallpaper(); /// /// Wallpaper set/removed. diff --git a/src/Lively/Lively/Core/WinDesktopCore.cs b/src/Lively/Lively/Core/WinDesktopCore.cs index 1a72e959..04a44792 100644 --- a/src/Lively/Lively/Core/WinDesktopCore.cs +++ b/src/Lively/Lively/Core/WinDesktopCore.cs @@ -17,6 +17,7 @@ using Lively.Views; using Microsoft.Extensions.DependencyInjection; using Microsoft.Win32; +using NLog.Layouts; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -787,6 +788,48 @@ private void RestoreDisconnectedWallpapers() } } + private ILibraryModel GetRandomWallpaper() + { + + var dir = new List(); + string[] folderPaths = { + Path.Combine(userSettings.Settings.WallpaperDir, Constants.CommonPartialPaths.WallpaperInstallDir), + Path.Combine(userSettings.Settings.WallpaperDir, Constants.CommonPartialPaths.WallpaperInstallTempDir) + }; + for (int i = 0; i < folderPaths.Count(); i++) + { + try + { + dir.AddRange(Directory.GetDirectories(folderPaths[i], "*", SearchOption.TopDirectoryOnly)); + } + catch { /* TODO */ } + } + + List libList = new List(); + + for (int i = 0; i < dir.Count; i++) + { + try + { + libList.Add(WallpaperUtil.ScanWallpaperFolder(dir[i])); + } + catch { } + } + + return libList[new Random().Next(libList.Count())]; + } + + public void SetRandomWallpaper() + { + + foreach(var screen in displayManager.DisplayMonitors.AsEnumerable()) + { + var libraryItem = GetRandomWallpaper(); + SetWallpaper(libraryItem, screen); + } + + } + private void RestoreWallpaper(List wallpaperLayout) { foreach (var layout in wallpaperLayout) @@ -847,6 +890,11 @@ public void RestoreWallpaper() { Logger.Error($"Failed to restore wallpaper: {e}"); } + if (userSettings.Settings.DoRandomWallpaper == true) + { + var task = new TimerService(TimeSpan.FromMinutes(userSettings.Settings.TimeToChangeWallpaper), displayManager, this); + task.Start(); + } } public void CloseAllWallpapers(bool terminate = false) diff --git a/src/Lively/Lively/Properties/Resources.Designer.cs b/src/Lively/Lively/Properties/Resources.Designer.cs index 84547470..e9c949d0 100644 --- a/src/Lively/Lively/Properties/Resources.Designer.cs +++ b/src/Lively/Lively/Properties/Resources.Designer.cs @@ -105,6 +105,24 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Create a dynamic wallpaper. + /// + public static string AddWallpaperAdvanced_Description { + get { + return ResourceManager.GetString("AddWallpaperAdvanced.Description", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Advanced. + /// + public static string AddWallpaperAdvanced_Header { + get { + return ResourceManager.GetString("AddWallpaperAdvanced.Header", resourceCulture); + } + } + /// /// Looks up a localized string similar to Add wallpaper. /// @@ -543,6 +561,42 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Approximating depth... + /// + public static string DescriptionDepthApprox_Content { + get { + return ResourceManager.GetString("DescriptionDepthApprox.Content", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Using AI transform photographs into 3D. + /// + public static string DescriptionDepthWallpaperItem_Content { + get { + return ResourceManager.GetString("DescriptionDepthWallpaperItem.Content", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to AI generated depth wallpaper. + /// + public static string DescriptionDepthWallpaperTemplate_Content { + get { + return ResourceManager.GetString("DescriptionDepthWallpaperTemplate.Content", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Additional files need to be downloaded for this feature. + /// + public static string DescriptionDownloadRequired_Text { + get { + return ResourceManager.GetString("DescriptionDownloadRequired.Text", resourceCulture); + } + } + /// /// Looks up a localized string similar to Adapt to wallpaper. /// @@ -771,6 +825,15 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Enable automatic wallpaper change. + /// + public static string EnableShuffle_Content { + get { + return ResourceManager.GetString("EnableShuffle.Content", resourceCulture); + } + } + /// /// Looks up a localized string similar to Set the web page of the URL you entered as wallpaper. /// @@ -1420,6 +1483,15 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Some functionalities may not be available when run as administrator. + /// + public static string RunningAsAdminWarning_Message { + get { + return ResourceManager.GetString("RunningAsAdminWarning.Message", resourceCulture); + } + } + /// /// Looks up a localized string similar to How screensaver is applied to connected to display devices. /// @@ -1510,6 +1582,15 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Shuffle settings. + /// + public static string Shuffle_Label { + get { + return ResourceManager.GetString("Shuffle.Label", resourceCulture); + } + } + /// /// Looks up a localized string similar to Show some love by dropping a star. /// @@ -1780,6 +1861,15 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Continue. + /// + public static string TextContinue_Content { + get { + return ResourceManager.GetString("TextContinue.Content", resourceCulture); + } + } + /// /// Looks up a localized string similar to Contributors. /// @@ -2185,15 +2275,6 @@ public class Resources { } } - /// - /// Looks up a localized string similar to Search. - /// - public static string TextSearch { - get { - return ResourceManager.GetString("TextSearch", resourceCulture); - } - } - /// /// Looks up a localized string similar to Select all. /// @@ -2446,6 +2527,15 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Time Intervall (in minutes). + /// + public static string TimeShuffle_Header { + get { + return ResourceManager.GetString("TimeShuffle.Header", resourceCulture); + } + } + /// /// Looks up a localized string similar to Lively adapts to system theme settings, to change appearance goto. /// @@ -2563,6 +2653,24 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Create Wallpaper. + /// + public static string TitleCreateWallpaper_Content { + get { + return ResourceManager.GetString("TitleCreateWallpaper.Content", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Create a simple wallpaper. + /// + public static string TitleCreateWallpaperOpenItem_Description { + get { + return ResourceManager.GetString("TitleCreateWallpaperOpenItem.Description", resourceCulture); + } + } + /// /// Looks up a localized string similar to Confirm Deletion. /// @@ -2572,6 +2680,33 @@ public class Resources { } } + /// + /// Looks up a localized string similar to AI Depth Wallpaper. + /// + public static string TitleDepthWallpaper_Content { + get { + return ResourceManager.GetString("TitleDepthWallpaper.Content", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Depth Wallpaper. + /// + public static string TitleDepthWallpaperItem_Content { + get { + return ResourceManager.GetString("TitleDepthWallpaperItem.Content", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Download. + /// + public static string TitleDownload_Content { + get { + return ResourceManager.GetString("TitleDownload.Content", resourceCulture); + } + } + /// /// Looks up a localized string similar to Download in Progress. /// @@ -2581,6 +2716,15 @@ public class Resources { } } + /// + /// Looks up a localized string similar to Download Required. + /// + public static string TitleDownloadRequired_Text { + get { + return ResourceManager.GetString("TitleDownloadRequired.Text", resourceCulture); + } + } + /// /// Looks up a localized string similar to Gallery. /// diff --git a/src/Lively/Lively/Properties/Resources.resx b/src/Lively/Lively/Properties/Resources.resx index dc004782..ff305821 100644 --- a/src/Lively/Lively/Properties/Resources.resx +++ b/src/Lively/Lively/Properties/Resources.resx @@ -1220,4 +1220,13 @@ If still not working, close & start Lively again/restart windows. Download + + Enable automatic wallpaper change + + + Shuffle settings + + + Time Intervall (in minutes) + \ No newline at end of file diff --git a/src/Lively/Lively/RPC/UserSettingsServer.cs b/src/Lively/Lively/RPC/UserSettingsServer.cs index 57e51b48..023618b2 100644 --- a/src/Lively/Lively/RPC/UserSettingsServer.cs +++ b/src/Lively/Lively/RPC/UserSettingsServer.cs @@ -174,6 +174,8 @@ public override Task SetSettings(SettingsDataModel req, ServerCallContext userSettings.Settings.ApplicationThemeBackground = (Common.AppThemeBackground)req.ApplicationThemeBackground; userSettings.Settings.ApplicationThemeBackgroundPath = req.ApplicationThemeBackgroundPath; userSettings.Settings.ThemeBundleVersion = req.ThemeBundleVersion; + userSettings.Settings.DoRandomWallpaper = req.DoRandomWallpaper; + userSettings.Settings.TimeToChangeWallpaper = req.TimeToChangeWallpaper; try { @@ -279,6 +281,8 @@ public override Task GetSettings(Empty _, ServerCallContext c ApplicationThemeBackground = (Grpc.Common.Proto.Settings.AppThemeBackground)settings.ApplicationThemeBackground, ApplicationThemeBackgroundPath = settings.ApplicationThemeBackgroundPath ?? string.Empty, ThemeBundleVersion = settings.ThemeBundleVersion, + DoRandomWallpaper = settings.DoRandomWallpaper, + TimeToChangeWallpaper = settings.TimeToChangeWallpaper, }; return Task.FromResult(resp); } diff --git a/src/Lively/Lively/RPC/WinDesktopCoreServer.cs b/src/Lively/Lively/RPC/WinDesktopCoreServer.cs index f2f6a7da..64d8a57d 100644 --- a/src/Lively/Lively/RPC/WinDesktopCoreServer.cs +++ b/src/Lively/Lively/RPC/WinDesktopCoreServer.cs @@ -352,5 +352,35 @@ public override async Task TakeScreenshot(WallpaperScreenshotRequest requ } return await Task.FromResult(new Empty()); } + + public override Task SetWallpaperLoop(SetWallpaperLoopRequest request, ServerCallContext context) + { + TimerService timerService = TimerService.Instance; + + if (timerService != null) + { + try + { + _ = timerService.Stop(); + } + catch(Exception e) + { + Logger.Error(e); + } + } + else + { + timerService = new TimerService(TimeSpan.FromMinutes(request.Intervall), displayManager, desktopCore); + } + + if(request.State == true) + { + timerService.ChangeTimerIntervall(TimeSpan.FromMinutes(request.Intervall)); + timerService.Start(); + } + + return Task.FromResult(new Empty()); + } + } } diff --git a/src/Lively/Lively/Services/TimerService.cs b/src/Lively/Lively/Services/TimerService.cs new file mode 100644 index 00000000..f66fe25f --- /dev/null +++ b/src/Lively/Lively/Services/TimerService.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Lively.Core; +using Lively.Core.Display; + +namespace Lively.Services +{ + internal class TimerService + { + public static TimerService Instance { get; private set; } + // get the logger to debug + private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); + + // basic variable for the repeating task + private Task? timerTask; + private PeriodicTimer timer; + private CancellationTokenSource cts = new(); + + private readonly IDisplayManager displayManager; + private readonly IDesktopCore desktopCore; + + // to create a new Timer Service you must specify the inerval with a TimeSpan object + // for example : TimeSpan intervall = TimeSpan.FromMinutes(15); + public TimerService(TimeSpan interval, IDisplayManager displayManager, IDesktopCore desktopCore) + { + this.displayManager = displayManager; + this.desktopCore = desktopCore; + timer = new PeriodicTimer(interval); + Instance = this; + } + + public void Start() + { + timerTask = RandomWallpaperCycle(); + } + + // this is where the program apply the new wallpaper + private async Task RandomWallpaperCycle() + { + try + { + Logger.Info("Succesfully launched the random wallpaper cycle !"); + while (await timer.WaitForNextTickAsync(cts.Token)) + { + // set wallpaper here + desktopCore.SetRandomWallpaper(); + } + } + catch { } + } + + public async Task Stop() + { + if(timerTask is null) + { + return; + } + cts.Cancel(); + await timerTask; + cts.Dispose(); + Logger.Info("Task was cancelled"); + } + + public void ChangeTimerIntervall(TimeSpan intervall) + { + timer = new PeriodicTimer(intervall); + cts = new(); + } + + } +}