Skip to content

Commit

Permalink
gio: more ergonomic ActionEntry initialization
Browse files Browse the repository at this point in the history
This aligns the initialization with other `gio` and `gtk4` builders.

Before:
```rs
ActionEntry::builder("name").build()
```

Proposed:
```rs
ActionEntry::new("name")`
ActionEntry::builder().name("name").build()
```

`ActionEntryBuilder` implements `Default` to satisfy Clippy/CI.
  • Loading branch information
allan2 committed May 23, 2024
1 parent 8339a2e commit 4305fe6
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions gio/src/action_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ impl<O> ActionEntry<O>
where
O: IsA<ActionMap>,
{
pub fn new(name: &str) -> Self {
Self {
name: name.to_owned(),
parameter_type: Default::default(),
state: Default::default(),
activate: Default::default(),
change_state: Default::default(),
}
}

pub fn name(&self) -> &str {
&self.name
}
Expand All @@ -34,8 +44,8 @@ where
self.state.as_ref()
}

pub fn builder(name: &str) -> ActionEntryBuilder<O> {
ActionEntryBuilder::new(name)
pub fn builder() -> ActionEntryBuilder<O> {
ActionEntryBuilder::new()
}
}

Expand All @@ -61,16 +71,21 @@ impl<O> ActionEntryBuilder<O>
where
O: IsA<ActionMap>,
{
pub fn new(name: &str) -> Self {
pub fn new() -> Self {
Self(ActionEntry {
name: name.to_owned(),
name: Default::default(),
parameter_type: Default::default(),
state: Default::default(),
activate: Default::default(),
change_state: Default::default(),
})
}

pub fn name(mut self, name: &str) -> Self {
self.0.name = name.to_owned();
self
}

pub fn parameter_type(mut self, parameter_type: Option<&VariantTy>) -> Self {
self.0.parameter_type = parameter_type.map(|vt| vt.to_owned());
self
Expand Down Expand Up @@ -102,6 +117,15 @@ where
}
}

impl<O> Default for ActionEntryBuilder<O>
where
O: IsA<ActionMap>,
{
fn default() -> Self {
Self::new()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -112,12 +136,14 @@ mod tests {
let app = crate::Application::new(None, Default::default());

app.add_action_entries(vec![
ActionEntry::builder("close")
ActionEntry::builder()
.name("close")
.activate(move |_app, _, _| {
//Do something
})
.build(),
ActionEntry::builder("enable")
ActionEntry::builder()
.name("enable")
.state(true.to_variant())
.change_state(move |_app, _, _| {
//Do something
Expand Down

0 comments on commit 4305fe6

Please sign in to comment.