-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCommand.vala
208 lines (164 loc) · 5.55 KB
/
Command.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
/*
* Plotinus - A searchable command palette in every modern GTK+ application
*
* Copyright (c) 2016-2017 Philipp Emanuel Weidmann <pew@worldwidemann.com>
*
* Nemo vir est qui mundum non reddat meliorem.
*
* Released under the terms of the GNU General Public License, version 3
* (https://gnu.org/licenses/gpl.html)
*/
namespace Plotinus {
[DBus(name="com.worldwidemann.plotinus.Command")]
abstract class Command : Object {
private static int next_id = 0;
public int id { get; private set; }
public string[] path { get; private set; }
public string label { get; private set; }
public string[] accelerators { get; private set; }
protected Command(string[] path, string label, string[] accelerators) {
id = next_id++;
this.path = path;
this.label = label;
this.accelerators = accelerators;
}
public abstract void execute();
public virtual bool is_active() {
return false;
}
public virtual Gtk.ButtonRole get_check_type() {
return Gtk.ButtonRole.NORMAL;
}
[DBus(visible=false)]
public virtual bool set_image(Gtk.CellRendererPixbuf cell) {
return false;
}
protected bool set_image_from_widget(Gtk.CellRendererPixbuf cell, Gtk.Widget? widget) {
if(widget == null || !(widget is Gtk.Image))
return false;
var image = widget as Gtk.Image;
cell.stock_size = Gtk.IconSize.MENU;
switch(image.get_storage_type()) {
case Gtk.ImageType.PIXBUF:
cell.pixbuf = image.pixbuf;
return true;
case Gtk.ImageType.ICON_NAME:
cell.icon_name = image.icon_name;
return true;
case Gtk.ImageType.GICON:
cell.gicon = image.gicon;
return true;
case Gtk.ImageType.STOCK:
cell.stock_id = image.stock;
return true;
default:
return false;
}
}
}
class SignalCommand : Command {
public SignalCommand(string[] path, string label, string[] accelerators) {
base(path, label, accelerators);
}
public override void execute() {
executed();
}
public signal void executed();
}
class ActionCommand : Command {
private Action action;
private Variant? parameter;
private Variant? icon;
public ActionCommand(string[] path, string label, string[] accelerators, Action action, Variant? parameter, Variant? icon) {
base(path, label, accelerators);
this.action = action;
this.parameter = parameter;
this.icon = icon;
}
public override void execute() {
action.activate(parameter);
}
public override bool is_active() {
switch(get_check_type()) {
case Gtk.ButtonRole.RADIO:
return parameter.equal(action.get_state());
case Gtk.ButtonRole.CHECK:
return action.get_state().get_boolean();
default:
return false;
}
}
public override Gtk.ButtonRole get_check_type() {
if(parameter != null && action.get_state() != null)
return Gtk.ButtonRole.RADIO;
if(action.get_state_type() != null && VariantType.BOOLEAN.equal(action.get_state_type()))
return Gtk.ButtonRole.CHECK;
return Gtk.ButtonRole.NORMAL;
}
public override bool set_image(Gtk.CellRendererPixbuf cell) {
if(this.icon == null)
return base.set_image(cell);
var icon = Icon.deserialize(this.icon);
cell.gicon = icon;
return true;
}
}
class MenuItemCommand : Command {
private Gtk.MenuItem menu_item;
public MenuItemCommand(string[] path, string label, string[] accelerators, Gtk.MenuItem menu_item) {
base(path, label, accelerators);
this.menu_item = menu_item;
}
public override void execute() {
menu_item.activate();
}
public override bool is_active() {
if(get_check_type() != Gtk.ButtonRole.NORMAL)
return (menu_item as Gtk.CheckMenuItem).active;
return false;
}
public override Gtk.ButtonRole get_check_type() {
if(menu_item is Gtk.CheckMenuItem) {
var checkable = menu_item as Gtk.CheckMenuItem;
if((menu_item is Gtk.RadioMenuItem) || checkable.draw_as_radio)
return Gtk.ButtonRole.RADIO;
return Gtk.ButtonRole.CHECK;
}
return Gtk.ButtonRole.NORMAL;
}
public override bool set_image(Gtk.CellRendererPixbuf cell) {
if(!(menu_item is Gtk.ImageMenuItem))
return base.set_image(cell);
var image_menu_item = menu_item as Gtk.ImageMenuItem;
if(!(image_menu_item.always_show_image || Gtk.Settings.get_default().gtk_menu_images))
return base.set_image(cell);
var widget = image_menu_item.get_image();
return base.set_image_from_widget(cell, widget);
}
}
class ButtonCommand : Command {
private Gtk.Button button;
public ButtonCommand(string[] path, string label, string[] accelerators, Gtk.Button button) {
base(path, label, accelerators);
this.button = button;
}
public override void execute() {
button.clicked();
}
public override bool is_active() {
if(get_check_type() != Gtk.ButtonRole.NORMAL)
return (button as Gtk.CheckButton).active;
return false;
}
public override Gtk.ButtonRole get_check_type() {
if(button is Gtk.RadioButton)
return Gtk.ButtonRole.RADIO;
if(button is Gtk.CheckButton)
return Gtk.ButtonRole.CHECK;
return Gtk.ButtonRole.NORMAL;
}
public override bool set_image(Gtk.CellRendererPixbuf cell) {
return base.set_image_from_widget(cell, button.get_image());
}
}
}