diff --git a/src/common/Display/dpi_aware.cpp b/src/common/Display/dpi_aware.cpp index 8397430c6d03..cb8c46df3551 100644 --- a/src/common/Display/dpi_aware.cpp +++ b/src/common/Display/dpi_aware.cpp @@ -109,6 +109,24 @@ namespace DPIAware } } + void InverseConvert(HMONITOR monitor_handle, RECT& rect) + { + if (monitor_handle == NULL) + { + const POINT ptZero = { 0, 0 }; + monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY); + } + + UINT dpi_x, dpi_y; + if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK) + { + rect.left = static_cast(std::round(rect.left * static_cast(DEFAULT_DPI) / dpi_x)); + rect.right = static_cast(std::round(rect.right * static_cast(DEFAULT_DPI) / dpi_x)); + rect.top = static_cast(std::round(rect.top * static_cast(DEFAULT_DPI) / dpi_y)); + rect.bottom = static_cast(std::round(rect.bottom * static_cast(DEFAULT_DPI) / dpi_y)); + } + } + void EnableDPIAwarenessForThisProcess() { SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); diff --git a/src/common/Display/dpi_aware.h b/src/common/Display/dpi_aware.h index a63365aa2fb7..bbbb61fd4046 100644 --- a/src/common/Display/dpi_aware.h +++ b/src/common/Display/dpi_aware.h @@ -15,6 +15,7 @@ namespace DPIAware void Convert(HMONITOR monitor_handle, RECT& rect); void ConvertByCursorPosition(float& width, float& height); void InverseConvert(HMONITOR monitor_handle, float& width, float& height); + void InverseConvert(HMONITOR monitor_handle, RECT& rect); void EnableDPIAwarenessForThisProcess(); enum AwarenessLevel diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs b/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs index 4a0897fef8d0..050591591a04 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Data/ProjectData.cs @@ -3,9 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; - -using Workspaces.Data; - using static WorkspacesEditor.Data.ProjectData; namespace WorkspacesEditor.Data diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs b/src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs index b3de2f5b4df2..a1600885b983 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Data/TempProjectData.cs @@ -8,13 +8,7 @@ namespace WorkspacesEditor.Data { public class TempProjectData : ProjectData { - public static string File - { - get - { - return FolderUtils.DataFolder() + "\\temp-workspaces.json"; - } - } + public static string File => FolderUtils.DataFolder() + "\\temp-workspaces.json"; public static void DeleteTempFile() { diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs b/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs index a16ba2c3c6c7..6e0d015905fa 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesData.cs @@ -3,8 +3,6 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; - -using Workspaces.Data; using WorkspacesEditor.Utils; using static WorkspacesEditor.Data.ProjectData; @@ -14,13 +12,7 @@ namespace WorkspacesEditor.Data { public class WorkspacesData : WorkspacesEditorData { - public string File - { - get - { - return FolderUtils.DataFolder() + "\\workspaces.json"; - } - } + public string File => FolderUtils.DataFolder() + "\\workspaces.json"; public struct WorkspacesListWrapper { diff --git a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs b/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs index e2492d839fa5..c2ad0a70a466 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Data/WorkspacesEditorData`1.cs @@ -3,28 +3,24 @@ // See the LICENSE file in the project root for more information. using System.Text.Json; - using WorkspacesEditor.Utils; -namespace Workspaces.Data +namespace WorkspacesEditor.Data { public class WorkspacesEditorData { protected JsonSerializerOptions JsonOptions { - get + get => new() { - return new JsonSerializerOptions - { - PropertyNamingPolicy = new DashCaseNamingPolicy(), - WriteIndented = true, - }; - } + PropertyNamingPolicy = new DashCaseNamingPolicy(), + WriteIndented = true, + }; } public T Read(string file) { - IOUtils ioUtils = new IOUtils(); + IOUtils ioUtils = new(); string data = ioUtils.ReadFile(file); return JsonSerializer.Deserialize(data, JsonOptions); } diff --git a/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml b/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml index 0c5eb55ccc36..29630a910c98 100644 --- a/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml +++ b/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml @@ -8,7 +8,7 @@ xmlns:ui="http://schemas.modernwpf.com/2019" x:Name="WorkspacesMainWindow" Title="{x:Static props:Resources.MainTitle}" - MinWidth="700" + MinWidth="750" MinHeight="680" ui:TitleBar.Background="{DynamicResource PrimaryBackgroundBrush}" ui:TitleBar.InactiveBackground="{DynamicResource TertiaryBackgroundBrush}" diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/AppListDataTemplateSelector.cs b/src/modules/Workspaces/WorkspacesEditor/Models/AppListDataTemplateSelector.cs index 660793c1c23f..78a88595f1ec 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/AppListDataTemplateSelector.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/AppListDataTemplateSelector.cs @@ -18,14 +18,7 @@ public AppListDataTemplateSelector() public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) { - if (item is MonitorHeaderRow) - { - return HeaderTemplate; - } - else - { - return AppTemplate; - } + return item is MonitorHeaderRow ? HeaderTemplate : AppTemplate; } } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs index 3d30c38cab51..7d8bed64dea8 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Application.cs @@ -1,25 +1,22 @@ -// Copyright (c) Microsoft Corporation +// Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.ComponentModel; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using System.Globalization; -using System.IO; -using System.Linq; using System.Text.Json.Serialization; -using System.Text.RegularExpressions; -using System.Windows.Media.Imaging; -using ManagedCommon; -using Windows.Management.Deployment; -using WorkspacesCsharpLibrary; using WorkspacesCsharpLibrary.Models; namespace WorkspacesEditor.Models { + public enum WindowPositionKind + { + Custom = 0, + Maximized = 1, + Minimized = 2, + } + public class Application : BaseApplication, IDisposable { private bool _isInitialized; @@ -79,7 +76,7 @@ public struct WindowPosition return left.X != right.X || left.Y != right.Y || left.Width != right.Width || left.Height != right.Height; } - public override bool Equals(object obj) + public override readonly bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { @@ -90,7 +87,7 @@ public override bool Equals(object obj) return X == pos.X && Y == pos.Y && Width == pos.Width && Height == pos.Height; } - public override int GetHashCode() + public override readonly int GetHashCode() { return base.GetHashCode(); } @@ -136,36 +133,24 @@ private void RedrawPreviewImage() } } - private bool _minimized; + public bool Minimized { get; set; } - public bool Minimized - { - get => _minimized; - set - { - _minimized = value; - OnPropertyChanged(new PropertyChangedEventArgs(nameof(Minimized))); - OnPropertyChanged(new PropertyChangedEventArgs(nameof(EditPositionEnabled))); - RedrawPreviewImage(); - } - } + public bool Maximized { get; set; } - private bool _maximized; + public bool EditPositionEnabled => !Minimized && !Maximized; - public bool Maximized + public int PositionComboboxIndex { - get => _maximized; + get => Maximized ? (int)WindowPositionKind.Maximized : Minimized ? (int)WindowPositionKind.Minimized : (int)WindowPositionKind.Custom; set { - _maximized = value; - OnPropertyChanged(new PropertyChangedEventArgs(nameof(Maximized))); + Maximized = value == (int)WindowPositionKind.Maximized; + Minimized = value == (int)WindowPositionKind.Minimized; OnPropertyChanged(new PropertyChangedEventArgs(nameof(EditPositionEnabled))); RedrawPreviewImage(); } } - public bool EditPositionEnabled { get => !Minimized && !Maximized; } - private string _appMainParams; public string AppMainParams @@ -183,7 +168,7 @@ public string AppMainParams } } - public bool IsAppMainParamVisible { get => !string.IsNullOrWhiteSpace(_appMainParams); } + public bool IsAppMainParamVisible => !string.IsNullOrWhiteSpace(_appMainParams); [JsonIgnore] public bool IsHighlighted { get; set; } @@ -192,13 +177,7 @@ public string AppMainParams public int RepeatIndex { get; set; } [JsonIgnore] - public string RepeatIndexString - { - get - { - return RepeatIndex <= 1 ? string.Empty : RepeatIndex.ToString(CultureInfo.InvariantCulture); - } - } + public string RepeatIndexString => RepeatIndex <= 1 ? string.Empty : RepeatIndex.ToString(CultureInfo.InvariantCulture); private WindowPosition _position; @@ -242,10 +221,7 @@ public MonitorSetup MonitorSetup { get { - if (_monitorSetup == null) - { - _monitorSetup = Parent.GetMonitorForApp(this); - } + _monitorSetup ??= Parent.GetMonitorForApp(this); return _monitorSetup; } @@ -271,7 +247,7 @@ public bool IsExpanded } } - public string DeleteButtonContent { get => _isIncluded ? Properties.Resources.Delete : Properties.Resources.AddBack; } + public string DeleteButtonContent => _isIncluded ? Properties.Resources.Delete : Properties.Resources.AddBack; private bool _isIncluded = true; @@ -298,15 +274,5 @@ internal void CommandLineTextChanged(string newCommandLineValue) CommandLineArguments = newCommandLineValue; OnPropertyChanged(new PropertyChangedEventArgs(nameof(AppMainParams))); } - - internal void MaximizedChecked() - { - Minimized = false; - } - - internal void MinimizedChecked() - { - Maximized = false; - } } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Monitor.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Monitor.cs index 2a911eb1fe65..368223c6e484 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Monitor.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Monitor.cs @@ -6,28 +6,18 @@ namespace WorkspacesEditor.Models { - public class Monitor + public class Monitor(string monitorName, string monitorInstanceId, int number, int dpi, Rect dpiAwareBounds, Rect dpiUnawareBounds) { - public string MonitorName { get; private set; } + public string MonitorName { get; private set; } = monitorName; - public string MonitorInstanceId { get; private set; } + public string MonitorInstanceId { get; private set; } = monitorInstanceId; - public int MonitorNumber { get; private set; } + public int MonitorNumber { get; private set; } = number; - public int Dpi { get; private set; } + public int Dpi { get; private set; } = dpi; - public Rect MonitorDpiUnawareBounds { get; private set; } + public Rect MonitorDpiUnawareBounds { get; private set; } = dpiUnawareBounds; - public Rect MonitorDpiAwareBounds { get; private set; } - - public Monitor(string monitorName, string monitorInstanceId, int number, int dpi, Rect dpiAwareBounds, Rect dpiUnawareBounds) - { - MonitorName = monitorName; - MonitorInstanceId = monitorInstanceId; - MonitorNumber = number; - Dpi = dpi; - MonitorDpiAwareBounds = dpiAwareBounds; - MonitorDpiUnawareBounds = dpiUnawareBounds; - } + public Rect MonitorDpiAwareBounds { get; private set; } = dpiAwareBounds; } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/MonitorSetup.cs b/src/modules/Workspaces/WorkspacesEditor/Models/MonitorSetup.cs index e44365a98803..a51ede204717 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/MonitorSetup.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/MonitorSetup.cs @@ -16,9 +16,9 @@ public void OnPropertyChanged(PropertyChangedEventArgs e) PropertyChanged?.Invoke(this, e); } - public string MonitorInfo { get => MonitorName; } + public string MonitorInfo => MonitorName; - public string MonitorInfoWithResolution { get => $"{MonitorName} {MonitorDpiAwareBounds.Width}x{MonitorDpiAwareBounds.Height}"; } + public string MonitorInfoWithResolution => $"{MonitorName} {MonitorDpiAwareBounds.Width}x{MonitorDpiAwareBounds.Height}"; public MonitorSetup(string monitorName, string monitorInstanceId, int number, int dpi, Rect dpiAwareBounds, Rect dpiUnawareBounds) : base(monitorName, monitorInstanceId, number, dpi, dpiAwareBounds, dpiUnawareBounds) diff --git a/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs b/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs index 19b867f38889..0c44440d394e 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Models/Project.cs @@ -29,10 +29,7 @@ public class Project : INotifyPropertyChanged public string Name { - get - { - return _name; - } + get => _name; set { @@ -68,8 +65,7 @@ public string LastLaunched DateTime lastLaunchDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(LastLaunchedTime); - var now = DateTime.UtcNow.Ticks; - var ts = DateTime.UtcNow - lastLaunchDateTime; + TimeSpan ts = DateTime.UtcNow - lastLaunchDateTime; double delta = Math.Abs(ts.TotalSeconds); if (delta < 1 * MINUTE) @@ -120,10 +116,7 @@ public string LastLaunched } } - public bool CanBeSaved - { - get => Name.Length > 0 && Applications.Count > 0; - } + public bool CanBeSaved => Name.Length > 0 && Applications.Count > 0; private bool _isRevertEnabled; @@ -145,10 +138,7 @@ public bool IsRevertEnabled [JsonIgnore] public bool IsPopupVisible { - get - { - return _isPopupVisible; - } + get => _isPopupVisible; set { @@ -163,11 +153,11 @@ public List ApplicationsListed { get { - List applicationsListed = new List(); + List applicationsListed = []; ILookup apps = Applications.Where(x => !x.Minimized).ToLookup(x => x.MonitorSetup); - foreach (var appItem in apps.OrderBy(x => x.Key.MonitorDpiUnawareBounds.Left).ThenBy(x => x.Key.MonitorDpiUnawareBounds.Top)) + foreach (IGrouping appItem in apps.OrderBy(x => x.Key.MonitorDpiUnawareBounds.Left).ThenBy(x => x.Key.MonitorDpiUnawareBounds.Top)) { - MonitorHeaderRow headerRow = new MonitorHeaderRow { MonitorName = "Screen " + appItem.Key.MonitorNumber, SelectString = Properties.Resources.SelectAllAppsOnMonitor + " " + appItem.Key.MonitorInfo }; + MonitorHeaderRow headerRow = new() { MonitorName = "Screen " + appItem.Key.MonitorNumber, SelectString = Properties.Resources.SelectAllAppsOnMonitor + " " + appItem.Key.MonitorInfo }; applicationsListed.Add(headerRow); foreach (Application app in appItem) { @@ -175,10 +165,10 @@ public List ApplicationsListed } } - var minimizedApps = Applications.Where(x => x.Minimized); + IEnumerable minimizedApps = Applications.Where(x => x.Minimized); if (minimizedApps.Any()) { - MonitorHeaderRow headerRow = new MonitorHeaderRow { MonitorName = Properties.Resources.Minimized_Apps, SelectString = Properties.Resources.SelectAllMinimizedApps }; + MonitorHeaderRow headerRow = new() { MonitorName = Properties.Resources.Minimized_Apps, SelectString = Properties.Resources.SelectAllMinimizedApps }; applicationsListed.Add(headerRow); foreach (Application app in minimizedApps) { @@ -219,17 +209,17 @@ public Project(Project selectedProject) int screenIndex = 1; - Monitors = new List(); - foreach (var item in selectedProject.Monitors.OrderBy(x => x.MonitorDpiAwareBounds.Left).ThenBy(x => x.MonitorDpiAwareBounds.Top)) + Monitors = []; + foreach (MonitorSetup item in selectedProject.Monitors.OrderBy(x => x.MonitorDpiAwareBounds.Left).ThenBy(x => x.MonitorDpiAwareBounds.Top)) { Monitors.Add(item); screenIndex++; } - Applications = new List(); - foreach (var item in selectedProject.Applications) + Applications = []; + foreach (Application item in selectedProject.Applications) { - Application newApp = new Application(item); + Application newApp = new(item); newApp.Parent = this; newApp.InitializationFinished(); Applications.Add(newApp); @@ -244,14 +234,14 @@ public Project(ProjectData.ProjectWrapper project) LastLaunchedTime = project.LastLaunchedTime; IsShortcutNeeded = project.IsShortcutNeeded; MoveExistingWindows = project.MoveExistingWindows; - Monitors = new List() { }; - Applications = new List { }; + Monitors = []; + Applications = []; - foreach (var app in project.Applications) + foreach (ProjectData.ApplicationWrapper app in project.Applications) { - Models.Application newApp = new Models.Application() + Models.Application newApp = new() { - Id = string.IsNullOrEmpty(app.Id) ? $"{{{Guid.NewGuid().ToString()}}}" : app.Id, + Id = string.IsNullOrEmpty(app.Id) ? $"{{{Guid.NewGuid()}}}" : app.Id, AppName = app.Application, AppPath = app.ApplicationPath, AppTitle = app.Title, @@ -278,20 +268,17 @@ public Project(ProjectData.ProjectWrapper project) Applications.Add(newApp); } - foreach (var monitor in project.MonitorConfiguration) + foreach (ProjectData.MonitorConfigurationWrapper monitor in project.MonitorConfiguration) { - System.Windows.Rect dpiAware = new System.Windows.Rect(monitor.MonitorRectDpiAware.Left, monitor.MonitorRectDpiAware.Top, monitor.MonitorRectDpiAware.Width, monitor.MonitorRectDpiAware.Height); - System.Windows.Rect dpiUnaware = new System.Windows.Rect(monitor.MonitorRectDpiUnaware.Left, monitor.MonitorRectDpiUnaware.Top, monitor.MonitorRectDpiUnaware.Width, monitor.MonitorRectDpiUnaware.Height); + System.Windows.Rect dpiAware = new(monitor.MonitorRectDpiAware.Left, monitor.MonitorRectDpiAware.Top, monitor.MonitorRectDpiAware.Width, monitor.MonitorRectDpiAware.Height); + System.Windows.Rect dpiUnaware = new(monitor.MonitorRectDpiUnaware.Left, monitor.MonitorRectDpiUnaware.Top, monitor.MonitorRectDpiUnaware.Width, monitor.MonitorRectDpiUnaware.Height); Monitors.Add(new MonitorSetup(monitor.Id, monitor.InstanceId, monitor.MonitorNumber, monitor.Dpi, dpiAware, dpiUnaware)); } } public BitmapImage PreviewIcons { - get - { - return _previewIcons; - } + get => _previewIcons; set { @@ -302,10 +289,7 @@ public BitmapImage PreviewIcons public BitmapImage PreviewImage { - get - { - return _previewImage; - } + get => _previewImage; set { @@ -316,10 +300,7 @@ public BitmapImage PreviewImage public double PreviewImageWidth { - get - { - return _previewImageWidth; - } + get => _previewImageWidth; set { @@ -366,6 +347,7 @@ public void UpdateAfterLaunchAndEdit(Project other) Id = other.Id; Name = other.Name; IsRevertEnabled = true; + MoveExistingWindows = other.MoveExistingWindows; } internal void CloseExpanders() @@ -378,13 +360,13 @@ internal void CloseExpanders() internal MonitorSetup GetMonitorForApp(Application app) { - var monitorSetup = Monitors.Where(x => x.MonitorNumber == app.MonitorNumber).FirstOrDefault(); + MonitorSetup monitorSetup = Monitors.Where(x => x.MonitorNumber == app.MonitorNumber).FirstOrDefault(); if (monitorSetup == null) { // monitors changed: try to determine monitor id based on middle point int middleX = app.Position.X + (app.Position.Width / 2); int middleY = app.Position.Y + (app.Position.Height / 2); - var monitorCandidate = Monitors.Where(x => + MonitorSetup monitorCandidate = Monitors.Where(x => (x.MonitorDpiUnawareBounds.Left < middleX) && (x.MonitorDpiUnawareBounds.Right > middleX) && (x.MonitorDpiUnawareBounds.Top < middleY) && diff --git a/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.Designer.cs b/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.Designer.cs index ce81b9785d04..f444c1dd2f6e 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.Designer.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.Designer.cs @@ -61,7 +61,7 @@ internal Resources() { } /// - /// Looks up a localized string similar to Add Back. + /// Looks up a localized string similar to Add back. /// public static string AddBack { get { @@ -78,15 +78,6 @@ public static string Admin { } } - /// - /// Looks up a localized string similar to Launch new app instances. - /// - public static string AlwaysLaunch { - get { - return ResourceManager.GetString("AlwaysLaunch", resourceCulture); - } - } - /// /// Looks up a localized string similar to app. /// @@ -169,7 +160,7 @@ public static string Created { } /// - /// Looks up a localized string similar to Create Desktop Shortcut. + /// Looks up a localized string similar to Create desktop shortcut. /// public static string CreateShortcut { get { @@ -186,6 +177,15 @@ public static string CreateWorkspace { } } + /// + /// Looks up a localized string similar to Custom. + /// + public static string Custom { + get { + return ResourceManager.GetString("Custom", resourceCulture); + } + } + /// /// Looks up a localized string similar to days ago. /// @@ -223,7 +223,7 @@ public static string Delete_Workspace_Dialog_Announce { } /// - /// Looks up a localized string similar to Remove Selected Apps. + /// Looks up a localized string similar to Remove selected apps. /// public static string DeleteSelected { get { @@ -322,7 +322,7 @@ public static string LaunchAsAdmin { } /// - /// Looks up a localized string similar to Launch & Edit. + /// Looks up a localized string similar to Launch & edit. /// public static string LaunchEdit { get { @@ -367,7 +367,7 @@ public static string Minimized { } /// - /// Looks up a localized string similar to Minimized Apps. + /// Looks up a localized string similar to Minimized apps. /// public static string Minimized_Apps { get { @@ -394,7 +394,7 @@ public static string MonthsAgo { } /// - /// Looks up a localized string similar to Move apps if present. + /// Looks up a localized string similar to Move existing windows. /// public static string MoveIfExist { get { @@ -502,7 +502,7 @@ public static string OneYearAgo { } /// - /// Looks up a localized string similar to Pin Workspaces to Taskbar. + /// Looks up a localized string similar to Pin Workspaces to taskbar. /// public static string PinToTaskbar { get { @@ -565,7 +565,7 @@ public static string SecondsAgo { } /// - /// Looks up a localized string similar to Select All Apps on. + /// Looks up a localized string similar to Select all apps on. /// public static string SelectAllAppsOnMonitor { get { @@ -574,7 +574,7 @@ public static string SelectAllAppsOnMonitor { } /// - /// Looks up a localized string similar to Select All Minimized Apps. + /// Looks up a localized string similar to Select all minimized apps. /// public static string SelectAllMinimizedApps { get { @@ -583,7 +583,7 @@ public static string SelectAllMinimizedApps { } /// - /// Looks up a localized string similar to Select All Apps in Workspace. + /// Looks up a localized string similar to Select all apps in Workspace. /// public static string SelectedAllInWorkspace { get { @@ -645,6 +645,15 @@ public static string Width { } } + /// + /// Looks up a localized string similar to Window position. + /// + public static string WindowPosition { + get { + return ResourceManager.GetString("WindowPosition", resourceCulture); + } + } + /// /// Looks up a localized string similar to Workspace name. /// diff --git a/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.resx b/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.resx index b768d6682a50..fc7079e9b641 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.resx +++ b/src/modules/Workspaces/WorkspacesEditor/Properties/Resources.resx @@ -123,9 +123,6 @@ Admin - - Launch new app instances - app @@ -160,6 +157,9 @@ Create desktop shortcut + + Custom + days ago @@ -231,7 +231,7 @@ months ago - Move apps if present + Move existing windows Name @@ -321,6 +321,9 @@ Width + + Window position + Write arguments here diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs index da215a5f0d5c..e70324567d7c 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/DrawHelper.cs @@ -20,21 +20,21 @@ namespace WorkspacesEditor.Utils { public class DrawHelper { - private static Font font = new("Tahoma", 24); - private static double scale = 0.1; + private static readonly Font Font = new("Tahoma", 24); + private static readonly double Scale = 0.1; private static double gapWidth; private static double gapHeight; public static BitmapImage DrawPreview(Project project, Rectangle bounds, Theme currentTheme) { - List horizontalGaps = new List(); - List verticalGaps = new List(); + List horizontalGaps = []; + List verticalGaps = []; gapWidth = bounds.Width * 0.01; gapHeight = bounds.Height * 0.01; int Scaled(double value) { - return (int)(value * scale); + return (int)(value * Scale); } int TransformX(double posX) @@ -54,7 +54,7 @@ Rectangle GetAppRect(Application app) if (app.Maximized) { Project project = app.Parent; - var monitor = project.GetMonitorForApp(app); + MonitorSetup monitor = project.GetMonitorForApp(app); if (monitor == null) { // unrealistic case, there are no monitors at all in the workspace, use original rect @@ -69,22 +69,23 @@ Rectangle GetAppRect(Application app) } } - Dictionary repeatCounter = new Dictionary(); + Dictionary repeatCounter = []; - var appsIncluded = project.Applications.Where(x => x.IsIncluded); + IEnumerable appsIncluded = project.Applications.Where(x => x.IsIncluded); foreach (Application app in appsIncluded) { - if (repeatCounter.TryGetValue(app.AppPath + app.AppTitle, out int value)) + string appIdentifier = app.AppPath + app.PwaAppId; + if (repeatCounter.TryGetValue(appIdentifier, out int value)) { - repeatCounter[app.AppPath + app.AppTitle] = ++value; + repeatCounter[appIdentifier] = ++value; } else { - repeatCounter.Add(app.AppPath + app.AppTitle, 1); + repeatCounter.Add(appIdentifier, 1); } - app.RepeatIndex = repeatCounter[app.AppPath + app.AppTitle]; + app.RepeatIndex = repeatCounter[appIdentifier]; } foreach (Application app in project.Applications.Where(x => !x.IsIncluded)) @@ -113,7 +114,7 @@ Rectangle GetAppRect(Application app) } } - Bitmap previewBitmap = new Bitmap(Scaled(bounds.Width + (verticalGaps.Count * gapWidth)), Scaled((bounds.Height * 1.2) + (horizontalGaps.Count * gapHeight))); + Bitmap previewBitmap = new(Scaled(bounds.Width + (verticalGaps.Count * gapWidth)), Scaled((bounds.Height * 1.2) + (horizontalGaps.Count * gapHeight))); double desiredIconSize = Scaled(Math.Min(bounds.Width, bounds.Height)) * 0.25; using (Graphics g = Graphics.FromImage(previewBitmap)) { @@ -131,7 +132,7 @@ Rectangle GetAppRect(Application app) g.FillRectangle(monitorBrush, new Rectangle(TransformX(monitor.MonitorDpiAwareBounds.Left), TransformY(monitor.MonitorDpiAwareBounds.Top), Scaled(monitor.MonitorDpiAwareBounds.Width), Scaled(monitor.MonitorDpiAwareBounds.Height))); } - var appsToDraw = appsIncluded.Where(x => !x.Minimized); + IEnumerable appsToDraw = appsIncluded.Where(x => !x.Minimized); // draw the highlighted app at the end to have its icon in the foreground for the case there are overlapping icons foreach (Application app in appsToDraw.Where(x => !x.IsHighlighted)) @@ -147,24 +148,22 @@ Rectangle GetAppRect(Application app) } // draw the minimized windows - Rectangle rectMinimized = new Rectangle(0, Scaled((bounds.Height * 1.02) + (horizontalGaps.Count * gapHeight)), Scaled(bounds.Width + (verticalGaps.Count * gapWidth)), Scaled(bounds.Height * 0.18)); + Rectangle rectMinimized = new(0, Scaled((bounds.Height * 1.02) + (horizontalGaps.Count * gapHeight)), Scaled(bounds.Width + (verticalGaps.Count * gapWidth)), Scaled(bounds.Height * 0.18)); DrawWindow(g, brush, brushForHighlight, rectMinimized, appsIncluded.Where(x => x.Minimized), currentTheme); } - using (var memory = new MemoryStream()) - { - previewBitmap.Save(memory, ImageFormat.Png); - memory.Position = 0; + using MemoryStream memory = new(); + previewBitmap.Save(memory, ImageFormat.Png); + memory.Position = 0; - var bitmapImage = new BitmapImage(); - bitmapImage.BeginInit(); - bitmapImage.StreamSource = memory; - bitmapImage.CacheOption = BitmapCacheOption.OnLoad; - bitmapImage.EndInit(); - bitmapImage.Freeze(); + BitmapImage bitmapImage = new(); + bitmapImage.BeginInit(); + bitmapImage.StreamSource = memory; + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; + bitmapImage.EndInit(); + bitmapImage.Freeze(); - return bitmapImage; - } + return bitmapImage; } public static void DrawWindow(Graphics graphics, Brush brush, Rectangle bounds, Application app, double desiredIconSize, Theme currentTheme) @@ -194,7 +193,7 @@ public static void DrawWindow(Graphics graphics, Brush brush, Rectangle bounds, } double iconSize = Math.Min(Math.Min(bounds.Width - 4, bounds.Height - 4), desiredIconSize); - Rectangle iconBounds = new Rectangle((int)(bounds.Left + (bounds.Width / 2) - (iconSize / 2)), (int)(bounds.Top + (bounds.Height / 2) - (iconSize / 2)), (int)iconSize, (int)iconSize); + Rectangle iconBounds = new((int)(bounds.Left + (bounds.Width / 2) - (iconSize / 2)), (int)(bounds.Top + (bounds.Height / 2) - (iconSize / 2)), (int)iconSize, (int)iconSize); try { @@ -203,13 +202,13 @@ public static void DrawWindow(Graphics graphics, Brush brush, Rectangle bounds, { string indexString = app.RepeatIndex.ToString(CultureInfo.InvariantCulture); int indexSize = (int)(iconBounds.Width * 0.5); - Rectangle indexBounds = new Rectangle(iconBounds.Right - indexSize, iconBounds.Bottom - indexSize, indexSize, indexSize); + Rectangle indexBounds = new(iconBounds.Right - indexSize, iconBounds.Bottom - indexSize, indexSize, indexSize); - var textSize = graphics.MeasureString(indexString, font); - var state = graphics.Save(); + SizeF textSize = graphics.MeasureString(indexString, Font); + GraphicsState state = graphics.Save(); graphics.TranslateTransform(indexBounds.Left, indexBounds.Top); graphics.ScaleTransform(indexBounds.Width / textSize.Width, indexBounds.Height / textSize.Height); - graphics.DrawString(indexString, font, Brushes.Black, PointF.Empty); + graphics.DrawString(indexString, Font, Brushes.Black, PointF.Empty); graphics.Restore(state); } } @@ -255,7 +254,7 @@ public static void DrawWindow(Graphics graphics, Brush brush, Brush brushForHigh for (int iconCounter = 0; iconCounter < appsCount; iconCounter++) { Application app = apps.ElementAt(iconCounter); - Rectangle iconBounds = new Rectangle((int)(bounds.Left + (bounds.Width / 2) - (iconSize * ((appsCount / 2) - iconCounter))), (int)(bounds.Top + (bounds.Height / 2) - (iconSize / 2)), (int)iconSize, (int)iconSize); + Rectangle iconBounds = new((int)(bounds.Left + (bounds.Width / 2) - (iconSize * ((appsCount / 2) - iconCounter))), (int)(bounds.Top + (bounds.Height / 2) - (iconSize / 2)), (int)iconSize, (int)iconSize); try { @@ -264,13 +263,13 @@ public static void DrawWindow(Graphics graphics, Brush brush, Brush brushForHigh { string indexString = app.RepeatIndex.ToString(CultureInfo.InvariantCulture); int indexSize = (int)(iconBounds.Width * 0.5); - Rectangle indexBounds = new Rectangle(iconBounds.Right - indexSize, iconBounds.Bottom - indexSize, indexSize, indexSize); + Rectangle indexBounds = new(iconBounds.Right - indexSize, iconBounds.Bottom - indexSize, indexSize, indexSize); - var textSize = graphics.MeasureString(indexString, font); - var state = graphics.Save(); + SizeF textSize = graphics.MeasureString(indexString, Font); + GraphicsState state = graphics.Save(); graphics.TranslateTransform(indexBounds.Left, indexBounds.Top); graphics.ScaleTransform(indexBounds.Width / textSize.Width, indexBounds.Height / textSize.Height); - graphics.DrawString(indexString, font, Brushes.Black, PointF.Empty); + graphics.DrawString(indexString, Font, Brushes.Black, PointF.Empty); graphics.Restore(state); } } @@ -289,14 +288,14 @@ public static BitmapImage DrawPreviewIcons(Project project) return null; } - Bitmap previewBitmap = new Bitmap(32 * appsCount, 24); + Bitmap previewBitmap = new(32 * appsCount, 24); using (Graphics graphics = Graphics.FromImage(previewBitmap)) { graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; int appIndex = 0; - foreach (var app in project.Applications) + foreach (Application app in project.Applications) { try { @@ -311,20 +310,18 @@ public static BitmapImage DrawPreviewIcons(Project project) } } - using (var memory = new MemoryStream()) - { - previewBitmap.Save(memory, ImageFormat.Png); - memory.Position = 0; + using MemoryStream memory = new(); + previewBitmap.Save(memory, ImageFormat.Png); + memory.Position = 0; - var bitmapImage = new BitmapImage(); - bitmapImage.BeginInit(); - bitmapImage.StreamSource = memory; - bitmapImage.CacheOption = BitmapCacheOption.OnLoad; - bitmapImage.EndInit(); - bitmapImage.Freeze(); + BitmapImage bitmapImage = new(); + bitmapImage.BeginInit(); + bitmapImage.StreamSource = memory; + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; + bitmapImage.EndInit(); + bitmapImage.Freeze(); - return bitmapImage; - } + return bitmapImage; } private static GraphicsPath RoundedRect(Rectangle bounds) @@ -333,9 +330,9 @@ private static GraphicsPath RoundedRect(Rectangle bounds) int radius = (int)(minorSize / 8); int diameter = radius * 2; - Size size = new Size(diameter, diameter); - Rectangle arc = new Rectangle(bounds.Location, size); - GraphicsPath path = new GraphicsPath(); + Size size = new(diameter, diameter); + Rectangle arc = new(bounds.Location, size); + GraphicsPath path = new(); if (radius == 0) { diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs index 075067cb142b..fe697775934d 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/IOUtils.cs @@ -26,18 +26,16 @@ public string ReadFile(string fileName) { if (_fileSystem.File.Exists(fileName)) { - var attempts = 0; + int attempts = 0; while (attempts < 10) { try { - using (FileSystemStream inputStream = _fileSystem.File.Open(fileName, FileMode.Open)) - using (StreamReader reader = new StreamReader(inputStream)) - { - string data = reader.ReadToEnd(); - inputStream.Close(); - return data; - } + using FileSystemStream inputStream = _fileSystem.File.Open(fileName, FileMode.Open); + using StreamReader reader = new(inputStream); + string data = reader.ReadToEnd(); + inputStream.Close(); + return data; } catch (Exception) { diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/MonitorHelper.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/MonitorHelper.cs index d2f2c0b0bf32..450afb5070ef 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/MonitorHelper.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/MonitorHelper.cs @@ -26,7 +26,7 @@ private void SaveDpiUnawareScreens() private Screen[] GetDpiUnawareScreenBounds() { - Thread dpiUnawareThread = new Thread(new ThreadStart(SaveDpiUnawareScreens)); + Thread dpiUnawareThread = new(new ThreadStart(SaveDpiUnawareScreens)); dpiUnawareThread.Start(); dpiUnawareThread.Join(); @@ -35,15 +35,15 @@ private Screen[] GetDpiUnawareScreenBounds() public static Screen[] GetDpiUnawareScreens() { - MonitorHelper monitorHelper = new MonitorHelper(); + MonitorHelper monitorHelper = new(); return monitorHelper.GetDpiUnawareScreenBounds(); } internal static double GetScreenDpiFromScreen(Screen screen) { - var point = new System.Drawing.Point(screen.Bounds.Left + 1, screen.Bounds.Top + 1); - var monitor = NativeMethods.MonitorFromPoint(point, NativeMethods._MONITOR_DEFAULTTONEAREST); - NativeMethods.GetDpiForMonitor(monitor, NativeMethods.DpiType.EFFECTIVE, out uint dpiX, out uint dpiY); + System.Drawing.Point point = new(screen.Bounds.Left + 1, screen.Bounds.Top + 1); + nint monitor = NativeMethods.MonitorFromPoint(point, NativeMethods._MONITOR_DEFAULTTONEAREST); + _ = NativeMethods.GetDpiForMonitor(monitor, NativeMethods.DpiType.EFFECTIVE, out uint dpiX, out _); return dpiX / 96.0; } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/ParsingResult.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/ParsingResult.cs index 59389f27d612..57f3bbf0003c 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/ParsingResult.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/ParsingResult.cs @@ -4,19 +4,12 @@ namespace WorkspacesEditor.Utils { - public struct ParsingResult + public readonly struct ParsingResult(bool result, string message = "", string data = "") { - public bool Result { get; } + public bool Result { get; } = result; - public string Message { get; } + public string Message { get; } = message; - public string MalformedData { get; } - - public ParsingResult(bool result, string message = "", string data = "") - { - Result = result; - Message = message; - MalformedData = data; - } + public string MalformedData { get; } = data; } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/Settings.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/Settings.cs index 48cbafb0aac7..29dd65d56f77 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/Settings.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/Settings.cs @@ -9,13 +9,13 @@ namespace WorkspacesEditor.Utils public class Settings { private const string WorkspacesModuleName = "Workspaces"; - private static SettingsUtils _settingsUtils = new SettingsUtils(); + private static readonly SettingsUtils _settingsUtils = new(); public static WorkspacesSettings ReadSettings() { if (!_settingsUtils.SettingsExists(WorkspacesModuleName)) { - var defaultWorkspacesSettings = new WorkspacesSettings(); + WorkspacesSettings defaultWorkspacesSettings = new(); defaultWorkspacesSettings.Save(_settingsUtils); return defaultWorkspacesSettings; } diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/StringUtils.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/StringUtils.cs index c01b10252c05..3d2d146de2f1 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/StringUtils.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/StringUtils.cs @@ -11,12 +11,9 @@ public static class StringUtils public static string UpperCamelCaseToDashCase(this string str) { // If it's single letter variable, leave it as it is - if (str.Length == 1) - { - return str; - } - - return string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "-" + x.ToString() : x.ToString())).ToLowerInvariant(); + return str.Length == 1 + ? str + : string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "-" + x.ToString() : x.ToString())).ToLowerInvariant(); } } } diff --git a/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs b/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs index 7ad416cf17db..c467259521f5 100644 --- a/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs +++ b/src/modules/Workspaces/WorkspacesEditor/Utils/WorkspacesEditorIO.cs @@ -24,7 +24,7 @@ public ParsingResult ParseWorkspaces(MainViewModel mainViewModel) { try { - WorkspacesData parser = new WorkspacesData(); + WorkspacesData parser = new(); if (!File.Exists(parser.File)) { Logger.LogWarning($"Workspaces storage file not found: {parser.File}"); @@ -56,14 +56,14 @@ public Project ParseTempProject() { try { - ProjectData parser = new ProjectData(); + ProjectData parser = new(); if (!File.Exists(TempProjectData.File)) { Logger.LogWarning($"ParseProject method. Workspaces storage file not found: {TempProjectData.File}"); return null; } - Project project = new Project(parser.Read(TempProjectData.File)); + Project project = new(parser.Read(TempProjectData.File)); return project; } catch (Exception e) @@ -75,13 +75,13 @@ public Project ParseTempProject() public void SerializeWorkspaces(List workspaces, bool useTempFile = false) { - WorkspacesData serializer = new WorkspacesData(); - WorkspacesData.WorkspacesListWrapper workspacesWrapper = new WorkspacesData.WorkspacesListWrapper { }; - workspacesWrapper.Workspaces = new List(); + WorkspacesData serializer = new(); + WorkspacesData.WorkspacesListWrapper workspacesWrapper = new() { }; + workspacesWrapper.Workspaces = []; foreach (Project project in workspaces) { - ProjectData.ProjectWrapper wrapper = new ProjectData.ProjectWrapper + ProjectData.ProjectWrapper wrapper = new() { Id = project.Id, Name = project.Name, @@ -89,11 +89,11 @@ public void SerializeWorkspaces(List workspaces, bool useTempFile = fal IsShortcutNeeded = project.IsShortcutNeeded, MoveExistingWindows = project.MoveExistingWindows, LastLaunchedTime = project.LastLaunchedTime, - Applications = new List { }, - MonitorConfiguration = new List { }, + Applications = [], + MonitorConfiguration = [], }; - foreach (var app in project.Applications.Where(x => x.IsIncluded)) + foreach (Application app in project.Applications.Where(x => x.IsIncluded)) { wrapper.Applications.Add(new ProjectData.ApplicationWrapper { @@ -120,7 +120,7 @@ public void SerializeWorkspaces(List workspaces, bool useTempFile = fal }); } - foreach (var monitor in project.Monitors) + foreach (MonitorSetup monitor in project.Monitors) { wrapper.MonitorConfiguration.Add(new ProjectData.MonitorConfigurationWrapper { @@ -150,7 +150,7 @@ public void SerializeWorkspaces(List workspaces, bool useTempFile = fal try { - IOUtils ioUtils = new IOUtils(); + IOUtils ioUtils = new(); ioUtils.WriteFile(useTempFile ? TempProjectData.File : serializer.File, serializer.Serialize(workspacesWrapper)); } catch (Exception e) @@ -162,7 +162,7 @@ public void SerializeWorkspaces(List workspaces, bool useTempFile = fal private bool AddWorkspaces(MainViewModel mainViewModel, WorkspacesData.WorkspacesListWrapper workspaces) { - foreach (var project in workspaces.Workspaces) + foreach (ProjectData.ProjectWrapper project in workspaces.Workspaces) { mainViewModel.Workspaces.Add(new Project(project)); } @@ -173,13 +173,13 @@ private bool AddWorkspaces(MainViewModel mainViewModel, WorkspacesData.Workspace private bool SetWorkspaces(MainViewModel mainViewModel, WorkspacesData.WorkspacesListWrapper workspaces) { - mainViewModel.Workspaces = new System.Collections.ObjectModel.ObservableCollection { }; + mainViewModel.Workspaces = []; return AddWorkspaces(mainViewModel, workspaces); } internal void SerializeTempProject(Project project) { - SerializeWorkspaces(new List() { project }, true); + SerializeWorkspaces([project], true); } } } diff --git a/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml b/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml index e176ff432b66..6bc7bfe63798 100644 --- a/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml +++ b/src/modules/Workspaces/WorkspacesEditor/WorkspacesEditorPage.xaml @@ -156,24 +156,33 @@ Content="{x:Static props:Resources.LaunchAsAdmin}" IsChecked="{Binding IsElevated, Mode=TwoWay}" IsEnabled="{Binding CanLaunchElevated, Mode=OneWay}" /> - - + + + + + + - -