From 4e1e900d0d57c0a9dfe313993235c7e87470ff94 Mon Sep 17 00:00:00 2001 From: emoacht Date: Sun, 23 Oct 2022 15:21:49 +0900 Subject: [PATCH 1/3] Suppress exception from and separate system information --- .../Models/Monitor/MonitorManager.cs | 10 +--- Source/Monitorian.Core/Models/SystemInfo.cs | 52 +++++++++++++++++++ Source/Monitorian.Core/Monitorian.Core.csproj | 1 + 3 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 Source/Monitorian.Core/Models/SystemInfo.cs diff --git a/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs b/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs index 6d27ec10..dd3709c4 100644 --- a/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs +++ b/Source/Monitorian.Core/Models/Monitor/MonitorManager.cs @@ -3,7 +3,6 @@ using System.Diagnostics; using System.IO; using System.Linq; -using System.Management; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; @@ -325,7 +324,7 @@ public MonitorData() public async Task PopulateAsync() { - System = GetSystem(); + System = $"Manufacturer: {SystemInfo.Manufacturer}, Model: {SystemInfo.Model}, OS: {Environment.OSVersion.Version}"; var sw = new Stopwatch(); @@ -371,13 +370,6 @@ Task GetTask(string name, Action action) => return $@"{name,-14} -> {elapsed.ToString($@"{(elapsed.Minutes > 0 ? @"m\:" : string.Empty)}s\.fff")}"; }); } - - private string GetSystem() - { - using var @class = new ManagementClass("Win32_ComputerSystem"); - using var instance = @class.GetInstances().Cast().FirstOrDefault(); - return $"Manufacturer: {instance?["Manufacturer"]}, Model: {instance?["Model"]}, OS: {Environment.OSVersion.Version}"; - } } #endregion diff --git a/Source/Monitorian.Core/Models/SystemInfo.cs b/Source/Monitorian.Core/Models/SystemInfo.cs new file mode 100644 index 00000000..0d122282 --- /dev/null +++ b/Source/Monitorian.Core/Models/SystemInfo.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Management; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Win32; + +namespace Monitorian.Core.Models +{ + /// + /// System information + /// + public static class SystemInfo + { + private class SystemInfoInternal + { + public string Manufacturer { get; } + public string Model { get; } + + public SystemInfoInternal() + { + try + { + using var @class = new ManagementClass("Win32_ComputerSystem"); + using var instance = @class.GetInstances().Cast().FirstOrDefault(); + + Manufacturer = instance?["Manufacturer"] as string; + Model = instance?["Model"] as string; + return; + } + catch (ManagementException) + { + Debug.WriteLine($"Failed to get instances by Win32_ComputerSystem"); + } + + const string keyName = @"SYSTEM\CurrentControlSet\Control\SystemInformation"; // HKLM + + using var key = Registry.LocalMachine.OpenSubKey(keyName); + + Manufacturer = key?.GetValue("SystemManufacturer") as string; + Model = key?.GetValue("SystemProductName") as string; + } + } + + private static readonly Lazy _instance = new(() => new()); + + public static string Manufacturer => _instance.Value.Manufacturer; + public static string Model => _instance.Value.Model; + } +} \ No newline at end of file diff --git a/Source/Monitorian.Core/Monitorian.Core.csproj b/Source/Monitorian.Core/Monitorian.Core.csproj index 6f777e6a..3628a0cd 100644 --- a/Source/Monitorian.Core/Monitorian.Core.csproj +++ b/Source/Monitorian.Core/Monitorian.Core.csproj @@ -93,6 +93,7 @@ + True From 011eaad3de231e8650dc255c1d544cc860d0d9b8 Mon Sep 17 00:00:00 2001 From: emoacht Date: Sun, 23 Oct 2022 15:22:04 +0900 Subject: [PATCH 2/3] Enable live ordering by monitors arrangement --- .../ViewModels/MainWindowViewModel.cs | 31 ++++++++++--------- .../ViewModels/MonitorViewModel.cs | 26 +++++++++++++++- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Source/Monitorian.Core/ViewModels/MainWindowViewModel.cs b/Source/Monitorian.Core/ViewModels/MainWindowViewModel.cs index d63474b1..9ed06440 100644 --- a/Source/Monitorian.Core/ViewModels/MainWindowViewModel.cs +++ b/Source/Monitorian.Core/ViewModels/MainWindowViewModel.cs @@ -32,7 +32,9 @@ public ListCollectionView MonitorsView _monitorsView = new ListCollectionView(_controller.Monitors); if (Settings.OrdersArrangement) { - _monitorsView.SortDescriptions.Add(new SortDescription(nameof(MonitorViewModel.MonitorTop), ListSortDirection.Ascending)); + _monitorsView.SortDescriptions.Add(new SortDescription(nameof(MonitorViewModel.MonitorTopLeft), ListSortDirection.Ascending)); + _monitorsView.IsLiveSorting = true; + _monitorsView.LiveSortingProperties.Add(nameof(MonitorViewModel.MonitorTopLeft)); } _monitorsView.SortDescriptions.Add(new SortDescription(nameof(MonitorViewModel.DisplayIndex), ListSortDirection.Ascending)); _monitorsView.SortDescriptions.Add(new SortDescription(nameof(MonitorViewModel.MonitorIndex), ListSortDirection.Ascending)); @@ -88,21 +90,22 @@ private void OnSettingsChanged(object sender, PropertyChangedEventArgs e) switch (e.PropertyName) { case nameof(Settings.OrdersArrangement): - var orderDescription = MonitorsView.SortDescriptions.FirstOrDefault(x => x.PropertyName == nameof(MonitorViewModel.MonitorTop)); + var description = new SortDescription(nameof(MonitorViewModel.MonitorTopLeft), ListSortDirection.Ascending); + int index = MonitorsView.SortDescriptions.IndexOf(description); - if (Settings.OrdersArrangement) + switch (Settings.OrdersArrangement, index) { - if (orderDescription != default) - return; - - MonitorsView.SortDescriptions.Insert(0, new SortDescription(nameof(MonitorViewModel.MonitorTop), ListSortDirection.Ascending)); - } - else - { - if (orderDescription == default) - return; - - MonitorsView.SortDescriptions.Remove(orderDescription); + case (true, < 0): + MonitorsView.SortDescriptions.Insert(0, description); + MonitorsView.IsLiveSorting = true; + MonitorsView.LiveSortingProperties.Add(description.PropertyName); + break; + + case (false, >= 0): + MonitorsView.SortDescriptions.RemoveAt(index); + MonitorsView.IsLiveSorting = false; + MonitorsView.LiveSortingProperties.Remove(description.PropertyName); + break; } MonitorsView.Refresh(); diff --git a/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs b/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs index e3817923..c0308a82 100644 --- a/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs +++ b/Source/Monitorian.Core/ViewModels/MonitorViewModel.cs @@ -23,6 +23,7 @@ public MonitorViewModel(AppControllerCore controller, IMonitor monitor) { this._controller = controller ?? throw new ArgumentNullException(nameof(controller)); this._monitor = monitor ?? throw new ArgumentNullException(nameof(monitor)); + SetTopLeft(); LoadCustomization(); } @@ -37,6 +38,7 @@ internal void Replace(IMonitor monitor) { this._monitor.Dispose(); this._monitor = monitor; + SetTopLeft(); } } else @@ -50,7 +52,6 @@ internal void Replace(IMonitor monitor) public byte DisplayIndex => _monitor.DisplayIndex; public byte MonitorIndex => _monitor.MonitorIndex; public Rect MonitorRect => _monitor.MonitorRect; - public double MonitorTop => _monitor.MonitorRect.Top; #region Customization @@ -463,6 +464,29 @@ public bool IsSelected #endregion + #region Arrangement + + public ulong MonitorTopLeft + { + get => _monitorTopLeft; + private set => SetProperty(ref _monitorTopLeft, value); + } + private ulong _monitorTopLeft; + + private void SetTopLeft() + { + MonitorTopLeft = GetTopLeft(_monitor.MonitorRect.Location); + + static ulong GetTopLeft(Point location) + { + var x = (long)Math.Round(location.X, MidpointRounding.AwayFromZero) + int.MaxValue; + var y = (long)Math.Round(location.Y, MidpointRounding.AwayFromZero) + int.MaxValue; + return (ulong)x | ((ulong)y << 32); + } + } + + #endregion + public bool IsTarget { get => _isTarget; From 502915c3a238c2b7efa272a2f7b614dc23f10ac3 Mon Sep 17 00:00:00 2001 From: emoacht Date: Sun, 23 Oct 2022 15:25:26 +0900 Subject: [PATCH 3/3] Increment minor version --- Source/Installer/Product.wxs | 2 +- Source/Monitorian.Core/Properties/AssemblyInfo.cs | 4 ++-- Source/Monitorian/Properties/AssemblyInfo.cs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Installer/Product.wxs b/Source/Installer/Product.wxs index 1d8f0d86..c7507bdb 100644 --- a/Source/Installer/Product.wxs +++ b/Source/Installer/Product.wxs @@ -1,6 +1,6 @@  -