Commit 4ad626ef authored by Christopher Davis's avatar Christopher Davis 🌱

lib: Add `add_style_class` macro

Cuts down on the boilerplate of adding one or more
style classes to a widget.
parent 675d2dab
......@@ -248,3 +248,53 @@ macro_rules! get_action {
.unwrap();
};
}
/// Add a style class or list of style classes to a widget
///
/// - Before:
///
/// Example 1:
///
/// ```no_run
/// let button = gtk::Button::new();
/// let ctx = button.get_style_context();
/// ctx.add_class("foo");
/// ```
///
/// Example 2:
///
/// ```no_run
/// let button = gtk::Button::new();
/// let ctx = button.get_style_context();
/// ctx.add_class("foo");
/// ctx.add_class("bar");
/// ```
///
/// - After:
///
/// Example 1:
///
/// ```no_run
/// let button = gtk::Button::new();
/// add_style_class!(button, @foo);
/// ```
///
/// Example 2:
///
/// ```no_run
/// let button = gtk::Button::new();
/// add_style_class!(button, &["foo", "bar"]);
/// ```
#[macro_export]
macro_rules! add_style_class {
($widget:expr, @$name:ident) => {{
let ctx = $widget.get_style_context();
ctx.add_class(stringify!($name));
}};
($widget:expr, $names:expr) => {{
let ctx = $widget.get_style_context();
for name in $names {
ctx.add_class(name);
}
}};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment