From e017bd565cce9088732501acfaecb840073c6eaf Mon Sep 17 00:00:00 2001 From: Yichao Yu Date: Fri, 1 Jul 2011 23:31:53 -0400 Subject: [PATCH] digit type; little clean up --- cpufreq-applet@yyc1992.gmail.com/extension.js | 72 +++++++++---------- ...gnome.shell.extensions.cpufreq.gschema.xml | 2 +- 2 files changed, 36 insertions(+), 38 deletions(-) diff --git a/cpufreq-applet@yyc1992.gmail.com/extension.js b/cpufreq-applet@yyc1992.gmail.com/extension.js index 1ee5c9b..96f1ce9 100644 --- a/cpufreq-applet@yyc1992.gmail.com/extension.js +++ b/cpufreq-applet@yyc1992.gmail.com/extension.js @@ -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) @@ -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 @@ -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}); @@ -152,7 +153,14 @@ 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() { @@ -160,12 +168,11 @@ Panel_Indicator.prototype = { 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(); }, @@ -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() { @@ -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()); @@ -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) @@ -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); @@ -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); } diff --git a/org.gnome.shell.extensions.cpufreq.gschema.xml b/org.gnome.shell.extensions.cpufreq.gschema.xml index 33529b6..6049cc2 100644 --- a/org.gnome.shell.extensions.cpufreq.gschema.xml +++ b/org.gnome.shell.extensions.cpufreq.gschema.xml @@ -37,7 +37,7 @@ [] Cpus Hidden - Names of cpus not shown + Names of cpus not shown, add "-1" to hide the summary one.