Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to create a "this" expression from GObject types #710

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/expressions/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ fn build_ui(app: &gtk::Application) {

// Instead of binding properties and manually unbinding them, you can create expressions.
// The value can be obtained even if it is several steps away.
let metadata_expression = list_item
list_item
.property_expression("item")
.chain_property::<Note>("metadata");

metadata_expression
.chain_property::<Note>("metadata")
.chain_property::<Metadata>("title")
.chain_closure_with_callback(|args| {
let title: String = args[1].get().unwrap();
format!("Title: {}", title)
})
.bind(&title_label, "label", gtk::Widget::NONE);

metadata_expression
// Property expressions can also start from the `this` value, which is set as the last
// argument to the `bind` function.
gtk::ListItem::this_expression("item")
.chain_property::<Note>("metadata")
.chain_property::<Metadata>("last-modified")
.chain_closure::<String>(closure!(
|_: gtk::ListItem, last_modified: glib::DateTime| {
Expand Down
9 changes: 9 additions & 0 deletions gtk4/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ pub trait GObjectPropertyExpressionExt {
// rustdoc-stripper-ignore-next
/// Create an expression looking up an object's property with a weak reference.
fn property_expression_weak(&self, property_name: &str) -> crate::PropertyExpression;

// rustdoc-stripper-ignore-next
/// Create an expression looking up a property in the bound `this` object.
fn this_expression(property_name: &str) -> crate::PropertyExpression;
}

impl<T: IsA<glib::Object>> GObjectPropertyExpressionExt for T {
Expand All @@ -289,6 +293,11 @@ impl<T: IsA<glib::Object>> GObjectPropertyExpressionExt for T {
let obj_expr = crate::ObjectExpression::new(self);
crate::PropertyExpression::new(T::static_type(), Some(&obj_expr), property_name)
}

fn this_expression(property_name: &str) -> crate::PropertyExpression {
skip_assert_initialized!();
crate::PropertyExpression::new(T::static_type(), Expression::NONE, property_name)
}
}

macro_rules! define_expression {
Expand Down