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:
parent
a9d1233909
commit
3613aef5c5
8 changed files with 85 additions and 97 deletions
|
@ -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)
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue