Skip to content

Commit

Permalink
feat: create providers for cpu, memory, host, network, and `b…
Browse files Browse the repository at this point in the history
…attery` (#9)
  • Loading branch information
lars-berger committed Nov 13, 2023
1 parent fa436cd commit 7e0ed55
Show file tree
Hide file tree
Showing 61 changed files with 1,784 additions and 333 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"rust-analyzer.linkedProjects": ["packages/desktop/Cargo.toml"],
"cssModules.camelCase": true
"cssModules.camelCase": true,
"rust-analyzer.showUnlinkedFileNotification": false
}
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ Zebar is a way to create customizable and cross-platform taskbars, desktop widge

## Contributing

To set up the project:
Zebar uses the Tauri desktop framework and requires Node.js + Rust.

### Installing Rust

[`rustup`](https://rustup.rs/) is the recommended way to set up the Rust toolchain. It can be installed via:

```shell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

### Installing Node.js

Install Node.js via [NVM](https://github.com/nvm-sh/nvm#installing-and-updating) (recommended for easily switching between Node.js versions) or the [official download](https://nodejs.org/en/download).

### Development

To start the project in development mode:

```shell
# Install pnpm (package manager) if not already installed.
Expand All @@ -16,3 +32,16 @@ pnpm i
# Start in development mode.
pnpm dev
```

## Providers

### Battery

### Host

- `hostname` -
- `os_name` - Name of the operating system. This is 'Darwin' on MacOS, 'Windows' on Windows, or the Linux distro name retrieved from either `/etc/os-release` or `/etc/lsb-release` (eg. 'Debian GNU/Linux' on Debian).
- `os_version` - Operating system version. This is the version number on MacOS (eg. '13.2.1'), the major version + build number on Windows (eg. '11 22000'), or the Linux distro version retrieved from either `/etc/os-release` or `/etc/lsb-release` (eg. '9' on Debian 9).
- `friendly_os_version` - Friendly name of operating system version (eg. 'MacOS 13.2.1', 'Windows 10 Pro', 'Linux Debian GNU/Linux 9').
- `boot_time` - Time when the system booted since UNIX epoch in milliseconds (eg. `1699452379304`).
- `uptime` - Time in milliseconds since boot.
2 changes: 1 addition & 1 deletion packages/client-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"@tauri-apps/api": "1.4.0",
"@tauri-apps/api": "1.5.1",
"axios": "1.4.0",
"glazewm": "1.1.1",
"solid-js": "1.8.4",
Expand Down
4 changes: 0 additions & 4 deletions packages/client-api/src/context/create-context-function.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/client-api/src/context/create-context-variable.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/client-api/src/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export * from './create-context-function';
export * from './create-context-store';
export * from './create-context-variable';
export * from './shared/element-context.model';
export * from './shared/element-type.model';
export * from './providers';
Original file line number Diff line number Diff line change
@@ -1,17 +1,83 @@
import { createEffect } from 'solid-js';
import { createStore } from 'solid-js/store';

import { listenProvider, onProviderEmit, unlistenProvider } from '~/desktop';
import {
BatteryProviderOptions,
BatteryProviderOptionsSchema,
} from '~/user-config';
import { memoize } from '~/utils';
import { memoize, simpleHash } from '~/utils';

const DEFAULT = BatteryProviderOptionsSchema.parse({});

export interface BatteryVariables {
isLoading: boolean;
chargePercent: number;
cycleCount: number;
healthPercent: number;
powerConsumption: number;
state: 'discharging' | 'charging';
timeTillEmpty: number | null;
timeTillFull: number | null;
voltage: number | null;
}

export const createBatteryProvider = memoize(
(options: BatteryProviderOptions = DEFAULT) => {
const [batteryVariables, setBatteryVariables] =
createStore<BatteryVariables>({
isLoading: true,
chargePercent: 0,
cycleCount: 0,
healthPercent: 0,
powerConsumption: 0,
state: 'discharging',
timeTillEmpty: null,
timeTillFull: null,
voltage: null,
});

createEffect(async () => {
const optionsHash = simpleHash(options);

onProviderEmit<BatteryVariables>(optionsHash, payload =>
setBatteryVariables({ ...payload, isLoading: false }),
);

await listenProvider({
optionsHash,
options,
trackedAccess: [],
});

return () => unlistenProvider(optionsHash);
});

return {
percent: '',
is_charging: true,
has_battery: true,
get chargePercent() {
return batteryVariables.chargePercent;
},
get cycleCount() {
return batteryVariables.cycleCount;
},
get healthPercent() {
return batteryVariables.healthPercent;
},
get powerConsumption() {
return batteryVariables.powerConsumption;
},
get state() {
return batteryVariables.state;
},
get timeTillEmpty() {
return batteryVariables.timeTillEmpty;
},
get timeTillFull() {
return batteryVariables.timeTillFull;
},
get voltage() {
return batteryVariables.voltage;
},
};
},
);
Original file line number Diff line number Diff line change
@@ -1,14 +1,64 @@
import { createEffect } from 'solid-js';
import { createStore } from 'solid-js/store';

import { onProviderEmit, listenProvider, unlistenProvider } from '~/desktop';
import { CpuProviderOptions, CpuProviderOptionsSchema } from '~/user-config';
import { memoize } from '~/utils';
import { memoize, simpleHash } from '~/utils';

const DEFAULT = CpuProviderOptionsSchema.parse({});

export interface CpuVariables {
isLoading: boolean;
frequency: number;
usage: number;
logicalCoreCount: number;
physicalCoreCount: number;
vendor: string;
}

export const createCpuProvider = memoize(
(options: CpuProviderOptions = DEFAULT) => {
return {
usage: 0,
temp: 0,
const [cpuVariables, setCpuVariables] = createStore<CpuVariables>({
isLoading: true,
frequency: 0,
usage: 0,
logicalCoreCount: 0,
physicalCoreCount: 0,
vendor: '',
});

createEffect(async () => {
const optionsHash = simpleHash(options);

onProviderEmit<CpuVariables>(optionsHash, payload =>
setCpuVariables({ ...payload, isLoading: false }),
);

await listenProvider({
optionsHash,
options,
trackedAccess: [],
});

return () => unlistenProvider(optionsHash);
});

return {
get frequency() {
return cpuVariables.frequency;
},
get usage() {
return cpuVariables.usage;
},
get logicalCoreCount() {
return cpuVariables.logicalCoreCount;
},
get physicalCoreCount() {
return cpuVariables.physicalCoreCount;
},
get vendor() {
return cpuVariables.vendor;
},
};
},
);
3 changes: 3 additions & 0 deletions packages/client-api/src/context/providers/create-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createBatteryProvider } from './battery/create-battery-provider';
import { createCpuProvider } from './cpu/create-cpu-provider';
import { createDateTimeProvider } from './date-time/create-date-time-provider';
import { createGlazewmProvider } from './glazewm/create-glazewm-provider';
import { createHostProvider } from './host/create-host-provider';
import { createIpProvider } from './ip/create-ip-provider';
import { createMemoryProvider } from './memory/create-memory-provider';
import { createNetworkProvider } from './network/create-network-provider';
Expand All @@ -23,6 +24,8 @@ export const createProvider = memoize((config: ProviderConfig) => {
return createDateTimeProvider(config);
case 'glazewm':
return createGlazewmProvider(config);
case 'host':
return createHostProvider(config);
case 'ip':
return createIpProvider(config);
case 'memory':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { createEffect } from 'solid-js';
import { createStore } from 'solid-js/store';

import { onProviderEmit, listenProvider, unlistenProvider } from '~/desktop';
import { HostProviderOptions, HostProviderOptionsSchema } from '~/user-config';
import { memoize, simpleHash } from '~/utils';

const DEFAULT = HostProviderOptionsSchema.parse({});

export interface HostVariables {
isLoading: boolean;
hostname: string | null;
osName: string | null;
osVersion: string | null;
friendlyOsVersion: string | null;
bootTime: number;
uptime: number;
}

export const createHostProvider = memoize(
(options: HostProviderOptions = DEFAULT) => {
const [hostVariables, setHostVariables] = createStore<HostVariables>({
isLoading: true,
hostname: null,
osName: null,
osVersion: null,
friendlyOsVersion: null,
bootTime: 0,
uptime: 0,
});

createEffect(async () => {
const optionsHash = simpleHash(options);

onProviderEmit<HostVariables>(optionsHash, payload =>
setHostVariables({ ...payload, isLoading: false }),
);

await listenProvider({
optionsHash,
options,
trackedAccess: [],
});

return () => unlistenProvider(optionsHash);
});

return {
get hostname() {
return hostVariables.hostname;
},
get osName() {
return hostVariables.osName;
},
get osVersion() {
return hostVariables.osVersion;
},
get friendlyOsVersion() {
return hostVariables.friendlyOsVersion;
},
get bootTime() {
return hostVariables.bootTime;
},
get uptime() {
return hostVariables.uptime;
},
};
},
);
Loading

0 comments on commit 7e0ed55

Please sign in to comment.