Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Cloudflare warp custom module #598

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/bar/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Weather } from '../../components/bar/modules/weather/index';
import { Power } from '../../components/bar/modules/power/index';
import { Hyprsunset } from '../../components/bar/modules/hyprsunset/index';
import { Hypridle } from '../../components/bar/modules/hypridle/index';
import { Warp } from '../../components/bar/modules/warp/index';

export {
Menu,
Expand Down Expand Up @@ -50,4 +51,5 @@ export {
Power,
Hyprsunset,
Hypridle,
Warp,
};
2 changes: 2 additions & 0 deletions src/components/bar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Power,
Hyprsunset,
Hypridle,
Warp
} from './exports';

import { WidgetContainer } from './shared/WidgetContainer';
Expand Down Expand Up @@ -62,6 +63,7 @@ const widget = {
power: (): JSX.Element => WidgetContainer(Power()),
hyprsunset: (): JSX.Element => WidgetContainer(Hyprsunset()),
hypridle: (): JSX.Element => WidgetContainer(Hypridle()),
warp: (): JSX.Element => WidgetContainer(Warp()),
};

export const Bar = (() => {
Expand Down
23 changes: 23 additions & 0 deletions src/components/bar/modules/warp/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { execAsync, Variable } from "astal";

export const isWarpConnectCommand = `bash -c "warp-cli status | awk {'print $3'}"`
export const isWarpDisconnectCommand = `bash -c "warp-cli status | head -1 | awk {'print $3'}"`

export const isWarpConnect = Variable(false);

export const toggleWarp = async (isWarpConnect: Variable<boolean>): Promise<void> => {
try {
await execAsync(`bash -c "warp-cli ${isWarpConnect.get() ? 'disconnect' : 'connect'}"`);

const res = await execAsync(isWarpConnect.get() ? isWarpConnectCommand : isWarpDisconnectCommand);
isWarpConnect.set(isWarpConnect.get() ? res === 'Disconnected' : res === 'Connected');
} catch (err) {
await execAsync(`bash -c "notify-send -a 'hyprpanel' 'Warp service not active!' 'Error: ${err}'"`);
}
};

export const checkWarpStatus = (): undefined => {
execAsync(isWarpConnectCommand).then((res) => {
isWarpConnect.set(res === 'Connected');
});
};
83 changes: 83 additions & 0 deletions src/components/bar/modules/warp/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import options from "src/options";
import { Module } from "../../shared/Module";
import { inputHandler, throttleInput } from "../../utils/helpers";
import { BarBoxChild } from "src/lib/types/bar";
import { checkWarpStatus, isWarpConnect, toggleWarp } from "./helpers";
import { bind, Variable } from "astal";
import { Astal } from "astal/gtk3";
import { FunctionPoller } from "src/lib/poller/FunctionPoller";

const {
label,
pollingInterval,
onIcon,
offIcon,
onLabel,
offLabel,
rightClick,
middleClick,
scrollUp,
scrollDown,
} = options.bar.customModules.warp;

const dummyVar = Variable(undefined);

checkWarpStatus();

const warpPoller = new FunctionPoller<undefined, []>(dummyVar, [], bind(pollingInterval), checkWarpStatus);

warpPoller.initialize('warp');

const thorttledToggleWarp = throttleInput(() => toggleWarp(isWarpConnect), 1000);

export const Warp = (): BarBoxChild => {
const iconBinding = Variable.derive([bind(isWarpConnect), bind(onIcon), bind(offIcon)], (active, onIcn, offIcn) => {
return active ? onIcn : offIcn;
})

const tooltipBinding = Variable.derive([isWarpConnect], (active) => {
return active ? 'Connect' : "Disconnect";
});

const labelBinding = Variable.derive([bind(isWarpConnect), bind(onLabel), bind(offLabel)], (active, onLbl, offLbl) => {
return active ? onLbl : offLbl;
});

const warpModule = Module({
textIcon: iconBinding(),
tooltipText: tooltipBinding(),
boxClass: 'warp',
label: labelBinding(),
showLabelBinding: bind(label),
props: {
setup: (self: Astal.Button) => {
inputHandler(self, {
onPrimaryClick: {
fn: () => {
thorttledToggleWarp();
},
},
onSecondaryClick: {
cmd: rightClick,
},
onMiddleClick: {
cmd: middleClick,
},
onScrollUp: {
cmd: scrollUp,
},
onScrollDown: {
cmd: scrollDown,
},
});
},
onDestroy: () => {
iconBinding.drop();
tooltipBinding.drop();
labelBinding.drop();
},
},
});

return warpModule;
};
3 changes: 2 additions & 1 deletion src/lib/types/options.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export type BarModule =
| 'power'
| 'systray'
| 'hypridle'
| 'hyprsunset';
| 'hyprsunset'
'warp';

export type BarLayout = {
left: BarModule[];
Expand Down
21 changes: 21 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ const options = mkOptions(CONFIG, {
icon_background: opt(colors.base2),
spacing: opt('0.45em'),
},
warp: {
enableBorder: opt(false),
border: opt(colors.yellow),
background: opt(colors.base2),
text: opt(colors.yellow),
icon: opt(colors.yellow),
icon_background: opt(colors.base2),
spacing: opt('0.45em'),
}
},
},
menus: {
Expand Down Expand Up @@ -1142,6 +1151,18 @@ const options = mkOptions(CONFIG, {
scrollUp: opt(''),
scrollDown: opt(''),
},
warp: {
label: opt(true),
onIcon: opt(''),
offIcon: opt('󰇖'),
onLabel: opt('On'),
offLabel: opt('Off'),
pollingInterval: opt(1000 * 2),
rightClick: opt(''),
middleClick: opt(''),
scrollUp: opt(''),
scrollDown: opt(''),
},
},
},

Expand Down
22 changes: 22 additions & 0 deletions src/scss/style/bar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,25 @@
// custom font size
1.075em //
);

@include styleModule(
//
// class name
'warp',
// label color
$bar-buttons-modules-warp-text,
// icon color
$bar-buttons-modules-warp-icon,
// icon background if split style is used
$bar-buttons-modules-warp-icon_background,
// label background
$bar-buttons-modules-warp-background,
// inner spacing
$bar-buttons-modules-warp-spacing,
// if border enabled
$bar-buttons-modules-warp-enableBorder,
// border color
$bar-buttons-modules-warp-border,
// custom font size
1.075em //
);