add memory usage module with collector, scene, and script
This commit is contained in:
parent
cd6295e26f
commit
4169e0c5b6
4 changed files with 129 additions and 0 deletions
36
panels/memory/memory_collector.gd
Normal file
36
panels/memory/memory_collector.gd
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
extends RefCounted
|
||||||
|
class_name MemoryCollector
|
||||||
|
|
||||||
|
|
||||||
|
func collect() -> float:
|
||||||
|
var meminfo := _read_meminfo()
|
||||||
|
if meminfo.is_empty():
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
var total := meminfo.get("MemTotal", 0)
|
||||||
|
var available := meminfo.get("MemAvailable", 0)
|
||||||
|
|
||||||
|
if total == 0:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
return 100.0 * (1.0 - float(available) / float(total))
|
||||||
|
|
||||||
|
|
||||||
|
func _read_meminfo() -> Dictionary:
|
||||||
|
var file := FileAccess.open("/proc/meminfo", FileAccess.READ)
|
||||||
|
if file == null:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
var result: Dictionary = {}
|
||||||
|
while not file.eof_reached():
|
||||||
|
var line: String = file.get_line().strip_edges()
|
||||||
|
if line.is_empty():
|
||||||
|
continue
|
||||||
|
var parts := line.split(":", false)
|
||||||
|
if parts.size() < 2:
|
||||||
|
continue
|
||||||
|
var key := parts[0].strip_edges()
|
||||||
|
var val_str := parts[1].strip_edges().split(" ", false)[0]
|
||||||
|
result[key] = val_str.to_int()
|
||||||
|
|
||||||
|
return result
|
||||||
49
panels/memory/memory_module.gd
Normal file
49
panels/memory/memory_module.gd
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
extends ModuleBase
|
||||||
|
class_name MemoryModule
|
||||||
|
|
||||||
|
|
||||||
|
@onready var title_label: Label = %Title
|
||||||
|
@onready var label: Label = %Label
|
||||||
|
@onready var progress: ProgressBar = %ProgressBar
|
||||||
|
|
||||||
|
var _collector: MemoryCollector
|
||||||
|
|
||||||
|
|
||||||
|
func initialize() -> void:
|
||||||
|
module_title = "Memory"
|
||||||
|
_collector = MemoryCollector.new()
|
||||||
|
_style()
|
||||||
|
|
||||||
|
|
||||||
|
func refresh(data: Dictionary) -> void:
|
||||||
|
var usage: float = _collector.collect()
|
||||||
|
if usage < 0.0:
|
||||||
|
return
|
||||||
|
|
||||||
|
var pct: int = roundi(usage)
|
||||||
|
label.text = "%d%%" % pct
|
||||||
|
progress.value = pct
|
||||||
|
|
||||||
|
|
||||||
|
func _style() -> void:
|
||||||
|
title_label.add_theme_color_override("font_color", Color(0.7, 0.7, 0.8, 1.0))
|
||||||
|
|
||||||
|
label.add_theme_color_override("font_color", Color(0.9, 0.9, 1.0, 1.0))
|
||||||
|
label.add_theme_font_size_override("font_size", 48)
|
||||||
|
|
||||||
|
var fg := StyleBoxFlat.new()
|
||||||
|
fg.bg_color = Color(0.2, 0.7, 0.4, 1.0)
|
||||||
|
fg.corner_radius_top_left = 4
|
||||||
|
fg.corner_radius_top_right = 4
|
||||||
|
fg.corner_radius_bottom_right = 4
|
||||||
|
fg.corner_radius_bottom_left = 4
|
||||||
|
|
||||||
|
var bg := StyleBoxFlat.new()
|
||||||
|
bg.bg_color = Color(0.1, 0.1, 0.14, 1.0)
|
||||||
|
bg.corner_radius_top_left = 4
|
||||||
|
bg.corner_radius_top_right = 4
|
||||||
|
bg.corner_radius_bottom_right = 4
|
||||||
|
bg.corner_radius_bottom_left = 4
|
||||||
|
|
||||||
|
progress.add_theme_stylebox_override("fill", fg)
|
||||||
|
progress.add_theme_stylebox_override("background", bg)
|
||||||
40
panels/memory/memory_module.tscn
Normal file
40
panels/memory/memory_module.tscn
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
[gd_scene format=3 uid="uid://d2d4uqrd2hh3d"]
|
||||||
|
|
||||||
|
[ext_resource type="Script" path="res://panels/memory/memory_module.gd" id="1"]
|
||||||
|
|
||||||
|
[node name="MemoryModule" type="PanelContainer"]
|
||||||
|
anchors_preset = 0
|
||||||
|
script = ExtResource("1")
|
||||||
|
|
||||||
|
[node name="MarginContainer" type="MarginContainer" parent="."]
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
theme_constant_overrides/margin_left = 12
|
||||||
|
theme_constant_overrides/margin_top = 12
|
||||||
|
theme_constant_overrides/margin_right = 12
|
||||||
|
theme_constant_overrides/margin_bottom = 12
|
||||||
|
|
||||||
|
[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"]
|
||||||
|
anchors_preset = 15
|
||||||
|
anchor_right = 1.0
|
||||||
|
anchor_bottom = 1.0
|
||||||
|
|
||||||
|
[node name="Title" type="Label" parent="MarginContainer/VBoxContainer"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
text = "Memory"
|
||||||
|
|
||||||
|
[node name="Spacer" type="Control" parent="MarginContainer/VBoxContainer"]
|
||||||
|
size_flags_vertical = 3
|
||||||
|
|
||||||
|
[node name="Label" type="Label" parent="MarginContainer/VBoxContainer"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
horizontal_alignment = 1
|
||||||
|
size_flags_horizontal = 4
|
||||||
|
|
||||||
|
[node name="ProgressBar" type="ProgressBar" parent="MarginContainer/VBoxContainer"]
|
||||||
|
unique_name_in_owner = true
|
||||||
|
size_flags_horizontal = 4
|
||||||
|
max_value = 100.0
|
||||||
|
value = 0.0
|
||||||
|
show_percentage = false
|
||||||
|
|
@ -27,6 +27,10 @@ func _add_modules() -> void:
|
||||||
grid.place_module(cpu, 0, 0)
|
grid.place_module(cpu, 0, 0)
|
||||||
_modules.append(cpu)
|
_modules.append(cpu)
|
||||||
|
|
||||||
|
var mem := preload("res://panels/memory/memory_module.tscn").instantiate()
|
||||||
|
grid.place_module(mem, 1, 0)
|
||||||
|
_modules.append(mem)
|
||||||
|
|
||||||
var timer := Timer.new()
|
var timer := Timer.new()
|
||||||
timer.timeout.connect(_refresh_modules)
|
timer.timeout.connect(_refresh_modules)
|
||||||
timer.autostart = true
|
timer.autostart = true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue