forked from war1025/GCalcSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
129 lines (100 loc) · 3.61 KB
/
extension.js
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
const St = imports.gi.St;
const Main = imports.ui.main;
const Search = imports.ui.search;
const SearchDisplay = imports.ui.searchDisplay;
const IconGrid = imports.ui.iconGrid;
const GLib = imports.gi.GLib;
const MAX_SEARCH_RESULTS_ROWS = 1;
const ICON_SIZE = 81;
let calcProvider = "";
function CalcResult(result) {
this._init(result);
}
CalcResult.prototype = {
_init: function(resultMeta) {
this.actor = new St.Bin({ style_class: 'contact',
reactive: true,
track_hover: true });
let content = new St.BoxLayout( { style_class: 'contact-content',
vertical: false });
this.actor.set_child(content);
let icon = new St.Icon({ icon_type: St.IconType.FULLCOLOR,
icon_size: ICON_SIZE,
icon_name: 'accessories-calculator',
style_class: 'contact-icon' });
content.add(icon, { x_fill: true,
y_fill: false,
x_align: St.Align.START,
y_align: St.Align.MIDDLE });
let result = new St.BoxLayout({ style_class: 'contact-details',
vertical: true });
content.add(result, { x_fill: true, x_align: St.Align.START });
let exprLabel = new St.Label({ text: resultMeta.expr,
style_class: 'result-expr' });
let resultLabel = new St.Label({ text: resultMeta.result,
style_class: 'result-result' });
result.add(exprLabel, { x_fill: false, x_align: St.Align.START });
result.add(resultLabel, { x_fill: false, x_align: St.Align.START });
}
};
function CalcProvider() {
this._init.apply(this, arguments);
}
CalcProvider.prototype = {
__proto__: Search.SearchProvider.prototype,
_init: function(title) {
Search.SearchProvider.prototype._init.call(this, title);
},
_validExpression: function(expression) {
return /.*[0-9]+.*/.test(expression);
},
getInitialResultSet: function(terms) {
let expr = "";
for(let i=0; i<terms.length; i++) {
expr += terms[i];
}
if (this._validExpression(expr)) {
try {
let [success, out, err, error] = GLib.spawn_sync(null, ["gcalctool", "-s", expr], null, 4, null)
if(error == 0) {
return [{'expr': expr, 'result': out.toString()}];
}
} catch(exp) {
}
}
return [];
},
getSubsearchResultSet: function(prevResults, terms) {
return this.getInitialResultSet(terms);
},
getResultMeta: function(result) {
return {
'id': 0,
'result': result.result,
'expr': result.expr
};
},
createResultActor: function(resultMeta, terms) {
let result = new CalcResult(resultMeta);
return result.actor;
},
createResultContainerActor: function() {
let grid = new IconGrid.IconGrid({ rowLimit: MAX_SEARCH_RESULTS_ROWS,
xAlign: St.Align.START });
grid.actor.style_class = 'contact-grid';
let actor = new SearchDisplay.GridSearchResults(this, grid);
return actor;
},
activateResult: function(resultId) {
return true;
}
}
function init() {
calcProvider = new CalcProvider('CALCULATOR');
}
function enable() {
Main.overview.addSearchProvider(calcProvider);
}
function disable() {
Main.overview.removeSearchProvider(calcProvider);
}