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

gio: more ergonomic ActionEntry initialization #1409

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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 {
name.clone_into(&mut self.0.name);
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
Loading