Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Widget customization #1819

Merged
merged 8 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CoreWidgetProvider/Widgets/CoreWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public override void OnWidgetContextChanged(WidgetContextChangedArgs contextChan

public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs) => throw new NotImplementedException();

public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs) => throw new NotImplementedException();

protected WidgetAction GetWidgetActionForVerb(string verb)
{
try
Expand Down
5 changes: 5 additions & 0 deletions CoreWidgetProvider/Widgets/Enums/WidgetAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

namespace CoreWidgetProvider.Widgets.Enums;

public enum WidgetAction
{
/// <summary>
Expand Down Expand Up @@ -43,4 +44,8 @@ public enum WidgetAction
/// Kill process #3.
/// </summary>
CpuKill3,

Save,

Cancel,
}
59 changes: 41 additions & 18 deletions CoreWidgetProvider/Widgets/SSHWalletWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ protected string ConfigFile
set => SetState(value);
}

private string _savedContentData = string.Empty;
private string _savedConfigFile = string.Empty;

public SSHWalletWidget()
{
}
Expand Down Expand Up @@ -114,24 +117,39 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
case WidgetAction.Unknown:
Log.Logger()?.ReportError(Name, ShortId, $"Unknown verb: {actionInvokedArgs.Verb}");
break;

case WidgetAction.Save:
_savedContentData = string.Empty;
_savedConfigFile = string.Empty;
ContentData = EmptyJson;
SetActive();
break;

case WidgetAction.Cancel:
ConfigFile = _savedConfigFile;
ContentData = _savedContentData;
SetActive();
break;
}
}

private void HandleConnect(WidgetActionInvokedArgs args)
public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
var data = args.Data;

Process cmd = new Process();
_savedContentData = ContentData;
_savedConfigFile = ConfigFile;
SetConfigure();
}

var info = new ProcessStartInfo
private void HandleConnect(WidgetActionInvokedArgs args)
{
var cmd = new Process();
cmd.StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/k \"ssh {data}\"",
Arguments = $"/k \"ssh {args.Data}\"",
UseShellExecute = true,
};

cmd.StartInfo = info;

cmd.Start();
}

Expand Down Expand Up @@ -161,8 +179,10 @@ private void HandleCheckPath(WidgetActionInvokedArgs args)

private MatchCollection? GetHostEntries()
{
FileStreamOptions options = new FileStreamOptions();
options.Access = FileAccess.Read;
var options = new FileStreamOptions()
{
Access = FileAccess.Read,
};

using var reader = new StreamReader(ConfigFile, options);

Expand All @@ -179,12 +199,7 @@ private void HandleCheckPath(WidgetActionInvokedArgs args)
private int GetNumberOfHostEntries()
{
var hostEntries = GetHostEntries();
if (hostEntries == null)
{
return 0;
}

return hostEntries.Count;
return (hostEntries != null) ? hostEntries.Count : 0;
}

private void SetupFileWatcher()
Expand Down Expand Up @@ -235,17 +250,25 @@ private JsonObject FillConfigurationData(bool hasConfiguration, string configFil
{
var configurationData = new JsonObject();

var currentOrDefaultConfigFile = string.IsNullOrEmpty(configFile) ? DefaultConfigFile : configFile;
// Determine what config file to suggest in configuration form.
// 1. If there is a currently selected configFile, show that.
// 2. Else, check if there is a _savedConfigFile. If so, the user
// is in the customize flow and we should show the _savedConfigFile.
// 3. Else, show the DefaultConfigFile.
var suggestedConfigFile = string.IsNullOrEmpty(configFile) ? _savedConfigFile : configFile;
suggestedConfigFile = string.IsNullOrEmpty(suggestedConfigFile) ? DefaultConfigFile : suggestedConfigFile;

var sshConfigData = new JsonObject
{
{ "configFile", configFile },
{ "currentOrDefaultConfigFile", currentOrDefaultConfigFile },
{ "currentOrDefaultConfigFile", suggestedConfigFile },
{ "numOfEntries", numOfEntries.ToString(CultureInfo.InvariantCulture) },
};

configurationData.Add("configuring", configuring);
configurationData.Add("hasConfiguration", hasConfiguration);
configurationData.Add("configuration", sshConfigData);
configurationData.Add("savedConfigFile", _savedConfigFile);
configurationData.Add("submitIcon", IconLoader.GetIconAsBase64("arrow.png"));

if (!string.IsNullOrEmpty(errorMessage))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,50 @@
"$data": "${$root.configuration}",
"$when": "${$root.hasConfiguration}",
"bleed": true
},
{
"type": "ColumnSet",
"spacing": "ExtraLarge",
"$when": "${$root.savedConfigFile != \"\"}",
"columns": [
{
"type": "Column",
"width": "stretch"
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Container",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.Execute",
"title": "Save",
"verb": "Save",
"tooltip": "Save",
"isEnabled": "${$root.hasConfiguration}"
},
{
"type": "Action.Execute",
"title": "Cancel",
"verb": "Cancel",
"tooltip": "Cancel"
}
]
}
]
}
]
},
{
"type": "Column",
"width": "stretch"
}
]
}
]
}
2 changes: 2 additions & 0 deletions CoreWidgetProvider/Widgets/WidgetImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ public void SetState(string state)

public abstract void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs);

public abstract void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs);

public abstract void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs);
}
13 changes: 12 additions & 1 deletion CoreWidgetProvider/Widgets/WidgetProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CoreWidgetProvider.Widgets;
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("F8B2DBB9-3687-4C6E-99B2-B92C82905937")]
internal class WidgetProvider : IWidgetProvider
internal class WidgetProvider : IWidgetProvider, IWidgetProvider2
{
private readonly Dictionary<string, IWidgetImplFactory> widgetDefinitionRegistry = new ();
private readonly Dictionary<string, WidgetImpl> runningWidgets = new ();
Expand Down Expand Up @@ -140,6 +140,17 @@ public void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
}
}

public void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
Log.Logger()?.ReportDebug($"OnCustomizationRequested id: {customizationRequestedArgs.WidgetContext.Id} definitionId: {customizationRequestedArgs.WidgetContext.DefinitionId}");
var widgetContext = customizationRequestedArgs.WidgetContext;
var widgetId = widgetContext.Id;
if (runningWidgets.ContainsKey(widgetId))
{
runningWidgets[widgetId].OnCustomizationRequested(customizationRequestedArgs);
}
}

public void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs)
{
Log.Logger()?.ReportDebug($"OnWidgetContextChanged id: {contextChangedArgs.WidgetContext.Id} definitionId: {contextChangedArgs.WidgetContext.DefinitionId}");
Expand Down
2 changes: 1 addition & 1 deletion common/DevHome.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.DevHome.SDK" Version="0.100.254" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.3.230724000" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231008000" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.Internal.Windows.DevHome.Helpers" Version="1.0.20230706-x2201" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.3" />
Expand Down
19 changes: 14 additions & 5 deletions src/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:genTemplate="http://schemas.microsoft.com/appx/developer/templatestudio" xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" xmlns:desktop6="http://schemas.microsoft.com/appx/manifest/desktop/windows10/6" IgnorableNamespaces="uap uap3 rescap genTemplate desktop6">
<Extensions>
<Extension Category="windows.activatableClass.proxyStub">
<ProxyStub ClassId="00000355-0000-0000-C000-000000000046">
<Path>Microsoft.Windows.Widgets.winmd</Path>
<Interface Name="Microsoft.Windows.Widgets.Providers.IWidgetProvider" InterfaceId="5C5774CC-72A0-452D-B9ED-075C0DD25EED" />
<Interface Name="Microsoft.Windows.Widgets.Providers.IWidgetProvider2" InterfaceId="38C3A963-DD93-479D-9276-04BF84EE1816" />
</ProxyStub>
</Extension>
</Extensions>
<Identity Name="Microsoft.Windows.DevHome.Dev" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" Version="0.0.0.0" />
<Properties>
<DisplayName>Dev Home (Dev)</DisplayName>
Expand Down Expand Up @@ -76,7 +85,7 @@
<TrustedPackageFamilyName>Microsoft.MicrosoftEdge.Stable_8wekyb3d8bbwe</TrustedPackageFamilyName>
</TrustedPackageFamilyNames>
<Definitions>
<Definition Id="SSH_Wallet" DisplayName="SSH keychain" Description="Quickly connect into to your favorite servers from your SSH config file" AllowMultiple="true">
<Definition Id="SSH_Wallet" DisplayName="SSH keychain" Description="Quickly connect into to your favorite servers from your SSH config file" AllowMultiple="true" IsCustomizable="true">
<Capabilities>
<Capability>
<Size Name="small" />
Expand All @@ -103,7 +112,7 @@
<LightMode />
</ThemeResources>
</Definition>
<Definition Id="System_Memory" DisplayName="Memory" Description="Microsoft System Memory Widget Description" AllowMultiple="true">
<Definition Id="System_Memory" DisplayName="Memory" Description="Microsoft System Memory Widget Description" AllowMultiple="true" IsCustomizable="false">
<Capabilities>
<Capability>
<Size Name="small" />
Expand All @@ -130,7 +139,7 @@
<LightMode />
</ThemeResources>
</Definition>
<Definition Id="System_NetworkUsage" DisplayName="Network" Description="Microsoft System Network Usage Widget Description" AllowMultiple="true">
<Definition Id="System_NetworkUsage" DisplayName="Network" Description="Microsoft System Network Usage Widget Description" AllowMultiple="true" IsCustomizable="false">
<Capabilities>
<Capability>
<Size Name="small" />
Expand All @@ -157,7 +166,7 @@
<LightMode />
</ThemeResources>
</Definition>
<Definition Id="System_GPUUsage" DisplayName="GPU" Description="Microsoft System GPU Usage Widget Description" AllowMultiple="true">
<Definition Id="System_GPUUsage" DisplayName="GPU" Description="Microsoft System GPU Usage Widget Description" AllowMultiple="true" IsCustomizable="false">
<Capabilities>
<Capability>
<Size Name="small" />
Expand All @@ -184,7 +193,7 @@
<LightMode />
</ThemeResources>
</Definition>
<Definition Id="System_CPUUsage" DisplayName="CPU" Description="Microsoft System CPU Usage Widget Description" AllowMultiple="true">
<Definition Id="System_CPUUsage" DisplayName="CPU" Description="Microsoft System CPU Usage Widget Description" AllowMultiple="true" IsCustomizable="false">
<Capabilities>
<Capability>
<Size Name="small" />
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 8 additions & 2 deletions tools/Dashboard/DevHome.Dashboard/Controls/WidgetControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Windows.Widgets.Hosts;

namespace DevHome.Dashboard.Controls;

public sealed partial class WidgetControl : UserControl
{
private MenuFlyoutItem _currentSelectedSize;
Expand Down Expand Up @@ -213,15 +214,20 @@ private void AddCustomizeToWidgetMenu(MenuFlyout widgetMenuFlyout, WidgetViewMod
};
menuItemCustomize.Click += OnCustomizeWidgetClick;
widgetMenuFlyout.Items.Add(menuItemCustomize);

if (!widgetViewModel.IsCustomizable)
{
menuItemCustomize.IsEnabled = false;
}
}

private void OnCustomizeWidgetClick(object sender, RoutedEventArgs e)
private async void OnCustomizeWidgetClick(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyoutItem customizeMenuItem)
{
if (customizeMenuItem?.Tag is WidgetViewModel widgetViewModel)
{
widgetViewModel.IsInEditMode = true;
await widgetViewModel.Widget.NotifyCustomizationRequestedAsync();
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions tools/Dashboard/DevHome.Dashboard/DevHome.Dashboard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<None Remove="Assets\HostConfigDark.json" />
<None Remove="Assets\HostConfigLight.json" />
<None Remove="Views\AddWidgetDialog.xaml" />
<None Remove="Views\CustomizeWidgetDialog.xaml" />
<None Remove="Views\DashboardView.xaml" />
<None Remove="Views\WidgetControl.xaml" />
</ItemGroup>
Expand All @@ -28,12 +27,12 @@
<ProjectReference Include="..\..\..\common\DevHome.Common.csproj" />
<ProjectReference Include="..\..\..\logging\DevHome.Logging.csproj" />
<Content Include=".\BuildAssets\Microsoft.Windows.Widgets.Internal.winmd" Link="Microsoft.Windows.Widgets.Internal.winmd" CopyToOutputDirectory="PreserveNewest" />
<Content Include=".\BuildAssets\Microsoft.Windows.Widgets.Hosts.winmd" Link="Microsoft.Windows.Widgets.Hosts.winmd" CopyToOutputDirectory="PreserveNewest" />
<Content Include=".\BuildAssets\Microsoft.Windows.Widgets.winmd" Link="Microsoft.Windows.Widgets.winmd" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.Windows.Widgets.Hosts">
<HintPath>BuildAssets\Microsoft.Windows.Widgets.Hosts.winmd</HintPath>
<HintPath>BuildAssets\Microsoft.Windows.Widgets.winmd</HintPath>
<IsWinMDFile>true</IsWinMDFile>
</Reference>
</ItemGroup>
Expand All @@ -57,9 +56,6 @@
<Page Update="Views\AddWidgetDialog.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\CustomizeWidgetDialog.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Views\DashboardView.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public partial class WidgetViewModel : ObservableObject
[ObservableProperty]
private WidgetSize _widgetSize;

[ObservableProperty]
private bool _isCustomizable;

[ObservableProperty]
private string _widgetDisplayTitle;

Expand All @@ -46,9 +49,6 @@ public partial class WidgetViewModel : ObservableObject
[ObservableProperty]
private FrameworkElement _widgetFrameworkElement;

[ObservableProperty]
private Microsoft.UI.Xaml.Media.Brush _widgetBackground;

public bool IsInAddMode { get; set; }

[ObservableProperty]
Expand Down Expand Up @@ -80,6 +80,7 @@ partial void OnWidgetDefinitionChanged(WidgetDefinition value)
{
WidgetDisplayTitle = WidgetDefinition.DisplayTitle;
WidgetProviderDisplayTitle = WidgetDefinition.ProviderDefinition.DisplayName;
IsCustomizable = WidgetDefinition.IsCustomizable;
}
}

Expand Down
Loading