Skip to content

Commit

Permalink
feature - default layout option on profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
MSeys committed Nov 14, 2023
1 parent e4a4f78 commit 1d77d0d
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 86 deletions.
2 changes: 2 additions & 0 deletions HandheldCompanion/Managers/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ private static void ControllerManager_ControllerPlugged(IController Controller,
UpdateProfileWrapper(profile);
}

public static Profile? GetProfileWithDefaultLayout() => profiles.Values.FirstOrDefault(p => p.Layout.IsDefaultLayout);

#region events

public static event DeletedEventHandler Deleted;
Expand Down
8 changes: 7 additions & 1 deletion HandheldCompanion/Misc/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public partial class Layout : ICloneable, IDisposable
public SortedDictionary<AxisLayoutFlags, IActions> AxisLayout { get; set; } = new();
public SortedDictionary<AxisLayoutFlags, IActions> GyroLayout { get; set; } = new();

public bool IsDefaultLayout { get; set; }

// gyro related

public Layout()
Expand Down Expand Up @@ -60,8 +62,12 @@ public object Clone()
{
var jsonString = JsonConvert.SerializeObject(this, Formatting.Indented,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
return JsonConvert.DeserializeObject<Layout>(jsonString,
var deserialized = JsonConvert.DeserializeObject<Layout>(jsonString,
new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });

deserialized.IsDefaultLayout = false; // Clone shouldn't be default layout in case it is true

return deserialized;
}

public void Dispose()
Expand Down
9 changes: 9 additions & 0 deletions HandheldCompanion/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2322,4 +2322,7 @@ Wenn die Bewegungseingabe aktiviert ist, verwenden Sie die ausgewählte(n) Taste
<data name="QuickProfilesPage_Waiting" xml:space="preserve">
<value>Waiting for foreground process...</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
5 changes: 4 additions & 1 deletion HandheldCompanion/Properties/Resources.es-ES.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2320,4 +2320,7 @@
<data name="ProfilesPage_AreYouSureApplyTemplate2" xml:space="preserve">
<value>No se puede deshacer esta acción. Plantilla aplicada anteriormente: {0}</value>
</data>
</root>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2311,4 +2311,7 @@ with motion input enabled, use selected button(s) to disable motion.</value>
<data name="ProfilesPage_AreYouSureApplyTemplate2" xml:space="preserve">
<value>You can't undo this action. Previously applied template: {0}</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.it-IT.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2320,4 +2320,7 @@
<data name="ProfilesPage_AreYouSureApplyTemplate2" xml:space="preserve">
<value>You can't undo this action. Previously applied template: {0}</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2320,4 +2320,7 @@
<data name="ProfilesPage_AreYouSureApplyTemplate2" xml:space="preserve">
<value>You can't undo this action. Previously applied template: {0}</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.pt-BR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2321,4 +2321,7 @@ com a entrada de movimento ligada, use o(s) botão(ões) selecionado(s) para des
<data name="ProfilesPage_GPU" xml:space="preserve">
<value>GPU</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2354,4 +2354,7 @@ with motion input enabled, use selected button(s) to disable motion.</value>
<data name="ControllerPage_XInputControllerWarning" xml:space="preserve">
<value>Please switch your controller off and then on again by pressing and holding the Guide button to finalize pairing.</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.ru-RU.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2302,4 +2302,7 @@
<data name="SettingsPage_VirtualControllerForceOrderDependencyTitle" xml:space="preserve">
<value>Warning</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2302,4 +2302,7 @@
<data name="ProfilesPage_AreYouSureApplyTemplate2" xml:space="preserve">
<value>You can't undo this action. Previously applied template: {0}</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
3 changes: 3 additions & 0 deletions HandheldCompanion/Properties/Resources.zh-Hant.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2302,4 +2302,7 @@
<data name="ProfilesPage_AreYouSureApplyTemplate2" xml:space="preserve">
<value>You can't undo this action. Previously applied template: {0}</value>
</data>
<data name="LayoutPage_SetAsDefault" xml:space="preserve">
<value>Make this the default layout</value>
</data>
</root>
175 changes: 93 additions & 82 deletions HandheldCompanion/Views/Pages/LayoutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,91 +113,102 @@
IsEnabled="false" />
</ComboBox>

<Grid>
<CheckBox
Name="CheckBoxDeviceLayouts"
Checked="CheckBoxDeviceLayouts_Checked"
Content="{x:Static resx:Resources.LayoutPage_ShowCurrentControllerTemplates}"
IsChecked="True"
Unchecked="CheckBoxDeviceLayouts_Checked" />

<ui:SimpleStackPanel
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="6">

<!-- Apply template -->
<Button
Name="ButtonApplyLayout"
Click="ButtonApplyLayout_Click"
Content="{x:Static resx:Resources.LayoutPage_ApplyTemplate}"
IsEnabled="False"
Style="{DynamicResource AccentButtonStyle}" />

<!-- Export layout -->
<Button Name="ButtonLayoutSettings" Content="Export layout">
<ui:FlyoutService.Flyout>
<ui:Flyout
x:Name="LayoutFlyout"
AreOpenCloseAnimationsEnabled="True"
Opening="Flyout_Opening"
Placement="Left">
<ui:SimpleStackPanel Width="400" Spacing="24">
<TextBlock Style="{StaticResource BaseTextBlockStyle}" Text="{x:Static resx:Resources.LayoutPage_ExportLayout}" />

<ui:SimpleStackPanel Spacing="6">
<TextBox
Name="LayoutTitle"
HorizontalAlignment="Stretch"
ui:ControlHelper.Header="{x:Static resx:Resources.LayoutPage_LayoutTitle}" />
<TextBox
Name="LayoutDescription"
HorizontalAlignment="Stretch"
ui:ControlHelper.Header="{x:Static resx:Resources.LayoutPage_LayoutDesc}" />
<TextBox
Name="LayoutAuthor"
HorizontalAlignment="Stretch"
ui:ControlHelper.Header="{x:Static resx:Resources.LayoutPage_LayoutAuthor}" />
<CheckBox x:Name="ExportForCurrent" Content="{x:Static resx:Resources.LayoutPage_ExportCurrentController}" />
<CheckBox
x:Name="SaveGameInfo"
Click="SaveGameInfo_Toggled"
Content="{x:Static resx:Resources.LayoutPage_SaveGameInfoLayout}"
IsChecked="True" />

<!-- Separator -->
<Separator
BorderBrush="{DynamicResource SystemControlBackgroundChromeMediumBrush}"
BorderThickness="0,1,0,0"
Opacity="0.25" />

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>

<Button
Name="LayoutExportButton"
Grid.Column="0"
<ui:SimpleStackPanel
Orientation="Vertical"
Spacing="6">

<Grid>
<CheckBox
Name="CheckBoxDeviceLayouts"
Checked="CheckBoxDeviceLayouts_Checked"
Content="{x:Static resx:Resources.LayoutPage_ShowCurrentControllerTemplates}"
IsChecked="True"
Unchecked="CheckBoxDeviceLayouts_Checked" />

<ui:SimpleStackPanel
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="6">

<!-- Apply template -->
<Button
Name="ButtonApplyLayout"
Click="ButtonApplyLayout_Click"
Content="{x:Static resx:Resources.LayoutPage_ApplyTemplate}"
IsEnabled="False"
Style="{DynamicResource AccentButtonStyle}" />

<!-- Export layout -->
<Button Name="ButtonLayoutSettings" Content="Export layout">
<ui:FlyoutService.Flyout>
<ui:Flyout
x:Name="LayoutFlyout"
AreOpenCloseAnimationsEnabled="True"
Opening="Flyout_Opening"
Placement="Left">
<ui:SimpleStackPanel Width="400" Spacing="24">
<TextBlock Style="{StaticResource BaseTextBlockStyle}" Text="{x:Static resx:Resources.LayoutPage_ExportLayout}" />

<ui:SimpleStackPanel Spacing="6">
<TextBox
Name="LayoutTitle"
HorizontalAlignment="Stretch"
ui:ControlHelper.Header="{x:Static resx:Resources.LayoutPage_LayoutTitle}" />
<TextBox
Name="LayoutDescription"
HorizontalAlignment="Stretch"
Click="LayoutExportButton_Click"
Content="{x:Static resx:Resources.LayoutPage_Confirm}" />
<Button
Name="LayoutCancelButton"
Grid.Column="2"
ui:ControlHelper.Header="{x:Static resx:Resources.LayoutPage_LayoutDesc}" />
<TextBox
Name="LayoutAuthor"
HorizontalAlignment="Stretch"
Click="LayoutCancelButton_Click"
Content="{x:Static resx:Resources.LayoutPage_Cancel}" />
</Grid>
ui:ControlHelper.Header="{x:Static resx:Resources.LayoutPage_LayoutAuthor}" />
<CheckBox x:Name="ExportForCurrent" Content="{x:Static resx:Resources.LayoutPage_ExportCurrentController}" />
<CheckBox
x:Name="SaveGameInfo"
Click="SaveGameInfo_Toggled"
Content="{x:Static resx:Resources.LayoutPage_SaveGameInfoLayout}"
IsChecked="True" />

<!-- Separator -->
<Separator
BorderBrush="{DynamicResource SystemControlBackgroundChromeMediumBrush}"
BorderThickness="0,1,0,0"
Opacity="0.25" />

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>

<Button
Name="LayoutExportButton"
Grid.Column="0"
HorizontalAlignment="Stretch"
Click="LayoutExportButton_Click"
Content="{x:Static resx:Resources.LayoutPage_Confirm}" />
<Button
Name="LayoutCancelButton"
Grid.Column="2"
HorizontalAlignment="Stretch"
Click="LayoutCancelButton_Click"
Content="{x:Static resx:Resources.LayoutPage_Cancel}" />
</Grid>
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
</ui:Flyout>
</ui:FlyoutService.Flyout>
</Button>
</ui:SimpleStackPanel>
</Grid>
</ui:Flyout>
</ui:FlyoutService.Flyout>
</Button>
</ui:SimpleStackPanel>
</Grid>

<CheckBox
Name="CheckBoxDefaultLayout"
Checked="CheckBoxDefaultLayout_Checked"
Content="{x:Static resx:Resources.LayoutPage_SetAsDefault}"
Unchecked="CheckBoxDefaultLayout_Checked" />
</ui:SimpleStackPanel>

<!-- Separator -->
<Separator
Expand Down
19 changes: 19 additions & 0 deletions HandheldCompanion/Views/Pages/LayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ private void UpdatePages()

// clear layout selection
cB_Layouts.SelectedValue = null;

CheckBoxDefaultLayout.IsChecked = currentTemplate.Layout.IsDefaultLayout;
CheckBoxDefaultLayout.IsEnabled = currentTemplate.Layout != LayoutManager.GetDesktop();
}
});
}
Expand Down Expand Up @@ -539,4 +542,20 @@ private void On_Navigated(object sender, NavigationEventArgs e)
parentNavView.Header = new TextBlock() { Text = header };
}
}

private void CheckBoxDefaultLayout_Checked(object sender, RoutedEventArgs e)
{
var isDefaultLayout = (bool)CheckBoxDefaultLayout.IsChecked;
var prevDefaultLayoutProfile = ProfileManager.GetProfileWithDefaultLayout();

currentTemplate.Layout.IsDefaultLayout = isDefaultLayout;
currentTemplate.Layout.UpdateLayout();

// If option is enabled and a different default layout profile exists, we want to set option false on prev profile.
if (isDefaultLayout && prevDefaultLayoutProfile != null && prevDefaultLayoutProfile.Layout != currentTemplate.Layout)
{
prevDefaultLayoutProfile.Layout.IsDefaultLayout = false;
ProfileManager.UpdateOrCreateProfile(prevDefaultLayoutProfile);
}
}
}
4 changes: 3 additions & 1 deletion HandheldCompanion/Views/Pages/ProfilesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -255,7 +256,8 @@ private async void b_CreateProfile_Click(object sender, RoutedEventArgs e)
}

var profile = new Profile(path);
profile.Layout = LayoutTemplate.DefaultLayout.Layout.Clone() as Layout;
var toCloneLayout = ProfileManager.GetProfileWithDefaultLayout()?.Layout ?? LayoutTemplate.DefaultLayout.Layout;
profile.Layout = toCloneLayout.Clone() as Layout;
profile.LayoutTitle = LayoutTemplate.DefaultLayout.Name;
profile.TDPOverrideValues = MainWindow.CurrentDevice.nTDP;

Expand Down
Loading

0 comments on commit 1d77d0d

Please sign in to comment.