From dea66415c2e11e34ba44d016aaa6cfb4ef7b9f9b Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sat, 6 May 2023 00:40:06 +0100 Subject: [PATCH] feat: module-level `name` and `class` options BREAKING CHANGE: To allow for the `name` property, any widgets that were previously targeted by name should be targeted by class instead. This affects **all modules and all popups**, as well as several widgets inside modules. **This will break a lot of rules in your stylesheet**. To attempt to mitigate the damage, a migration script can be found [here](https://raw.githubusercontent.com/JakeStanger/ironbar/master/scripts/migrate-styles.sh) that should get you most of the way. Resolves #75. --- docs/Configuration guide.md | 11 +++--- docs/Styling guide.md | 38 +++++++++++++++----- docs/modules/Clipboard.md | 18 +++++----- docs/modules/Clock.md | 10 +++--- docs/modules/Custom.md | 13 ++++--- docs/modules/Focused.md | 12 ++++--- docs/modules/Label.md | 8 +++-- docs/modules/Launcher.md | 16 +++++---- docs/modules/Music.md | 44 ++++++++++++----------- docs/modules/Script.md | 8 +++-- docs/modules/Sys-Info.md | 6 ++-- docs/modules/Tray.md | 6 ++-- docs/modules/Upower.md | 14 ++++---- docs/modules/Workspaces.md | 8 +++-- examples/style.css | 68 +++++++++++++++++------------------ scripts/migrate-styles.sh | 72 +++++++++++++++++++++++++++++++++++++ src/bar.rs | 10 ++++-- src/config/common.rs | 7 ++-- src/gtk_helpers.rs | 8 +++++ src/image/gtk.rs | 7 ++-- src/main.rs | 1 + src/modules/clipboard.rs | 6 +--- src/modules/clock.rs | 17 ++++----- src/modules/custom/mod.rs | 14 +------- src/modules/focused.rs | 8 +++-- src/modules/launcher/mod.rs | 5 +-- src/modules/mod.rs | 54 +++++++++++++++++++++------- src/modules/music/mod.rs | 50 +++++++++++--------------- src/modules/sysinfo.rs | 10 +++--- src/modules/upower.rs | 20 +++++------ 30 files changed, 353 insertions(+), 216 deletions(-) create mode 100755 scripts/migrate-styles.sh create mode 100644 src/gtk_helpers.rs diff --git a/docs/Configuration guide.md b/docs/Configuration guide.md index a5b77cee..a4e1684b 100644 --- a/docs/Configuration guide.md +++ b/docs/Configuration guide.md @@ -310,9 +310,12 @@ For information on the `Script` type, and embedding scripts in strings, see [her | `transition_type` | `slide_start` or `slide_end` or `crossfade` or `none` | `slide_start` | The transition animation to use when showing/hiding the widget. | | `transition_duration` | `Integer` | `250` | The length of the transition animation to use when showing/hiding the widget. | -#### Other +#### Appearance +| Name | Type | Default | Description | +|-----------|--------------------|---------|-----------------------------------------------------------------------------------| +| `tooltip` | `string` | `null` | Shows this text on hover. Supports embedding scripts between `{{double braces}}`. | +| `name` | `string` | `null` | Sets the unique widget name, allowing you to style it using `#name`. | +| `class` | `string` | `null` | Sets one or more CSS classes, allowing you to style it using `.class`. | -| Name | Type | Default | Description | -|-------------------|--------------------|---------|--------------------------------------------------------------------------------------------------------------------| -| `tooltip` | `string` | `null` | Shows this text on hover. Supports embedding scripts between `{{double braces}}`. | \ No newline at end of file +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/Styling guide.md b/docs/Styling guide.md index 3c350e84..233b3577 100644 --- a/docs/Styling guide.md +++ b/docs/Styling guide.md @@ -4,17 +4,37 @@ To style the bar, create a file at `~/.config/ironbar/style.css`. Style changes are hot-loaded so there is no need to reload the bar. -A reminder: since the bar is GTK-based, it uses GTK's implementation of CSS, +Since the bar is GTK-based, it uses [GTK's implementation of CSS](https://docs.gtk.org/gtk3/css-overview.html), which only includes a subset of the full web spec (plus a few non-standard properties). The below table describes the selectors provided by the bar itself. Information on styling individual modules can be found on their pages in the sidebar. -| Selector | Description | -|----------------|-------------------------------------------| -| `.background` | Top-level window | -| `#bar` | Bar root box | -| `#bar #start` | Bar left or top modules container box | -| `#bar #center` | Bar center modules container box | -| `#bar #end` | Bar right or bottom modules container box | -| `.container` | All of the above | +| Selector | Description | +|----------------|--------------------------------------------| +| `.background` | Top-level window. | +| `#bar` | Bar root box. | +| `#bar #start` | Bar left or top modules container box. | +| `#bar #center` | Bar center modules container box. | +| `#bar #end` | Bar right or bottom modules container box. | +| `.container` | All of the above. | +| `.popup` | Any popup box. | + +Every widget can be selected using a `kebab-case` class name matching its name. +You can also target popups by prefixing `popup-` to the name. For example, you can use `.clock` and `.popup-clock` respectively. + +Setting the `name` option on a widget allows you to target that specific instance using `#name`. +You can also add additional classes to re-use styles. In both cases, `popup-` is automatically prefixed to the popup (`#popup-name` or `.popup-my-class`). + +You can also target all GTK widgets of a certain type directly using their name. For example, `button:hover` will select the hover state on *all* buttons. +These names are all lower case with no separator, so `MenuBar` -> `menubar`. + +GTK CSS does not support custom properties, but it does have its own custom `@define-color` syntax which you can use for re-using colours: + +```css +@define-color color_bg #2d2d2d; + +box, menubar { + background-color: @color_bg; +} +``` \ No newline at end of file diff --git a/docs/modules/Clipboard.md b/docs/modules/Clipboard.md index 876f296e..bedac423 100644 --- a/docs/modules/Clipboard.md +++ b/docs/modules/Clipboard.md @@ -84,11 +84,13 @@ end: | Selector | Description | |--------------------------------------|------------------------------------------------------| -| `#clipboard` | Clipboard widget. | -| `#clipboard .btn` | Clipboard widget button. | -| `#popup-clipboard` | Clipboard popup box. | -| `#popup-clipboard .item` | Clipboard row item inside the popup. | -| `#popup-clipboard .item .btn` | Clipboard row item radio button. | -| `#popup-clipboard .item .btn.text` | Clipboard row item radio button (text values only). | -| `#popup-clipboard .item .btn.image` | Clipboard row item radio button (image values only). | -| `#popup-clipboard .item .btn-remove` | Clipboard row item remove button. | \ No newline at end of file +| `.clipboard` | Clipboard widget. | +| `.clipboard .btn` | Clipboard widget button. | +| `.popup-clipboard` | Clipboard popup box. | +| `.popup-clipboard .item` | Clipboard row item inside the popup. | +| `.popup-clipboard .item .btn` | Clipboard row item radio button. | +| `.popup-clipboard .item .btn.text` | Clipboard row item radio button (text values only). | +| `.popup-clipboard .item .btn.image` | Clipboard row item radio button (image values only). | +| `.popup-clipboard .item .btn-remove` | Clipboard row item remove button. | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Clock.md b/docs/modules/Clock.md index 99a944b7..a513987d 100644 --- a/docs/modules/Clock.md +++ b/docs/modules/Clock.md @@ -71,7 +71,9 @@ end: | Selector | Description | |--------------------------------|------------------------------------------------------------------------------------| -| `#clock` | Clock widget button | -| `#popup-clock` | Clock popup box | -| `#popup-clock #calendar-clock` | Clock inside the popup | -| `#popup-clock #calendar` | Calendar widget inside the popup. GTK provides some OOTB styling options for this. | \ No newline at end of file +| `.clock` | Clock widget button | +| `.popup-clock` | Clock popup box | +| `.popup-clock .calendar-clock` | Clock inside the popup | +| `.popup-clock .calendar` | Calendar widget inside the popup. GTK provides some OOTB styling options for this. | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Custom.md b/docs/modules/Custom.md index 52588b95..bd42ec4b 100644 --- a/docs/modules/Custom.md +++ b/docs/modules/Custom.md @@ -392,10 +392,13 @@ let { ## Styling -Since the widgets are all custom, you can use the `name` and `class` attributes, then target them using `#name` and `.class`. +Since the widgets are all custom, you can use their `name` and `class` attributes, then target them using `#name` and `.class`. -The following top-level selector is always available: +The following top-level selectors are always available: -| Selector | Description | -|-----------|-------------------------| -| `#custom` | Custom widget container | \ No newline at end of file +| Selector | Description | +|-----------------|--------------------------------| +| `.custom` | Custom widget container. | +| `.popup-custom` | Custom widget popup container. | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Focused.md b/docs/modules/Focused.md index 514cbcf0..4172e5f0 100644 --- a/docs/modules/Focused.md +++ b/docs/modules/Focused.md @@ -86,8 +86,10 @@ end: ## Styling -| Selector | Description | -|--------------------------|--------------------| -| `#focused` | Focused widget box | -| `#focused #icon` | App icon | -| `#focused #label` | App name | \ No newline at end of file +| Selector | Description | +|-------------------|--------------------| +| `.focused` | Focused widget box | +| `.focused .icon` | App icon | +| `.focused .label` | App name | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Label.md b/docs/modules/Label.md index 228a5743..2196c195 100644 --- a/docs/modules/Label.md +++ b/docs/modules/Label.md @@ -65,6 +65,8 @@ end: ## Styling -| Selector | Description | -|--------------------------------|------------------------------------------------------------------------------------| -| `#label` | Label widget | \ No newline at end of file +| Selector | Description | +|----------|------------------------------------------------------------------------------------| +| `.label` | Label widget | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Launcher.md b/docs/modules/Launcher.md index 8b0421e2..8e0d7d3e 100644 --- a/docs/modules/Launcher.md +++ b/docs/modules/Launcher.md @@ -90,10 +90,12 @@ start: | Selector | Description | |-------------------------------|--------------------------| -| `#launcher` | Launcher widget box | -| `#launcher .item` | App button | -| `#launcher .item.open` | App button (open app) | -| `#launcher .item.focused` | App button (focused app) | -| `#launcher .item.urgent` | App button (urgent app) | -| `#launcher-popup` | Popup container | -| `#launcher-popup .popup-item` | Window button in popup | +| `.launcher` | Launcher widget box | +| `.launcher .item` | App button | +| `.launcher .item.open` | App button (open app) | +| `.launcher .item.focused` | App button (focused app) | +| `.launcher .item.urgent` | App button (urgent app) | +| `.popup-launcher` | Popup container | +| `.popup-launcher .popup-item` | Window button in popup | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Music.md b/docs/modules/Music.md index f4dc390c..62ab6e06 100644 --- a/docs/modules/Music.md +++ b/docs/modules/Music.md @@ -135,24 +135,26 @@ and will be replaced with values from the currently playing track: | Selector | Description | |-------------------------------------|------------------------------------------| -| `#music` | Tray widget button | -| `#music #contents` | Tray widget button contents box | -| `#popup-music` | Popup box | -| `#popup-music #album-art` | Album art image inside popup box | -| `#popup-music #title` | Track title container inside popup box | -| `#popup-music #title .icon` | Track title icon label inside popup box | -| `#popup-music #title .label` | Track title label inside popup box | -| `#popup-music #album` | Track album container inside popup box | -| `#popup-music #album .icon` | Track album icon label inside popup box | -| `#popup-music #album .label` | Track album label inside popup box | -| `#popup-music #artist` | Track artist container inside popup box | -| `#popup-music #artist .icon` | Track artist icon label inside popup box | -| `#popup-music #artist .label` | Track artist label inside popup box | -| `#popup-music #controls` | Controls container inside popup box | -| `#popup-music #controls #btn-prev` | Previous button inside popup box | -| `#popup-music #controls #btn-play` | Play button inside popup box | -| `#popup-music #controls #btn-pause` | Pause button inside popup box | -| `#popup-music #controls #btn-next` | Next button inside popup box | -| `#popup-music #volume` | Volume container inside popup box | -| `#popup-music #volume #slider` | Volume slider popup box | -| `#popup-music #volume .icon` | Volume icon label inside popup box | \ No newline at end of file +| `.music` | Tray widget button | +| `.music .contents` | Tray widget button contents box | +| `.popup-music` | Popup box | +| `.popup-music .album-art` | Album art image inside popup box | +| `.popup-music .title` | Track title container inside popup box | +| `.popup-music .title .icon` | Track title icon label inside popup box | +| `.popup-music .title .label` | Track title label inside popup box | +| `.popup-music .album` | Track album container inside popup box | +| `.popup-music .album .icon` | Track album icon label inside popup box | +| `.popup-music .album .label` | Track album label inside popup box | +| `.popup-music .artist` | Track artist container inside popup box | +| `.popup-music .artist .icon` | Track artist icon label inside popup box | +| `.popup-music .artist .label` | Track artist label inside popup box | +| `.popup-music .controls` | Controls container inside popup box | +| `.popup-music .controls .btn-prev` | Previous button inside popup box | +| `.popup-music .controls .btn-play` | Play button inside popup box | +| `.popup-music .controls .btn-pause` | Pause button inside popup box | +| `.popup-music .controls .btn-next` | Next button inside popup box | +| `.popup-music .volume` | Volume container inside popup box | +| `.popup-music .volume .slider` | Volume slider popup box | +| `.popup-music .volume .icon` | Volume icon label inside popup box | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Script.md b/docs/modules/Script.md index dba8bb47..8adea5bc 100644 --- a/docs/modules/Script.md +++ b/docs/modules/Script.md @@ -82,6 +82,8 @@ end: ## Styling -| Selector | Description | -|---------------|---------------------| -| `#script` | Script widget label | \ No newline at end of file +| Selector | Description | +|-----------|---------------------| +| `.script` | Script widget label | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Sys-Info.md b/docs/modules/Sys-Info.md index 10e35884..dbfe7736 100644 --- a/docs/modules/Sys-Info.md +++ b/docs/modules/Sys-Info.md @@ -172,5 +172,7 @@ The following tokens can be used in the `format` configuration option: | Selector | Description | |------------------|------------------------------| -| `#sysinfo` | Sysinfo widget box | -| `#sysinfo #item` | Individual information label | \ No newline at end of file +| `.sysinfo` | Sysinfo widget box | +| `.sysinfo .item` | Individual information label | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Tray.md b/docs/modules/Tray.md index 0329bc97..7115b759 100644 --- a/docs/modules/Tray.md +++ b/docs/modules/Tray.md @@ -60,5 +60,7 @@ end: | Selector | Description | |---------------|------------------| -| `#tray` | Tray widget box | -| `#tray .item` | Tray icon button | +| `.tray` | Tray widget box | +| `.tray .item` | Tray icon button | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Upower.md b/docs/modules/Upower.md index 123d5fc6..1d91a6e3 100644 --- a/docs/modules/Upower.md +++ b/docs/modules/Upower.md @@ -72,9 +72,11 @@ end: | Selector | Description | |---------------------------------|-----------------------------| -| `#upower` | Upower widget container. | -| `#upower #icon` | Upower widget battery icon. | -| `#upower #button` | Upower widget button. | -| `#upower #button #label` | Upower widget button label. | -| `#popup-upower` | Upower popup box. | -| `#popup-upower #upower-details` | Label inside the popup. | +| `.upower` | Upower widget container. | +| `.upower .icon` | Upower widget battery icon. | +| `.upower .button` | Upower widget button. | +| `.upower .button .label` | Upower widget button label. | +| `.popup-upower` | Upower popup box. | +| `.popup-upower .upower-details` | Label inside the popup. | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/docs/modules/Workspaces.md b/docs/modules/Workspaces.md index 7509e66b..1a9d8ded 100644 --- a/docs/modules/Workspaces.md +++ b/docs/modules/Workspaces.md @@ -91,6 +91,8 @@ end: | Selector | Description | |-----------------------------|--------------------------------------| -| `#workspaces` | Workspaces widget box | -| `#workspaces .item` | Workspace button | -| `#workspaces .item.focused` | Workspace button (workspace focused) | \ No newline at end of file +| `.workspaces` | Workspaces widget box | +| `.workspaces .item` | Workspace button | +| `.workspaces .item.focused` | Workspace button (workspace focused) | + +For more information on styling, please see the [styling guide](styling-guide). \ No newline at end of file diff --git a/examples/style.css b/examples/style.css index f625d911..1eaa051f 100644 --- a/examples/style.css +++ b/examples/style.css @@ -14,16 +14,11 @@ border-radius: 0; } -box, menubar { +box, menubar, button { background-color: @color_bg; } -button { - color: @color_text; - background-color: @color_bg; -} - -label { +button, label { color: @color_text; } @@ -43,12 +38,12 @@ button:hover { /* -- clipboard -- */ -#clipboard { +.clipboard { margin-left: 5px; font-size: 1.1em; } -#popup-clipboard .item { +.popup-clipboard .item { padding-bottom: 0.3em; border-bottom: 1px solid @color_border; } @@ -56,125 +51,125 @@ button:hover { /* -- clock -- */ -#clock { +.clock { font-weight: bold; margin-left: 5px; } -#calendar-clock { +.popup-clock .calendar-clock { color: @color_text; font-size: 2.5em; padding-bottom: 0.1em; } -#popup-clock #calendar { +.popup-clock .calendar { background-color: @color_bg; color: @color_text; } -#popup-clock #calendar .header { +.popup-clock .calendar .header { padding-top: 1em; border-top: 1px solid @color_border; font-size: 1.5em; } -#popup-clock #calendar:selected { +.popup-clock .calendar:selected { background-color: @color_border_active; } /* -- launcher -- */ -#launcher .item { +.launcher .item { margin-right: 4px; } -#launcher .item:not(.focused):hover { +.launcher .item:not(.focused):hover { background-color: @color_bg_dark; } -#launcher .open { - border-bottom: 2px solid @color_text; +.launcher .open { + border-bottom: 1px solid @color_text; } -#launcher .focused { - border-bottom-color: @color_border_active; +.launcher .focused { + border-bottom: 2px solid @color_border_active; } -#launcher .urgent { +.launcher .urgent { border-bottom-color: @color_urgent; } -#popup-launcher { +.popup-launcher { padding: 0; } -#popup-launcher .popup-item:not(:first-child) { +.popup-launcher .popup-item:not(:first-child) { border-top: 1px solid @color_border; } /* -- music -- */ -#music:hover * { +.music:hover * { background-color: @color_bg_dark; } -#popup-music #album-art { +.popup-music .album-art { margin-right: 1em; } -#popup-music #title .icon *, #popup-music #title .label { +.popup-music .title .icon *, .popup-music .title .label { font-size: 1.7em; } -#popup-music #controls *:disabled { +.popup-music .controls *:disabled { color: @color_border; } -#popup-music #volume scale slider { +.popup-music .volume scale slider { border-radius: 100%; } /* volume icon */ -#popup-music #volume > box:last-child label { +.popup-music .volume > box:last-child label { margin-left: 6px; } /* -- script -- */ -#script { +.script { padding-left: 10px; } /* -- sys_info -- */ -#sysinfo { +.sysinfo { margin-left: 10px; } -#sysinfo #item { +.sysinfo .item { margin-left: 5px; } /* -- tray -- */ -#tray { +.tray { margin-left: 10px; } /* -- workspaces -- */ -#workspaces .item.focused { +.workspaces .item.focused { box-shadow: inset 0 -3px; background-color: @color_bg_dark; } -#workspaces .item:hover { +.workspaces .item:hover { box-shadow: inset 0 -3px; } @@ -185,7 +180,7 @@ button:hover { font-size: 1.4em; padding-bottom: 0.4em; margin-bottom: 0.6em; - border-bottom: 1px solid @color_border + border-bottom: 1px solid @color_border; } .popup-power-menu .power-btn { @@ -196,3 +191,4 @@ button:hover { .popup-power-menu #buttons > *:nth-child(1) .power-btn { margin-right: 1em; } + diff --git a/scripts/migrate-styles.sh b/scripts/migrate-styles.sh new file mode 100755 index 00000000..c498da2c --- /dev/null +++ b/scripts/migrate-styles.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash + +# Migrates CSS selectors from widget names to CSS classes. +# These changed as part of the 0.12 release. + +# ⚠ This script will **NOT** check for custom styles and may mangle them! +# ⚠ It is *highly recommended* that you back up your existing styles before running this! + +style_path="$HOME/.config/ironbar/style.css" + +# general +sed -i 's/#icon/.icon/g' "$style_path" +sed -i 's/#label/.label/g' "$style_path" +sed -i 's/#image/.image/g' "$style_path" + +# clipboard +sed -i 's/#clipboard/.clipboard/g' "$style_path" +sed -i 's/#popup-clipboard/.popup-clipboard/g' "$style_path" + +# clock +sed -i 's/#clock/.clock/g' "$style_path" +sed -i 's/#popup-clock/.popup-clock/g' "$style_path" +sed -i 's/#calendar-clock/.calendar-clock/g' "$style_path" +sed -i 's/#calendar/.calendar/g' "$style_path" + +# custom +sed -i 's/#custom/.custom/g' "$style_path" +sed -i 's/#popup-custom/.popup-custom/g' "$style_path" + +# focused +sed -i 's/#focused/.focused/g' "$style_path" + +# launcher +sed -i 's/#launcher/.launcher/g' "$style_path" +sed -i 's/#popup-launcher/.popup-launcher/g' "$style_path" +sed -i 's/#launcher-popup/.popup-launcher/g' "$style_path" # was incorrect in docs + +# music +sed -i 's/#music/.music/g' "$style_path" +sed -i 's/#contents/.contents/g' "$style_path" +sed -i 's/#popup-music/.popup-music/g' "$style_path" +sed -i 's/#album-art/.album-art/g' "$style_path" +sed -i 's/#title/.title/g' "$style_path" +sed -i 's/#album/.album/g' "$style_path" +sed -i 's/#artist/.artist/g' "$style_path" +sed -i 's/#controls/.controls/g' "$style_path" +sed -i 's/#btn-prev/.btn-prev/g' "$style_path" +sed -i 's/#btn-play/.btn-play/g' "$style_path" +sed -i 's/#btn-pause/.btn-pause/g' "$style_path" +sed -i 's/#btn-next/.btn-next/g' "$style_path" +sed -i 's/#volume/.volume/g' "$style_path" +sed -i 's/#slider/.slider/g' "$style_path" + +# script +sed -i 's/#script/.script/g' "$style_path" + +# sys_info +sed -i 's/#sysinfo/.sysinfo/g' "$style_path" +sed -i 's/#item/.item/g' "$style_path" + +# tray +sed -i 's/#tray/.tray/g' "$style_path" + +# upower +sed -i 's/#upower/.upower/g' "$style_path" +sed -i 's/#button/.button/g' "$style_path" +sed -i 's/#popup-upower/.popup-upower/g' "$style_path" +sed -i 's/#upower-details/.upower-details/g' "$style_path" + +# workspaces +sed -i 's/#workspaces/.workspaces/g' "$style_path" +sed -i 's/#item/.item/g' "$style_path" \ No newline at end of file diff --git a/src/bar.rs b/src/bar.rs index 9a46fd6a..34cc8de2 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -1,5 +1,7 @@ use crate::config::{BarPosition, MarginConfig, ModuleConfig}; -use crate::modules::{create_module, wrap_widget, ModuleInfo, ModuleLocation}; +use crate::modules::{ + create_module, set_widget_identifiers, wrap_widget, ModuleInfo, ModuleLocation, +}; use crate::popup::Popup; use crate::Config; use color_eyre::Result; @@ -195,8 +197,10 @@ fn add_modules( macro_rules! add_module { ($module:expr, $id:expr) => {{ let common = $module.common.take().expect("Common config did not exist"); - let widget = create_module(*$module, $id, &info, &Arc::clone(&popup))?; - let container = wrap_widget(&widget, common, orientation); + let widget_parts = create_module(*$module, $id, &info, &Arc::clone(&popup))?; + set_widget_identifiers(&widget_parts, &common); + + let container = wrap_widget(&widget_parts.widget, common, orientation); content.add(&container); }}; } diff --git a/src/config/common.rs b/src/config/common.rs index 0188cd3a..3bfe0853 100644 --- a/src/config/common.rs +++ b/src/config/common.rs @@ -10,8 +10,11 @@ use tracing::trace; /// Common configuration options /// which can be set on every module. -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Default, Deserialize, Clone)] pub struct CommonConfig { + pub class: Option, + pub name: Option, + pub show_if: Option, pub transition_type: Option, pub transition_duration: Option, @@ -54,7 +57,7 @@ impl TransitionType { impl CommonConfig { /// Configures the module's container according to the common config options. - pub fn install(mut self, container: &EventBox, revealer: &Revealer) { + pub fn install_events(mut self, container: &EventBox, revealer: &Revealer) { self.install_show_if(container, revealer); let left_click_script = self.on_click_left.map(Script::new_polling); diff --git a/src/gtk_helpers.rs b/src/gtk_helpers.rs new file mode 100644 index 00000000..6da9b280 --- /dev/null +++ b/src/gtk_helpers.rs @@ -0,0 +1,8 @@ +use glib::IsA; +use gtk::prelude::*; +use gtk::Widget; + +/// Adds a new CSS class to a widget. +pub fn add_class>(widget: &W, class: &str) { + widget.style_context().add_class(class); +} diff --git a/src/image/gtk.rs b/src/image/gtk.rs index 06c62ed4..aabbc959 100644 --- a/src/image/gtk.rs +++ b/src/image/gtk.rs @@ -1,4 +1,5 @@ use super::ImageProvider; +use crate::gtk_helpers::add_class; use gtk::prelude::*; use gtk::{Button, IconTheme, Image, Label, Orientation}; use tracing::error; @@ -9,7 +10,7 @@ pub fn new_icon_button(input: &str, icon_theme: &IconTheme, size: i32) -> Button if ImageProvider::is_definitely_image_input(input) { let image = Image::new(); - image.set_widget_name("image"); + add_class(&image, "image"); match ImageProvider::parse(input, icon_theme, size) .and_then(|provider| provider.load_into_image(image.clone())) @@ -36,7 +37,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo if ImageProvider::is_definitely_image_input(input) { let image = Image::new(); - image.set_widget_name("image"); + add_class(&image, "image"); container.add(&image); @@ -47,7 +48,7 @@ pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Bo } } else { let label = Label::new(Some(input)); - label.set_widget_name("label"); + add_class(&label, "label"); container.add(&label); } diff --git a/src/main.rs b/src/main.rs index 984126b8..951fe220 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod config; mod desktop_file; mod dynamic_string; mod error; +mod gtk_helpers; mod image; mod logging; mod macros; diff --git a/src/modules/clipboard.rs b/src/modules/clipboard.rs index 3e5e0611..90338784 100644 --- a/src/modules/clipboard.rs +++ b/src/modules/clipboard.rs @@ -154,11 +154,7 @@ impl Module