From 48faf52e59dc0b5ff386bb5ea6be9db8d72536c9 Mon Sep 17 00:00:00 2001 From: Jas Singh Date: Sat, 21 Dec 2024 21:12:12 -0800 Subject: [PATCH] Fixed the auto-hide functionality for the media bar module. (#588) --- src/components/bar/modules/media/index.tsx | 13 ++- src/components/bar/modules/network/helpers.ts | 16 ++-- .../menus/bluetooth/header/Controls/helper.ts | 8 +- .../menus/dashboard/controls/helpers.ts | 8 +- .../menus/network/ethernet/helpers.ts | 39 +++----- .../menus/network/wifi/Controls/helpers.ts | 8 +- .../menus/network/wifi/WirelessAPs/helpers.ts | 16 ++-- src/globals/media.ts | 88 +++++++------------ src/services/matugen/index.ts | 2 +- 9 files changed, 73 insertions(+), 125 deletions(-) diff --git a/src/components/bar/modules/media/index.tsx b/src/components/bar/modules/media/index.tsx index 8c79c40..7fd1a7d 100644 --- a/src/components/bar/modules/media/index.tsx +++ b/src/components/bar/modules/media/index.tsx @@ -14,15 +14,15 @@ import { activePlayer, mediaAlbum, mediaArtist, mediaTitle } from 'src/globals/m const { truncation, truncation_size, show_label, show_active_only, rightClick, middleClick, format } = options.bar.media; +const isVis = Variable(!show_active_only.get()); + +Variable.derive([bind(show_active_only), bind(mprisService, 'players')], (showActive, players) => { + isVis.set(!showActive || players?.length > 0); +}); + const Media = (): BarBoxChild => { activePlayer.set(mprisService.get_players()[0]); - const isVis = Variable(!show_active_only.get()); - - show_active_only.subscribe(() => { - isVis.set(!show_active_only.get() || mprisService.get_players().length > 0); - }); - const songIcon = Variable(''); const mediaLabel = Variable.derive( @@ -55,7 +55,6 @@ const Media = (): BarBoxChild => { { - isVis.drop(); songIcon.drop(); mediaLabel.drop(); componentClassName.drop(); diff --git a/src/components/bar/modules/network/helpers.ts b/src/components/bar/modules/network/helpers.ts index e6e2f19..400437e 100644 --- a/src/components/bar/modules/network/helpers.ts +++ b/src/components/bar/modules/network/helpers.ts @@ -5,8 +5,8 @@ import { networkService } from 'src/lib/constants/services'; export const wiredIcon: Variable = Variable(''); export const wirelessIcon: Variable = Variable(''); -let wiredIconBinding: Variable; -let wirelessIconBinding: Variable; +let wiredIconBinding: Variable | undefined; +let wirelessIconBinding: Variable | undefined; /** * Handles the wired network icon binding. @@ -15,10 +15,8 @@ let wirelessIconBinding: Variable; * then checks if the wired network service is available. If available, it binds the icon name to the `wiredIcon` variable. */ const handleWiredIcon = (): void => { - if (wiredIconBinding) { - wiredIconBinding(); - wiredIconBinding.drop(); - } + wiredIconBinding?.drop(); + wiredIconBinding = undefined; if (!networkService.wired) { return; @@ -36,10 +34,8 @@ const handleWiredIcon = (): void => { * then checks if the wireless network service is available. If available, it binds the icon name to the `wirelessIcon` variable. */ const handleWirelessIcon = (): void => { - if (wirelessIconBinding) { - wirelessIconBinding(); - wirelessIconBinding.drop(); - } + wirelessIconBinding?.drop(); + wirelessIconBinding = undefined; if (!networkService.wifi) { return; diff --git a/src/components/menus/bluetooth/header/Controls/helper.ts b/src/components/menus/bluetooth/header/Controls/helper.ts index 2e551cc..12bd88b 100644 --- a/src/components/menus/bluetooth/header/Controls/helper.ts +++ b/src/components/menus/bluetooth/header/Controls/helper.ts @@ -2,13 +2,11 @@ import { bind, Variable } from 'astal'; import { bluetoothService } from 'src/lib/constants/services'; export const isDiscovering: Variable = Variable(false); -let discoveringBinding: Variable; +let discoveringBinding: Variable | undefined; Variable.derive([bind(bluetoothService, 'adapter')], () => { - if (discoveringBinding) { - discoveringBinding(); - discoveringBinding.drop(); - } + discoveringBinding?.drop(); + discoveringBinding = undefined; if (!bluetoothService.adapter) { return; diff --git a/src/components/menus/dashboard/controls/helpers.ts b/src/components/menus/dashboard/controls/helpers.ts index 2f5d905..923dc00 100644 --- a/src/components/menus/dashboard/controls/helpers.ts +++ b/src/components/menus/dashboard/controls/helpers.ts @@ -2,13 +2,11 @@ import { bind, Variable } from 'astal'; import { networkService } from 'src/lib/constants/services'; export const isWifiEnabled: Variable = Variable(false); -let wifiEnabledBinding: Variable; +let wifiEnabledBinding: Variable | undefined; Variable.derive([bind(networkService, 'wifi')], () => { - if (wifiEnabledBinding) { - wifiEnabledBinding(); - wifiEnabledBinding.drop(); - } + wifiEnabledBinding?.drop(); + wifiEnabledBinding = undefined; if (!networkService.wifi) { return; diff --git a/src/components/menus/network/ethernet/helpers.ts b/src/components/menus/network/ethernet/helpers.ts index 5b88e99..04872b8 100644 --- a/src/components/menus/network/ethernet/helpers.ts +++ b/src/components/menus/network/ethernet/helpers.ts @@ -13,10 +13,10 @@ export const wiredSpeed: Variable = Variable(0); /******************************************* * Bindings * *******************************************/ -let wiredStateBinding: Variable; -let wiredInternetBinding: Variable; -let wiredIconBinding: Variable; -let wiredSpeedBinding: Variable; +let wiredStateBinding: Variable | undefined; +let wiredInternetBinding: Variable | undefined; +let wiredIconBinding: Variable | undefined; +let wiredSpeedBinding: Variable | undefined; /** * Retrieves the current state of the wired network. @@ -25,10 +25,8 @@ let wiredSpeedBinding: Variable; * If the wired network service is available, it updates the `wiredState` variable with the current state. */ const getWiredState = (): void => { - if (wiredStateBinding) { - wiredStateBinding(); - wiredStateBinding.drop(); - } + wiredStateBinding?.drop(); + wiredStateBinding = undefined; if (!networkService.wired) { wiredState.set(AstalNetwork.DeviceState.UNAVAILABLE); @@ -47,10 +45,8 @@ const getWiredState = (): void => { * If the wired network service is available, it updates the `wiredInternet` variable with the current internet status. */ const getWiredInternet = (): void => { - if (wiredInternetBinding) { - wiredInternetBinding(); - wiredInternetBinding.drop(); - } + wiredInternetBinding?.drop(); + wiredInternetBinding = undefined; if (!networkService.wired) { return; @@ -68,10 +64,8 @@ const getWiredInternet = (): void => { * If the wired network service is available, it updates the `wiredIcon` variable with the current icon name. */ const getWiredIcon = (): void => { - if (wiredIconBinding) { - wiredIconBinding(); - wiredIconBinding.drop(); - } + wiredIconBinding?.drop(); + wiredIconBinding = undefined; if (!networkService.wired) { wiredIcon.set('network-wired-symbolic'); @@ -90,10 +84,8 @@ const getWiredIcon = (): void => { * If the wired network service is available, it updates the `wiredSpeed` variable with the current speed. */ const getWiredSpeed = (): void => { - if (wiredSpeedBinding) { - wiredSpeedBinding(); - wiredSpeedBinding.drop(); - } + wiredSpeedBinding?.drop(); + wiredSpeedBinding = undefined; if (!networkService.wired) { return; @@ -110,10 +102,3 @@ Variable.derive([bind(networkService, 'wired')], () => { getWiredIcon(); getWiredSpeed(); }); - -Variable.derive([bind(networkService, 'wired')], () => { - getWiredState(); - getWiredInternet(); - getWiredIcon(); - getWiredSpeed(); -}); diff --git a/src/components/menus/network/wifi/Controls/helpers.ts b/src/components/menus/network/wifi/Controls/helpers.ts index 80ff173..fa92daa 100644 --- a/src/components/menus/network/wifi/Controls/helpers.ts +++ b/src/components/menus/network/wifi/Controls/helpers.ts @@ -2,13 +2,11 @@ import { bind, Variable } from 'astal'; import { networkService } from 'src/lib/constants/services'; export const isScanning: Variable = Variable(false); -let scanningBinding: Variable; +let scanningBinding: Variable | undefined; Variable.derive([bind(networkService, 'wifi')], () => { - if (scanningBinding) { - scanningBinding(); - scanningBinding.drop(); - } + scanningBinding?.drop(); + scanningBinding = undefined; if (!networkService.wifi) { return; diff --git a/src/components/menus/network/wifi/WirelessAPs/helpers.ts b/src/components/menus/network/wifi/WirelessAPs/helpers.ts index 5aa762c..e7f0c91 100644 --- a/src/components/menus/network/wifi/WirelessAPs/helpers.ts +++ b/src/components/menus/network/wifi/WirelessAPs/helpers.ts @@ -8,8 +8,8 @@ import { isPrimaryClick, Notify } from 'src/lib/utils'; export const isWifiEnabled: Variable = Variable(false); export const wifiAccessPoints: Variable = Variable([]); -let wifiEnabledBinding: Variable; -let accessPointBinding: Variable; +let wifiEnabledBinding: Variable | undefined; +let accessPointBinding: Variable | undefined; export const staging = Variable(undefined); export const connecting = Variable(''); @@ -21,10 +21,8 @@ export const connecting = Variable(''); * If the WiFi service is available, it updates the `isWifiEnabled` variable based on the enabled state. */ const wifiEnabled = (): void => { - if (wifiEnabledBinding) { - wifiEnabledBinding(); - wifiEnabledBinding.drop(); - } + wifiEnabledBinding?.drop(); + wifiEnabledBinding = undefined; if (!networkService.wifi) { return; @@ -42,10 +40,8 @@ const wifiEnabled = (): void => { * If the WiFi service is available, it updates the `wifiAccessPoints` variable with the list of access points. */ const accessPoints = (): void => { - if (accessPointBinding) { - accessPointBinding(); - accessPointBinding.drop(); - } + accessPointBinding?.drop(); + accessPointBinding = undefined; if (!networkService.wifi) { return; diff --git a/src/globals/media.ts b/src/globals/media.ts index ff9e0c3..e956274 100644 --- a/src/globals/media.ts +++ b/src/globals/media.ts @@ -44,27 +44,25 @@ export const mediaAlbum = Variable('-----'); export const mediaArtist = Variable('-----'); export const mediaArtUrl = Variable(''); -let positionUnsub: Variable; +let positionUnsub: Variable | undefined; -let loopUnsub: Variable; -let shuffleUnsub: Variable; +let loopUnsub: Variable | undefined; +let shuffleUnsub: Variable | undefined; -let canPlayUnsub: Variable; -let playbackStatusUnsub: Variable; +let canPlayUnsub: Variable | undefined; +let playbackStatusUnsub: Variable | undefined; -let canGoNextUnsub: Variable; -let canGoPreviousUnsub: Variable; +let canGoNextUnsub: Variable | undefined; +let canGoPreviousUnsub: Variable | undefined; -let titleUnsub: Variable; -let albumUnsub: Variable; -let artistUnsub: Variable; -let artUrlUnsub: Variable; +let titleUnsub: Variable | undefined; +let albumUnsub: Variable | undefined; +let artistUnsub: Variable | undefined; +let artUrlUnsub: Variable | undefined; const updatePosition = (player: AstalMpris.Player | undefined): void => { - if (positionUnsub) { - positionUnsub(); - positionUnsub.drop(); - } + positionUnsub?.drop(); + positionUnsub = undefined; if (player === undefined) { timeStamp.set('00:00'); @@ -91,10 +89,8 @@ const updatePosition = (player: AstalMpris.Player | undefined): void => { }; const updateLoop = (player: AstalMpris.Player | undefined): void => { - if (loopUnsub) { - loopUnsub(); - loopUnsub.drop(); - } + loopUnsub?.drop(); + loopUnsub = undefined; if (player === undefined) { loopStatus.set(AstalMpris.Loop.NONE); @@ -117,10 +113,8 @@ const updateLoop = (player: AstalMpris.Player | undefined): void => { }; const updateShuffle = (player: AstalMpris.Player | undefined): void => { - if (shuffleUnsub) { - shuffleUnsub(); - shuffleUnsub.drop(); - } + shuffleUnsub?.drop(); + shuffleUnsub = undefined; if (player === undefined) { shuffleStatus.set(AstalMpris.Shuffle.OFF); @@ -138,10 +132,8 @@ const updateShuffle = (player: AstalMpris.Player | undefined): void => { }; const updateCanPlay = (player: AstalMpris.Player | undefined): void => { - if (canPlayUnsub) { - canPlayUnsub(); - canPlayUnsub.drop(); - } + canPlayUnsub?.drop(); + canPlayUnsub = undefined; if (player === undefined) { canPlay.set(false); @@ -159,10 +151,8 @@ const updateCanPlay = (player: AstalMpris.Player | undefined): void => { }; const updatePlaybackStatus = (player: AstalMpris.Player | undefined): void => { - if (playbackStatusUnsub) { - playbackStatusUnsub(); - playbackStatusUnsub.drop(); - } + playbackStatusUnsub?.drop(); + playbackStatusUnsub = undefined; if (player === undefined) { playbackStatus.set(AstalMpris.PlaybackStatus.STOPPED); @@ -181,10 +171,8 @@ const updatePlaybackStatus = (player: AstalMpris.Player | undefined): void => { }; const updateCanGoNext = (player: AstalMpris.Player | undefined): void => { - if (canGoNextUnsub) { - canGoNextUnsub(); - canGoNextUnsub.drop(); - } + canGoNextUnsub?.drop(); + canGoNextUnsub = undefined; if (player === undefined) { canGoNext.set(false); @@ -202,10 +190,8 @@ const updateCanGoNext = (player: AstalMpris.Player | undefined): void => { }; const updateCanGoPrevious = (player: AstalMpris.Player | undefined): void => { - if (canGoPreviousUnsub) { - canGoPreviousUnsub(); - canGoPreviousUnsub.drop(); - } + canGoPreviousUnsub?.drop(); + canGoPreviousUnsub = undefined; if (player === undefined) { canGoPrevious.set(false); @@ -223,10 +209,8 @@ const updateCanGoPrevious = (player: AstalMpris.Player | undefined): void => { }; const updateTitle = (player: AstalMpris.Player | undefined): void => { - if (titleUnsub) { - titleUnsub(); - titleUnsub.drop(); - } + titleUnsub?.drop(); + titleUnsub = undefined; if (player === undefined) { mediaTitle.set(noMediaText.get()); @@ -248,10 +232,8 @@ const updateTitle = (player: AstalMpris.Player | undefined): void => { }; const updateAlbum = (player: AstalMpris.Player | undefined): void => { - if (albumUnsub) { - albumUnsub(); - albumUnsub.drop(); - } + albumUnsub?.drop(); + albumUnsub = undefined; if (player === undefined) { mediaAlbum.set('-----'); @@ -267,10 +249,8 @@ const updateAlbum = (player: AstalMpris.Player | undefined): void => { }; const updateArtist = (player: AstalMpris.Player | undefined): void => { - if (artistUnsub) { - artistUnsub(); - artistUnsub.drop(); - } + artistUnsub?.drop(); + artistUnsub = undefined; if (player === undefined) { mediaArtist.set('-----'); @@ -288,10 +268,8 @@ const updateArtist = (player: AstalMpris.Player | undefined): void => { }; const updateArtUrl = (player: AstalMpris.Player | undefined): void => { - if (artUrlUnsub) { - artUrlUnsub(); - artUrlUnsub.drop(); - } + artUrlUnsub?.drop(); + artUrlUnsub = undefined; if (player === undefined) { mediaArtUrl.set(''); diff --git a/src/services/matugen/index.ts b/src/services/matugen/index.ts index 2b93d52..f5bdc09 100644 --- a/src/services/matugen/index.ts +++ b/src/services/matugen/index.ts @@ -31,7 +31,7 @@ export async function generateMatugenColors(): Promise 1 ? 1 : contrast.get() < -1 ? -1 : contrast.get(); const contents = await bash( - `matugen image -q ${wallpaperPath} -t scheme-${scheme_type.get()} --contrast ${normalizedContrast} --json hex`, + `matugen image --dry-run -q ${wallpaperPath} -t scheme-${scheme_type.get()} --contrast ${normalizedContrast} --json hex`, ); return JSON.parse(contents).colors[options.theme.matugen_settings.mode.get()];