From 86ff27fd3e0e350aab43f560fc3cb779daf315d7 Mon Sep 17 00:00:00 2001 From: Jas Singh Date: Sat, 26 Oct 2024 17:21:27 -0700 Subject: [PATCH] Added the ability to enable dynamic network icons for netstat module. (#376) --- customModules/config.ts | 6 ++++++ customModules/module.ts | 20 ++++++++++---------- customModules/netstat/index.ts | 9 +++++++++ lib/types/bar.d.ts | 3 +-- options.ts | 1 + 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/customModules/config.ts b/customModules/config.ts index 36e2c4c..16d3f09 100644 --- a/customModules/config.ts +++ b/customModules/config.ts @@ -231,6 +231,12 @@ export const CustomModuleSettings = (): Scrollable => "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', diff --git a/customModules/module.ts b/customModules/module.ts index 5438ade..2686291 100644 --- a/customModules/module.ts +++ b/customModules/module.ts @@ -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, diff --git a/customModules/netstat/index.ts b/customModules/netstat/index.ts index abfe2ca..1672db8 100644 --- a/customModules/netstat/index.ts +++ b/customModules/netstat/index.ts @@ -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')], diff --git a/lib/types/bar.d.ts b/lib/types/bar.d.ts index 99066d8..3886964 100644 --- a/lib/types/bar.d.ts +++ b/lib/types/bar.d.ts @@ -22,6 +22,7 @@ export type LabelHook = (self: Label) => void; export type Module = { icon?: string | Binding; textIcon?: string | Binding; + useTextIcon?: Binding; label?: string | Binding; 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'; - - diff --git a/options.ts b/options.ts index 6a72f82..4f57b41 100644 --- a/options.ts +++ b/options.ts @@ -980,6 +980,7 @@ const options = mkOptions(OPTIONS, { netstat: { label: opt(true), networkInterface: opt(''), + dynamicIcon: opt(false), icon: opt('󰖟'), round: opt(true), labelType: opt('full'),