From 257250eb62f23309301e95264b928ba6fd0ead25 Mon Sep 17 00:00:00 2001 From: Ethan Brierley Date: Sat, 20 Apr 2024 10:13:02 +0100 Subject: [PATCH] refactor(power): extract performing power actions into its own method --- cosmic-applet-power/src/lib.rs | 37 ++++++++++++++-------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/cosmic-applet-power/src/lib.rs b/cosmic-applet-power/src/lib.rs index b97899a3..efffe423 100644 --- a/cosmic-applet-power/src/lib.rs +++ b/cosmic-applet-power/src/lib.rs @@ -66,6 +66,19 @@ enum PowerAction { Shutdown, } +impl PowerAction { + fn perform(self) -> iced::Command> { + let msg = |m| cosmic::app::message::app(Message::Zbus(m)); + match self { + PowerAction::Lock => iced::Command::perform(lock(), msg), + PowerAction::LogOut => iced::Command::perform(log_out(), msg), + PowerAction::Suspend => iced::Command::perform(suspend(), msg), + PowerAction::Restart => iced::Command::perform(restart(), msg), + PowerAction::Shutdown => iced::Command::perform(shutdown(), msg), + } + } +} + #[derive(Debug, Clone)] enum Message { Countdown, @@ -171,17 +184,7 @@ impl cosmic::Application for Power { } Message::Confirm => { if let Some((id, a, _)) = self.action_to_confirm.take() { - let msg = |m| cosmic::app::message::app(Message::Zbus(m)); - Command::batch(vec![ - destroy_layer_surface(id), - match a { - PowerAction::Lock => iced::Command::perform(lock(), msg), - PowerAction::LogOut => iced::Command::perform(log_out(), msg), - PowerAction::Suspend => iced::Command::perform(suspend(), msg), - PowerAction::Restart => iced::Command::perform(restart(), msg), - PowerAction::Shutdown => iced::Command::perform(shutdown(), msg), - }, - ]) + Command::batch(vec![destroy_layer_surface(id), a.perform()]) } else { Command::none() } @@ -200,17 +203,7 @@ impl cosmic::Application for Power { let a = *a; self.action_to_confirm = None; - let msg = |m: zbus::Result<()>| cosmic::app::message::app(Message::Zbus(m)); - return Command::batch(vec![ - destroy_layer_surface(id), - match a { - PowerAction::Lock => iced::Command::perform(lock(), msg), - PowerAction::LogOut => iced::Command::perform(log_out(), msg), - PowerAction::Suspend => iced::Command::perform(suspend(), msg), - PowerAction::Restart => iced::Command::perform(restart(), msg), - PowerAction::Shutdown => iced::Command::perform(shutdown(), msg), - }, - ]); + return Command::batch(vec![destroy_layer_surface(id), a.perform()]); } } Command::none()