Skip to content

Commit f3dae6e

Browse files
committed
use grid to OpenLogWindow,associate logview height with window size
1 parent 14e57fa commit f3dae6e

9 files changed

+126
-37
lines changed

src/WEventViewer/ErrorWindow.axaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6+
MinHeight="300"
7+
MaxHeight="500"
8+
MaxWidth="1000"
69
xmlns:vm="using:WEventViewer.ViewModel"
710
x:Class="WEventViewer.ErrorWindow"
811
x:DataType="vm:ErrorWindowViewModel"
912
Title="ErrorWindow">
1013
<Design.DataContext>
1114
<vm:ErrorWindowViewModel></vm:ErrorWindowViewModel>
1215
</Design.DataContext>
13-
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
14-
<ScrollViewer AllowAutoHide="True" VerticalScrollBarVisibility="Auto">
16+
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" MinHeight="200">
17+
<ScrollViewer AllowAutoHide="True" VerticalScrollBarVisibility="Auto" Margin="10">
1518
<TextBlock Text="{Binding Message}" TextWrapping="WrapWithOverflow"/>
1619
</ScrollViewer>
17-
<Button Content="Close"/>
20+
<Button Content="Close" Margin="10"/>
1821
</StackPanel>
1922
</Window>

src/WEventViewer/MainWindow.axaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
66
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
77
x:Class="WEventViewer.MainWindow"
88
x:DataType="vm:MainWindowViewModel"
9+
MinHeight="300"
10+
MinWidth="400"
11+
Height="{Binding Mode=OneWayToSource,Path=CurrentWindowHeight}"
912
Title="WEventViewer">
1013
<Design.DataContext>
1114
<vm:MainWindowViewModel></vm:MainWindowViewModel>
1215
</Design.DataContext>
1316

14-
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
17+
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" MaxHeight="{Binding $parent.Bounds.Height}">
1518
<Menu DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="1">
1619
<MenuItem Header="_File">
1720
<MenuItem Header="_Open" Command="{Binding OpenCommand}"/>
1821
<MenuItem Header="_Close" Command="{Binding CloseCommand}"/>
1922
</MenuItem>
2023
<MenuItem Header="_About"/>
2124
</Menu>
22-
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="20">
25+
<ScrollViewer HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="20" MinHeight="200" MinWidth="200" Height="{Binding Mode=OneWay,Path=ScrollViewerHeight}" Name="LogViewer">
2326
<DataGrid
2427
BorderBrush="Black"
2528
Margin="20"
2629
BorderThickness="1"
27-
IsReadOnly="True" ItemsSource="{Binding LogRecords}" VerticalAlignment="Stretch" MaxHeight="500" MinHeight="200">
30+
IsReadOnly="True" ItemsSource="{Binding LogRecords}" VerticalAlignment="Stretch" MinHeight="200" Height="{Binding LogViewMaxHeight,Mode=OneWay}">
2831
<DataGrid.Columns>
2932
<DataGridTextColumn Header="TimeCreated" Binding="{Binding TimeCreated}"/>
3033
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
@@ -37,6 +40,8 @@
3740
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="20" VerticalAlignment="Bottom">
3841
<Label Content="{Binding LogCount,Mode=OneWay}"/>
3942
<Label Content="{Binding LoadStatus,Mode=OneWay}"/>
43+
<Label Content="{Binding #LogViewer.Bounds.Height}"/>
44+
<Label Content="{Binding ScrollViewerHeight}"/>
4045
</StackPanel>
4146
</StackPanel>
4247
</Window>

src/WEventViewer/MainWindow.axaml.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public MainWindow()
2020
DataContext = vm
2121
};
2222
var ret = await dlg.ShowDialog<bool>(this);
23-
if(DataContext is MainWindowViewModel mwvm)
23+
if(ret && DataContext is MainWindowViewModel mwvm)
2424
{
25-
WeakReferenceMessenger.Default.Send<LoadLogMessage>(new(vm.LogName, vm.PathType));
25+
WeakReferenceMessenger.Default.Send<LoadLogMessage>(new(vm.LogName, vm.CurrentSelected.PathType));
2626
}
2727
});
2828
WeakReferenceMessenger.Default.Register<MainWindow, OpenErrorLogWindow>(this, async (mw, msg) =>
@@ -33,4 +33,12 @@ public MainWindow()
3333
});
3434
WeakReferenceMessenger.Default.Register<MainWindow, MainWindowCloseMessage>(this, (w, msg) => w.Close());
3535
}
36+
37+
private void Window_SizeChanged(object? sender, Avalonia.Controls.SizeChangedEventArgs e)
38+
{
39+
if (DataContext is MainWindowViewModel mwvm)
40+
{
41+
mwvm.CurrentWindowHeight = this.Height;
42+
}
43+
}
3644
}

src/WEventViewer/Model/EventLogRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ static string GetOpCodeDisplayName(EventRecord record)
104104
}
105105
internal class EventLogRepository
106106
{
107-
ObservableCollection<LogRecord> records = [];
108-
public ObservableCollection<LogRecord> Records => records;
107+
RangedObservableCollection<LogRecord> records = [];
108+
public RangedObservableCollection<LogRecord> Records => records;
109109
public void Clear()
110110
{
111111
records.Clear();

src/WEventViewer/OpenLogWindow.axaml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
<Window.Resources>
1515
<vm:PathTypeValueConverter x:Key="PathTypeConverter"/>
1616
</Window.Resources>
17-
<StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="20">
18-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
17+
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="20">
18+
<Grid.RowDefinitions>
19+
<RowDefinition Height="*"></RowDefinition>
20+
<RowDefinition Height="*"></RowDefinition>
21+
<RowDefinition Height="*"></RowDefinition>
22+
<RowDefinition Height="Auto"></RowDefinition>
23+
</Grid.RowDefinitions>
24+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Grid.Row="0" VerticalAlignment="Center">
1925
<Label Content="LogName"
2026
Margin="10,10,10,10"
2127
HorizontalAlignment="Left"
@@ -27,22 +33,35 @@
2733
Margin="10,10,10,10"
2834
HorizontalAlignment="Right"
2935
Text="{Binding LogName, Mode=TwoWay}"
30-
MinWidth="200" MaxWidth="1000"/>
36+
MinWidth="200" MaxWidth="1000"
37+
/>
3138
</StackPanel>
32-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
39+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Grid.Row="1" VerticalAlignment="Center">
3340
<Label Content="PathType"
3441
Margin="10,10,10,10"
3542
HorizontalAlignment="Left"
3643
VerticalAlignment="Center"/>
37-
<TextBox Margin="10,10,10,10"
38-
HorizontalAlignment="Right"
44+
<!--<TextBox Margin="10,10,10,10"
45+
HorizontalAlignment="Stretch"
3946
TextAlignment="Left"
4047
VerticalContentAlignment="Center"
41-
Text="{Binding Path=PathType, Mode=TwoWay,Converter={StaticResource PathTypeConverter}}"/>
48+
Text="{Binding Path=PathType, Mode=TwoWay,Converter={StaticResource PathTypeConverter}}"/>-->
49+
<ComboBox
50+
HorizontalAlignment="Stretch"
51+
Margin="10"
52+
ItemsSource="{Binding PathTypes}"
53+
SelectedValue="{Binding CurrentSelected}"
54+
>
55+
<ComboBox.ItemTemplate>
56+
<DataTemplate>
57+
<Label Content="{Binding DisplayName}"/>
58+
</DataTemplate>
59+
</ComboBox.ItemTemplate>
60+
</ComboBox>
4261
</StackPanel>
43-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
62+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="2" VerticalAlignment="Bottom">
4463
<Button Content="OK" Command="{ Binding OkCommand }" Margin="10" Padding="10"/>
4564
<Button Content="Cancel" Command="{ Binding CancelCommand }" Margin="10" Padding="10"/>
4665
</StackPanel>
47-
</StackPanel>
66+
</Grid>
4867
</Window>

src/WEventViewer/OpenLogWindow.axaml.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,4 @@ public OpenLogWindow()
1616
this.Close(msg.isOk);
1717
});
1818
}
19-
20-
private void Binding(object? sender, Avalonia.Controls.SizeChangedEventArgs e)
21-
{
22-
}
2319
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Collections.Specialized;
8+
9+
namespace WEventViewer
10+
{
11+
public class RangedObservableCollection<T> : ObservableCollection<T>
12+
{
13+
public void AddRange(IEnumerable<T> collection)
14+
{
15+
foreach (var item in collection)
16+
{
17+
Items.Add(item);
18+
}
19+
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
20+
}
21+
}
22+
}

src/WEventViewer/ViewModel/MainWindowViewModel.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public MainWindowViewModel()
4545
});
4646
});
4747
_EventLogRepository = new EventLogRepository();
48-
//_EventLogRepository.Records.CollectionChanged;
4948
OpenCommand = new RelayCommand(() =>
5049
{
5150
LoadStatus = "Loading";
@@ -58,10 +57,7 @@ public MainWindowViewModel()
5857
{
5958
Dispatcher.UIThread.Invoke(() =>
6059
{
61-
foreach (var item in lst)
62-
{
63-
_EventLogRepository.Records.Add(item);
64-
}
60+
_EventLogRepository.Records.AddRange(lst);
6561
OnPropertyChanged(nameof(LogCount));
6662
OnPropertyChanged(nameof(LogRecords));
6763
});
@@ -70,7 +66,7 @@ public MainWindowViewModel()
7066
{
7167
if (t.IsFaulted)
7268
{
73-
WeakReferenceMessenger.Default.Send(new OpenErrorLogWindow(t.Exception.ToString()));
69+
Dispatcher.UIThread.Invoke(() => WeakReferenceMessenger.Default.Send(new OpenErrorLogWindow(t.Exception.ToString())));
7470
}
7571
Dispatcher.UIThread.Invoke(() =>
7672
{
@@ -80,12 +76,7 @@ public MainWindowViewModel()
8076
});
8177
});
8278
CloseCommand = new RelayCommand(() => WeakReferenceMessenger.Default.Send(new MainWindowCloseMessage()));
83-
}
84-
85-
private void Records_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
86-
{
87-
//OnPropertyChanged(nameof(LogCount));
88-
//OnPropertyChanged(nameof(LogRecords));
79+
LoadStatus = string.Empty;
8980
}
9081

9182
Progress<long> _Progress;
@@ -106,8 +97,22 @@ public long LogCount
10697
}
10798
}
10899
}
109-
public ObservableCollection<LogRecord> LogRecords => _EventLogRepository.Records;
100+
public RangedObservableCollection<LogRecord> LogRecords => _EventLogRepository.Records;
110101
public string LoadStatus { get; private set; }
102+
double _CurrentWindowHeight;
103+
public double CurrentWindowHeight
104+
{
105+
get => _CurrentWindowHeight;
106+
set
107+
{
108+
_CurrentWindowHeight = value;
109+
OnPropertyChanged(nameof(CurrentWindowHeight));
110+
OnPropertyChanged(nameof(ScrollViewerHeight));
111+
OnPropertyChanged(nameof(LogViewMaxHeight));
112+
}
113+
}
114+
public double LogViewMaxHeight => ScrollViewerHeight - 40;
115+
public double ScrollViewerHeight => CurrentWindowHeight - 160;
111116
}
112117
public class MyRecord(string a, int b)
113118
{

src/WEventViewer/ViewModel/OpenLogWindowViewModel.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using Avalonia.Data.Converters;
1+
using Avalonia.Controls;
2+
using Avalonia.Data.Converters;
23
using CommunityToolkit.Mvvm.Input;
34
using CommunityToolkit.Mvvm.Messaging;
45
using System;
56
using System.Collections.Generic;
67
using System.ComponentModel;
8+
using System.Diagnostics;
79
using System.Diagnostics.Eventing.Reader;
810
using System.Globalization;
911
using System.Linq;
@@ -51,8 +53,14 @@ internal partial class PathTypeValueConverter : IValueConverter
5153
throw new ArgumentException($"ConvertBack: unsupported type({value?.GetType()})");
5254
}
5355
}
56+
public class PathTypeDefinition(PathType pathType, string displayName)
57+
{
58+
public PathType PathType => pathType;
59+
public string DisplayName => displayName;
60+
}
5461
internal partial class OpenLogWindowViewModel : INotifyPropertyChanged
5562
{
63+
private readonly static DiagnosticSource _DS = new DiagnosticListener("WEventView.OpenLogWindowViewMode");
5664
string _LogName = "";
5765
public string LogName { get => _LogName; set
5866
{
@@ -70,6 +78,29 @@ public PathType PathType
7078
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PathType)));
7179
}
7280
}
81+
PathTypeDefinition _CurrentSelected = _PathTypes[0];
82+
public PathTypeDefinition CurrentSelected
83+
{
84+
get => _CurrentSelected;
85+
set
86+
{
87+
_CurrentSelected = value;
88+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentSelected)));
89+
}
90+
}
91+
92+
[RelayCommand]
93+
void OnPathTypeChanged(Avalonia.Controls.SelectionChangedEventArgs evargs)
94+
{
95+
_DS.Write("OnPathTypeChanged", evargs);
96+
}
97+
static readonly PathTypeDefinition[] _PathTypes = [new PathTypeDefinition(PathType.LogName, "LogName"), new PathTypeDefinition(PathType.FilePath, "FilePath")];
98+
public IList<PathTypeDefinition> PathTypes
99+
{
100+
get => _PathTypes;
101+
private set { }
102+
}
103+
public string[] Hoge => ["a", "b", "c"];
73104
public string A { get; set; } = "";
74105
public OpenLogWindowViewModel()
75106
{

0 commit comments

Comments
 (0)