Skip to content

Commit 6d81eb9

Browse files
ryonakanostsdc
authored andcommitted
Indicator: Generalize IndicatorWidget
1 parent 6dc3744 commit 6d81eb9

File tree

9 files changed

+134
-54
lines changed

9 files changed

+134
-54
lines changed

po/POTFILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
src/Monitor.vala
22
src/MainWindow.vala
33
src/Utils.vala
4+
src/Indicator/Widgets/IndicatorWidgetFrequency.vala
45
src/Indicator/Widgets/PopoverWidget.vala
56
src/Views/ProcessView/ProcessInfoView/ProcessInfoHeader.vala
67
src/Views/ProcessView/ProcessInfoView/ProcessInfoIOStats.vala

src/Indicator/Indicator.vala

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,41 @@ public class Monitor.Indicator : Wingpanel.Indicator {
5252
dbusclient.interface.indicator_gpu_temperature_state.connect ((state) => display_widget.gpu_temperature_widget.visible = state);
5353

5454
dbusclient.interface.update.connect ((sysres) => {
55-
display_widget.cpu_widget.state_percentage = sysres.cpu_percentage;
56-
display_widget.cpu_frequency_widget.state_frequency = sysres.cpu_frequency;
57-
display_widget.cpu_temperature_widget.state_temperature = (int) Math.round (sysres.cpu_temperature);
58-
display_widget.memory_widget.state_percentage = sysres.memory_percentage;
59-
display_widget.network_up_widget.state_bandwidth = sysres.network_up;
60-
display_widget.network_down_widget.state_bandwidth = sysres.network_down;
61-
display_widget.gpu_widget.state_percentage = sysres.gpu_percentage;
62-
display_widget.gpu_memory_widget.state_percentage = sysres.gpu_memory_percentage;
63-
display_widget.gpu_temperature_widget.state_temperature = (int) Math.round (sysres.gpu_temperature);
55+
var cpu_percentage = Value (typeof (uint));
56+
cpu_percentage.set_uint (sysres.cpu_percentage);
57+
display_widget.cpu_widget.update_label (cpu_percentage);
58+
59+
var cpu_frequency = Value (typeof (double));
60+
cpu_frequency.set_double (sysres.cpu_frequency);
61+
display_widget.cpu_frequency_widget.update_label (cpu_frequency);
62+
63+
var cpu_temperature = Value (typeof (int));
64+
cpu_temperature.set_int ((int) Math.round (sysres.cpu_temperature));
65+
display_widget.cpu_temperature_widget.update_label (cpu_temperature);
66+
67+
var memory_percentage = Value (typeof (uint));
68+
memory_percentage.set_uint (sysres.memory_percentage);
69+
display_widget.memory_widget.update_label (memory_percentage);
70+
71+
var network_up = Value (typeof (uint64));
72+
network_up.set_uint64 (sysres.network_up);
73+
display_widget.network_up_widget.update_label (network_up);
74+
75+
var network_down = Value (typeof (uint64));
76+
network_down.set_uint64 (sysres.network_down);
77+
display_widget.network_down_widget.update_label (network_down);
78+
79+
var gpu_percentage = Value (typeof (uint));
80+
gpu_percentage.set_uint (sysres.gpu_percentage);
81+
display_widget.gpu_widget.update_label (gpu_percentage);
82+
83+
var gpu_memory_percentage = Value (typeof (uint));
84+
gpu_memory_percentage.set_uint (sysres.gpu_memory_percentage);
85+
display_widget.gpu_memory_widget.update_label (gpu_memory_percentage);
86+
87+
var gpu_temperature = Value (typeof (int));
88+
gpu_temperature.set_int ((int) Math.round (sysres.gpu_temperature));
89+
display_widget.gpu_temperature_widget.update_label (gpu_temperature);
6490
});
6591

6692
popover_widget.quit_monitor.connect (() => {

src/Indicator/Widgets/DisplayWidget.vala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
*/
55

66
public class Monitor.Widgets.DisplayWidget : Gtk.Grid {
7-
public IndicatorWidget cpu_widget = new IndicatorWidget ("cpu-symbolic");
8-
public IndicatorWidget cpu_frequency_widget = new IndicatorWidget ("cpu-symbolic");
9-
public IndicatorWidget cpu_temperature_widget = new IndicatorWidget ("temperature-sensor-symbolic");
7+
public IndicatorWidgetPercentage cpu_widget = new IndicatorWidgetPercentage ("cpu-symbolic");
8+
public IndicatorWidgetFrequency cpu_frequency_widget = new IndicatorWidgetFrequency ("cpu-symbolic");
9+
public IndicatorWidgetTemperature cpu_temperature_widget = new IndicatorWidgetTemperature ("temperature-sensor-symbolic");
1010

11-
public IndicatorWidget memory_widget = new IndicatorWidget ("ram-symbolic");
11+
public IndicatorWidgetPercentage memory_widget = new IndicatorWidgetPercentage ("ram-symbolic");
1212

13-
public IndicatorWidget network_up_widget = new IndicatorWidget ("go-up-symbolic");
14-
public IndicatorWidget network_down_widget = new IndicatorWidget ("go-down-symbolic");
13+
public IndicatorWidgetBandwidth network_up_widget = new IndicatorWidgetBandwidth ("go-up-symbolic");
14+
public IndicatorWidgetBandwidth network_down_widget = new IndicatorWidgetBandwidth ("go-down-symbolic");
1515

16-
public IndicatorWidget gpu_widget = new IndicatorWidget ("gpu-symbolic");
17-
public IndicatorWidget gpu_memory_widget = new IndicatorWidget ("gpu-vram-symbolic");
18-
public IndicatorWidget gpu_temperature_widget = new IndicatorWidget ("temperature-gpu-symbolic");
16+
public IndicatorWidgetPercentage gpu_widget = new IndicatorWidgetPercentage ("gpu-symbolic");
17+
public IndicatorWidgetPercentage gpu_memory_widget = new IndicatorWidgetPercentage ("gpu-vram-symbolic");
18+
public IndicatorWidgetTemperature gpu_temperature_widget = new IndicatorWidgetTemperature ("temperature-gpu-symbolic");
1919

2020
construct {
2121
valign = Gtk.Align.CENTER;

src/Indicator/Widgets/IndicatorWidget.vala

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,7 @@ public class Monitor.IndicatorWidget : Gtk.Box {
77

88
public string icon_name { get; construct; }
99

10-
public uint state_percentage {
11-
set {
12-
label.label = "%u%%".printf (value);
13-
label.get_style_context ().remove_class ("monitor-indicator-label-warning");
14-
label.get_style_context ().remove_class ("monitor-indicator-label-critical");
15-
16-
if (value > 80) {
17-
label.get_style_context ().add_class ("monitor-indicator-label-warning");
18-
}
19-
if (value > 90) {
20-
label.get_style_context ().add_class ("monitor-indicator-label-critical");
21-
}
22-
}
23-
}
24-
25-
public int state_temperature {
26-
set {
27-
label.label = "%i".printf (value);
28-
}
29-
}
30-
31-
public double state_frequency {
32-
set {
33-
label.label = ("%.2f %s").printf (value, _("GHz"));
34-
}
35-
}
36-
37-
public uint64 state_bandwidth {
38-
set {
39-
label.label = format_size (value);
40-
}
41-
}
42-
43-
private Gtk.Label label = new Gtk.Label (Utils.NOT_AVAILABLE);
10+
protected Gtk.Label label;
4411

4512
public IndicatorWidget (string icon_name) {
4613
Object (
@@ -52,9 +19,17 @@ public class Monitor.IndicatorWidget : Gtk.Box {
5219

5320
construct {
5421
var icon = new Gtk.Image.from_icon_name (icon_name, Gtk.IconSize.SMALL_TOOLBAR);
55-
label.margin = 2;
56-
label.width_chars = 4;
22+
23+
label = new Gtk.Label (Utils.NOT_AVAILABLE) {
24+
margin = 2,
25+
width_chars = 4,
26+
};
27+
5728
pack_start (icon);
5829
pack_start (label);
5930
}
31+
32+
public virtual void update_label (Value value) {
33+
// NOP; should be overridden by child classes
34+
}
6035
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
4+
*/
5+
6+
public class Monitor.IndicatorWidgetBandwidth : Monitor.IndicatorWidget {
7+
public IndicatorWidgetBandwidth (string icon_name) {
8+
base (icon_name);
9+
}
10+
11+
public override void update_label (Value value) {
12+
uint64 bandwidth = value.get_uint64 ();
13+
14+
label.label = format_size (bandwidth);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
4+
*/
5+
6+
public class Monitor.IndicatorWidgetFrequency : Monitor.IndicatorWidget {
7+
public IndicatorWidgetFrequency (string icon_name) {
8+
base (icon_name);
9+
}
10+
11+
public override void update_label (Value value) {
12+
double frequency = value.get_double ();
13+
14+
label.label = ("%.2f %s").printf (frequency, _("GHz"));
15+
}
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
4+
*/
5+
6+
public class Monitor.IndicatorWidgetPercentage : Monitor.IndicatorWidget {
7+
public IndicatorWidgetPercentage (string icon_name) {
8+
base (icon_name);
9+
}
10+
11+
public override void update_label (Value value) {
12+
uint percentage = value.get_uint ();
13+
14+
label.label = "%u%%".printf (percentage);
15+
label.get_style_context ().remove_class ("monitor-indicator-label-warning");
16+
label.get_style_context ().remove_class ("monitor-indicator-label-critical");
17+
18+
if (percentage > 80) {
19+
label.get_style_context ().add_class ("monitor-indicator-label-warning");
20+
}
21+
22+
if (percentage > 90) {
23+
label.get_style_context ().add_class ("monitor-indicator-label-critical");
24+
}
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: GPL-3.0-or-later
3+
* SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io)
4+
*/
5+
6+
public class Monitor.IndicatorWidgetTemperature : Monitor.IndicatorWidget {
7+
public IndicatorWidgetTemperature (string icon_name) {
8+
base (icon_name);
9+
}
10+
11+
public override void update_label (Value value) {
12+
int temperature = value.get_int ();
13+
14+
label.label = "%i".printf (temperature);
15+
}
16+
}

src/Indicator/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ source_indicator_files = [
33
'Services/DBusClient.vala',
44
'Widgets/DisplayWidget.vala',
55
'Widgets/IndicatorWidget.vala',
6+
'Widgets/IndicatorWidgetPercentage.vala',
7+
'Widgets/IndicatorWidgetTemperature.vala',
8+
'Widgets/IndicatorWidgetFrequency.vala',
9+
'Widgets/IndicatorWidgetBandwidth.vala',
610
'Widgets/PopoverWidget.vala',
711

812
meson.project_source_root() / 'src' / 'Resources/ResourcesSerialized.vala',

0 commit comments

Comments
 (0)