mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
feat: Add orientation support for custom label and button
This commit is contained in:
parent
702b0a63bf
commit
70b2c592b2
11 changed files with 145 additions and 39 deletions
|
@ -13,6 +13,7 @@ Clicking on the widget opens a popup with the time and a calendar.
|
|||
| `format` | `string` | `%d/%m/%Y %H:%M` | Date/time format string. Pango markup is supported. |
|
||||
| `format_popup` | `string` | `%H:%M:%S` | Date/time format string to display in the popup header. Pango markup is supported. |
|
||||
| `locale` | `string` | `$LC_TIME` or `$LANG` or `'POSIX'` | Locale to use (eg `en_GB`). Defaults to the system language (reading from env var). |
|
||||
| `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Orientation of the time on the clock button. |
|
||||
|
||||
> Detail on available tokens can be found here: <https://docs.rs/chrono/latest/chrono/format/strftime/index.html>
|
||||
|
||||
|
|
|
@ -57,6 +57,7 @@ A text label. Pango markup is supported.
|
|||
| Name | Type | Default | Description |
|
||||
|---------|-------------------------------------------------|---------|---------------------------------------------------------------------|
|
||||
| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Widget text label. Pango markup and embedded scripts are supported. |
|
||||
| `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Orientation of the label. |
|
||||
|
||||
#### Button
|
||||
|
||||
|
@ -69,6 +70,7 @@ A clickable button, which can run a command when clicked.
|
|||
| `label` | [Dynamic String](dynamic-values#dynamic-string) | `null` | Widget text label. Pango markup and embedded scripts are supported. Ignored if `widgets` is set. |
|
||||
| `widgets` | `(Module or Widget)[]` | `[]` | List of modules/widgets to add to this button. |
|
||||
| `on_click` | `string [command]` | `null` | Command to execute. More on this [below](#commands). |
|
||||
| `orientation` | `'horizontal'` or `'vertical'` (shorthand: `'h'` or `'v'`) | `'horizontal'` | Orientation of the button. |
|
||||
|
||||
#### Image
|
||||
|
||||
|
|
|
@ -43,10 +43,30 @@ pub enum TransitionType {
|
|||
#[derive(Debug, Deserialize, Clone)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ModuleOrientation {
|
||||
#[serde(alias = "h")]
|
||||
Horizontal,
|
||||
#[serde(alias = "v")]
|
||||
Vertical,
|
||||
}
|
||||
|
||||
impl ModuleOrientation {
|
||||
pub const fn to_angle(&self) -> f64 {
|
||||
match self {
|
||||
Self::Horizontal => 0.0,
|
||||
Self::Vertical => 90.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ModuleOrientation> for Orientation {
|
||||
fn from(o: ModuleOrientation) -> Self {
|
||||
match o {
|
||||
ModuleOrientation::Horizontal => Orientation::Horizontal,
|
||||
ModuleOrientation::Vertical => Orientation::Vertical
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ModuleOrientation {
|
||||
fn default() -> Self {
|
||||
ModuleOrientation::Horizontal
|
||||
|
|
|
@ -101,15 +101,10 @@ impl Module<Button> for ClockModule {
|
|||
info: &ModuleInfo,
|
||||
) -> Result<ModuleParts<Button>> {
|
||||
let button = Button::new();
|
||||
let label = if matches!(self.orientation, ModuleOrientation::Vertical) {
|
||||
Label::builder()
|
||||
.angle(info.bar_position.get_angle())
|
||||
.use_markup(true)
|
||||
} else {
|
||||
Label::builder()
|
||||
.use_markup(true)
|
||||
|
||||
}.build();
|
||||
let label = Label::builder()
|
||||
.angle(self.orientation.to_angle())
|
||||
.use_markup(true)
|
||||
.build();
|
||||
button.add(&label);
|
||||
|
||||
let tx = context.tx.clone();
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
use super::{try_get_orientation, CustomWidget, CustomWidgetContext};
|
||||
use super::{CustomWidget, CustomWidgetContext};
|
||||
use crate::build;
|
||||
use crate::config::ModuleOrientation;
|
||||
use crate::modules::custom::WidgetConfig;
|
||||
use gtk::prelude::*;
|
||||
use gtk::Orientation;
|
||||
use gtk::{prelude::*, Orientation};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct BoxWidget {
|
||||
name: Option<String>,
|
||||
class: Option<String>,
|
||||
orientation: Option<String>,
|
||||
orientation: Option<ModuleOrientation>,
|
||||
widgets: Option<Vec<WidgetConfig>>,
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ impl CustomWidget for BoxWidget {
|
|||
|
||||
if let Some(orientation) = self.orientation {
|
||||
container.set_orientation(
|
||||
try_get_orientation(&orientation).unwrap_or(Orientation::Horizontal),
|
||||
Orientation::from(orientation),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ use gtk::prelude::*;
|
|||
use gtk::{Button, Label, Orientation};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::config::ModuleOrientation;
|
||||
use crate::dynamic_value::dynamic_string;
|
||||
use crate::modules::PopupButton;
|
||||
use crate::{build, try_send};
|
||||
|
@ -15,6 +16,8 @@ pub struct ButtonWidget {
|
|||
label: Option<String>,
|
||||
on_click: Option<String>,
|
||||
widgets: Option<Vec<WidgetConfig>>,
|
||||
#[serde(default)]
|
||||
orientation: ModuleOrientation,
|
||||
}
|
||||
|
||||
impl CustomWidget for ButtonWidget {
|
||||
|
@ -35,6 +38,11 @@ impl CustomWidget for ButtonWidget {
|
|||
} else if let Some(text) = self.label {
|
||||
let label = Label::new(None);
|
||||
label.set_use_markup(true);
|
||||
|
||||
label.set_angle(
|
||||
self.orientation.to_angle(),
|
||||
);
|
||||
|
||||
button.add(&label);
|
||||
|
||||
dynamic_string(&text, move |string| {
|
||||
|
|
|
@ -3,6 +3,7 @@ use gtk::Label;
|
|||
use serde::Deserialize;
|
||||
|
||||
use crate::build;
|
||||
use crate::config::ModuleOrientation;
|
||||
use crate::dynamic_value::dynamic_string;
|
||||
|
||||
use super::{CustomWidget, CustomWidgetContext};
|
||||
|
@ -12,6 +13,8 @@ pub struct LabelWidget {
|
|||
name: Option<String>,
|
||||
class: Option<String>,
|
||||
label: String,
|
||||
#[serde(default)]
|
||||
orientation: ModuleOrientation,
|
||||
}
|
||||
|
||||
impl CustomWidget for LabelWidget {
|
||||
|
@ -20,6 +23,10 @@ impl CustomWidget for LabelWidget {
|
|||
fn into_widget(self, _context: CustomWidgetContext) -> Self::Widget {
|
||||
let label = build!(self, Self::Widget);
|
||||
|
||||
label.set_angle(
|
||||
self.orientation.to_angle(),
|
||||
);
|
||||
|
||||
label.set_use_markup(true);
|
||||
|
||||
{
|
||||
|
|
|
@ -113,17 +113,6 @@ pub fn set_length<W: WidgetExt>(widget: &W, length: i32, bar_orientation: Orient
|
|||
};
|
||||
}
|
||||
|
||||
/// Attempts to parse an `Orientation` from `String`.
|
||||
/// Will accept `horizontal`, `vertical`, `h` or `v`.
|
||||
/// Ignores case.
|
||||
fn try_get_orientation(orientation: &str) -> Result<Orientation> {
|
||||
match orientation.to_lowercase().as_str() {
|
||||
"horizontal" | "h" => Ok(Orientation::Horizontal),
|
||||
"vertical" | "v" => Ok(Orientation::Vertical),
|
||||
_ => Err(Report::msg("Invalid orientation string in config")),
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetOrModule {
|
||||
fn add_to(self, parent: >k::Box, context: &CustomWidgetContext, common: CommonConfig) {
|
||||
match self {
|
||||
|
|
|
@ -1,21 +1,24 @@
|
|||
use gtk::prelude::*;
|
||||
use gtk::Orientation;
|
||||
use gtk::ProgressBar;
|
||||
use serde::Deserialize;
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::error;
|
||||
|
||||
use crate::config::ModuleOrientation;
|
||||
use crate::dynamic_value::dynamic_string;
|
||||
use crate::modules::custom::set_length;
|
||||
use crate::script::{OutputStream, Script, ScriptInput};
|
||||
use crate::{build, glib_recv_mpsc, spawn, try_send};
|
||||
|
||||
use super::{try_get_orientation, CustomWidget, CustomWidgetContext};
|
||||
use super::{CustomWidget, CustomWidgetContext};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ProgressWidget {
|
||||
name: Option<String>,
|
||||
class: Option<String>,
|
||||
orientation: Option<String>,
|
||||
#[serde(default)]
|
||||
orientation: ModuleOrientation,
|
||||
label: Option<String>,
|
||||
value: Option<ScriptInput>,
|
||||
#[serde(default = "default_max")]
|
||||
|
@ -33,11 +36,9 @@ impl CustomWidget for ProgressWidget {
|
|||
fn into_widget(self, context: CustomWidgetContext) -> Self::Widget {
|
||||
let progress = build!(self, Self::Widget);
|
||||
|
||||
if let Some(orientation) = self.orientation {
|
||||
progress.set_orientation(
|
||||
try_get_orientation(&orientation).unwrap_or(context.bar_orientation),
|
||||
);
|
||||
}
|
||||
progress.set_orientation(
|
||||
Orientation::from(self.orientation),
|
||||
);
|
||||
|
||||
if let Some(length) = self.length {
|
||||
set_length(&progress, length, context.bar_orientation);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use glib::Propagation;
|
||||
use gtk::Orientation;
|
||||
use std::cell::Cell;
|
||||
use std::ops::Neg;
|
||||
|
||||
|
@ -8,17 +9,19 @@ use serde::Deserialize;
|
|||
use tokio::sync::mpsc;
|
||||
use tracing::error;
|
||||
|
||||
use crate::config::ModuleOrientation;
|
||||
use crate::modules::custom::set_length;
|
||||
use crate::script::{OutputStream, Script, ScriptInput};
|
||||
use crate::{build, glib_recv_mpsc, spawn, try_send};
|
||||
|
||||
use super::{try_get_orientation, CustomWidget, CustomWidgetContext, ExecEvent};
|
||||
use super::{CustomWidget, CustomWidgetContext, ExecEvent};
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct SliderWidget {
|
||||
name: Option<String>,
|
||||
class: Option<String>,
|
||||
orientation: Option<String>,
|
||||
#[serde(default)]
|
||||
orientation: ModuleOrientation,
|
||||
value: Option<ScriptInput>,
|
||||
on_change: Option<String>,
|
||||
#[serde(default = "default_min")]
|
||||
|
@ -45,11 +48,9 @@ impl CustomWidget for SliderWidget {
|
|||
fn into_widget(self, context: CustomWidgetContext) -> Self::Widget {
|
||||
let scale = build!(self, Self::Widget);
|
||||
|
||||
if let Some(orientation) = self.orientation {
|
||||
scale.set_orientation(
|
||||
try_get_orientation(&orientation).unwrap_or(context.bar_orientation),
|
||||
);
|
||||
}
|
||||
scale.set_orientation(
|
||||
Orientation::from(self.orientation),
|
||||
);
|
||||
|
||||
if let Some(length) = self.length {
|
||||
set_length(&scale, length, context.bar_orientation);
|
||||
|
|
82
test-configs/orientation.yaml
Normal file
82
test-configs/orientation.yaml
Normal file
|
@ -0,0 +1,82 @@
|
|||
position: "left"
|
||||
start:
|
||||
- type: clock
|
||||
format: "%H:%M"
|
||||
- type: clock
|
||||
format: "%H:%M"
|
||||
orientation: horizontal
|
||||
- type: clock
|
||||
format: "%H:%M"
|
||||
orientation: h
|
||||
- type: clock
|
||||
format: "%H:%M"
|
||||
orientation: vertical
|
||||
- type: clock
|
||||
format: "%H:%M"
|
||||
orientation: v
|
||||
- type: custom
|
||||
bar:
|
||||
- type: label
|
||||
label: "label"
|
||||
- type: label
|
||||
label: "label"
|
||||
orientation: horizontal
|
||||
- type: label
|
||||
label: "label"
|
||||
orientation: h
|
||||
- type: label
|
||||
label: "label"
|
||||
orientation: vertical
|
||||
- type: label
|
||||
label: "label"
|
||||
orientation: v
|
||||
- type: custom
|
||||
bar:
|
||||
- type: button
|
||||
label: "label"
|
||||
- type: button
|
||||
label: "label"
|
||||
orientation: horizontal
|
||||
- type: button
|
||||
label: "label"
|
||||
orientation: h
|
||||
- type: button
|
||||
label: "label"
|
||||
orientation: vertical
|
||||
- type: button
|
||||
label: "label"
|
||||
orientation: v
|
||||
- type: custom
|
||||
bar:
|
||||
- type: progress
|
||||
value: "echo 50"
|
||||
- type: progress
|
||||
value: "echo 50"
|
||||
orientation: horizontal
|
||||
- type: progress
|
||||
value: "echo 50"
|
||||
orientation: h
|
||||
- type: progress
|
||||
value: "echo 50"
|
||||
orientation: vertical
|
||||
- type: progress
|
||||
value: "echo 50"
|
||||
orientation: v
|
||||
- type: custom
|
||||
bar:
|
||||
- type: slider
|
||||
value: "echo 50"
|
||||
- type: slider
|
||||
value: "echo 50"
|
||||
orientation: horizontal
|
||||
- type: slider
|
||||
value: "echo 50"
|
||||
orientation: h
|
||||
- type: slider
|
||||
value: "echo 50"
|
||||
length: 100
|
||||
orientation: vertical
|
||||
- type: slider
|
||||
value: "echo 50"
|
||||
length: 100
|
||||
orientation: v
|
Loading…
Add table
Reference in a new issue