forked from war1025/Unit-Converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
154 lines (123 loc) · 4.45 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
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
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 Lang = imports.lang;
const MAX_SEARCH_RESULTS_ROWS = 1;
const ICON_SIZE = 81;
let unitProvider = "";
const CalcResult = new Lang.Class({
Name: 'CalcResult',
_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_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-expression' });
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 });
result.set_width(400);
}
});
const UnitProvider = new Lang.Class({
Name: 'UnitProvider',
Extends: Search.SearchProvider,
_init: function(title) {
this.parent(title);
},
_tempRegex: /^(-?[0-9]+\.?[0-9]*)\s*([FCK]) ([FCK])$/i,
_isTempConversion: function(term1, term2) {
return this._tempRegex.test(term1.concat(" ", term2))
},
_tempConversionRewrite: function(term1, term2) {
var parts = this._tempRegex.exec(term1.concat(" ", term2))
return ["temp".concat(parts[2].toUpperCase(), "(", parts[1], ")"), "temp".concat(parts[3].toUpperCase())];
},
getInitialResultSet: function(terms) {
terms = terms.slice();
var valid = false;
var split = 0;
for(var i in terms) {
if(terms[i] == "to") {
valid = true;
split = i;
break;
}
}
if (valid) {
let one = terms.splice(0, split).join(" ");
let two = terms.splice(1).join(" ");
let expr = one.concat(" to ", two);
if(this._isTempConversion(one, two)) {
[one, two] = this._tempConversionRewrite(one, two);
}
try {
let [success, out, err, error] = GLib.spawn_sync(null, ["units", "-t", one, two], null, 4, null)
if(error == 0) {
result = out.toString();
this._lastResult = result;
this.searchSystem.pushResults(this,
[{'expr': expr, 'result': result}]);
return [{'expr': expr, 'result': result}];
}
} catch(exp) {
}
}
this.searchSystem.pushResults(this, []);
return [];
},
getSubsearchResultSet: function(prevResults, terms) {
return this.getInitialResultSet(terms);
},
getResultMetas: function(result, callback) {
let metas = [];
for(let i = 0; i < result.length; i++) {
metas.push({'id' : i, 'result' : result[i].result, 'expr' : result[i].expr});
}
callback(metas)
return metas;
},
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) {
if(this._lastResult) {
St.Clipboard.get_default().set_text(this._lastResult.replace("\n", ""));
}
return true;
}
});
function init() {
unitProvider = new UnitProvider('UNIT CONVERTER');
}
function enable() {
Main.overview.addSearchProvider(unitProvider);
}
function disable() {
Main.overview.removeSearchProvider(unitProvider);
}