Skip to content

Commit

Permalink
digit type; little clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Jul 2, 2011
1 parent 6c5e75a commit e017bd5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
72 changes: 35 additions & 37 deletions cpufreq-applet@yyc1992.gmail.com/extension.js
Expand Up @@ -83,8 +83,8 @@ function num_to_freq(num) {
return Math.round(num / 1000) / 1000 + 'GHz';
return Math.round(num / 1000000) / 1000 + 'THz';
}
function percent_to_hex(num) {
return Math.min(Math.floor(num * 256), 255);
function percent_to_hex(str, num) {
return str.format(Math.min(Math.floor(num * 256), 255)).replace(' ', '0');
}
function num_to_color(num, max) {
if (max !== undefined)
Expand All @@ -95,10 +95,10 @@ function num_to_color(num, max) {
return '#00FFFF';
num *= 3;
if (num >= 2)
return '#FF%2x00'.format(3 - num).replace(' ', '0');
return percent_to_hex('#FF%2x00', 3 - num);
if (num >= 1)
return '#%2xFF00'.format(num - 1).replace(' ', '0');
return '#00FF%2x'.format(1 - num).replace(' ', '0');
return percent_to_hex('#%2xFF00', num - 1);
return percent_to_hex('#00FF%2x', 1 - num);
}

//signal functions
Expand Down Expand Up @@ -128,6 +128,7 @@ Panel_Indicator.prototype = {
this.actor.remove_style_class_name('panel-button');
this.actor.add_style_class_name('cfs-panel-button');
this.parent = parent;
this.color = new Clutter.Color();
this.label = new St.Label({ text: name, style_class: 'cfs-label'});
this.digit = new St.Label({ style_class: 'cfs-panel-value' });
this.graph = new St.DrawingArea({reactive: false});
Expand All @@ -152,20 +153,26 @@ Panel_Indicator.prototype = {
this.box.add_actor(this.digit);
this.actor.add_actor(this.box);
this.add_menu_items();
this._onChange();
apply_settings.call(this, 'digit-type', function(sender, value) {
this.set_digit = value == 'frequency' ? function () {
this.digit.text = num_to_freq_panel(this.parent.cur_freq);
} : function () {
this.digit.text = Math.round(this.parent.cur_freq / this.parent.max * 100) + ' %';
};
this._onChange();
});
this.parent.connect('cur-changed', Lang.bind(this, this._onChange));
},
_draw: function() {
if ((this.graph.visible || this.box.visible) == false) return;
let [width, heigth] = this.graph.get_surface_size();
let cr = this.graph.get_context();
let value = this.parent.cur_freq / this.parent.max;
let color = new Clutter.Color();
color.from_string(num_to_color(value));
this.color.from_string(num_to_color(value));
Clutter.cairo_set_source_color(cr, Background);
cr.rectangle(0, 0, width, height);
cr.fill();
Clutter.cairo_set_source_color(cr, color);
Clutter.cairo_set_source_color(cr, this.color);
cr.rectangle(0, height * (1 - value), width, height);
cr.fill();
},
Expand All @@ -175,7 +182,7 @@ Panel_Indicator.prototype = {
let id = this.menu_items[i].id;
this.menu_items[i].setShowDot(this.parent['avail_' + type + 's'][id] == this.parent['cur_' + type]);
}
this.digit.text = num_to_freq_panel(this.parent.cur_freq);
this.set_digit();
this.graph.queue_repaint();
},
add_menu_items: function() {
Expand All @@ -187,9 +194,6 @@ Panel_Indicator.prototype = {
menu_item.type = 'freq';
menu_item.addActor(val_label);
this.menu.addMenuItem(menu_item);
menu_item.connect('activate', Lang.bind(this, function(item) {
this.parent.set_freq(item.id);
}));
this.menu_items.push(menu_item);
}
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
Expand All @@ -200,25 +204,27 @@ Panel_Indicator.prototype = {
menu_item.type = 'governor'
menu_item.addActor(val_label);
this.menu.addMenuItem(menu_item);
menu_item.connect('activate', Lang.bind(this, function(item) {
this.parent.set_governor(item.id);
}));
this.menu_items.push(menu_item);
}
for (let i in this.menu_items) {
this.menu_items[i].connect('activate', Lang.bind(this, function(item) {
this.parent.set(item.type ,item.id);
}));
}

}
};

function Cpufreq_Selector() {
function CpufreqSelectorBase() {
this._init.apply(this, arguments);
}
Cpufreq_Selector.prototype = {
CpufreqSelectorBase.prototype = {
arg: { governor: '-g', freq: '-f'},
_init: function(cpu) {
this.cpunum = cpu.replace(/cpu/, '');
this.cpufreq_path = cpu_path + '/' + cpu + '/cpufreq/';
this.get_freqs();
this.get_governors();
this.get_cur_freq();
this.get_cur_governor();
this.get_avail();
this.get_cur();
this.indicator = new Panel_Indicator(cpu, this);
apply_settings.call(this, 'refresh-time', function(sender, value) {
if ('timeout' in this)
Expand All @@ -227,40 +233,32 @@ Cpufreq_Selector.prototype = {
});
},

get_freqs: function() {
get_avail: function() {
this.max = rd_nums_frm_file(this.cpufreq_path + '/scaling_max_freq')[0];
this.min = rd_nums_frm_file(this.cpufreq_path + '/scaling_min_freq')[0];
this.avail_freqs = rd_nums_frm_file(this.cpufreq_path + '/scaling_available_frequencies');
},
get_governors: function() {
this.avail_governors = rd_frm_file(this.cpufreq_path + '/scaling_available_governors');
},

get_cur_freq: function() {
get_cur: function() {
this.cur_freq = rd_nums_frm_file(this.cpufreq_path + '/scaling_cur_freq')[0];
},
get_cur_governor: function() {
this.cur_governor = rd_frm_file(this.cpufreq_path + '/scaling_governor')[0];
},

set_freq: function(index) {
Util.spawn(['cpufreq-selector', '-c', this.cpunum.toString(), '-f', this.avail_freqs[index].toString()]);
},
set_governor: function(index) {
Util.spawn(['cpufreq-selector', '-c', this.cpunum.toString(), '-g', this.avail_governors[index]]);
set: function(type, index) {
Util.spawn(['cpufreq-selector', '-c', this.cpunum.toString(), this.arg[type], this['avail_' + type + 's'][index].toString()]);
},

update: function() {
let old_freq = this.cur_freq;
let old_governor = this.cur_governor;
this.get_cur_freq();
this.get_cur_governor();
this.get_cur();
if (old_freq != this.cur_freq || old_governor != this.cur_governor)
this.emit('cur-changed');
return true;
}
};
Signals.addSignalMethods(Cpufreq_Selector.prototype);
Signals.addSignalMethods(CpufreqSelectorBase.prototype);

Signals.addSignalMethods(this);

Expand All @@ -270,7 +268,7 @@ function add_cpus_frm_files(cpu_child) {
if (pattern.test(cpu_child[i].get_name()))
cpus.push(cpu_child[i].get_name());
for (let i = cpus.length - 1;i >= 0;i--) {
selectors[i] = new Cpufreq_Selector(cpus[i]);
selectors[i] = new CpufreqSelectorBase(cpus[i]);
box.add_actor(selectors[i].indicator.actor);
Main.panel._menus.addMenu(selectors[i].indicator.menu);
}
Expand Down
2 changes: 1 addition & 1 deletion org.gnome.shell.extensions.cpufreq.gschema.xml
Expand Up @@ -37,7 +37,7 @@
<key name="cpus-hidden" type="as">
<default>[]</default>
<summary>Cpus Hidden</summary>
<description>Names of cpus not shown</description>
<description>Names of cpus not shown, add "-1" to hide the summary one.</description>
</key>
</schema>
</schemalist>

0 comments on commit e017bd5

Please sign in to comment.