-
Notifications
You must be signed in to change notification settings - Fork 0
/
PickyDockItem.vala
335 lines (246 loc) · 6.86 KB
/
PickyDockItem.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using Plank;
using Cairo;
namespace Picky {
public class PickyDockItem : DockletItem {
Gtk.Clipboard clipboard;
Gee.ArrayList<Color?> colors;
int cur_position = 0;
ColorSpecType type = ColorSpecType.HEX;
GLib.File palette;
Gdk.Pixbuf icon_pixbuf;
bool swatch;
PickyPreferences prefs;
public PickyDockItem.with_dockitem_file(GLib.File file) {
GLib.Object(Prefs: new PickyPreferences.with_file(file));
}
construct {
Logger.initialize("picky");
Logger.DisplayLevel = LogLevel.NOTIFY;
prefs = (PickyPreferences) Prefs;
swatch = prefs.Swatch;
Icon = "resource://" + Picky.G_RESOURCE_PATH + "/icons/color_picker.png";
CountVisible = prefs.Count;
try {
icon_pixbuf = new Gdk.Pixbuf.from_resource(Picky.G_RESOURCE_PATH + "/icons/color_picker.png");
}
catch (Error e) {
warning("Error: " + e.message);
}
clipboard = Gtk.Clipboard.get(Gdk.Atom.intern("CLIPBOARD", true));
switch (prefs.Format) {
case "rgb":
type = ColorSpecType.RGB;
break;
case "x11name":
type = ColorSpecType.X11NAME;
break;
case "hex":
default:
type = ColorSpecType.HEX;
break;
}
colors = new Gee.ArrayList<Color?>();
var datadir_path = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S,
Environment.get_user_data_dir(), // .local/share
Environment.get_application_name(), // plank
"picky"
);
Logger.notification(datadir_path);
if (!FileUtils.test(datadir_path, FileTest.EXISTS)) {
Logger.notification("Dir %s does not exist, trying to create it".printf(datadir_path));
File datadir = File.new_for_path(datadir_path);
try {
datadir.make_directory();
}
catch(Error e) {
Logger.notification("Error: " + e.message);
}
}
var filepath = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, datadir_path, Picky.PALETTE_FILE);
palette = GLib.File.new_for_path(filepath);
load_palette();
updated();
}
~PickyDockItem () {
save_palette();
}
protected override void draw_icon(Plank.Surface surface) {
Cairo.Context ctx = surface.Context;
/* var pixbuf = color.get_pixbuf(); */
Gdk.Pixbuf pb = icon_pixbuf.scale_simple(surface.Width, surface.Height, Gdk.InterpType.BILINEAR);
Gdk.cairo_set_source_pixbuf(ctx, pb, 0, 0);
ctx.paint();
Color color;
if (!swatch || colors.size == 0) {
return;
}
else if (cur_position == 0 || cur_position > colors.size) {
color = colors.get(colors.size - 1);
}
else {
color = colors.get(cur_position - 1);
}
ctx.set_source_rgb(color.red, color.green, color.blue);
ctx.arc(surface.Width / 2, surface.Height / 2, surface.Width / 6, 0, 2 * Math.PI);
ctx.fill();
ctx.arc(surface.Width / 2, surface.Height / 2, (surface.Width / 6) + 1, 0, 2 * Math.PI);
ctx.set_source_rgb(255,255,255);
ctx.set_line_width(2);
ctx.set_tolerance(0.1);
ctx.stroke();
}
void updated() {
if (colors.size == 0) {
Text = "No colors picked yet..";
}
else if (cur_position == 0 || cur_position > colors.size) {
Text = get_entry_at(colors.size);
}
else {
Text = get_entry_at(cur_position);
}
Count = colors.size;
save_palette();
/* needs_redraw(); */
reset_icon_buffer();
}
protected override AnimationType on_scrolled(Gdk.ScrollDirection direction, Gdk.ModifierType mod, uint32 event_time) {
switch (direction) {
case Gdk.ScrollDirection.UP:
if (++cur_position >= colors.size) {
cur_position = 0;
}
break;
case Gdk.ScrollDirection.DOWN:
if (--cur_position < 0) {
cur_position = colors.size - 1;
}
break;
}
copy_entry_at(cur_position);
/* needs_redraw(); */
reset_icon_buffer();
return AnimationType.NONE;
}
void add_color(Color color) {
unowned PickyPreferences prefs = (PickyPreferences) Prefs;
if (has_color(color)) {
return;
}
colors.add(color);
while (colors.size > prefs.MaxEntries) {
colors.remove_at(0);
}
cur_position = colors.size;
updated();
}
bool has_color(Color color) {
foreach (Color col in colors) {
if (col.red == color.red && col.green == color.green && col.blue == color.blue) {
return true;
}
}
return false;
}
string get_entry_at(int pos) {
Color color = colors.get(pos - 1);
return color.get_string(type);
}
void copy_entry_at(int pos) {
if (pos < 1 || pos > colors.size) {
return;
}
var color = colors.get(pos - 1 );
string str = color.get_string(type);
clipboard.set_text(str, (int)str.length);
updated();
}
/* void copy_entry() { */
/* if (cur_position == 0) { */
/* copy_entry_at(colors.size); */
/* } */
/* else { */
/* copy_entry_at(cur_position); */
/* } */
/* } */
void clear() {
clipboard.set_text("", 0);
clipboard.clear();
colors.clear();
cur_position = 0;
updated();
}
protected override AnimationType on_clicked(PopupButton button, Gdk.ModifierType mod, uint32 event_time) {
if (button == PopupButton.LEFT) {
var picker_window = new Picky.PickerWindow(type, prefs.PreviewSize);
picker_window.picked.connect( (color) => {
add_color(color);
/* State = ItemState.URGENT; */
/* Logger.notification("Pcked a color: " + color.get_string(type)); */
});
picker_window.open_picker();
return AnimationType.BOUNCE;
}
return AnimationType.NONE;
}
public override Gee.ArrayList<Gtk.MenuItem> get_menu_items() {
var items = new Gee.ArrayList<Gtk.MenuItem>();
for (var i = colors.size; i > 0; i--) {
Color color = colors.get(i - 1);
var label = color.get_string(type);
if (type != ColorSpecType.X11NAME) {
label += " - %s".printf(color.to_x11name());
}
var item = create_menu_item_with_pixbuf(label, color.get_pixbuf(16), true);
var pos = i;
item.activate.connect( () => {
copy_entry_at(pos);
});
items.add(item);
}
if (colors.size > 0) {
var item = create_menu_item("_Clear", "edit-clear-all", true);
item.activate.connect(clear);
items.add(item);
}
return items;
}
protected bool load_palette() {
try {
if (!palette.query_exists()) {
return false;
}
string line;
var dis = new DataInputStream(palette.read());
while ((line = dis.read_line(null)) != null) {
var color = Color();
if (color.parse(line)) {
add_color(color);
}
}
}
catch (Error e) {
return false;
}
return true;
}
protected bool save_palette() {
try {
if (palette.query_exists()) {
palette.delete();
}
// Don't save empty palettes
if (colors.size > 0) {
var dos = new DataOutputStream(palette.create(FileCreateFlags.REPLACE_DESTINATION));
foreach (Color color in colors) {
dos.put_string(color.get_string(type) + "\n");
}
}
}
catch (Error e) {
return false;
}
return true;
}
}
}