1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-17 14:51:04 +02:00

refactor(custom): reduce a lot of repeated code

This commit is contained in:
Jake Stanger 2023-04-10 13:51:07 +01:00
parent a9d1233909
commit 3613aef5c5
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
8 changed files with 85 additions and 97 deletions

View file

@ -1,4 +1,5 @@
use super::{CustomWidget, CustomWidgetContext};
use crate::build;
use crate::dynamic_string::DynamicString;
use gtk::prelude::*;
use gtk::Label;
@ -8,31 +9,21 @@ use serde::Deserialize;
pub struct LabelWidget {
name: Option<String>,
class: Option<String>,
label: Option<String>,
label: String,
}
impl CustomWidget for LabelWidget {
type Widget = Label;
fn into_widget(self, _context: CustomWidgetContext) -> Self::Widget {
let mut builder = Label::builder().use_markup(true);
let label = build!(self, Self::Widget);
if let Some(name) = self.name {
builder = builder.name(name);
}
let label = builder.build();
if let Some(class) = self.class {
label.style_context().add_class(&class);
}
let text = self.label.map_or_else(String::new, |text| text);
label.set_use_markup(true);
{
let label = label.clone();
DynamicString::new(&text, move |string| {
label.set_label(&string);
DynamicString::new(&self.label, move |string| {
label.set_markup(&string);
Continue(true)
});
}