|
| 1 | +import Widget from 'resource:///com/github/Aylur/ags/widget.js'; |
| 2 | +import Network from "resource:///com/github/Aylur/ags/service/network.js"; |
| 3 | +import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'; |
| 4 | +import { MaterialIcon } from '../../.commonwidgets/materialicon.js'; |
| 5 | +import { ConfigToggle } from '../../.commonwidgets/configwidgets.js'; |
| 6 | +import { setupCursorHover } from '../../.widgetutils/cursorhover.js'; |
| 7 | + |
| 8 | +const { execAsync } = Utils; |
| 9 | +const { Box, Button, Icon, Label, Scrollable, Stack } = Widget; |
| 10 | + |
| 11 | +const USE_SYMBOLIC_ICONS = true; |
| 12 | + |
| 13 | +const VPNConfig = (vpn) => { |
| 14 | + console.log(vpn.state); |
| 15 | + |
| 16 | + const vpnIcon = Icon({ |
| 17 | + vpack: 'center', |
| 18 | + className: 'sidebar-vpn-appicon', |
| 19 | + tooltipText: vpn.id, |
| 20 | + setup: (self) => self.hook(vpn, (self) => { |
| 21 | + self.icon = `${vpn.iconName}${USE_SYMBOLIC_ICONS ? '-symbolic' : ''}`; |
| 22 | + }) |
| 23 | + }); |
| 24 | + |
| 25 | + const vpnName = Box({ |
| 26 | + vpack: 'center', |
| 27 | + hexpand: true, |
| 28 | + vertical: true, |
| 29 | + children: [ |
| 30 | + Label({ |
| 31 | + setup: (self) => self.hook(vpn, (self) => { |
| 32 | + self.label = vpn.id; |
| 33 | + }), |
| 34 | + label: vpn.id, |
| 35 | + xalign: 0, |
| 36 | + truncate: 'end', |
| 37 | + className: 'txt-small', |
| 38 | + maxWidthChars: 1, |
| 39 | + }) |
| 40 | + ] |
| 41 | + }); |
| 42 | + |
| 43 | + const vpnConnectButton = ConfigToggle({ |
| 44 | + desc: 'Toggle connection', |
| 45 | + vpack: 'center', |
| 46 | + initValue: (vpn.state === 'connected'), |
| 47 | + expandWidget: false, |
| 48 | + onChange: (self, newValue) => { |
| 49 | + vpn.setConnection(newValue); |
| 50 | + }, |
| 51 | + extraSetup: (self) => self.hook(vpn, (self) => { |
| 52 | + Utils.timeout(200, () => self.enabled.value = (vpn.state === 'connected')); |
| 53 | + }) |
| 54 | + }); |
| 55 | + |
| 56 | + return Box({ |
| 57 | + className: 'spacing-h-10 sidebar-vpn-config', |
| 58 | + children: [ |
| 59 | + vpnIcon, |
| 60 | + vpnName, |
| 61 | + Box({ |
| 62 | + className: 'spacing-h-5', |
| 63 | + children: [ |
| 64 | + vpnConnectButton, |
| 65 | + ] |
| 66 | + }) |
| 67 | + ] |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +export default (props) => { |
| 72 | + const emptyContent = Box({ |
| 73 | + homogeneous: true, |
| 74 | + children: [ |
| 75 | + Box({ |
| 76 | + vpack: "center", |
| 77 | + vertical: true, |
| 78 | + className: 'txt spacing-v-10', |
| 79 | + children: [ |
| 80 | + Box({ |
| 81 | + vertical: true, |
| 82 | + className: 'spacing-v-5 txt-subtext', |
| 83 | + children: [ |
| 84 | + MaterialIcon('vpn_key_off', 'gigantic'), |
| 85 | + Label({ label: 'No VPN configured', className: 'txt-small' }) |
| 86 | + ] |
| 87 | + }) |
| 88 | + ] |
| 89 | + }) |
| 90 | + ] |
| 91 | + }); |
| 92 | + |
| 93 | + const vpnList = Scrollable({ |
| 94 | + vexpand: true, |
| 95 | + child: Box({ |
| 96 | + vertical: true, |
| 97 | + className: 'spacing-v-5', |
| 98 | + attribute: { |
| 99 | + 'updateVPNList': (self) => { |
| 100 | + self.children = Network.vpn.connections.map(vpn => VPNConfig(vpn)); |
| 101 | + } |
| 102 | + }, |
| 103 | + setup: (self) => self |
| 104 | + .hook(Network.vpn, self.attribute.updateVPNList, 'connection-added') |
| 105 | + .hook(Network.vpn, self.attribute.updateVPNList, 'connection-removed') |
| 106 | + }) |
| 107 | + }); |
| 108 | + |
| 109 | + const mainContent = Stack({ |
| 110 | + children: { |
| 111 | + 'list': vpnList, |
| 112 | + 'empty': emptyContent, |
| 113 | + }, |
| 114 | + setup: (self) => self.hook(Network, (self) => { |
| 115 | + self.shown = (Network.vpn.connections.length > 0 ? 'list' : 'empty'); |
| 116 | + }) |
| 117 | + }); |
| 118 | + |
| 119 | + const bottomBar = Box({ |
| 120 | + homogeneous: true, |
| 121 | + children: [ |
| 122 | + Button({ |
| 123 | + label: 'More', |
| 124 | + hpack: 'center', |
| 125 | + setup: setupCursorHover, |
| 126 | + className: 'txt-small txt sidebar-centermodules-bottombar-button', |
| 127 | + onClicked: () => { |
| 128 | + execAsync(['bash', '-c', `${userOptions.apps.vpn}`, '&']); |
| 129 | + closeEverything(); |
| 130 | + }, |
| 131 | + }) |
| 132 | + ] |
| 133 | + }); |
| 134 | + |
| 135 | + return Box({ |
| 136 | + ...props, |
| 137 | + vertical: true, |
| 138 | + className: 'spacing-v-5', |
| 139 | + children: [ |
| 140 | + mainContent, |
| 141 | + bottomBar |
| 142 | + ], |
| 143 | + }); |
| 144 | +} |
0 commit comments