Skip to content

Commit

Permalink
Search box in Edit Movies
Browse files Browse the repository at this point in the history
  • Loading branch information
electroly committed Nov 4, 2024
1 parent 53b9a14 commit d1c5e2a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 25 deletions.
116 changes: 96 additions & 20 deletions src/J.App/EditMoviesForm.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using System.Collections.Frozen;
using System.Data;
using System.Text.RegularExpressions;
using J.Core.Data;
using Microsoft.Extensions.DependencyInjection;

namespace J.App;

public sealed class EditMoviesForm : Form
public sealed partial class EditMoviesForm : Form
{
private readonly ContextMenuStrip _contextMenuStrip;
private readonly LibraryProviderAdapter _libraryProvider;
private readonly IServiceProvider _serviceProvider;
private readonly MovieExporter _movieExporter;
private readonly DataGridView _grid;
private readonly DataTable _data;
private readonly TableLayoutPanel _table;
private readonly FlowLayoutPanel _buttonFlow;
private readonly Button _closeButton;
private readonly TextBox _searchText;

public EditMoviesForm(
LibraryProviderAdapter libraryProvider,
Expand Down Expand Up @@ -52,32 +57,54 @@ MovieExporter movieExporter
_contextMenuStrip.Items.Add("Delete", null, DeleteMovie_Click);
}

Controls.Add(_grid = ui.NewDataGridView());
Controls.Add(_table = ui.NewTable(1, 2));
{
_grid.ContextMenuStrip = _contextMenuStrip;
_grid.DataSource = _data;

var col_name = _grid.Columns[_grid.Columns.Add("name", "Name")];
_table.Controls.Add(_buttonFlow = ui.NewFlowRow());
{
col_name.Width = ui.GetLength(550);
col_name.DataPropertyName = "name";
col_name.Frozen = true;
}
_buttonFlow.Padding = ui.DefaultPadding;

var col_date_added = _grid.Columns[_grid.Columns.Add("date_added", "Date Added")];
{
col_date_added.Width = ui.GetLength(225);
col_date_added.DataPropertyName = "date_added";
col_date_added.DividerWidth = ui.GetLength(3);
col_date_added.Frozen = true;
_buttonFlow.Controls.Add(_closeButton = ui.NewButton("Close"));
{
_closeButton.Margin += ui.RightSpacing;
_closeButton.Click += CloseButton_Click;
}

_buttonFlow.Controls.Add(_searchText = ui.NewTextBox(200));
{
ui.SetBigFont(_searchText);
_searchText.Margin += ui.GetPadding(0, 4);
ui.SetCueText(_searchText, "Search");
_searchText.KeyPress += SearchText_KeyPress;
}
}

foreach (var tagType in tagTypes)
_table.Controls.Add(_grid = ui.NewDataGridView(), 0, 1);
{
var col = _grid.Columns[_grid.Columns.Add(tagType.Id.Value, tagType.PluralName)];
_grid.ContextMenuStrip = _contextMenuStrip;
_grid.DataSource = _data;

var col_name = _grid.Columns[_grid.Columns.Add("name", "Name")];
{
col.Width = ui.GetLength(200);
col.DataPropertyName = tagType.Id.Value;
col_name.Width = ui.GetLength(550);
col_name.DataPropertyName = "name";
col_name.Frozen = true;
}

var col_date_added = _grid.Columns[_grid.Columns.Add("date_added", "Date Added")];
{
col_date_added.Width = ui.GetLength(225);
col_date_added.DataPropertyName = "date_added";
col_date_added.DividerWidth = ui.GetLength(3);
col_date_added.Frozen = true;
}

foreach (var tagType in tagTypes)
{
var col = _grid.Columns[_grid.Columns.Add(tagType.Id.Value, tagType.PluralName)];
{
col.Width = ui.GetLength(200);
col.DataPropertyName = tagType.Id.Value;
}
}
}
}
Expand All @@ -95,6 +122,21 @@ MovieExporter movieExporter
WindowState = FormWindowState.Maximized;
}

private void SearchText_KeyPress(object? sender, KeyPressEventArgs e)
{
// Did they press the enter key with no modifiers?
if (e.KeyChar == (char)Keys.Enter && ModifierKeys == Keys.None)
{
e.Handled = true;
UpdateList();
}
}

private void CloseButton_Click(object? sender, EventArgs e)
{
Close();
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Expand Down Expand Up @@ -317,6 +359,36 @@ private void UpdateList()
var tags = _libraryProvider.GetTags().ToDictionary(x => x.Id);
var movieTags = _libraryProvider.GetMovieTags().ToLookup(x => x.MovieId, x => x.TagId);

var searchTerms = WhitespaceRegex().Split(_searchText.Text);
movies = movies
.Where(movie =>
{
var thisMovieTags = movieTags[movie.Id].Select(tagId => tags[tagId]).ToList();

foreach (var term in searchTerms)
{
if (movie.Filename.Contains(term, StringComparison.InvariantCultureIgnoreCase))
continue;

var match = false;
foreach (var tag in thisMovieTags)
{
if (tag.Name.Contains(term, StringComparison.InvariantCultureIgnoreCase))
{
match = true;
break;
}
}
if (match)
continue;

return false;
}

return true;
})
.ToList();

_grid.DataSource = null;
_data.Rows.Clear();

Expand Down Expand Up @@ -355,6 +427,7 @@ private void UpdateList()
_grid.FirstDisplayedScrollingColumnIndex = firstColumnIndex;

// Re-select the originally selected rows, if possible (they might have been deleted).
_grid.ClearSelection();
foreach (DataGridViewRow viewRow in _grid.Rows)
{
var row = ((DataRowView)viewRow.DataBoundItem).Row;
Expand Down Expand Up @@ -425,4 +498,7 @@ private void ExportMovie_Click(object? sender, EventArgs e)
);
}
}

[GeneratedRegex(@"\s+")]
private static partial Regex WhitespaceRegex();
}
20 changes: 15 additions & 5 deletions src/J.App/Ui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@ public Padding GetPadding(int unscaledLeft, int unscaledTop, int unscaledRight,

public Padding RightSpacing => new(0, 0, GetLength(DefaultUnscaledPadding), 0);

public Font NewListFont() => new("Segoe UI", 11f);
public Font NewBigFont() => new("Segoe UI", 11f);

public void SetBigFont(Control control)
{
var font = NewBigFont();
control.Disposed += delegate
{
font.Dispose();
};
control.Font = font;
}

public (Control Parent, TextBox Child) NewLabeledTextBox(string text, int unscaledWidth)
{
Expand Down Expand Up @@ -421,7 +431,7 @@ public ToolStripDropDownButton NewToolStripDropDownButton(string text)

public ToolStripTextBox NewToolStripTextBox(int unscaledWidth)
{
var font = NewListFont();
var font = NewBigFont();
ToolStripTextBox box =
new()
{
Expand Down Expand Up @@ -482,7 +492,7 @@ public Label NewLabel(string text)

public ListBox NewListBox()
{
var font = NewListFont();
var font = NewBigFont();
ListBox listBox =
new()
{
Expand Down Expand Up @@ -557,7 +567,7 @@ protected override void OnMouseMove(MouseEventArgs e)

public DataGridView NewDataGridView()
{
var font = NewListFont();
var font = NewBigFont();
DoubleBufferedDataGridView grid =
new()
{
Expand Down Expand Up @@ -633,7 +643,7 @@ public GroupBox NewGroupBox(string text)

public TextBox NewTextBox(int unscaledWidth)
{
return new() { AutoSize = false, Width = GetLength(unscaledWidth) };
return new() { AutoSize = true, Width = GetLength(unscaledWidth) };
}

public CheckBox NewCheckBox(string text)
Expand Down

0 comments on commit d1c5e2a

Please sign in to comment.