Skip to content

Commit 3e107a9

Browse files
committed
feat: cloudflare warp custom modules
1 parent e4a5e80 commit 3e107a9

File tree

7 files changed

+138
-1
lines changed

7 files changed

+138
-1
lines changed

src/components/bar/exports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Weather } from '../../components/bar/modules/weather/index';
2323
import { Power } from '../../components/bar/modules/power/index';
2424
import { Hyprsunset } from '../../components/bar/modules/hyprsunset/index';
2525
import { Hypridle } from '../../components/bar/modules/hypridle/index';
26+
import { Warp } from '../../components/bar/modules/warp/index';
2627

2728
export {
2829
Menu,
@@ -50,4 +51,5 @@ export {
5051
Power,
5152
Hyprsunset,
5253
Hypridle,
54+
Warp,
5355
};

src/components/bar/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Power,
2525
Hyprsunset,
2626
Hypridle,
27+
Warp
2728
} from './exports';
2829

2930
import { WidgetContainer } from './shared/WidgetContainer';
@@ -62,6 +63,7 @@ const widget = {
6263
power: (): JSX.Element => WidgetContainer(Power()),
6364
hyprsunset: (): JSX.Element => WidgetContainer(Hyprsunset()),
6465
hypridle: (): JSX.Element => WidgetContainer(Hypridle()),
66+
warp: (): JSX.Element => WidgetContainer(Warp()),
6567
};
6668

6769
export const Bar = (() => {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { execAsync, Variable } from "astal";
2+
3+
export const isWarpConnectCommand = `bash -c "warp-cli status | awk {'print $3'}"`
4+
export const isWarpDisconnectCommand = `bash -c "warp-cli status | head -1 | awk {'print $3'}"`
5+
6+
export const isWarpConnect = Variable(false);
7+
8+
export const toggleWarp = async (isWarpConnect: Variable<boolean>): Promise<void> => {
9+
try {
10+
await execAsync(`bash -c "warp-cli ${isWarpConnect.get() ? 'disconnect' : 'connect'}"`);
11+
12+
const res = await execAsync(isWarpConnect.get() ? isWarpConnectCommand : isWarpDisconnectCommand);
13+
isWarpConnect.set(isWarpConnect.get() ? res === 'Disconnected' : res === 'Connected');
14+
} catch (err) {
15+
await execAsync(`bash -c "notify-send -a 'hyprpanel' 'Warp service not active!' 'Error: ${err}'"`);
16+
}
17+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import options from "src/options";
2+
import { Module } from "../../shared/Module";
3+
import { inputHandler, throttleInput } from "../../utils/helpers";
4+
import { BarBoxChild } from "src/lib/types/bar";
5+
import { isWarpConnect, toggleWarp } from "./helpers";
6+
import { bind, Variable } from "astal";
7+
import { Astal } from "astal/gtk3";
8+
9+
const {
10+
label,
11+
onIcon,
12+
offIcon,
13+
onLabel,
14+
offLabel,
15+
rightClick,
16+
middleClick,
17+
scrollUp,
18+
scrollDown,
19+
} = options.bar.customModules.warp;
20+
21+
const thorttledToggleWarp = throttleInput(() => toggleWarp(isWarpConnect), 1000);
22+
23+
export const Warp = (): BarBoxChild => {
24+
const iconBinding = Variable.derive([bind(isWarpConnect), bind(onIcon), bind(offIcon)], (active, onIcn, offIcn) => {
25+
return active ? onIcn : offIcn;
26+
})
27+
28+
const tooltipBinding = Variable.derive([isWarpConnect], (active) => {
29+
return active ? 'Connect' : "Disconnect";
30+
});
31+
32+
const labelBinding = Variable.derive([bind(isWarpConnect), bind(onLabel), bind(offLabel)], (active, onLbl, offLbl) => {
33+
return active ? onLbl : offLbl;
34+
});
35+
36+
const warpModule = Module({
37+
textIcon: iconBinding(),
38+
tooltipText: tooltipBinding(),
39+
boxClass: 'warp',
40+
label: labelBinding(),
41+
showLabelBinding: bind(label),
42+
props: {
43+
setup: (self: Astal.Button) => {
44+
inputHandler(self, {
45+
onPrimaryClick: {
46+
fn: () => {
47+
thorttledToggleWarp();
48+
},
49+
},
50+
onSecondaryClick: {
51+
cmd: rightClick,
52+
},
53+
onMiddleClick: {
54+
cmd: middleClick,
55+
},
56+
onScrollUp: {
57+
cmd: scrollUp,
58+
},
59+
onScrollDown: {
60+
cmd: scrollDown,
61+
},
62+
});
63+
},
64+
onDestroy: () => {
65+
iconBinding.drop();
66+
tooltipBinding.drop();
67+
labelBinding.drop();
68+
},
69+
},
70+
});
71+
72+
return warpModule;
73+
};

src/lib/types/options.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export type BarModule =
4747
| 'power'
4848
| 'systray'
4949
| 'hypridle'
50-
| 'hyprsunset';
50+
| 'hyprsunset'
51+
'warp';
5152

5253
export type BarLayout = {
5354
left: BarModule[];

src/options.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@ const options = mkOptions(CONFIG, {
395395
icon_background: opt(colors.base2),
396396
spacing: opt('0.45em'),
397397
},
398+
warp: {
399+
enableBorder: opt(false),
400+
border: opt(colors.yellow),
401+
background: opt(colors.base2),
402+
text: opt(colors.yellow),
403+
icon: opt(colors.yellow),
404+
icon_background: opt(colors.base2),
405+
spacing: opt('0.45em'),
406+
}
398407
},
399408
},
400409
menus: {
@@ -1142,6 +1151,17 @@ const options = mkOptions(CONFIG, {
11421151
scrollUp: opt(''),
11431152
scrollDown: opt(''),
11441153
},
1154+
warp: {
1155+
label: opt(true),
1156+
onIcon: opt(''),
1157+
offIcon: opt('󰇖'),
1158+
onLabel: opt('On'),
1159+
offLabel: opt('Off'),
1160+
rightClick: opt(''),
1161+
middleClick: opt(''),
1162+
scrollUp: opt(''),
1163+
scrollDown: opt(''),
1164+
},
11451165
},
11461166
},
11471167

src/scss/style/bar/style.scss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,25 @@
448448
// custom font size
449449
1.075em //
450450
);
451+
452+
@include styleModule(
453+
//
454+
// class name
455+
'warp',
456+
// label color
457+
$bar-buttons-modules-warp-text,
458+
// icon color
459+
$bar-buttons-modules-warp-icon,
460+
// icon background if split style is used
461+
$bar-buttons-modules-warp-icon_background,
462+
// label background
463+
$bar-buttons-modules-warp-background,
464+
// inner spacing
465+
$bar-buttons-modules-warp-spacing,
466+
// if border enabled
467+
$bar-buttons-modules-warp-enableBorder,
468+
// border color
469+
$bar-buttons-modules-warp-border,
470+
// custom font size
471+
1.075em //
472+
);

0 commit comments

Comments
 (0)