diff --git a/Petrroll.Helpers/ObservableCollectionWhereShim.cs b/Petrroll.Helpers/ObservableCollectionWhereShim.cs index 4ddc115..61d80e1 100644 --- a/Petrroll.Helpers/ObservableCollectionWhereShim.cs +++ b/Petrroll.Helpers/ObservableCollectionWhereShim.cs @@ -24,7 +24,7 @@ public static ObservableCollectionWhereSwitchableShim WhereObservableSwitc public class ObservableCollectionWhereSwitchableShim : ObservableCollectionWhereShim, IEnumerable where C : INotifyCollectionChanged, ICollection where T : INotifyPropertyChanged { - protected IEnumerable currentlySwichtedCollection => (FilterOn) ? filteredCollection : BaseCollection; + protected IEnumerable currentlySwitchedCollection => (FilterOn) ? filteredCollection : BaseCollection; #region Constructor public ObservableCollectionWhereSwitchableShim(C baseCollection, Func predicate, bool filterOn) : base(baseCollection, predicate) @@ -96,12 +96,12 @@ private void addFilteredElements() #endregion #region OtherMethods - public override int Count => currentlySwichtedCollection.Count(); - public override bool Contains(T item) => currentlySwichtedCollection.Contains(item); - public override void CopyTo(T[] array, int arrayIndex) => currentlySwichtedCollection.ToList().CopyTo(array, arrayIndex); + public override int Count => currentlySwitchedCollection.Count(); + public override bool Contains(T item) => currentlySwitchedCollection.Contains(item); + public override void CopyTo(T[] array, int arrayIndex) => currentlySwitchedCollection.ToList().CopyTo(array, arrayIndex); - public override IEnumerator GetEnumerator() => currentlySwichtedCollection.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => currentlySwichtedCollection.GetEnumerator(); + public override IEnumerator GetEnumerator() => currentlySwitchedCollection.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => currentlySwitchedCollection.GetEnumerator(); #endregion } @@ -147,7 +147,7 @@ protected virtual void Sch_PropertyChanged(object sender, PropertyChangedEventAr else if (oldNumberOfItems > numberOfFilteredItems) { #if DEBUG - Console.WriteLine($"DC:Added:{sender}"); + Console.WriteLine($"DC:Added:{sender}"); #endif var changeIndex = BaseCollection.Take(BaseCollection.IndexOf(sender)).Count(Predicate); RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, sender, changeIndex)); diff --git a/PowerSwitcher.TrayApp/App.xaml.cs b/PowerSwitcher.TrayApp/App.xaml.cs index 31f92c4..13ef53e 100644 --- a/PowerSwitcher.TrayApp/App.xaml.cs +++ b/PowerSwitcher.TrayApp/App.xaml.cs @@ -28,7 +28,7 @@ private void Application_Startup(object sender, StartupEventArgs e) if (!tryToCreateMutex()) return; var configurationManager = new ConfigurationManagerXML(Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Petrroll", "PowerSwitcher", "PowerSwitcherSettings.xml" )); @@ -44,6 +44,7 @@ private void Application_Startup(object sender, StartupEventArgs e) Configuration.Data.PropertyChanged += Configuration_PropertyChanged; if (Configuration.Data.ShowOnShortcutSwitch) { registerHotkeyFromConfiguration(); } + if (Configuration.Data.CycleNextSchemaSwitch) { registerHotkeyFromConfiguration(true); } TrayApp.CreateAltMenu(); } @@ -66,24 +67,47 @@ private void Configuration_PropertyChanged(object sender, System.ComponentModel. if (Configuration.Data.ShowOnShortcutSwitch) { registerHotkeyFromConfiguration(); } else { unregisterHotkeyFromConfiguration(); } } + + if(e.PropertyName == nameof(PowerSwitcherSettings.CycleNextSchemaSwitch)) + { + if (Configuration.Data.CycleNextSchemaSwitch) { registerHotkeyFromConfiguration(true); } + else { unregisterHotkeyFromConfiguration(true); } + } + } - private void unregisterHotkeyFromConfiguration() + private void unregisterHotkeyFromConfiguration(bool isCycleNextSchemaSwitch = false) { - HotKeyManager.Unregister(new HotKey(Configuration.Data.ShowOnShortcutKey, Configuration.Data.ShowOnShortcutKeyModifier)); + if(isCycleNextSchemaSwitch) + HotKeyManager.Unregister(new HotKey(Configuration.Data.CycleNextSchemaKey, Configuration.Data.CycleNextSchemaKeyModifier)); + else + HotKeyManager.Unregister(new HotKey(Configuration.Data.ShowOnShortcutKey, Configuration.Data.ShowOnShortcutKeyModifier)); } - private bool registerHotkeyFromConfiguration() + private bool registerHotkeyFromConfiguration(bool isCycleNextSchemaSwitch = false) { - var newHotKey = new HotKey(Configuration.Data.ShowOnShortcutKey, Configuration.Data.ShowOnShortcutKeyModifier); + HotKey newHotKey = null; + if(isCycleNextSchemaSwitch) + newHotKey = new HotKey(Configuration.Data.CycleNextSchemaKey, Configuration.Data.CycleNextSchemaKeyModifier); + else + newHotKey = new HotKey(Configuration.Data.ShowOnShortcutKey, Configuration.Data.ShowOnShortcutKeyModifier); bool success = HotKeyManager.Register(newHotKey); if(!success) { HotKeyFailed = true; return false; } - newHotKey.HotKeyFired += (this.MainWindow as MainWindow).ToggleWindowVisibility; + + if (isCycleNextSchemaSwitch) + newHotKey.HotKeyFired += CycleNextPowerSchema; + else + newHotKey.HotKeyFired += (this.MainWindow as MainWindow).ToggleWindowVisibility; return true; } + private void CycleNextPowerSchema() + { + PowerManager.CycleNextPowerSchema(); + } + private bool tryToCreateMutex() { var assembly = Assembly.GetExecutingAssembly(); diff --git a/PowerSwitcher.TrayApp/Configuration/PowerSwitcherConfiguration.cs b/PowerSwitcher.TrayApp/Configuration/PowerSwitcherConfiguration.cs index ce3ca81..c27fd67 100644 --- a/PowerSwitcher.TrayApp/Configuration/PowerSwitcherConfiguration.cs +++ b/PowerSwitcher.TrayApp/Configuration/PowerSwitcherConfiguration.cs @@ -1,6 +1,7 @@ using Petrroll.Helpers; using PowerSwitcher.TrayApp.Services; using System; +using System.Windows.Forms; using System.Windows.Input; namespace PowerSwitcher.TrayApp.Configuration @@ -18,12 +19,15 @@ public class PowerSwitcherSettings : ObservableObject //TODO: Fix so that it can be changed during runtime public Key ShowOnShortcutKey { get; set; } = Key.L; public KeyModifier ShowOnShortcutKeyModifier { get; set; } = KeyModifier.Shift | KeyModifier.Win; - bool showOnShortcutSwitch = false; public bool ShowOnShortcutSwitch { get { return showOnShortcutSwitch; } set { showOnShortcutSwitch = value; RaisePropertyChangedEvent(nameof(ShowOnShortcutSwitch)); } } + public Key CycleNextSchemaKey { get; set; } = Key.N; + public KeyModifier CycleNextSchemaKeyModifier { get; set; } = KeyModifier.Shift | KeyModifier.Win; + bool cycleNextSchemaSwitch = false; + public bool CycleNextSchemaSwitch { get { return cycleNextSchemaSwitch; } set { cycleNextSchemaSwitch = value; RaisePropertyChangedEvent(nameof(CycleNextSchemaSwitch)); } } + bool showOnlyDefaultSchemas = false; public bool ShowOnlyDefaultSchemas { get { return showOnlyDefaultSchemas; } set { showOnlyDefaultSchemas = value; RaisePropertyChangedEvent(nameof(ShowOnlyDefaultSchemas)); } } - } } diff --git a/PowerSwitcher.TrayApp/MainWindow.xaml.cs b/PowerSwitcher.TrayApp/MainWindow.xaml.cs index f6d43da..92c4cc4 100644 --- a/PowerSwitcher.TrayApp/MainWindow.xaml.cs +++ b/PowerSwitcher.TrayApp/MainWindow.xaml.cs @@ -1,19 +1,10 @@ using PowerSwitcher.TrayApp.Extensions; using PowerSwitcher.TrayApp.Services; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; + namespace PowerSwitcher.TrayApp { diff --git a/PowerSwitcher.TrayApp/Resources/AppStrings.Designer.cs b/PowerSwitcher.TrayApp/Resources/AppStrings.Designer.cs index a02314a..c1be006 100644 --- a/PowerSwitcher.TrayApp/Resources/AppStrings.Designer.cs +++ b/PowerSwitcher.TrayApp/Resources/AppStrings.Designer.cs @@ -19,7 +19,7 @@ namespace PowerSwitcher.TrayApp.Resources { // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class AppStrings { @@ -168,12 +168,21 @@ internal static string ShowOnlyDefaultSchemas { } } + /// + /// Looks up a localized string similar to Enable shortcut for cycling next schema. + /// + internal static string ToggleCycleNextSchemaSwitch { + get { + return ResourceManager.GetString("ToggleCycleNextSchemaSwitch", resourceCulture); + } + } + /// /// Looks up a localized string similar to Enable shortcut for flyout toggle. /// - internal static string ToggleOnShowrtcutSwitch { + internal static string ToggleOnShortcutSwitch { get { - return ResourceManager.GetString("ToggleOnShowrtcutSwitch", resourceCulture); + return ResourceManager.GetString("ToggleOnShortcutSwitch", resourceCulture); } } } diff --git a/PowerSwitcher.TrayApp/Resources/AppStrings.resx b/PowerSwitcher.TrayApp/Resources/AppStrings.resx index 3e25f52..5845c73 100644 --- a/PowerSwitcher.TrayApp/Resources/AppStrings.resx +++ b/PowerSwitcher.TrayApp/Resources/AppStrings.resx @@ -1,17 +1,17 @@  - @@ -153,7 +153,10 @@ Show only default power schemas - + Enable shortcut for flyout toggle + + Enable shortcut for cycling next schema + \ No newline at end of file diff --git a/PowerSwitcher.TrayApp/Services/GlobalShortcutService.cs b/PowerSwitcher.TrayApp/Services/GlobalShortcutService.cs index 74e99a7..ab8f268 100644 --- a/PowerSwitcher.TrayApp/Services/GlobalShortcutService.cs +++ b/PowerSwitcher.TrayApp/Services/GlobalShortcutService.cs @@ -20,7 +20,7 @@ public class HotKey public event Action HotKeyFired; public int VirtualKeyCode => KeyInterop.VirtualKeyFromKey(Key); - public int Id => VirtualKeyCode + ((int)KeyModifiers* 0x10000); + public int Id => VirtualKeyCode + ((int)KeyModifiers* 0x10000); public HotKey(Key k, KeyModifier keyModifiers) { @@ -72,10 +72,10 @@ public void Unregister(HotKey hotkey) { if (!_dictHotKeyToCalBackProc.ContainsKey(hotkey.Id)) { throw new InvalidOperationException($"Trying to unregister not-registred Hotkey {hotkey.Id}"); } - var success = UnregisterHotKey(IntPtr.Zero, hotkey.Id); + var success = UnregisterHotKey(IntPtr.Zero, hotkey.Id); if (!success) { throw new PowerSwitcherWrappersException($"UnregisterHotKey() failed|{Marshal.GetLastWin32Error()}"); } - _dictHotKeyToCalBackProc.Remove(hotkey.Id); + _dictHotKeyToCalBackProc.Remove(hotkey.Id); } public const int WmHotKey = 0x0312; diff --git a/PowerSwitcher.TrayApp/TrayApp.cs b/PowerSwitcher.TrayApp/TrayApp.cs index 0bca37c..b165139 100644 --- a/PowerSwitcher.TrayApp/TrayApp.cs +++ b/PowerSwitcher.TrayApp/TrayApp.cs @@ -74,11 +74,16 @@ public void CreateAltMenu() onlyDefaultSchemasItem.Checked = configuration.Data.ShowOnlyDefaultSchemas; onlyDefaultSchemasItem.Click += OnlyDefaultSchemas_Click; - var enableShortcutsToggleItem = contextMenuSettings.MenuItems.Add($"{AppStrings.ToggleOnShowrtcutSwitch} ({configuration.Data.ShowOnShortcutKeyModifier} + {configuration.Data.ShowOnShortcutKey})"); + var enableShortcutsToggleItem = contextMenuSettings.MenuItems.Add($"{AppStrings.ToggleOnShortcutSwitch} ({configuration.Data.ShowOnShortcutKeyModifier} + {configuration.Data.ShowOnShortcutKey})"); enableShortcutsToggleItem.Enabled = !(Application.Current as App).HotKeyFailed; enableShortcutsToggleItem.Checked = configuration.Data.ShowOnShortcutSwitch; enableShortcutsToggleItem.Click += EnableShortcutsToggleItem_Click; + var enableCycleNextToggleItem = contextMenuSettings.MenuItems.Add($"{AppStrings.ToggleCycleNextSchemaSwitch} ({configuration.Data.CycleNextSchemaKeyModifier} + {configuration.Data.CycleNextSchemaKey})"); + enableCycleNextToggleItem.Enabled = !(Application.Current as App).HotKeyFailed; + enableCycleNextToggleItem.Checked = configuration.Data.CycleNextSchemaSwitch; + enableCycleNextToggleItem.Click += EnableCycleNextToggleItem_Click; + var aboutItem = contextMenuRootItems.Add($"{AppStrings.About} ({Assembly.GetEntryAssembly().GetName().Version})"); aboutItem.Click += About_Click; @@ -111,6 +116,18 @@ private void EnableShortcutsToggleItem_Click(object sender, EventArgs e) configuration.Save(); } + private void EnableCycleNextToggleItem_Click(object sender, EventArgs e) + { + WF.MenuItem enableCycleNextToggleItem = (WF.MenuItem)sender; + + configuration.Data.CycleNextSchemaSwitch = !configuration.Data.CycleNextSchemaSwitch; + enableCycleNextToggleItem.Checked = configuration.Data.CycleNextSchemaSwitch; + enableCycleNextToggleItem.Enabled = !(Application.Current as App).HotKeyFailed; + + configuration.Save(); + } + + private void AutomaticHideItem_Click(object sender, EventArgs e) { WF.MenuItem automaticHideItem = (WF.MenuItem)sender; diff --git a/PowerSwitcher/PowerManager.cs b/PowerSwitcher/PowerManager.cs index 9328752..3ca310b 100644 --- a/PowerSwitcher/PowerManager.cs +++ b/PowerSwitcher/PowerManager.cs @@ -20,6 +20,8 @@ public interface IPowerManager : INotifyPropertyChanged, IDisposable void SetPowerSchema(IPowerSchema schema); void SetPowerSchema(Guid guid); + + void CycleNextPowerSchema(); } public class PowerManager : ObservableObject, IPowerManager @@ -53,10 +55,10 @@ public void UpdateSchemas() { var originalSchema = Schemas.FirstOrDefault(sch => sch.Guid == newSchema.Guid); if (originalSchema == null) { insertNewSchema(newSchemas, newSchema); originalSchema = newSchema; } - + if (newSchema.Guid == currSchemaGuid && originalSchema?.IsActive != true) { setNewCurrSchema(originalSchema); } - + if (originalSchema?.Name != newSchema.Name) { ((PowerSchema)originalSchema).Name = newSchema.Name; } } @@ -125,8 +127,20 @@ private void powerChangedEvent(PowerPlugStatus newStatus) RaisePropertyChangedEvent(nameof(CurrentPowerStatus)); } + public void CycleNextPowerSchema() + { + var nextSchemaIndex = (Schemas.IndexOf(CurrentSchema) + 1); + if ((nextSchemaIndex + 1) > Schemas.Count) + nextSchemaIndex = 0; + + var nextPowerSchema = Schemas[nextSchemaIndex]; + + SetPowerSchema(nextPowerSchema); + } + + #region IDisposable Support - private bool disposedValue = false; + private bool disposedValue = false; protected virtual void Dispose(bool disposing) { @@ -143,8 +157,8 @@ public void Dispose() { Dispose(true); - //No destructor so isn't required (yet) - // GC.SuppressFinalize(this); + //No destructor so isn't required (yet) + // GC.SuppressFinalize(this); } #endregion