Skip to content

Commit

Permalink
Merge pull request #81 from fernandreu/feature/context-menu
Browse files Browse the repository at this point in the history
Add context menu entry for known extensions
  • Loading branch information
fernandreu authored Dec 26, 2019
2 parents 43ca395 + b340ac3 commit d1ae09b
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 7 deletions.
73 changes: 73 additions & 0 deletions src/OfficeRibbonXEditor/Models/FileAssociationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Reflection;
using Microsoft.Win32;

namespace OfficeRibbonXEditor.Models
{
public class FileAssociationHelper
{
private const string MenuEntryName = "OfficeXRibbonEdit";

private readonly string extension;

public FileAssociationHelper(string extension)
{
this.extension = extension;
}

public bool CheckAssociation()
{
var type = this.GetFileType();
if (string.IsNullOrEmpty(type))
{
return false;
}

var menuKey = this.GetMenuKey(type, false);
return menuKey != null;
}

public void RemoveAssociation()
{
var type = this.GetFileType();
if (string.IsNullOrEmpty(type))
{
return;
}

Registry.CurrentUser.DeleteSubKeyTree($@"Software\Classes\{type}\shell\{MenuEntryName}");
}

public void AddAssociation()
{
var type = this.GetFileType();
if (string.IsNullOrEmpty(type))
{
return;
}

var key = Registry.CurrentUser.CreateSubKey($@"Software\Classes\{type}\shell\{MenuEntryName}");
if (key == null)
{
// TODO: Throw
return;
}
key.SetValue(null, "Edit with OfficeRibbonXEditor");
var exePath = Assembly.GetExecutingAssembly().Location;
key.SetValue("Icon", exePath);
var subKey = key.CreateSubKey("command");
subKey?.SetValue(null, $"\"{exePath}\" \"%1\"");
}

private string GetFileType()
{
var key = Registry.ClassesRoot.OpenSubKey(this.extension)?.GetValue(null)?.ToString();
return key;
}

private RegistryKey GetMenuKey(string fileType, bool writable = true)
{
var key = Registry.CurrentUser.OpenSubKey($@"Software\Classes\{fileType}\shell\{MenuEntryName}", writable);
return key;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,33 @@
using GalaSoft.MvvmLight.Command;
using OfficeRibbonXEditor.Interfaces;
using OfficeRibbonXEditor.Properties;
using OfficeRibbonXEditor.ViewModels.Shell;
using OfficeRibbonXEditor.ViewModels.Tabs;

namespace OfficeRibbonXEditor.ViewModels.Dialogs
{

public class SettingsDialogViewModel : DialogBase, IContentDialog<ICollection<ITabItemViewModel>>
{
private static readonly ICollection<string> extensions = new List<string>
{
".docx",
".docm",
".dotx",
".dotm",
".pptx",
".pptm",
".ppsx",
".ppsm",
".potx",
".potm",
".ppam",
".xlsx",
".xlsm",
".xltm",
".xlam",
};

private readonly string[] usedProperties =
{
nameof(Settings.Default.TextColor),
Expand All @@ -35,6 +55,14 @@ public SettingsDialogViewModel()
this.ResetCommand = new RelayCommand(this.ResetToDefault);
this.ApplyCommand = new RelayCommand(this.ApplySettings);
this.AcceptCommand = new RelayCommand(this.AcceptSettings);
this.SetAllAssociationsCommand = new RelayCommand<bool>(this.SetAllAssociations);

foreach (var extension in extensions)
{
var association = new FileAssociationViewModel(extension);
association.ValueChanged += (o, e) => this.SettingsChanged = true;
this.FileAssociations.Add(association);
}

Settings.Default.PropertyChanged += (o, e) => this.SettingsChanged = true;
}
Expand All @@ -56,12 +84,16 @@ public bool SettingsChanged

public ICollection<ITabItemViewModel> Tabs { get; private set; }

public ICollection<FileAssociationViewModel> FileAssociations { get; } = new List<FileAssociationViewModel>();

public RelayCommand ResetCommand { get; }

public RelayCommand ApplyCommand { get; }

public RelayCommand AcceptCommand { get; }

public RelayCommand<bool> SetAllAssociationsCommand { get; }

private void LoadCurrent()
{
this.currentValues.Clear();
Expand All @@ -78,6 +110,11 @@ private void ResetToCurrent()
{
Settings.Default[pair.Key] = pair.Value;
}

foreach (var association in this.FileAssociations)
{
association.ResetToCurrent();
}
}

private void ResetToDefault()
Expand All @@ -92,6 +129,11 @@ private void ResetToDefault()
Settings.Default[name] = propertyValue.PropertyValue;
}

foreach (var association in this.FileAssociations)
{
association.ResetToDefault();
}

this.ApplySettings();
}

Expand All @@ -104,18 +146,27 @@ private void ApplySettings()
tab.Lexer?.Update();
}

foreach (var association in this.FileAssociations)
{
association.Apply();
}

this.SettingsChanged = false;
}

private void AcceptSettings()
{
Settings.Default.Save();
this.ApplySettings();
this.IsCancelled = false;
foreach (var tab in this.Tabs.OfType<EditorTabViewModel>())
this.Close();
}

private void SetAllAssociations(bool newValue)
{
foreach (var association in this.FileAssociations)
{
tab.Lexer?.Update();
association.NewValue = newValue;
}
this.Close();
}

protected override void OnClosing(CancelEventArgs args)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using GalaSoft.MvvmLight;
using OfficeRibbonXEditor.Models;

namespace OfficeRibbonXEditor.ViewModels.Shell
{
public class FileAssociationViewModel : ViewModelBase
{
public FileAssociationViewModel(string extension)
{
this.Extension = extension;
this.PreviousValue = GetCurrentValue();
this.NewValue = this.PreviousValue;
}

public event EventHandler ValueChanged;

public string Extension { get; }

private bool PreviousValue { get; set; }

private bool newValue;

public bool NewValue
{
get => this.newValue;
set
{
if (!this.Set(ref this.newValue, value))
{
return;
}

this.ValueChanged?.Invoke(this, EventArgs.Empty);
}
}

public bool GetCurrentValue()
{
return new FileAssociationHelper(this.Extension).CheckAssociation();
}

public void Apply()
{
if (this.PreviousValue == this.NewValue)
{
return;
}

if (this.NewValue)
{
new FileAssociationHelper(this.Extension).AddAssociation();
}
else
{
new FileAssociationHelper(this.Extension).RemoveAssociation();
}

this.PreviousValue = this.NewValue;
}

public void ResetToCurrent()
{
this.NewValue = this.PreviousValue;
}

public void ResetToDefault()
{
this.NewValue = false;
}
}
}
62 changes: 59 additions & 3 deletions src/OfficeRibbonXEditor/Views/Dialogs/SettingsDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:converters="clr-namespace:OfficeRibbonXEditor.Converters"
xmlns:controls="clr-namespace:OfficeRibbonXEditor.Views.Controls"
xmlns:viewModels="clr-namespace:OfficeRibbonXEditor.ViewModels.Dialogs"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="Settings"
Icon="/Resources/Images/settings.png"
Expand All @@ -23,8 +24,9 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GroupBox Header="Editor colors">
Expand Down Expand Up @@ -119,7 +121,11 @@
<CheckBox Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Content="Auto-indent new lines" Margin="8" IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=AutoIndent}"/>
</Grid>
</GroupBox>
<GroupBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Header="Misc">
<GroupBox
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3"
Header="Misc">
<StackPanel Margin="2">
<CheckBox
Margin="0,0,0,4"
Expand All @@ -141,7 +147,57 @@
MinHeight="60"/>
</StackPanel>
</GroupBox>
<StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="2">
<GroupBox
Grid.Row="2"
Grid.Column="0"
Grid.ColumnSpan="3"
Header="File Associations"
ToolTip="A context menu entry will appear in Windows Explorer to edit these files directly with the tool">
<Grid Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ItemsControl
Grid.Column="0"
Margin="0,0,4,0"
ItemsSource="{Binding FileAssociations}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Extension}" IsChecked="{Binding NewValue}" Margin="0,0,8,0"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="6"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<StackPanel
Grid.Column="1"
Orientation="Vertical"
VerticalAlignment="Bottom">
<Button
Content="Check all"
Margin="0,0,0,8"
Padding="8,4"
Command="{Binding SetAllAssociationsCommand}">
<Button.CommandParameter>
<system:Boolean>True</system:Boolean>
</Button.CommandParameter>
</Button>
<Button
Content="Uncheck all"
Padding="8,4"
Command="{Binding SetAllAssociationsCommand}">
<Button.CommandParameter>
<system:Boolean>False</system:Boolean>
</Button.CommandParameter>
</Button>
</StackPanel>
</Grid>
</GroupBox>
<StackPanel Orientation="Horizontal" Grid.Row="3" Grid.Column="2">
<Button Content="Reset Defaults" Margin="3" Padding="8,4" Command="{Binding ResetCommand}"/>
<Button Content="Apply" Margin="3" Padding="8,4" Command="{Binding ApplyCommand}" IsEnabled="{Binding SettingsChanged}"/>
<Button Content="Accept" Margin="3" Padding="8,4" Command="{Binding AcceptCommand}" IsDefault="True" IsEnabled="{Binding SettingsChanged}"/>
Expand Down

0 comments on commit d1ae09b

Please sign in to comment.