refactor(power): extract performing power actions into its own method

This commit is contained in:
Ethan Brierley 2024-04-20 10:13:02 +01:00 committed by Ashley Wulber
parent e4a6f94723
commit 257250eb62

View file

@ -66,6 +66,19 @@ enum PowerAction {
Shutdown, Shutdown,
} }
impl PowerAction {
fn perform(self) -> iced::Command<cosmic::app::Message<Message>> {
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)] #[derive(Debug, Clone)]
enum Message { enum Message {
Countdown, Countdown,
@ -171,17 +184,7 @@ impl cosmic::Application for Power {
} }
Message::Confirm => { Message::Confirm => {
if let Some((id, a, _)) = self.action_to_confirm.take() { 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), a.perform()])
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),
},
])
} else { } else {
Command::none() Command::none()
} }
@ -200,17 +203,7 @@ impl cosmic::Application for Power {
let a = *a; let a = *a;
self.action_to_confirm = None; 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), a.perform()]);
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),
},
]);
} }
} }
Command::none() Command::none()