const Desklet = imports.ui.desklet; const Settings = imports.ui.settings; const Mainloop = imports.mainloop; const Lang = imports.lang; const Clutter = imports.gi.Clutter; const Cinnamon = imports.gi.Cinnamon; const Gio = imports.gi.Gio; const Cairo = imports.cairo; const St = imports.gi.St; function MyDesklet(metadata, desklet_id) { this._init(metadata, desklet_id); } function main(metadata, desklet_id) { return new MyDesklet(metadata, desklet_id); } MyDesklet.prototype = { __proto__: Desklet.Desklet.prototype, _init: function(metadata, desklet_id) { Desklet.Desklet.prototype._init.call(this, metadata, desklet_id); // initialize settings this.settings = new Settings.DeskletSettings(this, this.metadata["uuid"], desklet_id); this.settings.bindProperty(Settings.BindingDirection.IN, "type", "type", this.on_setting_changed); this.settings.bindProperty(Settings.BindingDirection.IN, "refresh-interval", "refresh_interval", this.on_setting_changed); this.settings.bindProperty(Settings.BindingDirection.IN, "line-color", "line_color", this.on_setting_changed); // initialize desklet GUI this.setupUI(); }, setupUI: function(){ // initialize size this.canvas = new Clutter.Actor(); this.canvas.remove_all_children(); this.text1 = new St.Label(); this.text2 = new St.Label(); this.canvas.add_actor(this.text1); this.canvas.add_actor(this.text2); this.n_values = 30 + 1; this.values = new Array(this.n_values).fill(0.0); this.setContent(this.canvas); // set decoration settings // flag to indicate the first loop of the Desklet this.first_run = true; // update loop for Desklet this.update(); }, update: function() { // Desklet proportions let unit_size = 15; // pixels var margin_up = 3 * unit_size; var graph_w = 20 * unit_size; var graph_h = 4 * unit_size; let desklet_w = graph_w + (2 * unit_size); let desklet_h = graph_h + (4 * unit_size); // let desklet_w = 330; // pixels // let desklet_h = 120; // pixels // let size_margin = 15; // pixels // var graph_w = desklet_w - (2 * size_margin); //var graph_h = desklet_h - (4 * size_margin); let graph_step = 2 * unit_size / 3; // this depends on the interval, and refresh let text1_size = 5 * unit_size / 3; let text2_size = 4 * unit_size / 3; let n_values = this.n_values; let values = this.values; var value = 0.0; var text1 = ''; var text2 = ''; var line_r; var line_g; var line_b; // current values switch (this.type) { case "cpu": // CPU usage https://rosettacode.org/wiki/Linux_CPU_utilization text1 = 'CPU'; let cpu_values = this.get_cpu_times(); let cpu_tot = cpu_values[0]; let cpu_idl = cpu_values[1]; if (this.first_run){ this.first_run = false; this.cpu_tot = cpu_tot; this.cpu_idl = cpu_idl; } else { let cpu_use = 100 * (1 - (cpu_idl - this.cpu_idl) / (cpu_tot - this.cpu_tot)); this.cpu_tot = cpu_tot; this.cpu_idl = cpu_idl; value = cpu_use / 100; text2 = Math.round(cpu_use).toString() + "%"; } break; case "ram": value = 12; this.text1.set_text(type); this.text2.set_text(value.toString()); global.log("update ram"); break; } // concatenate new value values.push(value); values.shift(); this.values = values; // line_color let line_colors = this.line_color.match(/\((.*?)\)/)[1].split(","); // get contents inside brackets: "rgb(...)" line_r = parseInt(line_colors[0])/255; line_g = parseInt(line_colors[1])/255; line_b = parseInt(line_colors[2])/255; let canvas = new Clutter.Canvas(); canvas.set_size(desklet_w, desklet_h); canvas.connect('draw', function (canvas, ctx, desklet_w, desklet_h) { ctx.save(); ctx.setOperator(Cairo.Operator.CLEAR); ctx.paint(); ctx.restore(); ctx.setOperator(Cairo.Operator.OVER); ctx.setLineWidth(2); // desklet background let radius = 10; let degrees = Math.PI / 180.0; ctx.newSubPath(); ctx.arc(desklet_w - radius, radius, radius, -90 * degrees, 0 * degrees); ctx.arc(desklet_w - radius, desklet_h - radius, radius, 0 * degrees, 90 * degrees); ctx.arc(radius, desklet_h - radius, radius, 90 * degrees, 180 * degrees); ctx.arc(radius, radius, radius, 180 * degrees, 270 * degrees); ctx.closePath(); ctx.setSourceRGB(0.2, 0.2, 0.2); ctx.fill(); // graph border ctx.setSourceRGBA(line_r, line_g, line_b, 1); ctx.rectangle(unit_size, margin_up, graph_w, graph_h); ctx.stroke(); // graph midlines ctx.setSourceRGBA(line_r, line_g, line_b, 0.2); let n_midlines = 4; for (let i = 1; i