diff --git a/docs/modules/Custom.md b/docs/modules/Custom.md index 7ec0001..fc4332d 100644 --- a/docs/modules/Custom.md +++ b/docs/modules/Custom.md @@ -17,11 +17,11 @@ You can think of these like HTML elements and their attributes. Every widget has the following options available; `type` is mandatory. -| Name | Type | Default | Description | -|---------|-----------------------------------------|---------|-------------------------------| -| `type` | `box` or `label` or `button` or `image` | `null` | Type of GTK widget to create. | -| `name` | `string` | `null` | Widget name. | -| `class` | `string` | `null` | Widget class name. | +| Name | Type | Default | Description | +|---------|-----------------------------------------------------|---------|-------------------------------| +| `type` | `box` or `label` or `button` or `image` or `slider` | `null` | Type of GTK widget to create. | +| `name` | `string` | `null` | Widget name. | +| `class` | `string` | `null` | Widget class name. | #### Box @@ -50,10 +50,10 @@ A clickable button, which can run a command when clicked. > Type `button` -| Name | Type | Default | Description | -|------------|----------|--------------|---------------------------------------------------------------------| -| `label` | `string` | `horizontal` | Widget text label. Pango markup and embedded scripts are supported. | -| `on_click` | `string` | `null` | Command to execute. More on this [below](#commands). | +| Name | Type | Default | Description | +|------------|--------------------|--------------|---------------------------------------------------------------------| +| `label` | `string` | `horizontal` | Widget text label. Pango markup and embedded scripts are supported. | +| `on_click` | `string [command]` | `null` | Command to execute. More on this [below](#commands). | #### Image @@ -66,8 +66,51 @@ An image or icon from disk or http. | `src` | `image` | `null` | Image source. See [here](images) for information on images. | | `size` | `integer` | `null` | Width/height of the image. Aspect ratio is preserved. | +#### Slider + +A draggable slider. + +> Type: `slider` + +Note that `on_change` will provide the **floating point** value as an argument. +If your input program requires an integer, you will need to round it. + +| Name | Type | Default | Description | +|---------------|----------------------------------------------------|--------------|------------------------------------------------------------------------------| +| `src` | `image` | `null` | Image source. See [here](images) for information on images. | +| `size` | `integer` | `null` | Width/height of the image. Aspect ratio is preserved. | +| `orientation` | `horizontal` or `vertical` (shorthand: `h` or `v`) | `horizontal` | Orientation of the slider. | +| `value` | `Script` | `null` | Script to run to get the slider value. Output must be a valid number. | +| `on_change` | `string [command]` | `null` | Command to execute when the slider changes. More on this [below](#commands). | +| `min` | `float` | `0` | Minimum slider value. | +| `max` | `float` | `100` | Maximum slider value. | +| `length` | `integer` | `null` | Slider length. GTK will automatically size if left unset. | + +Note that `on_change` will provide the **floating point** value as an argument. +If your input program requires an integer, you will need to round it. + +The example slider widget below shows a volume control for MPC, +which updates the server when changed, and polls the server for volume changes to keep the slider in sync. + +```corn +$slider = { + type = "custom" + bar = [ + { + type = "slider" + length = 100 + max = 100 + on_change="!mpc volume ${0%.*}" + value = "200:mpc volume | cut -d ':' -f2 | cut -d '%' -f1" + } + ] +} +``` + ### Label Attributes +> ℹ This is different to the `label` widget, although applies to it. + Any widgets with a `label` attribute support embedded scripts, meaning you can interpolate text from scripts to dynamically show content. @@ -90,6 +133,9 @@ To execute shell commands, prefix them with an `!`. For example, if you want to run `~/.local/bin/my-script.sh` on click, you'd set `on_click` to `!~/.local/bin/my-script.sh`. +Some widgets provide a value when they run the command, such as `slider`. +This is passed as an argument and can be accessed using `$0`. + The following bar commands are supported: - `popup:toggle` diff --git a/src/bar.rs b/src/bar.rs index fbb126e..98e82b7 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -373,7 +373,7 @@ fn setup_module_common_options(container: EventBox, common: CommonConfig) { let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); spawn(async move { script - .run(|(_, success)| { + .run(None, |_, success| { send!(tx, success); }) .await; @@ -456,7 +456,7 @@ fn setup_module_common_options(container: EventBox, common: CommonConfig) { } fn run_script(script: &Script) { - match await_sync(async { script.get_output().await }) { + match await_sync(async { script.get_output(None).await }) { Ok((OutputStream::Stderr(out), _)) => error!("{out}"), Err(err) => error!("{err:?}"), _ => {} diff --git a/src/dynamic_string.rs b/src/dynamic_string.rs index b3f7b79..43c7e89 100644 --- a/src/dynamic_string.rs +++ b/src/dynamic_string.rs @@ -79,7 +79,7 @@ impl DynamicString { spawn(async move { script - .run(|(out, _)| { + .run(None, |out, _| { if let OutputStream::Stdout(out) = out { let mut label_parts = lock!(label_parts); diff --git a/src/modules/clipboard.rs b/src/modules/clipboard.rs index 0357228..8307fe5 100644 --- a/src/modules/clipboard.rs +++ b/src/modules/clipboard.rs @@ -124,7 +124,7 @@ impl Module