refactor(power): use built in dialog widget

This commit is contained in:
Ashley Wulber 2024-05-06 15:16:34 -04:00 committed by Michael Murphy
parent efae49e90b
commit ba9100ec9d
15 changed files with 86 additions and 60 deletions

View file

@ -9,7 +9,7 @@ restart = Neustart
shutdown = Ausschalten shutdown = Ausschalten
confirm = Bestätigen confirm = Bestätigen
cancel = Abbrechen cancel = Abbrechen
confirm-question = Sind Sie sicher? { $action -> confirm-body = Sind Sie sicher? { $action ->
[restart] Ihr System wird [restart] Ihr System wird
[suspend] Ihr System wird [suspend] Ihr System wird
[shutdown] Ihr System wird [shutdown] Ihr System wird

View file

@ -9,13 +9,29 @@ restart = Restart
shutdown = Shutdown shutdown = Shutdown
confirm = Confirm confirm = Confirm
cancel = Cancel cancel = Cancel
confirm-question = confirm-button = {
Are you sure? { $action -> $action ->
[restart] { restart }
[suspend] { suspend}
[shutdown] Power off
[log-out] { log-out }
*[other] { confirm}
}
confirm-title =
{ $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }
[shutdown] { shutdown } [shutdown] { shutdown }
[lock-screen] Locking the screen [log-out] { log-out }
[log-out] Logging out *[other] Apply the selected action
*[other] The selected action } now?
} will continue in { $countdown } seconds. confirm-body =
The system will { $action ->
[restart] restart
[suspend] suspend
[shutdown] power off
[lock-screen] lock the screen
[log-out] log out
*[other] apply the selected action
} automatically in { $countdown } seconds.

View file

@ -9,7 +9,7 @@ restart = Reiniciar
shutdown = Apagar shutdown = Apagar
confirm = Vale confirm = Vale
cancel = Cancelar cancel = Cancelar
confirm-question = confirm-body =
¿Está bien? { $action -> ¿Está bien? { $action ->
[restart] El ordenador se reiniciará [restart] El ordenador se reiniciará
[suspend] El ordenador se suspenderá [suspend] El ordenador se suspenderá

View file

@ -9,7 +9,7 @@ restart = Uudelleenkäynnistä
shutdown = Sammuta shutdown = Sammuta
confirm = Varmista confirm = Varmista
cancel = Peruuta cancel = Peruuta
confirm-question = confirm-body =
Oletko Varma? { $action -> Oletko Varma? { $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -9,7 +9,7 @@ restart = Redémarrer
shutdown = Éteindre shutdown = Éteindre
confirm = Confirmer confirm = Confirmer
cancel = Annuler cancel = Annuler
confirm-question = confirm-body =
Êtes-vous sûr ? { $action -> Êtes-vous sûr ? { $action ->
[restart] Redémarrer l'ordinateur [restart] Redémarrer l'ordinateur
[suspend] Mettre en veille [suspend] Mettre en veille

View file

@ -9,7 +9,7 @@ restart = Riavvia
shutdown = Spegni shutdown = Spegni
confirm = Conferma confirm = Conferma
cancel = Annulla cancel = Annulla
confirm-question = confirm-body =
Confermi? { $action -> Confermi? { $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -9,7 +9,7 @@ restart = 再起動
shutdown = シャットダウン shutdown = シャットダウン
confirm = 確認 confirm = 確認
cancel = キャンセル cancel = キャンセル
confirm-question = confirm-body =
よろしいでしょうか? { $countdown }秒後にシステムは自動的に{ $action -> よろしいでしょうか? { $countdown }秒後にシステムは自動的に{ $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -9,7 +9,7 @@ restart = 다시 시작
shutdown = 시스템 종료 shutdown = 시스템 종료
confirm = 확인 confirm = 확인
cancel = 취소 cancel = 취소
confirm-question = confirm-body =
계속하시겠습니까? { $countdown }초 후 { $action -> 계속하시겠습니까? { $countdown }초 후 { $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -9,7 +9,7 @@ restart = Herstarten
shutdown = Afsluiten shutdown = Afsluiten
confirm = Bevestigen confirm = Bevestigen
cancel = Annuleren cancel = Annuleren
confirm-question = confirm-body =
Weet je het zeker? { $action -> Weet je het zeker? { $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -9,7 +9,7 @@ restart = Reiniciar
shutdown = Encerrar shutdown = Encerrar
confirm = Confirmar confirm = Confirmar
cancel = Cancelar cancel = Cancelar
confirm-question = confirm-body =
Tem a certeza? { $action -> Tem a certeza? { $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -9,7 +9,7 @@ restart = Перезагрузка
shutdown = Выключение shutdown = Выключение
confirm = Подтвердить confirm = Подтвердить
cancel = Отмена cancel = Отмена
confirm-question = confirm-body =
Вы уверены? { $action -> Вы уверены? { $action ->
[restart] Перезагрузка [restart] Перезагрузка
[suspend] Переход в режим сна [suspend] Переход в режим сна

View file

@ -9,7 +9,7 @@ restart = Поново покрени
shutdown = Искључи shutdown = Искључи
confirm = Потврди confirm = Потврди
cancel = Откажи cancel = Откажи
confirm-question = confirm-body =
Јесте ли сигурни? { $action -> Јесте ли сигурни? { $action ->
[restart] Поновно покретање [restart] Поновно покретање
[suspend] Стање спавања [suspend] Стање спавања

View file

@ -9,7 +9,7 @@ restart = Ponovo pokreni
shutdown = Isključi shutdown = Isključi
confirm = Potvrdi confirm = Potvrdi
cancel = Otkaži cancel = Otkaži
confirm-question = confirm-body =
Jeste li sigurni? { $action -> Jeste li sigurni? { $action ->
[restart] Ponovno pokretanje [restart] Ponovno pokretanje
[suspend] Stanje spavanja [suspend] Stanje spavanja

View file

@ -9,7 +9,7 @@ restart = Перезавантажити
shutdown = Вимкнути shutdown = Вимкнути
confirm = Підтвердити confirm = Підтвердити
cancel = Скасувати cancel = Скасувати
confirm-question = confirm-body =
Are you sure? { $action -> Are you sure? { $action ->
[restart] { restart } [restart] { restart }
[suspend] { suspend } [suspend] { suspend }

View file

@ -10,7 +10,6 @@ use cosmic::iced::event::{listen_with, PlatformSpecific};
use cosmic::iced::time; use cosmic::iced::time;
use cosmic::iced::wayland::actions::layer_surface::SctkLayerSurfaceSettings; use cosmic::iced::wayland::actions::layer_surface::SctkLayerSurfaceSettings;
use cosmic::iced::wayland::popup::{destroy_popup, get_popup}; use cosmic::iced::wayland::popup::{destroy_popup, get_popup};
use cosmic::iced_core::{Border, Shadow};
use cosmic::iced_runtime::core::layout::Limits; use cosmic::iced_runtime::core::layout::Limits;
use cosmic::iced_sctk::commands::layer_surface::{ use cosmic::iced_sctk::commands::layer_surface::{
destroy_layer_surface, get_layer_surface, Anchor, KeyboardInteractivity, destroy_layer_surface, get_layer_surface, Anchor, KeyboardInteractivity,
@ -19,7 +18,6 @@ use cosmic::iced_widget::mouse_area;
use cosmic::widget::{button, divider, icon}; use cosmic::widget::{button, divider, icon};
use cosmic::Renderer; use cosmic::Renderer;
use cosmic::iced::Color;
use cosmic::iced::{ use cosmic::iced::{
widget::{self, column, container, row, space::Space, text}, widget::{self, column, container, row, space::Space, text},
window, Alignment, Length, Subscription, window, Alignment, Length, Subscription,
@ -31,6 +29,7 @@ use cosmic::{app::Command, Element, Theme};
use logind_zbus::manager::ManagerProxy; use logind_zbus::manager::ManagerProxy;
use logind_zbus::session::{SessionProxy, SessionType}; use logind_zbus::session::{SessionProxy, SessionType};
use logind_zbus::user::UserProxy; use logind_zbus::user::UserProxy;
use once_cell::sync::Lazy;
use rustix::process::getuid; use rustix::process::getuid;
use zbus::Connection; use zbus::Connection;
@ -48,6 +47,7 @@ pub fn run() -> cosmic::iced::Result {
} }
const COUNTDOWN_LENGTH: u8 = 60; const COUNTDOWN_LENGTH: u8 = 60;
static CONFIRM_ID: Lazy<iced::id::Id> = Lazy::new(|| iced::id::Id::new("confirm-id"));
#[derive(Default)] #[derive(Default)]
struct Power { struct Power {
@ -89,6 +89,7 @@ enum Message {
Cancel, Cancel,
Zbus(Result<(), zbus::Error>), Zbus(Result<(), zbus::Error>),
Closed(window::Id), Closed(window::Id),
LayerFocus,
} }
impl cosmic::Application for Power { impl cosmic::Application for Power {
@ -126,6 +127,9 @@ impl cosmic::Application for Power {
cosmic::iced::Event::PlatformSpecific(PlatformSpecific::Wayland( cosmic::iced::Event::PlatformSpecific(PlatformSpecific::Wayland(
wayland::Event::Layer(LayerEvent::Unfocused, ..), wayland::Event::Layer(LayerEvent::Unfocused, ..),
)) => Some(Message::Cancel), )) => Some(Message::Cancel),
cosmic::iced::Event::PlatformSpecific(PlatformSpecific::Wayland(
wayland::Event::Layer(LayerEvent::Focused, ..),
)) => Some(Message::LayerFocus),
_ => None, _ => None,
})); }));
if self.action_to_confirm.is_some() { if self.action_to_confirm.is_some() {
@ -219,6 +223,7 @@ impl cosmic::Application for Power {
} }
Command::none() Command::none()
} }
Message::LayerFocus => button::focus(CONFIRM_ID.clone()),
} }
} }
@ -290,53 +295,58 @@ impl cosmic::Application for Power {
PowerAction::Restart => "restart", PowerAction::Restart => "restart",
PowerAction::Shutdown => "shutdown", PowerAction::Shutdown => "shutdown",
}; };
let title = fl!(
"confirm-title",
HashMap::from_iter(vec![("action", action)])
);
let countdown = &countdown.to_string(); let countdown = &countdown.to_string();
let content = column![ let mut dialog = cosmic::widget::dialog(title)
text(fl!( .body(fl!(
"confirm-question", "confirm-body",
HashMap::from_iter(vec![("action", action), ("countdown", countdown)]) HashMap::from_iter(vec![("action", action), ("countdown", countdown)])
)) ))
.size(16), .primary_action(
row![ button(
button(text(fl!("confirm")).size(14)) text(fl!("confirm", HashMap::from_iter(vec![("action", action)]))).size(14),
.padding(8) )
.style(theme::Button::Suggested) .id(CONFIRM_ID.clone())
.on_press(Message::Confirm), .padding(8)
.style(theme::Button::Suggested)
.on_press(Message::Confirm),
)
.secondary_action(
button(text(fl!("cancel")).size(14)) button(text(fl!("cancel")).size(14))
.padding(8) .padding(8)
.style(theme::Button::Standard) .style(theme::Button::Standard)
.on_press(Message::Cancel), .on_press(Message::Cancel),
]
.spacing(24)
]
.align_items(Alignment::Center)
.spacing(12)
.padding(24);
mouse_area(
container(
container(content)
.style(cosmic::theme::Container::custom(|theme| {
container::Appearance {
icon_color: Some(theme.cosmic().background.on.into()),
text_color: Some(theme.cosmic().background.on.into()),
background: Some(
Color::from(theme.cosmic().background.base).into(),
),
border: Border {
radius: 12.0.into(),
width: 2.0,
color: theme.cosmic().bg_divider().into(),
},
shadow: Shadow::default(),
}
}))
.width(Length::Shrink)
.height(Length::Shrink),
) )
.align_x(Horizontal::Center) .icon(text_icon(
.align_y(Vertical::Center) match power_action {
.width(Length::Fill) PowerAction::Lock => "system-lock-screen-symbolic",
.height(Length::Fill), PowerAction::LogOut => "system-log-out-symbolic",
PowerAction::Suspend => "system-suspend-symbolic",
PowerAction::Restart => "system-restart-symbolic",
PowerAction::Shutdown => "system-shutdown-symbolic",
},
60,
));
if matches!(power_action, PowerAction::Shutdown) {
dialog = dialog.tertiary_action(
button(text(fl!("restart")).size(14))
.padding(8)
.style(theme::Button::Link)
.on_press(Message::Action(PowerAction::Restart)),
);
}
mouse_area(
container(dialog)
.align_x(Horizontal::Center)
.align_y(Vertical::Center)
.width(Length::Fill)
.height(Length::Fill),
) )
.on_press(Message::Cancel) .on_press(Message::Cancel)
.on_right_press(Message::Cancel) .on_right_press(Message::Cancel)