Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for gnome-shell 3.8 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 49 additions & 53 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ const Main = imports.ui.main;
const Search = imports.ui.search;
const SearchDisplay = imports.ui.searchDisplay;
const IconGrid = imports.ui.iconGrid;
const Lang = imports.lang;
const GLib = imports.gi.GLib;

const MAX_SEARCH_RESULTS_ROWS = 1;
const ICON_SIZE = 81;

let unitProvider = "";

function CalcResult(result) {
this._init(result);
}
const UnitResult = new Lang.Class({
Name: 'UnitResult',

CalcResult.prototype = {
_init: function(resultMeta) {

this.actor = new St.Bin({ style_class: 'contact',
Expand Down Expand Up @@ -46,67 +45,64 @@ CalcResult.prototype = {

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);
result.set_width(400);
}

};

function UnitProvider() {
this._init.apply(this, arguments);
}
});

UnitProvider.prototype = {
__proto__: Search.SearchProvider.prototype,
const UnitProvider = new Lang.Class({
Name: 'UnitProvider',
Extends: Search.SearchProvider,

_init: function(title) {
Search.SearchProvider.prototype._init.call(this, title);
this.parent(title);
},

_tempRegex: /^(-?[0-9]+\.?[0-9]*)\s*([FCK]) ([FCK])$/i,

_isTempConversion: function(term1, term2) {
return this._tempRegex.test(term1.concat(" ", 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())];
},
_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();
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);
}
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}];
}
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, []);
this.searchSystem.pushResults(this, []);
return [];
},

Expand All @@ -115,16 +111,16 @@ UnitProvider.prototype = {
},

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)
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);
let result = new UnitResult(resultMeta);
return result.actor;
},

Expand All @@ -138,12 +134,12 @@ UnitProvider.prototype = {
},

activateResult: function(resultId) {
if(this._lastResult) {
St.Clipboard.get_default().set_text(this._lastResult.replace("\n", ""));
}
if(this._lastResult) {
St.Clipboard.get_default().set_text(this._lastResult.replace("\n", ""));
}
return true;
}
}
});

function init() {
unitProvider = new UnitProvider('UNIT CONVERTER');
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"shell-version": ["3.6"],
"shell-version": ["3.6", "3.8"],
"uuid": "[email protected]",
"name": "Unit Converter",
"description": "Convert units from the overview search. In the form \"x this to that\""
Expand Down