From 5e3642e53906d7ebf85051e72a45196fc9e9c3f0 Mon Sep 17 00:00:00 2001 From: Stone-Red-Code <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sat, 30 Oct 2021 21:06:53 +0200 Subject: [PATCH] - Add `ComboBox` element to the API --- .../MusicVisualizerPlugin.cs | 4 +- .../BuiltInWindowElements/TimePlugin.cs | 8 +- src/DesktopMagic/Helpers/CalendarManagment.cs | 91 ------------------- .../Helpers/SettingElementGenerator.cs | 32 ++++++- src/DesktopMagic/MainWindow.xaml.cs | 7 +- src/DesktopMagic/Plugins/PluginWindow.xaml.cs | 6 +- .../DesktopMagicPluginAPI.md | 46 ++++++++++ .../DesktopMagicPluginAPI.xml | 22 +++++ src/DesktopMagicPluginAPI/Inputs/ComboBox.cs | 46 ++++++++++ src/DesktopMagicPluginAPI/Plugin.cs | 12 +-- 10 files changed, 161 insertions(+), 113 deletions(-) delete mode 100644 src/DesktopMagic/Helpers/CalendarManagment.cs create mode 100644 src/DesktopMagicPluginAPI/Inputs/ComboBox.cs diff --git a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs index 24216a0..79c8ad4 100644 --- a/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/MusicVisualizerPlugin.cs @@ -17,9 +17,11 @@ namespace DesktopMagic.BuiltInWindowElements private volatile bool calculate = true; private Bitmap output = new Bitmap(880, 300); + public override int UpdateInterval => 0; + public override void Start() { - sampleAggregator.FftCalculated += new EventHandler(FftCalculated); + sampleAggregator.FftCalculated += FftCalculated; sampleAggregator.PerformFFT = true; waveIn = new WasapiLoopbackCapture(); diff --git a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs index ae45b58..1cf8fa8 100644 --- a/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs +++ b/src/DesktopMagic/BuiltInWindowElements/TimePlugin.cs @@ -1,5 +1,6 @@ using DesktopMagicPluginAPI; using DesktopMagicPluginAPI.Drawing; +using DesktopMagicPluginAPI.Inputs; using System; using System.Drawing; @@ -9,11 +10,14 @@ namespace DesktopMagic.BuiltInWindowElements { internal class TimePlugin : Plugin { + [Element("Display Seconds:")] + private CheckBox checkBox = new CheckBox(true); + public override int UpdateInterval => 1000; public override Bitmap Main() { - string time = DateTime.Now.ToLongTimeString(); + string time = checkBox.Value ? DateTime.Now.ToLongTimeString() : DateTime.Now.ToShortTimeString(); Font font = new Font(Application.Theme.Font, 200); @@ -36,7 +40,7 @@ namespace DesktopMagic.BuiltInWindowElements private SizeF CalculateSize(Graphics graphics, Font font) { - string template = "##:##:##"; + string template = checkBox.Value ? "##:##:##" : "##:##"; SizeF size = new SizeF(); for (int i = 0; i < 9; i++) { diff --git a/src/DesktopMagic/Helpers/CalendarManagment.cs b/src/DesktopMagic/Helpers/CalendarManagment.cs deleted file mode 100644 index a927fab..0000000 --- a/src/DesktopMagic/Helpers/CalendarManagment.cs +++ /dev/null @@ -1,91 +0,0 @@ -using Google.Apis.Auth.OAuth2; -using Google.Apis.Calendar.v3; -using Google.Apis.Calendar.v3.Data; -using Google.Apis.Services; -using Google.Apis.Util.Store; - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Threading; - -namespace DesktopMagic -{ - internal class CalendarManagment - { - private static string[] Scopes = { CalendarService.Scope.CalendarReadonly }; - - private static string ApplicationName = "Google Calendar API .NET " + App.AppName; - - [Obsolete] - public (List, List) GetEvents() - { - List upcomingEventNames = new List(); - List upcomingEventTimes = new List(); - UserCredential credential; - - if (!File.Exists("credentials.json")) - { - return (new(), new()); - } - - using (FileStream stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) - { - // The file token.json stores the user's access and refresh tokens, and is created - // automatically when the authorization flow completes for the first time. - string credPath = "token.json"; - credential = GoogleWebAuthorizationBroker.AuthorizeAsync( - GoogleClientSecrets.Load(stream).Secrets, - Scopes, - "user", - CancellationToken.None, - new FileDataStore(credPath, true)).Result; - Debug.WriteLine("Credential file saved to: " + credPath); - } - - // Create Google Calendar API service. - CalendarService service = new CalendarService(new BaseClientService.Initializer() - { - HttpClientInitializer = credential, - ApplicationName = ApplicationName, - }); - - // Define parameters of request. - EventsResource.ListRequest request = service.Events.List("primary"); - request.TimeMin = DateTime.Now; - request.ShowDeleted = false; - request.SingleEvents = true; - request.MaxResults = 10; - request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime; - - // List events. - Events events = request.Execute(); - if (events.Items != null && events.Items.Count > 0) - { - foreach (Event eventItem in events.Items) - { - if (upcomingEventNames.Count < 10) - { - string when = eventItem.Start.DateTime.ToString(); - if (string.IsNullOrEmpty(when)) - { - when = eventItem.Start.Date; - } - upcomingEventNames.Add(eventItem.Summary); - upcomingEventTimes.Add(when); - } - } - } - return (upcomingEventNames, upcomingEventTimes); - } - } - - internal class CalendarItems - { - public string Eventname { get; set; } - public string DateTime { get; set; } - public string Font { get; set; } - public string Color { get; set; } - } -} \ No newline at end of file diff --git a/src/DesktopMagic/Helpers/SettingElementGenerator.cs b/src/DesktopMagic/Helpers/SettingElementGenerator.cs index 95e9362..98e72e0 100644 --- a/src/DesktopMagic/Helpers/SettingElementGenerator.cs +++ b/src/DesktopMagic/Helpers/SettingElementGenerator.cs @@ -193,6 +193,36 @@ namespace DesktopMagic.Helpers _ = dockPanel.Children.Add(slider); } + else if (settingElement.Element is DesktopMagicPluginAPI.Inputs.ComboBox eComboBox) + { + ComboBox comboBox = new() + { + ItemsSource = eComboBox.Items, + IsEditable = false, + VerticalAlignment = VerticalAlignment.Center, + HorizontalAlignment = HorizontalAlignment.Stretch, + SelectedIndex = 0 + }; + + comboBox.SelectionChanged += (_s, _e) => + { + try + { + eComboBox.Value = comboBox.Text; + } + catch (Exception ex) + { + DisplayException(ex.Message); + } + }; + + eComboBox.OnValueChanged += () => + { + comboBox.SelectedItem = eComboBox.Value; + }; + + _ = dockPanel.Children.Add(comboBox); + } } private void DisplayException(string message) @@ -201,7 +231,7 @@ namespace DesktopMagic.Helpers _ = MessageBox.Show("File execution error:\n" + message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); int index = MainWindow.WindowNames.IndexOf(optionsComboBox.SelectedItem.ToString()); - PluginWindow window = MainWindow.Windows[index] as PluginWindow; + PluginWindow window = MainWindow.Windows[index]; window?.Exit(); } } diff --git a/src/DesktopMagic/MainWindow.xaml.cs b/src/DesktopMagic/MainWindow.xaml.cs index 22fe8a5..b4ddf55 100644 --- a/src/DesktopMagic/MainWindow.xaml.cs +++ b/src/DesktopMagic/MainWindow.xaml.cs @@ -206,11 +206,11 @@ namespace DesktopMagic switch (checkBox.Name) { case "TimeCb": - window = new PluginWindow(new TimePlugin()); + window = new PluginWindow(new TimePlugin(), checkBox.Content.ToString()); break; case "DateCb": - window = new PluginWindow(new DatePlugin()); + window = new PluginWindow(new DatePlugin(), checkBox.Content.ToString()); break; case "CpuUsageCb": @@ -218,7 +218,7 @@ namespace DesktopMagic break; case "MusicVisualizerCb": - window = new PluginWindow(new MusicVisualizerPlugin()); + window = new PluginWindow(new MusicVisualizerPlugin(), checkBox.Content.ToString()); break; default: @@ -285,7 +285,6 @@ namespace DesktopMagic Windows.RemoveAt(index); WindowNames.RemoveAt(index); - //window.Close(); } catch { } } diff --git a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs index 5198894..8655936 100644 --- a/src/DesktopMagic/Plugins/PluginWindow.xaml.cs +++ b/src/DesktopMagic/Plugins/PluginWindow.xaml.cs @@ -70,7 +70,7 @@ namespace DesktopMagic Width = double.Parse(key.GetValue(pluginName + "WindowWidth", 500).ToString()); } - public PluginWindow(Plugin pluginClassInstance) : this(pluginClassInstance.GetType().Name) + public PluginWindow(Plugin pluginClassInstance, string pluginName) : this(pluginName) { this.pluginClassInstance = pluginClassInstance; } @@ -125,8 +125,8 @@ namespace DesktopMagic else { viewBox.Margin = new Thickness(MainWindow.Theme.Margin); - border.Width = viewBox.ActualWidth + MainWindow.Theme.Margin * 2; - border.Height = viewBox.ActualHeight + MainWindow.Theme.Margin * 2; + border.Width = viewBox.ActualWidth + (MainWindow.Theme.Margin * 2); + border.Height = viewBox.ActualHeight + (MainWindow.Theme.Margin * 2); rectangleGeometry.Rect = new Rect(-MainWindow.Theme.Margin, -MainWindow.Theme.Margin, border.ActualWidth, border.ActualHeight); border.Background = MainWindow.Theme.BackgroundBrush; border.CornerRadius = new CornerRadius(MainWindow.Theme.CornerRadius); diff --git a/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.md b/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.md index 1d1c374..1326acc 100644 --- a/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.md +++ b/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.md @@ -10,6 +10,10 @@ - [CheckBox](#T-DesktopMagicPluginAPI-Inputs-CheckBox 'DesktopMagicPluginAPI.Inputs.CheckBox') - [#ctor(value)](#M-DesktopMagicPluginAPI-Inputs-CheckBox-#ctor-System-Boolean- 'DesktopMagicPluginAPI.Inputs.CheckBox.#ctor(System.Boolean)') - [Value](#P-DesktopMagicPluginAPI-Inputs-CheckBox-Value 'DesktopMagicPluginAPI.Inputs.CheckBox.Value') +- [ComboBox](#T-DesktopMagicPluginAPI-Inputs-ComboBox 'DesktopMagicPluginAPI.Inputs.ComboBox') + - [#ctor(items)](#M-DesktopMagicPluginAPI-Inputs-ComboBox-#ctor-System-String[]- 'DesktopMagicPluginAPI.Inputs.ComboBox.#ctor(System.String[])') + - [Items](#P-DesktopMagicPluginAPI-Inputs-ComboBox-Items 'DesktopMagicPluginAPI.Inputs.ComboBox.Items') + - [Value](#P-DesktopMagicPluginAPI-Inputs-ComboBox-Value 'DesktopMagicPluginAPI.Inputs.ComboBox.Value') - [Element](#T-DesktopMagicPluginAPI-Inputs-Element 'DesktopMagicPluginAPI.Inputs.Element') - [ValueChanged()](#M-DesktopMagicPluginAPI-Inputs-Element-ValueChanged 'DesktopMagicPluginAPI.Inputs.Element.ValueChanged') - [ElementAttribute](#T-DesktopMagicPluginAPI-Inputs-ElementAttribute 'DesktopMagicPluginAPI.Inputs.ElementAttribute') @@ -151,6 +155,48 @@ Initializes a new instance of the [CheckBox](#T-DesktopMagicPluginAPI-Inputs-Che Gets or set a value indicating whether the [CheckBox](#T-DesktopMagicPluginAPI-Inputs-CheckBox 'DesktopMagicPluginAPI.Inputs.CheckBox') is in the checked state. + +## ComboBox `type` + +##### Namespace + +DesktopMagicPluginAPI.Inputs + +##### Summary + +Represents a selection control with a drop-down list. + + +### #ctor(items) `constructor` + +##### Summary + +Initializes a new instance of the [Label](#T-DesktopMagicPluginAPI-Inputs-Label 'DesktopMagicPluginAPI.Inputs.Label') class with the provided `items`. + +##### Parameters + +| Name | Type | Description | +| ---- | ---- | ----------- | +| items | [System.String[]](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String[] 'System.String[]') | | + + +### Items `property` + +##### Summary + +Gets the collection used to generate the content of the [ComboBox](#T-DesktopMagicPluginAPI-Inputs-ComboBox 'DesktopMagicPluginAPI.Inputs.ComboBox'). + + +### Value `property` + +##### Summary + +Gets the currently selected item associated with this [ComboBox](#T-DesktopMagicPluginAPI-Inputs-ComboBox 'DesktopMagicPluginAPI.Inputs.ComboBox'). + +##### Remarks + +If you assign a value to this property, the displayed text in the user interface will not be changed. + ## Element `type` diff --git a/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.xml b/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.xml index e5780db..e11ef9c 100644 --- a/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.xml +++ b/src/DesktopMagicPluginAPI/DesktopMagicPluginAPI.xml @@ -145,6 +145,28 @@ A value indicating whether the is in the checked state. + + + Represents a selection control with a drop-down list. + + + + + Gets the collection used to generate the content of the . + + + + + Gets the currently selected item associated with this . + + If you assign a value to this property, the displayed text in the user interface will not be changed. + + + + Initializes a new instance of the class with the provided . + + + The element base class. diff --git a/src/DesktopMagicPluginAPI/Inputs/ComboBox.cs b/src/DesktopMagicPluginAPI/Inputs/ComboBox.cs new file mode 100644 index 0000000..3bfa2d5 --- /dev/null +++ b/src/DesktopMagicPluginAPI/Inputs/ComboBox.cs @@ -0,0 +1,46 @@ +using System.Collections.ObjectModel; + +namespace DesktopMagicPluginAPI.Inputs +{ + /// + /// Represents a selection control with a drop-down list. + /// + public class ComboBox : Element + { + private string _value; + + /// + /// Gets the collection used to generate the content of the . + /// + public ObservableCollection Items { get; } = new ObservableCollection(); + + /// + /// Gets the currently selected item associated with this . + /// + /// If you assign a value to this property, the displayed text in the user interface will not be changed. + public string Value + { + get => _value; + set + { + if (_value != value) + { + _value = value; + ValueChanged(); + } + } + } + + /// + /// Initializes a new instance of the class with the provided . + /// + /// + public ComboBox(params string[] items) + { + foreach (string item in items) + { + Items.Add(item); + } + } + } +} \ No newline at end of file diff --git a/src/DesktopMagicPluginAPI/Plugin.cs b/src/DesktopMagicPluginAPI/Plugin.cs index a52aee4..fb0cb81 100644 --- a/src/DesktopMagicPluginAPI/Plugin.cs +++ b/src/DesktopMagicPluginAPI/Plugin.cs @@ -19,17 +19,7 @@ namespace DesktopMagicPluginAPI public IPluginData Application { get => application; - set - { - if (application is null) - { - application = value; - } - else - { - throw new InvalidOperationException($"You cannot set the value of the {nameof(Application)} property"); - } - } + set => application = application is null ? value : throw new InvalidOperationException($"You cannot set the value of the {nameof(Application)} property"); } ///