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

feat: search loaded FileMaker clips in the ui by name #96

Merged
merged 3 commits into from
Aug 27, 2024
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
74 changes: 39 additions & 35 deletions MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="225" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Grid
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="2"
Margin="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75" />
Expand All @@ -63,39 +60,46 @@
<Button Grid.Column="2" HorizontalAlignment="Right" Command="{Binding OpenFolderPicker}" Content="Browse" />
</Grid>

<ListBox
Grid.Row="1"
Grid.Column="0"
ItemsSource="{Binding FileMakerClips}"
SelectedItem="{Binding SelectedClip}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,8">
<TextBox Margin="0" Text="{Binding Name, Mode=TwoWay}" />
<ComboBox
DisplayMemberBinding="{Binding DisplayName}"
ItemsSource="{Binding Clip.ClipTypes}"
SelectedValue="{Binding ClipType, Mode=TwoWay}"
SelectedValueBinding="{Binding KeyId}" />
<TextBlock MaxLines="1" Text="{Binding ClipType}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

<AvaloniaEdit:TextEditor
x:Name="avaloniaEditor"
Grid.Row="1"
Grid.Column="1"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace"
ShowLineNumbers="True"
SyntaxHighlighting="Xml"
WordWrap="False">
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding SelectedClip.ClipXml, Mode=TwoWay}" />
</i:Interaction.Behaviors>
</AvaloniaEdit:TextEditor>
<DockPanel Grid.Row="1">
<StackPanel
Width="225"
DockPanel.Dock="Left">
<TextBox Margin="8" Watermark="Search" Text="{Binding SearchText}" />
<ListBox
ItemsSource="{Binding FilteredClips}"
SelectedItem="{Binding SelectedClip}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.Styles>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="8">
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<ComboBox
DisplayMemberBinding="{Binding DisplayName}"
ItemsSource="{Binding Clip.ClipTypes}"
SelectedValue="{Binding ClipType, Mode=TwoWay}"
SelectedValueBinding="{Binding KeyId}" />
<TextBlock MaxLines="1" Text="{Binding ClipType}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>

<AvaloniaEdit:TextEditor
x:Name="avaloniaEditor"
FontFamily="Cascadia Code,Consolas,Menlo,Monospace"
ShowLineNumbers="True"
SyntaxHighlighting="Xml"
WordWrap="False">
<i:Interaction.Behaviors>
<behaviors:DocumentTextBindingBehavior Text="{Binding SelectedClip.ClipXml, Mode=TwoWay}" />
</i:Interaction.Behaviors>
</AvaloniaEdit:TextEditor>
</DockPanel>
</Grid>
</DockPanel>

Expand Down
27 changes: 27 additions & 0 deletions ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
public MainWindowViewModel(ILogger logger)
{
_logger = logger;

// default to the local app data folder + \SharpFM, otherwise use provided path
_currentPath ??= Path.Join(
path1: Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
path2: Path.Join("SharpFM", "Clips")
);

FileMakerClips = [];
FileMakerClips.CollectionChanged += (sender, e) =>
{
// reset search text, which will trigger a property notify changed
// that will re-run the search with empty value (which shows all)
SearchText = string.Empty;
};

FilteredClips = [];

LoadClips(CurrentPath);
}
Expand Down Expand Up @@ -233,6 +242,8 @@ public static string Version

public ObservableCollection<ClipViewModel> FileMakerClips { get; set; }

public ObservableCollection<ClipViewModel> FilteredClips { get; set; }

private ClipViewModel? _selectedClip;
public ClipViewModel? SelectedClip
{
Expand All @@ -244,6 +255,22 @@ public ClipViewModel? SelectedClip
}
}

private string _searchText = string.Empty;
public string SearchText
{
get => _searchText;
set
{
_searchText = value;
FilteredClips.Clear();
foreach (var c in FileMakerClips.Where(c => c.Name.Contains(_searchText)))
{
FilteredClips.Add(c);
}
NotifyPropertyChanged();
}
}

private string _currentPath;
public string CurrentPath
{
Expand Down