Skip to content

Commit

Permalink
Add edit dialog for changing highlight pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrietS committed Oct 20, 2024
1 parent 6e49d3c commit 9e2768f
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/LogAlligator.App/Controls/BookmarksView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Avalonia.Controls;
using LogAlligator.App.Utils;
using Serilog;

namespace LogAlligator.App.Controls;

Expand Down
30 changes: 30 additions & 0 deletions src/LogAlligator.App/Controls/Dialogs/EditHighlightDialog.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Window xmlns="https://github.com/avaloniaui"
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"
mc:Ignorable="d"
x:Class="LogAlligator.App.Controls.EditHighlightDialog"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="Edit highlight"
Width="300"
Height="95"
CanResize="False"
ShowInTaskbar="False"
ExtendClientAreaChromeHints="NoChrome"
ExtendClientAreaToDecorationsHint="True"
WindowStartupLocation="CenterOwner"
ShowActivated="True">
<Window.KeyBindings>
<KeyBinding Gesture="Escape" Command="{Binding OnEscape}" />
</Window.KeyBindings>
<StackPanel Margin="10 5 10 10" Spacing="2">
<TextBlock FontSize="{DynamicResource FontSizeNormal}">Edit highlight</TextBlock>
<Separator Background="Transparent"/>
<TextBox Name="HighlightPatternTextBox" Focusable="True" Watermark="Highlight pattern"/>
<Separator Height="6" Background="Transparent"/>
<StackPanel Orientation="Horizontal" Spacing="5" HorizontalAlignment="Center">
<Button Command="{Binding OnOkClick}" Width="60" IsDefault="True">OK</Button>
<Button Command="{Binding OnCancelClick}" Width="60">Cancel</Button>
</StackPanel>
</StackPanel>
</Window>
43 changes: 43 additions & 0 deletions src/LogAlligator.App/Controls/Dialogs/EditHighlightDialog.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using LogAlligator.App.Utils;

namespace LogAlligator.App.Controls;

public partial class EditHighlightDialog : Window
{
Highlight? _highlightToEdit = null;

public EditHighlightDialog()
{
InitializeComponent();
}

public void Initialize(Highlight highlight)
{
_highlightToEdit = highlight;
HighlightPatternTextBox.Text = highlight.Pattern.Pattern.ToString();
}

public void OnEscape()
{
Close();
}

public void OnOkClick()
{
if (!string.IsNullOrEmpty(HighlightPatternTextBox.Text))
{
_highlightToEdit!.Pattern.Pattern = HighlightPatternTextBox.Text.AsMemory();
}

Close();
}

public void OnCancelClick()
{
Close();
}
}
27 changes: 26 additions & 1 deletion src/LogAlligator.App/Controls/HighlightsView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Avalonia.Controls;
using LogAlligator.App.Utils;
using Serilog;

namespace LogAlligator.App.Controls;

Expand Down Expand Up @@ -62,7 +63,7 @@ private List<ListBoxItem> PrepareListOfHighlights()

foreach (var highlight in _highlights ?? [])
{
var item = new ListBoxItem { Content = highlight.ToString(), ContextMenu = BuildContextMenu() };
var item = new ListBoxItem { Content = highlight.ToString(), DataContext = highlight, ContextMenu = BuildContextMenu() };
items.Add(item);
}

Expand All @@ -74,8 +75,32 @@ private ContextMenu BuildContextMenu()
var delete = new MenuItem { Header = "Remove" };
delete.Click += (_, _) => OnDelete();

var edit = new MenuItem { Header = "Edit" };
edit.Click += (_, _) => EditHighlight();

var contextMenu = new ContextMenu();
contextMenu.Items.Add(edit);
contextMenu.Items.Add(delete);
return contextMenu;
}

private async void EditHighlight()
{
try
{
if (ListOfHighlights.SelectedItem is null)
return;

Highlight selectedHighlight = (Highlight)((ListBoxItem)ListOfHighlights.SelectedItem).DataContext!;
var dialog = new EditHighlightDialog();
dialog.Initialize(selectedHighlight);
await dialog.ShowDialog((Window)VisualRoot!);

_highlights?.ForceRefresh();
}
catch (Exception)
{
Log.Warning("Exception caught from edit highlight dialog");
}
}
}
20 changes: 3 additions & 17 deletions src/LogAlligator.App/LogAlligator.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,18 @@
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.0-beta1" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.2.0-beta1" />

<PackageReference Include="MessageBox.Avalonia" Version="3.1.6" />
<PackageReference Include="Serilog" Version="4.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="LogAlligator.Tests"/>
<InternalsVisibleTo Include="LogAlligator.Tests" />
</ItemGroup>

<ItemGroup>
<AvaloniaResource Include="Assets\**"/>
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<Compile Update="Controls\Dialogs\BookmarkDialog.axaml.cs">
<DependentUpon>BookmarkDialog.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="Controls\Dialogs\LoadingDataDialog.axaml.cs">
<DependentUpon>LoadingDataDialog.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Update="Controls\Dialogs\GrepDialog.axaml.cs">
<DependentUpon>GrepDialog.axaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
</Project>
10 changes: 8 additions & 2 deletions src/LogAlligator.App/Utils/Highlights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace LogAlligator.App.Utils;

public record struct Highlight
public record class Highlight
{
private const double MINIMAL_CONTRAST_RATIO = 7.5;
public SearchPattern Pattern { get; set; }
public required SearchPattern Pattern { get; set; }
public Color Background { get; set; }

public bool HasEnoughContrastWith(Color color) => ContrastRatio(Background, color) >= MINIMAL_CONTRAST_RATIO;
Expand Down Expand Up @@ -81,6 +81,12 @@ public bool Contains(ReadOnlyMemory<char> pattern)
return _highlights.Any(h => h.Pattern.Equals(pattern));
}

// FIXME: Just use a proper data binding, please...
public void ForceRefresh()
{
OnChange?.Invoke(this, EventArgs.Empty);
}

public IEnumerator<Highlight> GetEnumerator()
{
return _highlights.GetEnumerator();
Expand Down

0 comments on commit 9e2768f

Please sign in to comment.