Added the ability to enable dynamic network icons for netstat module. (#376)

This commit is contained in:
Jas Singh
2024-10-26 17:21:27 -07:00
committed by GitHub
parent c1bbb11b86
commit 86ff27fd3e
5 changed files with 27 additions and 12 deletions

View File

@@ -231,6 +231,12 @@ export const CustomModuleSettings = (): Scrollable<GtkWidget, Attribute> =>
"Name of the network interface to poll.\nHINT: Get list of interfaces with 'cat /proc/net/dev'",
type: 'string',
}),
Option({
opt: options.bar.customModules.netstat.dynamicIcon,
title: 'Use Network Icon',
subtitle: 'If enabled, shows the current network icon indicators instead of the static icon',
type: 'boolean',
}),
Option({
opt: options.bar.customModules.netstat.icon,
title: 'Netstat Icon',

View File

@@ -1,6 +1,5 @@
import { BarBoxChild, Module } from 'lib/types/bar';
import { BarButtonStyles } from 'lib/types/options';
import { Bind } from 'lib/types/variable';
import { GtkWidget } from 'lib/types/widget';
import options from 'options';
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
@@ -12,6 +11,7 @@ const undefinedVar = Variable(undefined);
export const module = ({
icon,
textIcon,
useTextIcon = Variable(false).bind('value'),
label,
tooltipText,
boxClass,
@@ -21,19 +21,19 @@ export const module = ({
labelHook,
hook,
}: Module): BarBoxChild => {
const getIconWidget = (): GtkWidget | undefined => {
const getIconWidget = (useTxtIcn: boolean): GtkWidget | undefined => {
let iconWidget: Gtk.Widget | undefined;
if (icon !== undefined) {
if (icon !== undefined && !useTxtIcn) {
iconWidget = Widget.Icon({
class_name: `txt-icon bar-button-icon module-icon ${boxClass}`,
icon: icon,
}) as unknown as Gtk.Widget;
});
} else if (textIcon !== undefined) {
iconWidget = Widget.Label({
class_name: `txt-icon bar-button-icon module-icon ${boxClass}`,
label: textIcon,
}) as unknown as Gtk.Widget;
});
}
return iconWidget;
@@ -55,25 +55,25 @@ export const module = ({
},
),
tooltip_text: tooltipText,
children: Utils.merge([showLabelBinding], (showLabelBinding): Gtk.Widget[] => {
children: Utils.merge([showLabelBinding, useTextIcon], (showLabel, forceTextIcon): Gtk.Widget[] => {
const childrenArray: Gtk.Widget[] = [];
const iconWidget = getIconWidget();
const iconWidget = getIconWidget(forceTextIcon);
if (iconWidget !== undefined) {
childrenArray.push(iconWidget);
}
if (showLabelBinding) {
if (showLabel) {
childrenArray.push(
Widget.Label({
class_name: `bar-button-label module-label ${boxClass}`,
label: label,
setup: labelHook,
}) as unknown as Gtk.Widget,
}),
);
}
return childrenArray;
}) as Bind,
}),
setup: hook,
}),
tooltip_text: tooltipText,

View File

@@ -1,3 +1,4 @@
const network = await Service.import('network');
import options from 'options';
import { module } from '../module';
import { inputHandler } from 'customModules/utils';
@@ -15,6 +16,7 @@ const {
labelType,
networkInterface,
rateUnit,
dynamicIcon,
icon,
round,
leftClick,
@@ -59,6 +61,13 @@ export const Netstat = (): BarBoxChild => {
};
const netstatModule = module({
useTextIcon: dynamicIcon.bind('value').as((useDynamicIcon) => !useDynamicIcon),
icon: Utils.merge([network.bind('primary'), network.bind('wifi'), network.bind('wired')], (pmry, wfi, wrd) => {
if (pmry === 'wired') {
return wrd.icon_name;
}
return wfi.icon_name;
}),
textIcon: icon.bind('value'),
label: Utils.merge(
[networkUsage.bind('value'), labelType.bind('value')],

3
lib/types/bar.d.ts vendored
View File

@@ -22,6 +22,7 @@ export type LabelHook = (self: Label<Gtk.Widget>) => void;
export type Module = {
icon?: string | Binding<string>;
textIcon?: string | Binding<string>;
useTextIcon?: Binding<boolean>;
label?: string | Binding<string>;
labelHook?: LabelHook;
boundLabel?: string;
@@ -38,5 +39,3 @@ export type ResourceLabelType = 'used/total' | 'used' | 'percentage' | 'free';
export type NetstatLabelType = 'full' | 'in' | 'out';
export type RateUnit = 'GiB' | 'MiB' | 'KiB' | 'auto';

View File

@@ -980,6 +980,7 @@ const options = mkOptions(OPTIONS, {
netstat: {
label: opt(true),
networkInterface: opt(''),
dynamicIcon: opt(false),
icon: opt('󰖟'),
round: opt(true),
labelType: opt<NetstatLabelType>('full'),