RGBController/RGBController2/ViewModels/MainViewModel.cs

57 lines
1.6 KiB
C#
Raw Normal View History

2020-10-25 19:52:14 +00:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Text;
using System.Windows.Input;
using RGBController2.Commands;
using RGBController2.ViewModels.Tabs;
namespace RGBController2.ViewModels
{
class MainViewModel
{
private readonly ObservableCollection<ITab> _tabs;
public ICommand NewTabCommand { get; }
2020-10-26 17:20:01 +00:00
public static ICollection<ITab> Tabs { get; set; }
2020-10-25 19:52:14 +00:00
public MainViewModel()
{
NewTabCommand = new ActionCommand(p => NewTab());
_tabs = new ObservableCollection<ITab>();
_tabs.CollectionChanged += Tabs_CollectionChanged;
Tabs = _tabs;
}
private void Tabs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
ITab tab;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
tab = (ITab) e.NewItems[0];
tab.CloseRequested += OnTabCloseRequested;
break;
case NotifyCollectionChangedAction.Remove:
tab = (ITab) e.OldItems[0];
tab.CloseRequested -= OnTabCloseRequested;
break;
}
}
private void OnTabCloseRequested(object sender, EventArgs e)
{
Tabs.Remove((ITab) sender);
}
private void NewTab()
{
Tabs.Add(new ArduinoTab(DateTime.UtcNow.ToString()));
}
}
}