Skip to content

Commit 63ffa15

Browse files
committed
implement: Don't add the same color to color history twice #24
1 parent 3de7a37 commit 63ffa15

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

dev-build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sudo rm -r build
2+
meson build
3+
cd build
4+
mesonconf -Dprefix=/usr
5+
ninja
6+
./com.github.ronnydo.colorpicker
7+
cd ..

src/Widgets/ColorHistory.vala

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,29 @@ namespace ColorPicker.Widgets {
8787
}
8888

8989
public void add_color (Gdk.RGBA color) {
90-
if (color_list.size >= max_colors) {
91-
remove_oldest_color ();
92-
}
90+
int c_index = get_index_of (color);
91+
92+
if (c_index < 0) {
93+
if (color_list.size >= max_colors) {
94+
color_list.remove_at (0);
95+
}
96+
} else {
97+
color_list.remove_at (c_index);
98+
}
99+
93100
color_list.add (color);
94101
store_colors ();
95-
96102
selected_color = get_color_list ().size - 1;
97103
}
98104

99-
public void remove_oldest_color () {
100-
color_list.remove_at (0);
101-
store_colors ();
102-
}
105+
public int get_index_of (Gdk.RGBA color) {
106+
for (int i = 0; i < color_list.size; i++) {
107+
if (color_list[i] == color) {
108+
return i;
109+
}
110+
}
111+
return -1;
112+
}
103113

104114
public Gee.ArrayList<Gdk.RGBA?> get_color_list () {
105115
return color_list;

0 commit comments

Comments
 (0)