fix: use new glib::clone! syntax

This commit is contained in:
Gabriele Musco 2024-07-26 17:48:52 +02:00
parent ce805c6b72
commit 2473d81817
5 changed files with 162 additions and 92 deletions

View file

@ -506,9 +506,11 @@ impl SimpleComponent for App {
.valign(gtk::Align::Center)
.halign(gtk::Align::Center)
.build();
btn.connect_clicked(
clone!(@strong cmd => move |_| copy_text(&cmd)),
);
btn.connect_clicked(clone!(
#[to_owned]
cmd,
move |_| copy_text(&cmd)
));
container.append(
&gtk::ScrolledWindow::builder()
.vscrollbar_policy(gtk::PolicyType::Never)
@ -827,53 +829,79 @@ impl SimpleComponent for App {
stateless_action!(
actions,
BuildProfileAction,
clone!(@strong sender => move |_| {
sender.input_sender().emit(Msg::BuildProfile(false));
})
clone!(
#[strong]
sender,
move |_| {
sender.input_sender().emit(Msg::BuildProfile(false));
}
)
);
stateless_action!(
actions,
BuildProfileCleanAction,
clone!(@strong sender => move |_| {
sender.input_sender().emit(Msg::BuildProfile(true));
})
clone!(
#[strong]
sender,
move |_| {
sender.input_sender().emit(Msg::BuildProfile(true));
}
)
);
stateless_action!(
actions,
QuitAction,
clone!(@strong sender => move |_| {
sender.input(Msg::Quit);
})
clone!(
#[strong]
sender,
move |_| {
sender.input(Msg::Quit);
}
)
);
stateless_action!(
actions,
DebugOpenDataAction,
clone!(@strong sender => move |_| {
sender.input(Msg::DebugOpenData);
})
clone!(
#[strong]
sender,
move |_| {
sender.input(Msg::DebugOpenData);
}
)
);
stateless_action!(
actions,
DebugOpenPrefixAction,
clone!(@strong sender => move |_| {
sender.input(Msg::DebugOpenPrefix);
})
clone!(
#[strong]
sender,
move |_| {
sender.input(Msg::DebugOpenPrefix);
}
)
);
stateless_action!(
actions,
DebugCopyEnvVarsAction,
clone!(@strong sender => move |_| {
sender.input(Msg::DebugCopyEnvVars);
})
clone!(
#[strong]
sender,
move |_| {
sender.input(Msg::DebugCopyEnvVars);
}
)
);
// this bypasses the macro because I need the underlying gio action
// to enable/disable it in update()
let configure_wivrn_action = {
let action = RelmAction::<ConfigureWivrnAction>::new_stateless(
clone!(@strong sender => move |_| {
let action = RelmAction::<ConfigureWivrnAction>::new_stateless(clone!(
#[strong]
sender,
move |_| {
sender.input(Msg::OpenWivrnConfig);
}),
);
}
));
let ret = action.gio_action().clone();
actions.add_action(action);
ret.set_enabled(false);
@ -946,11 +974,15 @@ impl SimpleComponent for App {
}
actions.add_action(RelmAction::<DebugViewToggleAction>::new_stateful(
&model.enable_debug_view,
clone!(@strong sender => move |_, state| {
let s = *state;
*state = !s;
sender.input(Msg::EnableDebugViewChanged(*state));
}),
clone!(
#[strong]
sender,
move |_, state| {
let s = *state;
*state = !s;
sender.input(Msg::EnableDebugViewChanged(*state));
}
),
));
root.insert_action_group(AppActionGroup::NAME, Some(&actions.into_action_group()));
@ -969,10 +1001,14 @@ impl SimpleComponent for App {
glib::timeout_add_local(
Duration::from_millis(1000),
clone!(@strong sender => move || {
sender.input(Msg::ClockTicking);
glib::ControlFlow::Continue
}),
clone!(
#[strong]
sender,
move || {
sender.input(Msg::ClockTicking);
glib::ControlFlow::Continue
}
),
);
model.split_view = Some(widgets.split_view.clone());

View file

@ -211,18 +211,26 @@ impl SimpleComponent for DebugView {
if let Some(btn) = log_level_dropdown.first_child() {
btn.add_css_class("flat");
}
log_level_dropdown.connect_selected_notify(clone!(@strong sender => move |dd| {
sender.input(Self::Input::LogLevelChanged(
*LogLevel::iter()
.as_slice()
.get(dd.selected() as usize)
.unwrap(),
));
}));
log_level_dropdown.connect_selected_notify(clone!(
#[strong]
sender,
move |dd| {
sender.input(Self::Input::LogLevelChanged(
*LogLevel::iter()
.as_slice()
.get(dd.selected() as usize)
.unwrap(),
));
}
));
adw::StyleManager::default().connect_dark_notify(clone!(@strong sender => move |_| {
sender.input(Self::Input::SetColorScheme);
}));
adw::StyleManager::default().connect_dark_notify(clone!(
#[strong]
sender,
move |_| {
sender.input(Self::Input::SetColorScheme);
}
));
let mut model = Self {
xrservice_active: false,

View file

@ -190,26 +190,46 @@ pub fn path_row<F: Fn(Option<String>) + 'static + Clone>(
.valign(gtk::Align::Center)
.build();
row.add_suffix(&clear_btn);
clear_btn.connect_clicked(clone!(@strong path_label, @strong cb => move |_| {
path_label.set_label("(None)");
cb(None)
}));
clear_btn.connect_clicked(clone!(
#[weak]
path_label,
#[strong]
cb,
move |_| {
path_label.set_label("(None)");
cb(None)
}
));
let filedialog = gtk::FileDialog::builder()
.modal(true)
.title(format!("Select Path for {}", title))
.build();
row.connect_activated(clone!(@strong path_label => move |_| {
filedialog.select_folder(root_win.as_ref(), gio::Cancellable::NONE, clone!(@strong path_label, @strong cb => move |res| {
if let Ok(file) = res {
if let Some(path) = file.path() {
let path_s = path.to_str().unwrap().to_string();
path_label.set_text(path_s.as_str());
cb(Some(path_s))
}
}
}))
}));
row.connect_activated(clone!(
#[weak]
path_label,
move |_| {
filedialog.select_folder(
root_win.as_ref(),
gio::Cancellable::NONE,
clone!(
#[weak]
path_label,
#[strong]
cb,
move |res| {
if let Ok(file) = res {
if let Some(path) = file.path() {
let path_s = path.to_str().unwrap().to_string();
path_label.set_text(path_s.as_str());
cb(Some(path_s))
}
}
}
),
)
}
));
row
}

View file

@ -11,7 +11,7 @@ use crate::{
},
};
use adw::prelude::*;
use gtk::glib::clone;
use gtk::glib::{self, clone};
use relm4::{factory::AsyncFactoryVecDeque, prelude::*};
use std::{cell::RefCell, path::PathBuf, rc::Rc};
@ -95,14 +95,14 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"Profile Name",
model.profile.borrow().name.as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
prof.borrow_mut().name = row.text().to_string();
})
),
add: &switch_row(
"Update on Build", None,
model.profile.borrow().pull_on_build,
clone!(@strong prof => move |_, state| {
clone!(#[strong] prof, move |_, state| {
prof.borrow_mut().pull_on_build = state;
gtk::glib::Propagation::Proceed
})
@ -112,13 +112,13 @@ impl SimpleComponent for ProfileEditor {
None,
Some(model.profile.borrow().prefix.to_string_lossy().to_string()),
Some(init.root_win.clone()),
clone!(@strong prof => move |n_path| {
clone!(#[strong] prof, move |n_path| {
prof.borrow_mut().prefix = n_path.unwrap_or_default().into();
}),
),
add: &entry_row("Autostart Command",
model.profile.borrow().autostart_command.as_ref().unwrap_or(&String::default()),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let txt = row.text().trim().to_string();
prof.borrow_mut().autostart_command =
if txt.is_empty() {None} else {Some(txt)};
@ -139,7 +139,7 @@ impl SimpleComponent for ProfileEditor {
XRServiceType::iter()
.map(XRServiceType::to_string)
.collect::<Vec<String>>(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
prof.borrow_mut().xrservice_type =
XRServiceType::from_number(row.selected());
}),
@ -147,7 +147,7 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"XR Service Launch Options",
model.profile.borrow().xrservice_launch_options.as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
prof.borrow_mut().xrservice_launch_options = row.text().trim().to_string();
})
),
@ -163,7 +163,7 @@ impl SimpleComponent for ProfileEditor {
LighthouseDriver::iter()
.map(LighthouseDriver::to_string)
.collect::<Vec<String>>(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
prof.borrow_mut().lighthouse_driver =
LighthouseDriver::from_number(row.selected());
})
@ -173,14 +173,14 @@ impl SimpleComponent for ProfileEditor {
None,
Some(model.profile.borrow().xrservice_path.clone().to_string_lossy().to_string()),
Some(init.root_win.clone()),
clone!(@strong prof => move |n_path| {
clone!(#[strong] prof, move |n_path| {
prof.borrow_mut().xrservice_path = n_path.unwrap_or_default().into();
}),
),
add: &entry_row(
"XR Service Repo",
model.profile.borrow().xrservice_repo.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().xrservice_repo = (!n_val.is_empty()).then_some(n_val);
})
@ -188,7 +188,7 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"XR Service Branch",
model.profile.borrow().xrservice_branch.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().xrservice_branch = (!n_val.is_empty()).then_some(n_val);
})
@ -202,14 +202,14 @@ impl SimpleComponent for ProfileEditor {
"OpenComposite Path", None,
Some(model.profile.borrow().opencomposite_path.clone().to_string_lossy().to_string()),
Some(init.root_win.clone()),
clone!(@strong prof => move |n_path| {
clone!(#[strong] prof, move |n_path| {
prof.borrow_mut().opencomposite_path = n_path.unwrap_or_default().into();
})
),
add: &entry_row(
"OpenComposite Repo",
model.profile.borrow().opencomposite_repo.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().opencomposite_repo = (!n_val.is_empty()).then_some(n_val);
})
@ -217,7 +217,7 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"OpenComposite Branch",
model.profile.borrow().opencomposite_branch.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().opencomposite_branch = (!n_val.is_empty()).then_some(n_val);
})
@ -229,7 +229,7 @@ impl SimpleComponent for ProfileEditor {
add: &switch_row(
"Enable Libsurvive", None,
model.profile.borrow().features.libsurvive.enabled,
clone!(@strong prof => move |_, state| {
clone!(#[strong] prof, move |_, state| {
prof.borrow_mut().features.libsurvive.enabled = state;
gtk::glib::Propagation::Proceed
})
@ -238,14 +238,14 @@ impl SimpleComponent for ProfileEditor {
"Libsurvive Path", None,
model.profile.borrow().features.libsurvive.path.clone().map(|p| p.to_string_lossy().to_string()),
Some(init.root_win.clone()),
clone!(@strong prof => move |n_path| {
clone!(#[strong] prof, move |n_path| {
prof.borrow_mut().features.libsurvive.path = n_path.map(PathBuf::from);
})
),
add: &entry_row(
"Libsurvive Repo",
model.profile.borrow().features.libsurvive.repo.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().features.libsurvive.repo = (!n_val.is_empty()).then_some(n_val);
})
@ -253,7 +253,7 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"Libsurvive Branch",
model.profile.borrow().features.libsurvive.branch.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().features.libsurvive.branch = (!n_val.is_empty()).then_some(n_val);
})
@ -265,7 +265,7 @@ impl SimpleComponent for ProfileEditor {
add: &switch_row(
"Enable OpenHMD", None,
model.profile.borrow().features.openhmd.enabled,
clone!(@strong prof => move |_, state| {
clone!(#[strong] prof, move |_, state| {
prof.borrow_mut().features.openhmd.enabled = state;
gtk::glib::Propagation::Proceed
})
@ -274,14 +274,14 @@ impl SimpleComponent for ProfileEditor {
"OpenHMD Path", None,
model.profile.borrow().features.openhmd.path.clone().map(|p| p.to_string_lossy().to_string()),
Some(init.root_win.clone()),
clone!(@strong prof => move |n_path| {
clone!(#[strong] prof, move |n_path| {
prof.borrow_mut().features.openhmd.path = n_path.map(PathBuf::from);
})
),
add: &entry_row(
"OpenHMD Repo",
model.profile.borrow().features.openhmd.repo.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().features.openhmd.repo = (!n_val.is_empty()).then_some(n_val);
})
@ -289,7 +289,7 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"OpenHMD Branch",
model.profile.borrow().features.openhmd.branch.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().features.openhmd.branch = (!n_val.is_empty()).then_some(n_val);
})
@ -301,7 +301,7 @@ impl SimpleComponent for ProfileEditor {
add: &switch_row(
"Enable Basalt", None,
model.profile.borrow().features.basalt.enabled,
clone!(@strong prof => move |_, state| {
clone!(#[strong] prof, move |_, state| {
prof.borrow_mut().features.basalt.enabled = state;
gtk::glib::Propagation::Proceed
})
@ -310,14 +310,14 @@ impl SimpleComponent for ProfileEditor {
"Basalt Path", None,
model.profile.borrow().features.basalt.path.clone().map(|p| p.to_string_lossy().to_string()),
Some(init.root_win.clone()),
clone!(@strong prof => move |n_path| {
clone!(#[strong] prof, move |n_path| {
prof.borrow_mut().features.basalt.path = n_path.map(PathBuf::from);
})
),
add: &entry_row(
"Basalt Repo",
model.profile.borrow().features.basalt.repo.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().features.basalt.repo = (!n_val.is_empty()).then_some(n_val);
})
@ -325,7 +325,7 @@ impl SimpleComponent for ProfileEditor {
add: &entry_row(
"Basalt Branch",
model.profile.borrow().features.basalt.branch.clone().unwrap_or_default().as_str(),
clone!(@strong prof => move |row| {
clone!(#[strong] prof, move |row| {
let n_val = row.text().to_string();
prof.borrow_mut().features.basalt.branch = (!n_val.is_empty()).then_some(n_val);
})
@ -337,7 +337,7 @@ impl SimpleComponent for ProfileEditor {
add: &switch_row(
"Enable Mercury", None,
model.profile.borrow().features.mercury_enabled,
clone!(@strong prof => move |_, state| {
clone!(#[strong] prof, move |_, state| {
prof.borrow_mut().features.mercury_enabled = state;
gtk::glib::Propagation::Proceed
})
@ -473,8 +473,14 @@ impl SimpleComponent for ProfileEditor {
.halign(gtk::Align::End)
.build();
add_btn.connect_clicked(
clone!(@strong sender, @strong name_entry, @strong popover => move |_| {
add_btn.connect_clicked(clone!(
#[strong]
sender,
#[weak]
name_entry,
#[weak]
popover,
move |_| {
let key_gstr = name_entry.text();
let key = key_gstr.trim();
if !key.is_empty() {
@ -482,8 +488,8 @@ impl SimpleComponent for ProfileEditor {
name_entry.set_text("");
sender.input($event(key.to_string()));
}
})
);
}
));
btn
}};
}

View file

@ -135,7 +135,7 @@ impl SimpleComponent for WivrnConfEditor {
"will use both TCP and UDP."
)),
model.conf.tcp_only,
clone!(@strong sender => move |_, val| {
clone!(#[strong] sender, move |_, val| {
sender.input(Self::Input::TcpOnlyChanged(val));
gtk::glib::Propagation::Proceed
}),