From 6983502c3468ba572f3bdd1ad368020517c9a7de Mon Sep 17 00:00:00 2001 From: GiraffeLars <203875086+GiraffeLars@users.noreply.github.com> Date: Mon, 9 Nov 2026 11:12:02 +0100 Subject: [PATCH 1/2] Fix CellStateChanged events firing when a cell was set to the value it already is --- src/NonoSharp/Grid.cs | 33 +++++++++++++------- src/NonoSharp/MoveManager.cs | 5 +++ src/NonoSharp/Nonogram.cs | 3 +- tests/NonoSharp.Tests/GridTests.cs | 16 ++++++++++ tests/NonoSharp.Tests/NonogramEventsTests.cs | 16 ++++++++++ 5 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/NonoSharp/Grid.cs b/src/NonoSharp/Grid.cs index c9e827b..8ac05d3 100644 --- a/src/NonoSharp/Grid.cs +++ b/src/NonoSharp/Grid.cs @@ -49,17 +49,31 @@ public void SetCell(int x, int y, CellType value) { ValidateInputCoordinates(x, y); - if (grid[x, y] != CellType.Filled && value == CellType.Filled) + if (ChangeCell(x, y, value)) + { + OnCellStateChanged(new([new(x, y)])); + } + } + + /// + /// Changes the cell at (x, y) to . + /// + /// true if the cell was changed, false otherwise. + private bool ChangeCell(int x, int y, CellType newType) + { + if (grid[x, y] == newType) return false; + + if (grid[x, y] != CellType.Filled && newType == CellType.Filled) { Filled++; // Keeps track of whether the same amount of cells are filled as the solution for efficiency } - else if (grid[x, y] == CellType.Filled && value != CellType.Filled) + else if (grid[x, y] == CellType.Filled && newType != CellType.Filled) { Filled--; } - grid[x, y] = value; - OnCellStateChanged(new([new(x, y)])); + grid[x, y] = newType; + return true; } /// @@ -308,13 +322,10 @@ internal LinkedList GetGroupsInColumn(int col) } set { - if (grid[x, y] == value) return; - if (grid[x, y] == CellType.Filled) Filled--; - - grid[x, y] = value; - - if (value == CellType.Filled) Filled++; - OnCellStateChanged(new([new(x, y)])); + if (ChangeCell(x, y, value)) + { + OnCellStateChanged(new([new(x, y)])); + } } } diff --git a/src/NonoSharp/MoveManager.cs b/src/NonoSharp/MoveManager.cs index 1de56bf..b276817 100644 --- a/src/NonoSharp/MoveManager.cs +++ b/src/NonoSharp/MoveManager.cs @@ -31,6 +31,11 @@ internal MoveManager(NonogramOptions? options, Puzzle puzzle) /// List of CellPositions of the cells that were changed. public List DoMove(int x, int y, CellType newType) { + if (puzzle.Grid.GetCell(x, y) == newType) + { + return []; + } + newType = DoAutoCorrect(x, y, newType); // Check if auto-crosses are possible (i.e. a cell goes to filled or from filled) bool mightAutoCross = newType == CellType.Filled || puzzle.Grid.GetCell(x, y) == CellType.Filled; diff --git a/src/NonoSharp/Nonogram.cs b/src/NonoSharp/Nonogram.cs index d44fc26..54833d8 100644 --- a/src/NonoSharp/Nonogram.cs +++ b/src/NonoSharp/Nonogram.cs @@ -317,7 +317,8 @@ public void EmptyCell(CellPosition position) public void SetCell(int x, int y, CellType newCellType) { var changes = moveManager.DoMove(x, y, newCellType); - OnCellStateChanged(new(changes)); + + if (changes.Count > 0) OnCellStateChanged(new(changes)); } /// diff --git a/tests/NonoSharp.Tests/GridTests.cs b/tests/NonoSharp.Tests/GridTests.cs index e488c5b..96d028e 100644 --- a/tests/NonoSharp.Tests/GridTests.cs +++ b/tests/NonoSharp.Tests/GridTests.cs @@ -101,5 +101,21 @@ public void TestGroups() Assert.Equal(1, groups.First()); Assert.Equal(2, groups.Last()); } + + [Fact] + public void TestSettingCellToSameStateDoesNotFireEvent() + { + Grid grid = new(1, 1); + bool eventFired = false; + grid.CellStateChanged += (s, e) => eventFired = true; + + foreach (CellType type in Enum.GetValues()) + { + grid.SetCell(0, 0, type); + eventFired = false; + grid.SetCell(0, 0, type); + Assert.False(eventFired); + } + } } } diff --git a/tests/NonoSharp.Tests/NonogramEventsTests.cs b/tests/NonoSharp.Tests/NonogramEventsTests.cs index dfd6e91..90da4bd 100644 --- a/tests/NonoSharp.Tests/NonogramEventsTests.cs +++ b/tests/NonoSharp.Tests/NonogramEventsTests.cs @@ -213,5 +213,21 @@ public void TestResetEvent() Assert.Equal(0, cells[0].X); Assert.Equal(0, cells[0].Y); } + + [Fact] + public void TestSettingCellToSameStateDoesNotFireEvent() + { + Nonogram nonogram = new(new Puzzle(1, 1, null)); + bool eventFired = false; + nonogram.CellStateChanged += (s, e) => eventFired = true; + + foreach (CellType type in Enum.GetValues()) + { + nonogram.SetCell(0, 0, type); + eventFired = false; + nonogram.SetCell(0, 0, type); + Assert.False(eventFired); + } + } } } From 412a7a810dca035d56add52ef796ef66d20c0129 Mon Sep 17 00:00:00 2001 From: GiraffeLars <203875086+GiraffeLars@users.noreply.github.com> Date: Mon, 9 Nov 2026 11:33:46 +0100 Subject: [PATCH 2/2] Fix CellStateChangedEvent firing when using SetRow and SetColumn in Grid.cs --- src/NonoSharp/Grid.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/NonoSharp/Grid.cs b/src/NonoSharp/Grid.cs index 8ac05d3..b36394c 100644 --- a/src/NonoSharp/Grid.cs +++ b/src/NonoSharp/Grid.cs @@ -213,13 +213,16 @@ internal void SetRow(int row, CellType[] newRow) List changed = []; for (int i = 0; i < Width; i++) { - if (newRow[i] != this[i, row]) + if (ChangeCell(i, row, newRow[i])) { - grid[i, row] = newRow[i]; changed.Add(new(i, row)); } } - OnCellStateChanged(new(changed)); + + if (changed.Count > 0) + { + OnCellStateChanged(new(changed)); + } } internal void SetColumn(int column, CellType[] newColumn) @@ -235,13 +238,16 @@ internal void SetColumn(int column, CellType[] newColumn) List changed = []; for (int i = 0; i < Height; i++) { - if (newColumn[i] != this[column, i]) + if (ChangeCell(column, i, newColumn[i])) { - grid[column, i] = newColumn[i]; changed.Add(new(column, i)); } } - OnCellStateChanged(new(changed)); + + if (changed.Count > 0) + { + OnCellStateChanged(new(changed)); + } } ///