using System.ComponentModel;
using System.Runtime.CompilerServices;
using XSDVisualiser.Core;
namespace XSDVisualiser.Desktop.ViewModels;
///
/// View model exposing the parsed schema for binding in the main window.
/// Provides selection state and information about the currently loaded XSD file.
///
public class MainWindowViewModel(SchemaModel model) : INotifyPropertyChanged
{
private string? _currentFilePath;
private SchemaModel _model = model ?? throw new ArgumentNullException(nameof(model));
private SchemaNode? _selectedNode;
private SchemaNode? _selectedRootElement;
///
/// Parsed schema model currently displayed by the UI.
/// Setting this property updates dependent properties and notifies bindings.
///
public SchemaModel Model
{
get => _model;
set
{
if (ReferenceEquals(_model, value)) return;
_model = value ?? throw new ArgumentNullException(nameof(value));
OnPropertyChanged();
OnPropertyChanged(nameof(RootElements));
OnPropertyChanged(nameof(VisibleRootElements));
}
}
///
/// All global elements defined in the loaded schema.
///
public IEnumerable RootElements => _model?.RootElements ?? Enumerable.Empty();
///
/// If set, filters the view to show only this root element and its subtree.
///
public SchemaNode? SelectedRootElement
{
get => _selectedRootElement;
set
{
if (EqualityComparer.Default.Equals(_selectedRootElement, value)) return;
_selectedRootElement = value;
OnPropertyChanged();
OnPropertyChanged(nameof(VisibleRootElements));
}
}
public SchemaNode? SelectedNode
{
get => _selectedNode;
set
{
if (EqualityComparer.Default.Equals(_selectedNode, value)) return;
_selectedNode = value;
OnPropertyChanged();
}
}
public IEnumerable VisibleRootElements =>
SelectedRootElement != null ? [SelectedRootElement] : RootElements;
public string? CurrentFilePath
{
get => _currentFilePath;
set
{
if (string.Equals(_currentFilePath, value, StringComparison.Ordinal)) return;
_currentFilePath = value;
OnPropertyChanged();
OnPropertyChanged(nameof(CurrentFileName));
OnPropertyChanged(nameof(CurrentDirectory));
}
}
public string? CurrentFileName =>
string.IsNullOrEmpty(_currentFilePath) ? null : Path.GetFileName(_currentFilePath);
public string? CurrentDirectory =>
string.IsNullOrEmpty(_currentFilePath) ? null : Path.GetDirectoryName(_currentFilePath);
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string? name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}