From ba9100ec9d8fb294e0e1b54f645312f8f800ef1d Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 6 May 2024 15:16:34 -0400 Subject: [PATCH] refactor(power): use built in dialog widget --- .../i18n/de/cosmic_applet_power.ftl | 2 +- .../i18n/en/cosmic_applet_power.ftl | 28 ++++-- .../i18n/es-ES/cosmic_applet_power.ftl | 2 +- .../i18n/fi/cosmic_applet_power.ftl | 2 +- .../i18n/fr/cosmic_applet_power.ftl | 2 +- .../i18n/it/cosmic_applet_power.ftl | 2 +- .../i18n/ja/cosmic_applet_power.ftl | 2 +- .../i18n/ko/cosmic_applet_power.ftl | 2 +- .../i18n/nl/cosmic_applet_power.ftl | 2 +- .../i18n/pt/cosmic_applet_power.ftl | 2 +- .../i18n/ru/cosmic_applet_power.ftl | 2 +- .../i18n/sr-Cyrl/cosmic_applet_power.ftl | 2 +- .../i18n/sr-Latn/cosmic_applet_power.ftl | 2 +- .../i18n/uk/cosmic_applet_power.ftl | 2 +- cosmic-applet-power/src/lib.rs | 92 ++++++++++--------- 15 files changed, 86 insertions(+), 60 deletions(-) diff --git a/cosmic-applet-power/i18n/de/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/de/cosmic_applet_power.ftl index a4eac9bf..537f3f5f 100644 --- a/cosmic-applet-power/i18n/de/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/de/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Neustart shutdown = Ausschalten confirm = Bestätigen cancel = Abbrechen -confirm-question = Sind Sie sicher? { $action -> +confirm-body = Sind Sie sicher? { $action -> [restart] Ihr System wird [suspend] Ihr System wird [shutdown] Ihr System wird diff --git a/cosmic-applet-power/i18n/en/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/en/cosmic_applet_power.ftl index 7650d4af..6b084325 100644 --- a/cosmic-applet-power/i18n/en/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/en/cosmic_applet_power.ftl @@ -9,13 +9,29 @@ restart = Restart shutdown = Shutdown confirm = Confirm cancel = Cancel -confirm-question = - Are you sure? { $action -> +confirm-button = { + $action -> + [restart] { restart } + [suspend] { suspend} + [shutdown] Power off + [log-out] { log-out } + *[other] { confirm} +} +confirm-title = + { $action -> [restart] { restart } [suspend] { suspend } [shutdown] { shutdown } - [lock-screen] Locking the screen - [log-out] Logging out - *[other] The selected action - } will continue in { $countdown } seconds. + [log-out] { log-out } + *[other] Apply the selected action + } now? +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. diff --git a/cosmic-applet-power/i18n/es-ES/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/es-ES/cosmic_applet_power.ftl index 1b56d3c5..28767420 100644 --- a/cosmic-applet-power/i18n/es-ES/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/es-ES/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Reiniciar shutdown = Apagar confirm = Vale cancel = Cancelar -confirm-question = +confirm-body = ¿Está bien? { $action -> [restart] El ordenador se reiniciará [suspend] El ordenador se suspenderá diff --git a/cosmic-applet-power/i18n/fi/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/fi/cosmic_applet_power.ftl index c47639da..d0940943 100644 --- a/cosmic-applet-power/i18n/fi/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/fi/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Uudelleenkäynnistä shutdown = Sammuta confirm = Varmista cancel = Peruuta -confirm-question = +confirm-body = Oletko Varma? { $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/i18n/fr/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/fr/cosmic_applet_power.ftl index 70f07a48..3f3001c8 100644 --- a/cosmic-applet-power/i18n/fr/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/fr/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Redémarrer shutdown = Éteindre confirm = Confirmer cancel = Annuler -confirm-question = +confirm-body = Êtes-vous sûr ? { $action -> [restart] Redémarrer l'ordinateur [suspend] Mettre en veille diff --git a/cosmic-applet-power/i18n/it/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/it/cosmic_applet_power.ftl index 0940d4fb..4a020f8c 100644 --- a/cosmic-applet-power/i18n/it/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/it/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Riavvia shutdown = Spegni confirm = Conferma cancel = Annulla -confirm-question = +confirm-body = Confermi? { $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/i18n/ja/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/ja/cosmic_applet_power.ftl index 24ba8374..258be25b 100644 --- a/cosmic-applet-power/i18n/ja/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/ja/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = 再起動 shutdown = シャットダウン confirm = 確認 cancel = キャンセル -confirm-question = +confirm-body = よろしいでしょうか? { $countdown }秒後にシステムは自動的に{ $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/i18n/ko/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/ko/cosmic_applet_power.ftl index d5750215..cbd92c5a 100644 --- a/cosmic-applet-power/i18n/ko/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/ko/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = 다시 시작 shutdown = 시스템 종료 confirm = 확인 cancel = 취소 -confirm-question = +confirm-body = 계속하시겠습니까? { $countdown }초 후 { $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/i18n/nl/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/nl/cosmic_applet_power.ftl index 374e0f20..810c2272 100644 --- a/cosmic-applet-power/i18n/nl/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/nl/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Herstarten shutdown = Afsluiten confirm = Bevestigen cancel = Annuleren -confirm-question = +confirm-body = Weet je het zeker? { $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/i18n/pt/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/pt/cosmic_applet_power.ftl index 46bbb4ca..de32d7fc 100644 --- a/cosmic-applet-power/i18n/pt/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/pt/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Reiniciar shutdown = Encerrar confirm = Confirmar cancel = Cancelar -confirm-question = +confirm-body = Tem a certeza? { $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/i18n/ru/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/ru/cosmic_applet_power.ftl index c5973c92..5330728e 100644 --- a/cosmic-applet-power/i18n/ru/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/ru/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Перезагрузка shutdown = Выключение confirm = Подтвердить cancel = Отмена -confirm-question = +confirm-body = Вы уверены? { $action -> [restart] Перезагрузка [suspend] Переход в режим сна diff --git a/cosmic-applet-power/i18n/sr-Cyrl/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/sr-Cyrl/cosmic_applet_power.ftl index 9232a372..910cd608 100644 --- a/cosmic-applet-power/i18n/sr-Cyrl/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/sr-Cyrl/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Поново покрени shutdown = Искључи confirm = Потврди cancel = Откажи -confirm-question = +confirm-body = Јесте ли сигурни? { $action -> [restart] Поновно покретање [suspend] Стање спавања diff --git a/cosmic-applet-power/i18n/sr-Latn/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/sr-Latn/cosmic_applet_power.ftl index f1387409..07e107cd 100644 --- a/cosmic-applet-power/i18n/sr-Latn/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/sr-Latn/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Ponovo pokreni shutdown = Isključi confirm = Potvrdi cancel = Otkaži -confirm-question = +confirm-body = Jeste li sigurni? { $action -> [restart] Ponovno pokretanje [suspend] Stanje spavanja diff --git a/cosmic-applet-power/i18n/uk/cosmic_applet_power.ftl b/cosmic-applet-power/i18n/uk/cosmic_applet_power.ftl index 6b00252b..74320fbd 100644 --- a/cosmic-applet-power/i18n/uk/cosmic_applet_power.ftl +++ b/cosmic-applet-power/i18n/uk/cosmic_applet_power.ftl @@ -9,7 +9,7 @@ restart = Перезавантажити shutdown = Вимкнути confirm = Підтвердити cancel = Скасувати -confirm-question = +confirm-body = Are you sure? { $action -> [restart] { restart } [suspend] { suspend } diff --git a/cosmic-applet-power/src/lib.rs b/cosmic-applet-power/src/lib.rs index eee7c2a6..fdeb4b54 100644 --- a/cosmic-applet-power/src/lib.rs +++ b/cosmic-applet-power/src/lib.rs @@ -10,7 +10,6 @@ use cosmic::iced::event::{listen_with, PlatformSpecific}; use cosmic::iced::time; use cosmic::iced::wayland::actions::layer_surface::SctkLayerSurfaceSettings; 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_sctk::commands::layer_surface::{ 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::Renderer; -use cosmic::iced::Color; use cosmic::iced::{ widget::{self, column, container, row, space::Space, text}, window, Alignment, Length, Subscription, @@ -31,6 +29,7 @@ use cosmic::{app::Command, Element, Theme}; use logind_zbus::manager::ManagerProxy; use logind_zbus::session::{SessionProxy, SessionType}; use logind_zbus::user::UserProxy; +use once_cell::sync::Lazy; use rustix::process::getuid; use zbus::Connection; @@ -48,6 +47,7 @@ pub fn run() -> cosmic::iced::Result { } const COUNTDOWN_LENGTH: u8 = 60; +static CONFIRM_ID: Lazy = Lazy::new(|| iced::id::Id::new("confirm-id")); #[derive(Default)] struct Power { @@ -89,6 +89,7 @@ enum Message { Cancel, Zbus(Result<(), zbus::Error>), Closed(window::Id), + LayerFocus, } impl cosmic::Application for Power { @@ -126,6 +127,9 @@ impl cosmic::Application for Power { cosmic::iced::Event::PlatformSpecific(PlatformSpecific::Wayland( wayland::Event::Layer(LayerEvent::Unfocused, ..), )) => Some(Message::Cancel), + cosmic::iced::Event::PlatformSpecific(PlatformSpecific::Wayland( + wayland::Event::Layer(LayerEvent::Focused, ..), + )) => Some(Message::LayerFocus), _ => None, })); if self.action_to_confirm.is_some() { @@ -219,6 +223,7 @@ impl cosmic::Application for Power { } Command::none() } + Message::LayerFocus => button::focus(CONFIRM_ID.clone()), } } @@ -290,53 +295,58 @@ impl cosmic::Application for Power { PowerAction::Restart => "restart", PowerAction::Shutdown => "shutdown", }; + + let title = fl!( + "confirm-title", + HashMap::from_iter(vec![("action", action)]) + ); let countdown = &countdown.to_string(); - let content = column![ - text(fl!( - "confirm-question", + let mut dialog = cosmic::widget::dialog(title) + .body(fl!( + "confirm-body", HashMap::from_iter(vec![("action", action), ("countdown", countdown)]) )) - .size(16), - row![ - button(text(fl!("confirm")).size(14)) - .padding(8) - .style(theme::Button::Suggested) - .on_press(Message::Confirm), + .primary_action( + button( + text(fl!("confirm", HashMap::from_iter(vec![("action", action)]))).size(14), + ) + .id(CONFIRM_ID.clone()) + .padding(8) + .style(theme::Button::Suggested) + .on_press(Message::Confirm), + ) + .secondary_action( button(text(fl!("cancel")).size(14)) .padding(8) .style(theme::Button::Standard) .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) - .align_y(Vertical::Center) - .width(Length::Fill) - .height(Length::Fill), + .icon(text_icon( + match power_action { + PowerAction::Lock => "system-lock-screen-symbolic", + 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_right_press(Message::Cancel)