Skip to content

Commit

Permalink
feat: files and folder picker for files-to-ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskopf authored and mariru27 committed Jul 17, 2024
1 parent 98144b3 commit b99b4b6
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Commands\FindViewMenuCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Commands\LookInMenu.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Commands\OptimizeIncludesCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Convertors\BooleanToGridLengthConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LaunchCompilationDbProgrammatically.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ManageEncoding.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MVVM\AutoCompleteHistory\ASTMatchers.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace ClangPowerTools.Convertors
{
public class BooleanToGridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool valueAsBool && valueAsBool)
{
return new GridLength(1, GridUnitType.Auto);
}
return new GridLength(0);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ClangPowerTools.Helpers;
using ClangPowerTools.Helpers;
using ClangPowerTools.MVVM.Models;
using Microsoft.Win32;
using System.Collections.Generic;
Expand Down Expand Up @@ -70,7 +70,7 @@ protected string[] BrowseForFolderFiles(string searchFilePattern, SearchOption s
/// </summary>
/// <param name="searchFilePattern">Search pattern to apply in the file search</param>
/// <param name="searchOption">Information about how to search inside the selected folder</param>
/// <returns>Array of files path</returns>
/// <returns>Path to the selected folder</returns>
protected string BrowseForFolderFiles()
{
using var folderBrowseDialog = new System.Windows.Forms.FolderBrowserDialog();
Expand Down Expand Up @@ -108,9 +108,9 @@ protected void WriteContentToFile(string path, string content)
FileSystem.WriteContentToFile(path, content);
}

protected string OpenContentDialog(string content)
protected string OpenContentDialog(string content, bool showFilesPicker = false, bool showFolderPicker = false)
{
InputDataViewModel inputDataViewModel = new InputDataViewModel(content);
InputDataViewModel inputDataViewModel = new InputDataViewModel(content, showFilesPicker, showFolderPicker);
inputDataViewModel.ShowViewDialog();
string input = CreateInput(inputDataViewModel.Inputs.ToList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void UpdateCompileFlags()

private void UpdateFilesToIgnore()
{
compilerModel.FilesToIgnore = OpenContentDialog(compilerModel.FilesToIgnore);
compilerModel.FilesToIgnore = OpenContentDialog(compilerModel.FilesToIgnore, true, true);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CompilerModel"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void UpdateFileExtensions()

private void UpdateFilesToIgnore()
{
formatModel.FilesToIgnore = OpenContentDialog(formatModel.FilesToIgnore);
formatModel.FilesToIgnore = OpenContentDialog(formatModel.FilesToIgnore, true, true);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FormatModel"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using ClangPowerTools.MVVM.Commands;
using ClangPowerTools.MVVM.Commands;
using ClangPowerTools.MVVM.Models;
using ClangPowerTools.Views;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Input;

namespace ClangPowerTools
Expand All @@ -17,15 +19,21 @@ public class InputDataViewModel : CommonSettingsFunctionality, INotifyPropertyCh
private string inputToAdd;
private InputDataView inputDataView;
private ICommand addCommand;
private bool showFilePicker;
private ICommand pickFilesCommand;
private bool showFolderPicker;
private ICommand pickFolderCommand;

#endregion


#region Constructor

public InputDataViewModel(string content)
public InputDataViewModel(string content, bool showFilesPicker = false, bool showFolderPicker = false)
{
CreateInputsCollection(content);
ShowFilesPicker = showFilesPicker;
ShowFolderPicker = showFolderPicker;
}

public InputDataViewModel() { }
Expand All @@ -44,25 +52,68 @@ public string InputToAdd
set
{
inputToAdd = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("InputToAdd"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InputToAdd)));
}
}

public ObservableCollection<InputDataModel> Inputs { get; set; } = new ObservableCollection<InputDataModel>();

public ICommand AddCommand
public bool ShowFilesPicker
{
get => addCommand ?? (addCommand = new RelayCommand(() => AddInput(), () => CanExecute));
get
{
return showFilePicker;
}
set
{
showFilePicker = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowFilesPicker)));
}
}

public bool CanExecute
public bool ShowFolderPicker
{
get
{
return true;
return showFolderPicker;
}
set
{
showFolderPicker = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowFolderPicker)));
}
}

public ICommand AddCommand
{
get => addCommand ??= new RelayCommand(AddInput, () => CanExecute);
}

public bool CanExecute
{
get => true;
}

public ICommand PickFilesCommand
{
get => pickFilesCommand ??= new RelayCommand(PickFile, () => CanPickFilesExecute);
}

public bool CanPickFilesExecute
{
get => ShowFilesPicker;
}

public ICommand PickFolderCommand
{
get => pickFolderCommand ??= new RelayCommand(PickFolder, () => CanPickFolderExecute);
}

public bool CanPickFolderExecute
{
get => ShowFolderPicker;
}

#endregion


Expand All @@ -74,6 +125,24 @@ public void ShowViewDialog()
inputDataView.ShowDialog();
}

private void PickFile()
{
var filesToAdd = OpenFiles(string.Empty, ".h", "Header files|*.h");
if (filesToAdd == null)
return; // no file selected
foreach (var file in filesToAdd)
{
InputToAdd = file;
AddInput(); // automatically add selected file
}
}

private void PickFolder()
{
InputToAdd = BrowseForFolderFiles();
AddInput(); // automatically add selected folder
}

public void DeleteInput(int index)
{
if (index < 0 || index >= Inputs.Count)
Expand All @@ -88,7 +157,17 @@ private void AddInput()
return;

if (IsDuplicate(inputToAdd))
{
MessageBox.Show($"Ignored to add duplicate: {InputToAdd}", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
InputToAdd = string.Empty;
return;
}

if(!File.Exists(inputToAdd) && !Directory.Exists(inputToAdd))
{
MessageBox.Show($"The file or folder does not exist: {InputToAdd}", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

AddNewElement(inputToAdd);
InputToAdd = string.Empty;
Expand All @@ -102,8 +181,8 @@ private void CreateInputsCollection(string content)
return;

var splitContent = content.Split(';').ToList();
for (int index = 0; index < splitContent.Count; ++index)
AddNewElement(splitContent[index]);
foreach (var splitItem in splitContent)
AddNewElement(splitItem);
}

private void AddNewElement(string newElement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:conv="clr-namespace:ClangPowerTools.Convertors"
Name="_InputList"
d:DesignHeight="450"
d:DesignWidth="800"
Expand All @@ -19,6 +20,7 @@
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="13" />
</Style>
<conv:BooleanToGridLengthConverter x:Key="BooleanToGridLengthConverter"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Expand All @@ -42,6 +44,8 @@
<Grid Grid.Row="0" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="{Binding CanPickFilesExecute, Converter={StaticResource BooleanToGridLengthConverter}}" />
<ColumnDefinition Width="{Binding CanPickFolderExecute, Converter={StaticResource BooleanToGridLengthConverter}}" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

Expand All @@ -52,7 +56,10 @@
Margin="0,0,5,0"
VerticalContentAlignment="Center"
FontSize="13"
Text="{Binding InputToAdd, ElementName=_InputList}">
Text="{Binding InputToAdd, ElementName=_InputList, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Return" Command="{Binding AddCommand, ElementName=_InputList}" />
</TextBox.InputBindings>
<TextBox.Style>
<Style xmlns:sys="clr-namespace:System;assembly=mscorlib" TargetType="TextBox">
<Style.Resources>
Expand Down Expand Up @@ -82,10 +89,32 @@
</TextBox.Style>
</TextBox>

<Button
x:Name="InputButton"
<Button
x:Name="PickFilesButton"
Grid.Column="1"
HorizontalAlignment="Right"
Visibility="Visible"
Command="{Binding PickFilesCommand, ElementName=_InputList}"
Style="{StaticResource SettingsButton}">
<Button.Content>
<Image Source="{StaticResource BrowseFileImage}" />
</Button.Content>
</Button>
<Button
x:Name="PickFolderButton"
Grid.Column="2"
HorizontalAlignment="Right"
Visibility="Visible"
Command="{Binding PickFolderCommand, ElementName=_InputList}"
Style="{StaticResource SettingsButton}">
<Button.Content>
<Image Source="{StaticResource BrowseImage}" />
</Button.Content>
</Button>
<Button
x:Name="InputButton"
Grid.Column="3"
HorizontalAlignment="Right"
Command="{Binding AddCommand, ElementName=_InputList}"
Style="{StaticResource SettingsButton}">
<Button.Content>
Expand All @@ -99,6 +128,7 @@
x:Name="CollectionItems"
Grid.Row="2"
Grid.Column="1"
Grid.ColumnSpan="3"
Margin="0,5,0,0"
Background="White"
ItemsSource="{Binding Collection, ElementName=_InputList, Mode=TwoWay}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public ICommand AddCommand
public static readonly DependencyProperty AddCommandProperty =
DependencyProperty.Register("AddCommand", typeof(ICommand), typeof(InputList), new PropertyMetadata(null));

public ICommand PickFilesCommand
{
get { return (ICommand)GetValue(PickFilesCommandProperty); }
set { SetValue(PickFilesCommandProperty, value); }
}

public static readonly DependencyProperty PickFilesCommandProperty =
DependencyProperty.Register("PickFilesCommand", typeof(ICommand), typeof(InputList), new PropertyMetadata(null));

public ICommand PickFolderCommand
{
get { return (ICommand)GetValue(PickFolderCommandProperty); }
set { SetValue(PickFolderCommandProperty, value); }
}

public static readonly DependencyProperty PickFolderCommandProperty =
DependencyProperty.Register("PickFolderCommand", typeof(ICommand), typeof(InputList), new PropertyMetadata(null));

public ObservableCollection<InputDataModel> Collection
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window
<Window
x:Class="ClangPowerTools.Views.InputDataView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Expand Down Expand Up @@ -40,6 +40,8 @@
x:Name="InputList"
AddCommand="{Binding AddCommand}"
Collection="{Binding Inputs, Mode=TwoWay}"
InputToAdd="{Binding InputToAdd, Mode=TwoWay}" />
InputToAdd="{Binding InputToAdd, Mode=TwoWay}"
PickFilesCommand="{Binding PickFilesCommand}"
PickFolderCommand="{Binding PickFolderCommand}"/>

</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<BitmapImage x:Key="AddInputImage" UriSource="/ClangPowerTools;component/Resources/AddInput.png" />
<BitmapImage x:Key="BrowseImage" UriSource="/ClangPowerTools;component/Resources/Browse.png" />
<BitmapImage x:Key="BrowseFileImage" UriSource="/ClangPowerTools;component/Resources/FileIcon.png" />
<BitmapImage x:Key="AccountAvatarIcon" UriSource="/ClangPowerTools;component/Resources/AccountAvatarIcon.png" />
<BitmapImage x:Key="GitHubIcon" UriSource="/ClangPowerTools;component/Resources/GitHubMark.png" />
<BitmapImage x:Key="LogoutIcon" UriSource="/ClangPowerTools;component/Resources/LogoutIcon.png" />
Expand Down

0 comments on commit b99b4b6

Please sign in to comment.