public void ClearWithHistory_Removes_All_Items_Single_Item() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); target.Add(item0); Assert.Equal(1, target.Count); Assert.Equal(0, history.Undos.Count); Assert.Equal(0, history.Redos.Count); target.ClearWithHistory(history); Assert.Equal(0, target.Count); Assert.Equal(1, history.Undos.Count); Assert.Equal(0, history.Redos.Count); history.Undo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(0, history.Undos.Count); Assert.Equal(1, history.Redos.Count); history.Redo(); Assert.Equal(0, target.Count); Assert.Equal(1, history.Undos.Count); Assert.Equal(0, history.Redos.Count); }
public void Invoking_Redo_Should_Invoke_Redo_Action_And_Push_State_To_Undos() { int undoCount = 0; int redoCount = 0; var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { target.Snapshot(() => undoCount++, () => redoCount++); var undo1 = target.Undos.Peek(); var result1 = target.Undo(); var redo1 = target.Redos.Peek(); var result2 = target.Redo(); Assert.Single(target.Undos); Assert.Empty(target.Redos); Assert.True(result1); Assert.True(result2); Assert.Equal(new bool[] { true, false, true }, helper.CanUndos.ToArray()); Assert.Equal(new bool[] { true, false }, helper.CanRedos.ToArray()); Assert.Equal(new bool[] { true, true, true }, helper.CanClears.ToArray()); var undo2 = target.Undos.Peek(); Assert.Equal(undo1, undo2); Assert.Equal(undo1, redo1); Assert.Equal(1, undoCount); Assert.Equal(1, redoCount); Assert.True(result1); } }
public void ObserveWithHistory_Sets_CurrentValue() { var history = new StackHistory(); using (var subject = new Subject <int>()) { var target = new List <int>(); var initialValue = 10; using (subject.AsObservable().ObserveWithHistory(x => target.Add(x), currentValue: initialValue, history: history)) { subject.OnNext(initialValue); // empty -> 10 (the initial state of variable) subject.OnNext(2); // empty -> 10 -> 2 subject.OnNext(3); // empty -> 10 -> 2 -> 3 history.Undo(); // 3 -> 2 history.Undo(); // 2 -> 10 (finally restores initial state) Assert.Equal(0, history.Undos.Count); Assert.Equal(2, history.Redos.Count); Assert.Equal(new int[] { 2, 10 }, target); history.Redo(); // 10 -> 2 history.Redo(); // 2 -> 3 Assert.Equal(2, history.Undos.Count); Assert.Equal(0, history.Redos.Count); Assert.Equal(new int[] { 2, 10, 2, 3 }, target); } } }
public void ReplaceWithHistory_Replaces_Items_List_Middle() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); var item1 = new Item("item1"); var item2 = new Item("item2"); var item3 = new Item("item3"); var replace1 = new Item("replace1"); var replace2 = new Item("replace2"); target.Add(item0); target.Add(item1); target.Add(item2); target.Add(item3); Assert.Equal(4, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); Assert.Equal(item3, target[3]); target.ReplaceWithHistory(1, replace1, history); target.ReplaceWithHistory(2, replace2, history); Assert.Equal(4, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(replace1, target[1]); Assert.Equal(replace2, target[2]); Assert.Equal(item3, target[3]); history.Undo(); Assert.Equal(4, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(replace1, target[1]); Assert.Equal(item2, target[2]); Assert.Equal(item3, target[3]); history.Undo(); Assert.Equal(4, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); Assert.Equal(item3, target[3]); history.Redo(); Assert.Equal(4, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(replace1, target[1]); Assert.Equal(item2, target[2]); Assert.Equal(item3, target[3]); history.Redo(); Assert.Equal(4, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(replace1, target[1]); Assert.Equal(replace2, target[2]); Assert.Equal(item3, target[3]); }
public void RemoveWithHistory_Removes_Index_Item_List_Middle() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); var item1 = new Item("item1"); var item2 = new Item("item2"); target.Add(item0); target.Add(item1); target.Add(item2); Assert.Equal(3, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); target.RemoveWithHistory(1, history); Assert.Equal(2, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item2, target[1]); target.RemoveWithHistory(1, history); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); target.RemoveWithHistory(0, history); Assert.Equal(0, target.Count); history.Undo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); history.Undo(); Assert.Equal(2, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item2, target[1]); history.Undo(); Assert.Equal(3, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); history.Redo(); Assert.Equal(2, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item2, target[1]); history.Redo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); history.Redo(); Assert.Equal(0, target.Count); }
public void ObserveWithHistory_Skips_First_Value() { var target = new StackHistory(); using (var subject = new Subject <int>()) using (subject.AsObservable().ObserveWithHistory(x => { }, 0, target)) { subject.OnNext(1); Assert.Equal(0, target.Undos.Count); } }
public void Invoking_Redo_Should_Not_Throw_When_Redos_Are_Empty() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { var result = target.Redo(); Assert.False(result); Assert.Equal(new bool[] { }, helper.CanUndos.ToArray()); Assert.Equal(new bool[] { }, helper.CanRedos.ToArray()); Assert.Equal(new bool[] { }, helper.CanClears.ToArray()); } }
public void ObserveWithHistory_Creates_History_Snapshot() { var target = new StackHistory(); using (var subject = new Subject <int>()) using (subject.AsObservable().ObserveWithHistory(x => { }, 0, target)) { subject.OnNext(1); subject.OnNext(2); subject.OnNext(3); Assert.Equal(2, target.Undos.Count); } }
public void First_Snapshot_Should_Push_One_Undo_State() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { target.Snapshot(() => { }, () => { }); Assert.Single(target.Undos); Assert.Empty(target.Redos); Assert.Equal(new bool[] { true }, helper.CanUndos.ToArray()); Assert.Equal(new bool[] { }, helper.CanRedos.ToArray()); Assert.Equal(new bool[] { true }, helper.CanClears.ToArray()); } }
public void ClearWithHistory_Does_Nothing_Empty_List() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); Assert.Equal(0, target.Count); Assert.Equal(0, history.Undos.Count); Assert.Equal(0, history.Redos.Count); target.ClearWithHistory(history); Assert.Equal(0, target.Count); Assert.Equal(0, history.Undos.Count); Assert.Equal(0, history.Redos.Count); }
/// <summary> /// 保存棋谱 /// </summary> public static void SaveQP() { try { //保存棋谱 string tempstr = ""; Array arr = StackHistory.ToArray(); if (arr.Length > 0) { for (int i = arr.Length - 1; i >= 0; i--) { tempstr += arr.GetValue(i).ToString() + "\r\n"; } tempstr += "CreateBy [email protected] http://www.singoo.top \r\n"; tempstr += System.DateTime.Now.ToString(); SaveFileDialog sd = new SaveFileDialog(); sd.Filter = "文本文件(*.txt)|*.txt"; string saveFolder = Path.Combine(System.Environment.CurrentDirectory, "QiPu"); if (!Directory.Exists(saveFolder)) { Directory.CreateDirectory(saveFolder); } sd.InitialDirectory = saveFolder; //棋谱的默认保存目录 sd.FileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; //sd.RestoreDirectory = true; if (sd.ShowDialog() == DialogResult.OK) { //保存文件 FileStream fs = new FileStream(sd.FileName, FileMode.Create, FileAccess.ReadWrite); StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("gb2312")); sw.Write(tempstr); sw.Flush(); sw.Close(); fs.Close(); } } else { MessageBox.Show("没有可用棋谱", "温馨提示", MessageBoxButtons.OK); } } catch { MessageBox.Show("程序错误,保存失败", "温馨提示", MessageBoxButtons.OK); } }
private void InitNotify(string title, string url) { gridSubtitle.Children.Clear(); NowSubtitle = title; NowCaption = ""; NowContainer = null; if (StackHistory == null) { StackHistory = new Stack <Pair>(); } StackHistory.Clear(); RefreshSubtitle("Anime", new Pair(title, url)); }
public void Undos_And_Redos_Shuould_Be_Initialized() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { Assert.NotNull(target.Undos); Assert.NotNull(target.Redos); Assert.Empty(target.Undos); Assert.Empty(target.Redos); Assert.False(target.IsPaused); Assert.Equal(new bool[] { }, helper.CanUndos.ToArray()); Assert.Equal(new bool[] { }, helper.CanRedos.ToArray()); Assert.Equal(new bool[] { }, helper.CanClears.ToArray()); } }
public void Undo_Sets_IsPaused_True_While_Invoking_Undo_Redo_State() { var target = new StackHistory(); target.Snapshot( undo: () => Assert.True(target.IsPaused), redo: () => Assert.True(target.IsPaused)); Assert.False(target.IsPaused); target.Undo(); Assert.False(target.IsPaused); Assert.False(target.IsPaused); target.Redo(); Assert.False(target.IsPaused); }
public void InsertWithHistory_Inserts_Item_Empty_List() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); target.InsertWithHistory(0, item0, history); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); history.Undo(); Assert.Equal(0, target.Count); history.Redo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); }
public void AddWithHistory_Adds_Item_Empty_List() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); target.AddWithHistory(item0, history); Assert.Single(target); Assert.Equal(item0, target[0]); history.Undo(); Assert.Empty(target); history.Redo(); Assert.Single(target); Assert.Equal(item0, target[0]); }
private void InitSubtitle(string title) { int week = Data.DictSeason[title].Week; gridSubtitle.Children.Clear(); NowSubtitle = title; NowCaption = ""; NowContainer = null; if (StackHistory == null) { StackHistory = new Stack <Pair>(); } StackHistory.Clear(); RefreshSubtitle("Weekdata", week.ToString()); }
public void AddWithHistory_Adds_Items_As_Last() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); var item1 = new Item("item1"); var item2 = new Item("item2"); target.AddWithHistory(item0, history); target.AddWithHistory(item1, history); target.AddWithHistory(item2, history); Assert.Equal(3, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); history.Undo(); Assert.Equal(2, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); history.Undo(); Assert.Single(target); Assert.Equal(item0, target[0]); history.Undo(); Assert.Empty(target); history.Redo(); Assert.Single(target); Assert.Equal(item0, target[0]); history.Redo(); Assert.Equal(2, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); history.Redo(); Assert.Equal(3, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); }
public void InsertWithHistory_Inserts_Items_List_Head() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); var item1 = new Item("item1"); var item2 = new Item("item2"); target.InsertWithHistory(0, item0, history); target.InsertWithHistory(0, item1, history); target.InsertWithHistory(0, item2, history); Assert.Equal(3, target.Count); Assert.Equal(item2, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item0, target[2]); history.Undo(); Assert.Equal(2, target.Count); Assert.Equal(item1, target[0]); Assert.Equal(item0, target[1]); history.Undo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); history.Undo(); Assert.Equal(0, target.Count); history.Redo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); history.Redo(); Assert.Equal(2, target.Count); Assert.Equal(item1, target[0]); Assert.Equal(item0, target[1]); history.Redo(); Assert.Equal(3, target.Count); Assert.Equal(item2, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item0, target[2]); }
public void RemoveWithHistory_Removes_Index_Empty_List() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); target.Add(item0); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); target.RemoveWithHistory(0, history); Assert.Equal(0, target.Count); history.Undo(); Assert.Equal(1, target.Count); Assert.Equal(item0, target[0]); history.Redo(); Assert.Equal(0, target.Count); }
public void ObserveWithHistory_Does_Not_Create_History_Snapshot_When_IsPaused_True() { var target = new StackHistory(); using (var subject = new Subject <int>()) using (subject.AsObservable().ObserveWithHistory(x => { }, 0, target)) { subject.OnNext(1); subject.OnNext(2); target.IsPaused = true; subject.OnNext(3); subject.OnNext(4); target.IsPaused = false; subject.OnNext(5); subject.OnNext(6); Assert.Equal(3, target.Undos.Count); } }
public void Dispose_Should_Release_Allocated_Resources() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { target.Snapshot(() => { }, () => { }); target.Snapshot(() => { }, () => { }); var result = target.Undo(); Assert.Equal(1, target.Undos.Count); Assert.Equal(1, target.Redos.Count); Assert.Equal(true, result); target.Dispose(); Assert.Null(target.Undos); Assert.Null(target.Redos); Assert.Throws(typeof(ObjectDisposedException), () => target.CanUndo.Subscribe(_ => { })); Assert.Throws(typeof(ObjectDisposedException), () => target.CanRedo.Subscribe(_ => { })); Assert.Throws(typeof(ObjectDisposedException), () => target.CanClear.Subscribe(_ => { })); } }
public BaseHistoryViewModel() { Disposables = new CompositeDisposable(); var history = new StackHistory().AddTo(Disposables); History = history; var undo = ReactiveCommand.Create(Undo, History.CanUndo); undo.Subscribe().DisposeWith(this.Disposables); UndoCommand = undo; var redo = ReactiveCommand.Create(Redo, History.CanRedo); redo.Subscribe().DisposeWith(this.Disposables); RedoCommand = redo; var clear = ReactiveCommand.Create(History.Clear, History.CanClear); clear.Subscribe().DisposeWith(this.Disposables); ClearHistoryCommand = clear; }
public HistoryViewModel() { Disposable = new CompositeDisposable(); var history = new StackHistory().AddTo(Disposable); History = history; var undo = new ReactiveCommand(History.CanUndo, false); undo.Subscribe(_ => Undo()).AddTo(this.Disposable); UndoCommand = undo; var redo = new ReactiveCommand(History.CanRedo, false); redo.Subscribe(_ => Redo()).AddTo(this.Disposable); RedoCommand = redo; var clear = new ReactiveCommand(History.CanClear, false); clear.Subscribe(_ => History.Clear()).AddTo(this.Disposable); ClearCommand = clear; }
public void Snapshot_Should_Clear_Redos() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { target.Snapshot(() => { }, () => { }); Assert.Single(target.Undos); Assert.Empty(target.Redos); var result = target.Undo(); Assert.Empty(target.Undos); Assert.Single(target.Redos); Assert.True(result); target.Snapshot(() => { }, () => { }); Assert.Single(target.Undos); Assert.Empty(target.Redos); Assert.Equal(new bool[] { true, false, true }, helper.CanUndos.ToArray()); Assert.Equal(new bool[] { true, false }, helper.CanRedos.ToArray()); Assert.Equal(new bool[] { true, true, true }, helper.CanClears.ToArray()); } }
public void Dispose_Should_Release_Allocated_Resources() { var target = new StackHistory(); using (var helper = new HistoryHelper(target)) { target.Snapshot(() => { }, () => { }); target.Snapshot(() => { }, () => { }); var result = target.Undo(); Assert.Single(target.Undos); Assert.Single(target.Redos); Assert.True(result); target.Dispose(); Assert.Empty(target.Undos); Assert.Empty(target.Redos); Assert.Throws <ObjectDisposedException>(() => target.CanUndo.Subscribe(_ => { })); Assert.Throws <ObjectDisposedException>(() => target.CanRedo.Subscribe(_ => { })); Assert.Throws <ObjectDisposedException>(() => target.CanClear.Subscribe(_ => { })); } }
public void ReplaceWithHistory_Replaces_Item_Empty_List() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); var item1 = new Item("item1"); target.Add(item0); Assert.Single(target); Assert.Equal(item0, target[0]); target.ReplaceWithHistory(0, item1, history); Assert.Single(target); Assert.Equal(item1, target[0]); history.Undo(); Assert.Single(target); Assert.Equal(item0, target[0]); history.Redo(); Assert.Single(target); Assert.Equal(item1, target[0]); }
public LineShapeViewModel(LineShape line, IHistory history) { Disposable = new CompositeDisposable(); var lineHistoryScope = new StackHistory().AddTo(this.Disposable); this.Name = line.ToReactivePropertyAsSynchronized(l => l.Name) .SetValidateNotifyError(name => string.IsNullOrWhiteSpace(name) ? "Name can not be null or whitespace." : null) .AddTo(this.Disposable); var startInitialValue = new PointShapeViewModel(line.Start, lineHistoryScope).AddTo(this.Disposable); this.Start = new ReactiveProperty <PointShapeViewModel>(startInitialValue) .SetValidateNotifyError(start => start == null ? "Point can not be null." : null) .AddTo(this.Disposable); var endInitialValue = new PointShapeViewModel(line.End, lineHistoryScope).AddTo(this.Disposable); this.End = new ReactiveProperty <PointShapeViewModel>(endInitialValue) .SetValidateNotifyError(end => end == null ? "Point can not be null." : null) .AddTo(this.Disposable); this.Name.ObserveWithHistory(name => line.Name = name, line.Name, lineHistoryScope).AddTo(this.Disposable); this.DeleteCommand = new ReactiveCommand(); this.DeleteCommand.Subscribe((x) => Delete(line, history)).AddTo(this.Disposable); UndoCommand = new ReactiveCommand(lineHistoryScope.CanUndo, false); UndoCommand.Subscribe(_ => lineHistoryScope.Undo()).AddTo(this.Disposable); RedoCommand = new ReactiveCommand(lineHistoryScope.CanRedo, false); RedoCommand.Subscribe(_ => lineHistoryScope.Redo()).AddTo(this.Disposable); ClearCommand = new ReactiveCommand(lineHistoryScope.CanClear, false); ClearCommand.Subscribe(_ => lineHistoryScope.Clear()).AddTo(this.Disposable); }
public void ClearWithHistory_Removes_All_Items_Multiple_Item() { var history = new StackHistory(); var target = new ObservableCollection <Item>(); var item0 = new Item("item0"); var item1 = new Item("item1"); var item2 = new Item("item2"); target.Add(item0); target.Add(item1); target.Add(item2); Assert.Equal(3, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); Assert.Empty(history.Undos); Assert.Empty(history.Redos); target.ClearWithHistory(history); Assert.Empty(target); Assert.Single(history.Undos); Assert.Empty(history.Redos); history.Undo(); Assert.Equal(3, target.Count); Assert.Equal(item0, target[0]); Assert.Equal(item1, target[1]); Assert.Equal(item2, target[2]); Assert.Empty(history.Undos); Assert.Single(history.Redos); history.Redo(); Assert.Empty(target); Assert.Single(history.Undos); Assert.Empty(history.Redos); }
public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime) { var disposable = new CompositeDisposable(); // Model object owner = new object(); var layer1 = new Layer(owner, "layer1"); var start11 = new PointShape(owner, "start11", 100, 100); var end11 = new PointShape(owner, "end11", 200, 100); var line1 = new LineShape(layer1, "line1", start11, end11); start11.Owner = line1; end11.Owner = line1; layer1.Shapes.Add(line1); var start21 = new PointShape(owner, "start21", 100, 200); var end21 = new PointShape(owner, "end21", 200, 200); var line2 = new LineShape(layer1, "line2", start21, end21); start21.Owner = line2; line2.Owner = line2; layer1.Shapes.Add(line2); // ViewModel var history = new StackHistory().AddTo(disposable); var layerViewModel = new LayerViewModel(layer1, history).AddTo(disposable); // Window var mainWindow = new MainWindow() { DataContext = layerViewModel }; var layerCanvas = mainWindow.FindControl <LayerCanvas>("layerCanvas"); LineShape?line = null; layerCanvas.PointerPressed += (sender, e) => { if (e.GetCurrentPoint(layerCanvas).Properties.IsLeftButtonPressed) { var point = e.GetPosition(layerCanvas); if (line == null) { var start = new PointShape(layer1.Owner, "start", point.X, point.Y); var end = new PointShape(layer1.Owner, "end", point.X, point.Y); line = new LineShape(layer1, "line", start, end); line.Start.Owner = line; line.End.Owner = line; //layer1.Shapes.AddWithHistory(line, history); layer1.Shapes.Add(line); //history.IsPaused = true; layerCanvas.InvalidateVisual(); } else { line.End.X = point.X; line.End.Y = point.Y; layer1.Shapes.Remove(line); layer1.Shapes.AddWithHistory(line, history); //history.IsPaused = false; line = null; layerCanvas.InvalidateVisual(); } } else if (e.GetCurrentPoint(layerCanvas).Properties.IsRightButtonPressed) { if (line != null) { layer1.Shapes.Remove(line); line = null; layerCanvas.InvalidateVisual(); } } }; layerCanvas.PointerMoved += (sender, args) => { var point = args.GetPosition(layerCanvas); if (line != null) { line.End.X = point.X; line.End.Y = point.Y; layerCanvas.InvalidateVisual(); } }; history.CanClear.Subscribe(_ => layerCanvas.InvalidateVisual()).AddTo(disposable); desktopLifetime.MainWindow = mainWindow; desktopLifetime.Exit += (sennder, e) => { disposable.Dispose(); }; } else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewLifetime) { //singleViewLifetime.MainView = new MainView(); } base.OnFrameworkInitializationCompleted(); }