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

refactor: Use static dispatch #829

Merged
merged 1 commit into from
Oct 5, 2024
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
48 changes: 29 additions & 19 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use gettextrs::ngettext;
use glib::clone;
use gtk::gio;
use gtk::glib;
use gtk::glib::clone::Downgrade;

use crate::config;
use crate::APPLICATION_OPTS;
Expand Down Expand Up @@ -158,23 +159,23 @@ pub(crate) fn format_id(id: &str) -> String {
id.chars().take(12).collect::<String>()
}

pub(crate) fn root(widget: &gtk::Widget) -> gtk::Window {
pub(crate) fn root<W: IsA<gtk::Widget>>(widget: &W) -> gtk::Window {
widget.root().unwrap().downcast::<gtk::Window>().unwrap()
}

pub(crate) struct Dialog<'a> {
widget: &'a gtk::Widget,
content: &'a gtk::Widget,
pub(crate) struct Dialog<'a, P, C> {
parent: &'a P,
content: &'a C,
height: Option<i32>,
width: Option<i32>,
follows_content_size: Option<bool>,
}

impl<'a> Dialog<'a> {
impl<'a, C, P> Dialog<'a, C, P> {
#[must_use]
pub(crate) fn new(widget: &'a gtk::Widget, content: &'a gtk::Widget) -> Self {
pub(crate) fn new(widget: &'a C, content: &'a P) -> Self {
Self {
widget,
parent: widget,
content,
height: None,
width: None,
Expand All @@ -193,7 +194,13 @@ impl<'a> Dialog<'a> {
self.follows_content_size = Some(follows_content_size);
self
}
}

impl<'a, C, P> Dialog<'a, C, P>
where
C: IsA<gtk::Widget>,
P: IsA<gtk::Widget>,
{
pub(crate) fn present(self) {
let toast_overlay = adw::ToastOverlay::new();
toast_overlay.set_child(Some(self.content));
Expand All @@ -206,11 +213,11 @@ impl<'a> Dialog<'a> {
.follows_content_size(self.follows_content_size.unwrap_or(false))
.build();

dialog.present(Some(self.widget));
dialog.present(Some(self.parent));
}
}

pub(crate) fn show_toast(widget: &gtk::Widget, title: impl Into<glib::GString>) {
pub(crate) fn show_toast<W: IsA<gtk::Widget>>(widget: &W, title: impl Into<glib::GString>) {
widget
.ancestor(adw::ToastOverlay::static_type())
.unwrap()
Expand All @@ -225,17 +232,17 @@ pub(crate) fn show_toast(widget: &gtk::Widget, title: impl Into<glib::GString>)
);
}

pub(crate) fn show_error_toast(widget: &gtk::Widget, title: &str, msg: &str) {
pub(crate) fn show_error_toast<W: IsA<gtk::Widget>>(widget: &W, title: &str, msg: &str) {
show_toast(widget, format!("{title}: {msg}"));
}

pub(crate) fn try_navigation_view(widget: &gtk::Widget) -> Option<adw::NavigationView> {
pub(crate) fn try_navigation_view<W: IsA<gtk::Widget>>(widget: &W) -> Option<adw::NavigationView> {
widget
.ancestor(adw::NavigationView::static_type())
.and_downcast::<adw::NavigationView>()
}

pub(crate) fn navigation_view(widget: &gtk::Widget) -> adw::NavigationView {
pub(crate) fn navigation_view<W: IsA<gtk::Widget>>(widget: &W) -> adw::NavigationView {
try_navigation_view(widget).unwrap()
}

Expand Down Expand Up @@ -383,7 +390,7 @@ pub(crate) fn run_stream_with_finish_handler<A, P, I, F, X>(
});
}

pub(crate) fn css_classes(widget: &gtk::Widget) -> Vec<String> {
pub(crate) fn css_classes<W: IsA<gtk::Widget>>(widget: &W) -> Vec<String> {
widget
.css_classes()
.iter()
Expand All @@ -392,8 +399,8 @@ pub(crate) fn css_classes(widget: &gtk::Widget) -> Vec<String> {
}

pub(crate) struct ChildIter(Option<gtk::Widget>);
impl From<&gtk::Widget> for ChildIter {
fn from(widget: &gtk::Widget) -> Self {
impl<W: IsA<gtk::Widget>> From<&W> for ChildIter {
fn from(widget: &W) -> Self {
Self(widget.first_child())
}
}
Expand All @@ -407,12 +414,13 @@ impl Iterator for ChildIter {
}
}

pub(crate) fn unparent_children(widget: &gtk::Widget) {
pub(crate) fn unparent_children<W: IsA<gtk::Widget>>(widget: &W) {
ChildIter::from(widget).for_each(|child| child.unparent());
}

pub(crate) async fn show_open_file_dialog<F>(request: OpenFileRequest, widget: &gtk::Widget, op: F)
pub(crate) async fn show_open_file_dialog<W, F>(request: OpenFileRequest, widget: &W, op: F)
where
W: IsA<gtk::Widget> + Downgrade<Weak = glib::WeakRef<W>>,
F: Fn(SelectedFiles) + 'static,
{
do_async(
Expand All @@ -425,8 +433,9 @@ where
);
}

pub(crate) async fn show_save_file_dialog<F>(request: SaveFileRequest, widget: &gtk::Widget, op: F)
pub(crate) async fn show_save_file_dialog<W, F>(request: SaveFileRequest, widget: &W, op: F)
where
W: IsA<gtk::Widget> + Downgrade<Weak = glib::WeakRef<W>>,
F: Fn(SelectedFiles) + 'static,
{
do_async(
Expand All @@ -439,8 +448,9 @@ where
);
}

fn show_file_dialog<F>(files: Result<SelectedFiles, ashpd::Error>, widget: &gtk::Widget, op: F)
fn show_file_dialog<W, F>(files: Result<SelectedFiles, ashpd::Error>, widget: &W, op: F)
where
W: IsA<gtk::Widget>,
F: Fn(SelectedFiles),
{
match files {
Expand Down
4 changes: 2 additions & 2 deletions src/view/action_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down Expand Up @@ -308,7 +308,7 @@ impl ActionPage {
self.activate_action("win.close", None).unwrap();
}
None => utils::show_error_toast(
self.upcast_ref(),
self,
&gettext("Error on opening artifact"),
&gettext("Artifact has been deleted"),
),
Expand Down
4 changes: 2 additions & 2 deletions src/view/action_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ mod imp {
description_expr.bind(obj, "tooltip-markup", Some(obj));
description_expr.bind(&*self.description_label, "label", Some(obj));

let classes = utils::css_classes(self.state_label.upcast_ref());
let classes = utils::css_classes(&*self.state_label);
state_expr
.chain_closure::<Vec<String>>(closure!(
|_: Self::Type, state: model::ActionState| {
Expand Down Expand Up @@ -151,7 +151,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/view/actions_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/view/actions_sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand All @@ -135,11 +135,7 @@ mod imp {
.downcast::<model::Action>()
.unwrap();

utils::Dialog::new(
self.obj().upcast_ref(),
view::ActionPage::from(&action).upcast_ref(),
)
.present();
utils::Dialog::new(&*self.obj(), &view::ActionPage::from(&action)).present();
}

pub(super) fn set_action_list(&self, value: Option<&model::ActionList>) {
Expand Down
2 changes: 1 addition & 1 deletion src/view/client_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/view/connection_chooser_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down Expand Up @@ -105,7 +105,7 @@ mod imp {
obj,
move |result| if let Err(e) = result {
utils::show_error_toast(
obj.upcast_ref(),
&obj,
&gettext("Error on establishing connection"),
&e.to_string(),
);
Expand Down
8 changes: 4 additions & 4 deletions src/view/connection_creation_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mod imp {
let obj = &*self.obj();

obj.abort();
utils::unparent_children(obj.upcast_ref());
utils::unparent_children(obj);
}
}

Expand All @@ -176,11 +176,11 @@ mod imp {
glib::ControlFlow::Break
}
));
utils::root(widget.upcast_ref()).set_default_widget(Some(&*self.connect_button));
utils::root(widget).set_default_widget(Some(&*self.connect_button));
}

fn unroot(&self) {
utils::root(self.obj().upcast_ref()).set_default_widget(gtk::Widget::NONE);
utils::root(&*self.obj()).set_default_widget(gtk::Widget::NONE);
self.parent_unroot()
}
}
Expand Down Expand Up @@ -297,6 +297,6 @@ impl ConnectionCreationPage {
}

fn on_error(&self, msg: &str) {
utils::show_error_toast(self.upcast_ref(), &gettext("Error"), msg);
utils::show_error_toast(self, &gettext("Error"), msg);
}
}
13 changes: 5 additions & 8 deletions src/view/connection_custom_info_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down Expand Up @@ -136,33 +136,30 @@ impl ConnectionCustomInfoDialog {
label.select_region(0, -1);
label.emit_copy_clipboard();

utils::show_toast(self.upcast_ref(), gettext("systemd unit path copied"));
utils::show_toast(self, gettext("systemd unit path copied"));
}

fn copy_root_systemd_unit_content(&self) {
let buffer = &*self.imp().root_systemd_unit_content_buffer;
buffer.select_range(&buffer.start_iter(), &buffer.end_iter());
buffer.copy_clipboard(&gdk::Display::default().unwrap().clipboard());

utils::show_toast(self.upcast_ref(), gettext("systemd unit content copied"));
utils::show_toast(self, gettext("systemd unit content copied"));
}

fn copy_root_socket_acivation_command(&self) {
let label = &*self.imp().root_socket_activation_command_label;
label.select_region(0, -1);
label.emit_copy_clipboard();

utils::show_toast(
self.upcast_ref(),
gettext("socket activation command copied"),
);
utils::show_toast(self, gettext("socket activation command copied"));
}

fn copy_root_url(&self) {
let label = &*self.imp().root_url_label;
label.select_region(0, -1);
label.emit_copy_clipboard();

utils::show_toast(self.upcast_ref(), gettext("URL copied"));
utils::show_toast(self, gettext("URL copied"));
}
}
4 changes: 2 additions & 2 deletions src/view/connection_row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ mod imp {
)
.bind(&*self.url_label, "label", Some(obj));

let classes = utils::css_classes(self.image.upcast_ref());
let classes = utils::css_classes(&*self.image);
is_active_expr
.chain_closure::<Vec<String>>(closure!(|_: Self::Type, is_active: bool| {
classes
Expand Down Expand Up @@ -142,7 +142,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/view/connections_sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod imp {
}

fn dispose(&self) {
utils::unparent_children(self.obj().upcast_ref());
utils::unparent_children(&*self.obj());
}
}

Expand Down Expand Up @@ -103,7 +103,7 @@ mod imp {
obj,
move |result| if let Err(e) = result {
utils::show_error_toast(
obj.upcast_ref(),
&obj,
&gettext("Error on switching connection"),
&e.to_string(),
);
Expand Down
16 changes: 11 additions & 5 deletions src/view/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use gettextrs::gettext;
use glib::clone;
use glib::closure;
use gtk::glib;
use gtk::glib::clone::Downgrade;

use crate::model;
use crate::utils;
Expand Down Expand Up @@ -52,7 +53,10 @@ pub(crate) fn container_status_combined_css_class(
}
}

pub(crate) fn rename(widget: &gtk::Widget, container: Option<&model::Container>) {
pub(crate) fn rename<W>(widget: &W, container: Option<&model::Container>)
where
W: IsA<gtk::Widget> + Downgrade<Weak = glib::WeakRef<W>>,
{
if let Some(container) = container {
let container_renamer = view::ContainerRenamer::from(container);

Expand Down Expand Up @@ -129,12 +133,14 @@ pub(crate) fn rename(widget: &gtk::Widget, container: Option<&model::Container>)

macro_rules! container_action {
(fn $name:ident => $action:ident($($param:literal),*) => $error:tt) => {
pub(crate) fn $name(widget: &gtk::Widget) {
use gtk::glib;
if let Some(container) = <gtk::Widget as gtk::prelude::ObjectExt>::property::<Option<crate::model::Container>>(widget, "container") {
pub(crate) fn $name<W>(widget: &W, container: Option<crate::model::Container>)
where
W: gtk::glib::prelude::IsA<gtk::Widget> + gtk::glib::clone::Downgrade<Weak = gtk::glib::WeakRef<W>>,
{
if let Some(container) = container {
container.$action(
$($param,)*
glib::clone!(#[weak] widget, move |result| if let Err(e) = result {
gtk::glib::clone!(#[weak] widget, move |result| if let Err(e) = result {
crate::utils::show_error_toast(
&widget,
&$error,
Expand Down
Loading
Loading