From e51da627ea7c36dae213c836202aee298119ad54 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 2 Jul 2023 19:25:27 +0200 Subject: [PATCH] Replace unneeded if else expressions with set_visible --- src/components/comment_row.rs | 48 +++++++---------- src/components/community_page.rs | 56 ++++++++++---------- src/components/community_row.rs | 12 ++--- src/components/mention_row.rs | 29 ++++------ src/components/moderates_row.rs | 16 ++---- src/components/post_page.rs | 76 ++++++++++++--------------- src/components/post_row.rs | 40 ++++++-------- src/components/private_message_row.rs | 12 ++--- src/components/profile_page.rs | 19 ++++--- 9 files changed, 125 insertions(+), 183 deletions(-) diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index ff9515f..1dadf4a 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -56,14 +56,10 @@ impl FactoryComponent for CommentRow { set_spacing: 10, set_vexpand: false, - if self.comment.creator.avatar.is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - community_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + community_image -> gtk::Box { + set_hexpand: false, + set_visible: self.comment.creator.avatar.is_some(), }, gtk::Button { @@ -91,32 +87,24 @@ impl FactoryComponent for CommentRow { #[local_ref] voting_row -> gtk::Box {}, - if settings::get_current_account().jwt.is_some() { - gtk::Button { - set_icon_name: "mail-replied", - connect_clicked => CommentRowMsg::OpenEditor(true), - } - } else { - gtk::Box {} + gtk::Button { + set_icon_name: "mail-replied", + connect_clicked => CommentRowMsg::OpenEditor(true), + #[watch] + set_visible: settings::get_current_account().jwt.is_some(), }, - if self.comment.creator.id.0 == settings::get_current_account().id { - gtk::Button { - set_icon_name: "document-edit", - connect_clicked => CommentRowMsg::OpenEditor(false), - } - } else { - gtk::Box {} + gtk::Button { + set_icon_name: "document-edit", + connect_clicked => CommentRowMsg::OpenEditor(false), + set_visible: self.comment.creator.id.0 == settings::get_current_account().id, }, - if self.comment.creator.id.0 == settings::get_current_account().id { - gtk::Button { - set_icon_name: "edit-delete", - connect_clicked => CommentRowMsg::DeleteComment, - } - } else { - gtk::Box {} - }, + gtk::Button { + set_icon_name: "edit-delete", + connect_clicked => CommentRowMsg::DeleteComment, + set_visible: self.comment.creator.id.0 == settings::get_current_account().id, + } }, gtk::Separator {} diff --git a/src/components/community_page.rs b/src/components/community_page.rs index c3b7f7c..06c134d 100644 --- a/src/components/community_page.rs +++ b/src/components/community_page.rs @@ -83,39 +83,37 @@ impl SimpleComponent for CommunityPage { }, }, - if settings::get_current_account().jwt.is_some() { - gtk::Box { - set_orientation: gtk::Orientation::Horizontal, - set_halign: gtk::Align::Center, - set_margin_top: 10, + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_halign: gtk::Align::Center, + set_margin_top: 10, + #[watch] + set_visible: settings::get_current_account().jwt.is_some(), - match model.info.subscribed { - SubscribedType::Subscribed => { - gtk::Button { - set_label: "Unsubscribe", - connect_clicked => CommunityInput::ToggleSubscription, - } + match model.info.subscribed { + SubscribedType::Subscribed => { + gtk::Button { + set_label: "Unsubscribe", + connect_clicked => CommunityInput::ToggleSubscription, } - SubscribedType::NotSubscribed => { - gtk::Button { - set_label: "Subscribe", - connect_clicked => CommunityInput::ToggleSubscription, - } - } - SubscribedType::Pending => { - gtk::Label { - set_label: "Subscription pending", - } - } - }, - gtk::Button { - set_label: "Create post", - set_margin_start: 10, - connect_clicked => CommunityInput::OpenCreatePostDialog, } + SubscribedType::NotSubscribed => { + gtk::Button { + set_label: "Subscribe", + connect_clicked => CommunityInput::ToggleSubscription, + } + } + SubscribedType::Pending => { + gtk::Label { + set_label: "Subscription pending", + } + } + }, + gtk::Button { + set_label: "Create post", + set_margin_start: 10, + connect_clicked => CommunityInput::OpenCreatePostDialog, } - } else { - gtk::Box {} }, gtk::Separator { diff --git a/src/components/community_row.rs b/src/components/community_row.rs index 5a42670..61b5540 100644 --- a/src/components/community_row.rs +++ b/src/components/community_row.rs @@ -43,14 +43,10 @@ impl FactoryComponent for CommunityRow { set_orientation: gtk::Orientation::Horizontal, set_spacing: 10, - if self.community.community.icon.is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - community_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + community_image -> gtk::Box { + set_visible: self.community.community.icon.is_some(), + set_hexpand: false, }, gtk::Label { diff --git a/src/components/mention_row.rs b/src/components/mention_row.rs index 1a6c4cc..dadf590 100644 --- a/src/components/mention_row.rs +++ b/src/components/mention_row.rs @@ -59,18 +59,13 @@ impl FactoryComponent for MentionRow { gtk::Label { set_label: "in", }, - if self.comment.community.icon.clone().is_some() { - gtk::Box { - set_hexpand: false, - set_margin_start: 10, - set_margin_end: 10, - #[local_ref] - community_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + community_image -> gtk::Box { + set_hexpand: false, + set_margin_start: 10, + set_margin_end: 10, + set_visible: self.comment.community.icon.clone().is_some(), }, - gtk::Button { set_label: &self.comment.community.title, connect_clicked => MentionRowMsg::OpenCommunity, @@ -83,14 +78,10 @@ impl FactoryComponent for MentionRow { set_margin_top: 10, set_vexpand: false, - if self.comment.creator.avatar.is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - creator_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + creator_image -> gtk::Box { + set_hexpand: false, + set_visible: self.comment.creator.avatar.is_some(), }, gtk::Button { diff --git a/src/components/moderates_row.rs b/src/components/moderates_row.rs index 19816da..b4614a1 100644 --- a/src/components/moderates_row.rs +++ b/src/components/moderates_row.rs @@ -43,17 +43,11 @@ impl FactoryComponent for ModeratesRow { set_orientation: gtk::Orientation::Horizontal, set_spacing: 10, - if self.community.community.icon.is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - community_image -> gtk::Box { - set_height_request: 35, - set_width_request: 35, - } - } - } else { - gtk::Box {} + #[local_ref] + community_image -> gtk::Box { + set_size_request: (35, 35), + set_hexpand: false, + set_visible: self.community.community.icon.is_some(), }, gtk::Label { diff --git a/src/components/post_page.rs b/src/components/post_page.rs index ae217bc..7166477 100644 --- a/src/components/post_page.rs +++ b/src/components/post_page.rs @@ -64,6 +64,8 @@ impl SimpleComponent for PostPage { set_height_request: 400, set_margin_bottom: 20, set_margin_top: 20, + #[watch] + set_visible: model.info.post_view.post.thumbnail_url.is_some(), }, gtk::Label { #[watch] @@ -88,15 +90,11 @@ impl SimpleComponent for PostPage { set_text: "posted by" }, - if model.info.post_view.creator.avatar.is_some() { - gtk::Box { - set_hexpand: false, - set_margin_start: 10, - #[local_ref] - creator_avatar -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + creator_avatar -> gtk::Box { + set_hexpand: false, + set_margin_start: 10, + set_visible: model.info.post_view.creator.avatar.is_some(), }, gtk::Button { @@ -109,14 +107,10 @@ impl SimpleComponent for PostPage { set_text: " in " }, - if model.info.community_view.community.icon.is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - community_avatar -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + community_avatar -> gtk::Box { + set_hexpand: false, + set_visible: model.info.community_view.community.icon.is_some(), }, gtk::Button { @@ -139,24 +133,20 @@ impl SimpleComponent for PostPage { connect_clicked => PostPageInput::OpenLink, }, - if model.info.post_view.creator.id.0 == settings::get_current_account().id { - gtk::Button { - set_icon_name: "document-edit", - connect_clicked => PostPageInput::OpenEditPostDialog, - set_margin_start: 5, - } - } else { - gtk::Box {} + gtk::Button { + set_icon_name: "document-edit", + connect_clicked => PostPageInput::OpenEditPostDialog, + set_margin_start: 5, + #[watch] + set_visible: model.info.post_view.creator.id.0 == settings::get_current_account().id, }, - if model.info.post_view.creator.id.0 == settings::get_current_account().id { - gtk::Button { - set_icon_name: "edit-delete", - connect_clicked => PostPageInput::DeletePost, - set_margin_start: 5, - } - } else { - gtk::Box {} + gtk::Button { + set_icon_name: "edit-delete", + connect_clicked => PostPageInput::DeletePost, + set_margin_start: 5, + #[watch] + set_visible: model.info.post_view.creator.id.0 == settings::get_current_account().id, } }, @@ -174,14 +164,12 @@ impl SimpleComponent for PostPage { #[watch] set_text: &format!("{} comments", model.info.post_view.counts.comments), }, - if settings::get_current_account().jwt.is_some() { - gtk::Button { - set_label: "Comment", - set_margin_start: 10, - connect_clicked => PostPageInput::OpenCreateCommentDialog, - } - } else { - gtk::Box {} + gtk::Button { + set_label: "Comment", + set_margin_start: 10, + connect_clicked => PostPageInput::OpenCreateCommentDialog, + #[watch] + set_visible: settings::get_current_account().jwt.is_some(), } }, @@ -208,7 +196,9 @@ impl SimpleComponent for PostPage { .transient_for(root) .launch(EditorType::Comment) .forward(sender.input_sender(), |msg| match msg { - EditorOutput::CreateRequest(comment, _) => PostPageInput::CreateCommentRequest(comment), + EditorOutput::CreateRequest(comment, _) => { + PostPageInput::CreateCommentRequest(comment) + } EditorOutput::EditRequest(post, _) => PostPageInput::EditPostRequest(post), }); let voting_row = VotingRowModel::builder() @@ -340,7 +330,7 @@ impl SimpleComponent for PostPage { .body .clone() .unwrap_or(String::from("")), - url: reqwest::Url::parse(&url).ok() + url: reqwest::Url::parse(&url).ok(), }; let sender = self.create_comment_dialog.sender(); sender.emit(DialogMsg::UpdateData(data)); diff --git a/src/components/post_row.rs b/src/components/post_row.rs index ab2e95c..3bfda50 100644 --- a/src/components/post_row.rs +++ b/src/components/post_row.rs @@ -47,14 +47,10 @@ impl FactoryComponent for PostRow { set_vexpand: false, set_hexpand: true, - if self.post.community.icon.clone().is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - community_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + community_image -> gtk::Box { + set_hexpand: false, + set_visible: self.post.community.icon.clone().is_some(), }, gtk::Button { @@ -62,15 +58,11 @@ impl FactoryComponent for PostRow { connect_clicked => PostRowMsg::OpenCommunity, }, - if self.post.creator.avatar.clone().is_some() { - gtk::Box { - set_hexpand: false, - set_margin_start: 10, - #[local_ref] - author_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + author_image -> gtk::Box { + set_hexpand: false, + set_margin_start: 10, + set_visible: self.post.creator.avatar.clone().is_some(), }, gtk::Button { @@ -105,14 +97,12 @@ impl FactoryComponent for PostRow { set_halign: gtk::Align::Start, set_text: &format!("{} comments", self.post.counts.comments.clone()), }, - if self.post.creator.id.0 == settings::get_current_account().id { - gtk::Button { - set_icon_name: "edit-delete", - connect_clicked => PostRowMsg::DeletePost, - set_margin_start: 10, - } - } else { - gtk::Box {} + gtk::Button { + set_icon_name: "edit-delete", + connect_clicked => PostRowMsg::DeletePost, + set_margin_start: 10, + #[watch] + set_visible: self.post.creator.id.0 == settings::get_current_account().id, } }, diff --git a/src/components/private_message_row.rs b/src/components/private_message_row.rs index 73efdd4..b731ce5 100644 --- a/src/components/private_message_row.rs +++ b/src/components/private_message_row.rs @@ -40,14 +40,10 @@ impl FactoryComponent for PrivateMessageRow { set_vexpand: false, set_hexpand: true, - if self.message.creator.avatar.is_some() { - gtk::Box { - set_hexpand: false, - #[local_ref] - creator_image -> gtk::Box {} - } - } else { - gtk::Box {} + #[local_ref] + creator_image -> gtk::Box { + set_hexpand: false, + set_visible: self.message.creator.avatar.is_some(), }, gtk::Button { diff --git a/src/components/profile_page.rs b/src/components/profile_page.rs index f9ba660..6356b92 100644 --- a/src/components/profile_page.rs +++ b/src/components/profile_page.rs @@ -49,9 +49,11 @@ impl SimpleComponent for ProfilePage { #[local_ref] avatar -> gtk::Box { - set_size_request: (100, 100), + set_size_request: (150, 150), set_margin_bottom: 20, set_margin_top: 20, + #[watch] + set_visible: model.info.person_view.person.avatar.is_some(), }, gtk::Label { #[watch] @@ -75,15 +77,12 @@ impl SimpleComponent for ProfilePage { #[watch] set_text: &format!("{} posts, {} comments", model.info.person_view.counts.post_count, model.info.person_view.counts.comment_count), }, - if settings::get_current_account().jwt.is_some() { - gtk::Button { - set_label: "Send message", - connect_clicked => ProfileInput::SendMessageRequest, - set_margin_start: 10, - } - } else { - gtk::Box {} - }, + gtk::Button { + set_label: "Send message", + connect_clicked => ProfileInput::SendMessageRequest, + set_margin_start: 10, + set_visible: settings::get_current_account().jwt.is_some(), + } }, gtk::Separator {},