Skip to content

Commit

Permalink
added shortcut guide settings (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lavius Motileng committed Apr 20, 2020
1 parent 0417b62 commit cae77ae
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 49 deletions.
29 changes: 29 additions & 0 deletions src/core/Microsoft.PowerToys.Settings.UI.Lib/DoubleProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.PowerToys.Settings.UI.Lib
{

// Represents the configuration property of the settings that store Double type.
public class DoubleProperty
{
public DoubleProperty()
{
this.Value = 0.0;
}

// Gets or sets the double value of the settings configuration.
[JsonPropertyName("value")]
public double Value { get; set; }

// Returns a JSON version of the class settings configuration class.
public override string ToString()
{
return JsonSerializer.Serialize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.PowerToys.Settings.UI.Lib
Expand All @@ -14,7 +15,7 @@ public EnabledModules()
this.ImageResizer = false;
this.FileExplorerPreview = false;
this.PowerRename = false;
this.ShortcutGuide = true;
this.ShortcutGuide = false;
}

[JsonPropertyName("FancyZones")]
Expand All @@ -30,5 +31,10 @@ public EnabledModules()
public bool ShortcutGuide { get; set; }

public bool PowerRename { get; set; }

public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}
5 changes: 3 additions & 2 deletions src/core/Microsoft.PowerToys.Settings.UI.Lib/SettingsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
public static class SettingsUtils
{
private const string DefaultFileName = "settings.json";
private const string DefaultModuleName = "";

public static bool SettingsFolderExists(string powertoy)
{
Expand Down Expand Up @@ -49,14 +50,14 @@ public static bool SettingsExists(string powertoy, string fileName = DefaultFile
/// Get a Deserialized object of the json settings string.
/// </summary>
/// <returns>Deserialized json settings object.</returns>
public static T GetSettings<T>(string powertoy, string fileName = DefaultFileName)
public static T GetSettings<T>(string powertoy = DefaultModuleName, string fileName = DefaultFileName)
{
var jsonSettingsString = File.ReadAllText(GetSettingsPath(powertoy, fileName));
return JsonSerializer.Deserialize<T>(jsonSettingsString);
}

// Save settings to a json file.
public static void SaveSettings(string jsonSettings, string powertoy, string fileName = DefaultFileName)
public static void SaveSettings(string jsonSettings, string powertoy = DefaultModuleName, string fileName = DefaultFileName)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Microsoft.PowerToys.Settings.UI.Lib
{

public class ShortcutGuideProperties
{
public ShortcutGuideProperties()
{
OverlayOpacity = new IntProperty();
PressTime = new IntProperty();
Theme = new StringProperty();
}

[JsonPropertyName("overlay_opacity")]
public IntProperty OverlayOpacity { get; set; }

[JsonPropertyName("press_time")]
public IntProperty PressTime { get; set; }

[JsonPropertyName("theme")]
public StringProperty Theme { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.PowerToys.Settings.UI.Lib
{
public class ShortcutGuideSettings
{
public ShortcutGuideSettings()
{
Name = "Shortcut Guide";
Properties = new ShortcutGuideProperties();
Version = "1.0";
}

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("properties")]
public ShortcutGuideProperties Properties { get; set; }

[JsonPropertyName("version")]
public string Version { get; set; }

public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SndModuleSettings<T>

public SndModuleSettings(T settings)
{
powertoys = settings;
this.powertoys = settings;
}

public string ToJsonString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Microsoft.PowerToys.Settings.UI.Lib
{

public class SndShortcutGuideSettings
{
[JsonPropertyName("Shortcut Guide")]
public ShortcutGuideSettings ShortcutGuide { get; set; }

public SndShortcutGuideSettings(ShortcutGuideSettings settings)
{
this.ShortcutGuide = settings;
}

public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<comment>Navigation view item name for PowerRename</comment>
</data>
<data name="Shell_ShortcutGuide.Content" xml:space="preserve">
<value>Shortcut Guide [Not Functional]</value>
<value>Shortcut Guide</value>
<comment>Navigation view item name for Shortcut Guide</comment>
</data>
<data name="Shell_PowerPreview.Content" xml:space="preserve">
Expand Down Expand Up @@ -410,4 +410,19 @@
<data name="FancyZones_SaveZoneInActiveColor.Content" xml:space="preserve">
<value>Save Zone Inactive Color Choice</value>
</data>
<data name="ShortcutGuide_Description.Text" xml:space="preserve">
<value>Shows a help overlay with Windows shortcuts when the Windows key is pressed.</value>
</data>
<data name="ShortcutGuide_PressTime.Header" xml:space="preserve">
<value>How long to press the Windows key before showing the Shortcut Guide</value>
</data>
<data name="ShortcutGuide_Appearance_Behaiviour.Text" xml:space="preserve">
<value>Appearance &amp; behaviour</value>
</data>
<data name="ShortcutGuide_Enable.Header" xml:space="preserve">
<value>Enable Shortcut Guide</value>
</data>
<data name="ShortcutGuide_OverlayOpacity.Header" xml:space="preserve">
<value>Opacity of the Shortcut Guide's overlay background (%)</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,172 @@
// 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.Runtime.CompilerServices;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Views;

namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
public class ShortcutGuideViewModel : Observable
{
private ShortcutGuideSettings Settings { get; set; }

private const string ModuleName = "Shortcut Guide";

public ShortcutGuideViewModel()
{
try
{
Settings = SettingsUtils.GetSettings<ShortcutGuideSettings>(ModuleName);
}
catch
{
Settings = new ShortcutGuideSettings();
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
}

GeneralSettings generalSettings;

try
{
generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
}
catch
{
generalSettings = new GeneralSettings();
SettingsUtils.SaveSettings(generalSettings.ToJsonString(), string.Empty);
}

this._isEnabled = generalSettings.Enabled.ShortcutGuide;
this._pressTime = Settings.Properties.PressTime.Value;
this._opacity = Settings.Properties.OverlayOpacity.Value;

string theme = Settings.Properties.Theme.Value;

if (theme == "dark")
{
_themeIndex = 0;
}

if (theme == "light")
{
_themeIndex = 1;
}

if (theme == "system")
{
_themeIndex = 2;
}
}

private bool _isEnabled = false;
private int _themeIndex = 0;
private int _pressTime = 0;
private int _opacity = 0;

public bool IsEnabled
{
get
{
return _isEnabled;
}

set
{
if (value != _isEnabled)
{
_isEnabled = value;
GeneralSettings generalSettings = SettingsUtils.GetSettings<GeneralSettings>(string.Empty);
generalSettings.Enabled.ShortcutGuide = value;
OutGoingGeneralSettings snd = new OutGoingGeneralSettings(generalSettings);

ShellPage.DefaultSndMSGCallback(snd.ToString());
RaisePropertyChanged();
}
}
}

public int ThemeIndex
{
get
{
return _themeIndex;
}

set
{
if (_themeIndex != value)
{
if (value == 0)
{
// set theme to dark.
Settings.Properties.Theme.Value = "dark";
_themeIndex = value;
RaisePropertyChanged();
}

if (value == 1)
{
// set theme to light.
Settings.Properties.Theme.Value = "light";
_themeIndex = value;
RaisePropertyChanged();
}

if (value == 2)
{
// set theme to system default.
Settings.Properties.Theme.Value = "system";
_themeIndex = value;
RaisePropertyChanged();
}
}
}
}

public int PressTime
{
get
{
return _pressTime;
}

set
{
if (_pressTime != value)
{
_pressTime = value;
Settings.Properties.PressTime.Value = value;
RaisePropertyChanged();
}
}
}

public int OverlayOpacity
{
get
{
return _opacity;
}

set
{
if (_opacity != value)
{
_opacity = value;
Settings.Properties.OverlayOpacity.Value = value;
RaisePropertyChanged();
}
}
}

public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
OnPropertyChanged(propertyName);
SndShortcutGuideSettings outsettings = new SndShortcutGuideSettings(Settings);
SndModuleSettings<SndShortcutGuideSettings> ipcMessage = new SndModuleSettings<SndShortcutGuideSettings>(outsettings);
ShellPage.DefaultSndMSGCallback(ipcMessage.ToJsonString());
}
}
}

0 comments on commit cae77ae

Please sign in to comment.