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'", "Name of the network interface to poll.\nHINT: Get list of interfaces with 'cat /proc/net/dev'",
type: 'string', 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({ Option({
opt: options.bar.customModules.netstat.icon, opt: options.bar.customModules.netstat.icon,
title: 'Netstat Icon', title: 'Netstat Icon',

View File

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

View File

@@ -1,3 +1,4 @@
const network = await Service.import('network');
import options from 'options'; import options from 'options';
import { module } from '../module'; import { module } from '../module';
import { inputHandler } from 'customModules/utils'; import { inputHandler } from 'customModules/utils';
@@ -15,6 +16,7 @@ const {
labelType, labelType,
networkInterface, networkInterface,
rateUnit, rateUnit,
dynamicIcon,
icon, icon,
round, round,
leftClick, leftClick,
@@ -59,6 +61,13 @@ export const Netstat = (): BarBoxChild => {
}; };
const netstatModule = module({ 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'), textIcon: icon.bind('value'),
label: Utils.merge( label: Utils.merge(
[networkUsage.bind('value'), labelType.bind('value')], [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 = { export type Module = {
icon?: string | Binding<string>; icon?: string | Binding<string>;
textIcon?: string | Binding<string>; textIcon?: string | Binding<string>;
useTextIcon?: Binding<boolean>;
label?: string | Binding<string>; label?: string | Binding<string>;
labelHook?: LabelHook; labelHook?: LabelHook;
boundLabel?: string; boundLabel?: string;
@@ -38,5 +39,3 @@ export type ResourceLabelType = 'used/total' | 'used' | 'percentage' | 'free';
export type NetstatLabelType = 'full' | 'in' | 'out'; export type NetstatLabelType = 'full' | 'in' | 'out';
export type RateUnit = 'GiB' | 'MiB' | 'KiB' | 'auto'; export type RateUnit = 'GiB' | 'MiB' | 'KiB' | 'auto';

View File

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