summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJoakim Hulthe <joakim@hulthe.net>2025-02-25 11:49:51 +0100
committerDavid Lönnhager <david.l@mullvad.net>2025-03-05 23:32:21 +0100
commitf7dd7dfc4e69997f77f2448c44bb19be212a549a (patch)
tree244c32be5a108b7524f8930f851fb7645994acdb
parent7bfd655b73de6015ad79581df869b01a04f5b36f (diff)
downloadmullvadvpn-f7dd7dfc4e69997f77f2448c44bb19be212a549a.tar.xz
mullvadvpn-f7dd7dfc4e69997f77f2448c44bb19be212a549a.zip
Refactor buttons slightly
-rw-r--r--installer-downloader/src/cacao_impl/delegate.rs45
-rw-r--r--installer-downloader/src/cacao_impl/ui.rs55
2 files changed, 51 insertions, 49 deletions
diff --git a/installer-downloader/src/cacao_impl/delegate.rs b/installer-downloader/src/cacao_impl/delegate.rs
index a9ff24d108..cf9d20e1d1 100644
--- a/installer-downloader/src/cacao_impl/delegate.rs
+++ b/installer-downloader/src/cacao_impl/delegate.rs
@@ -15,28 +15,21 @@ impl AppDelegate for AppWindow {
where
F: Fn() + Send + 'static,
{
- let cb = Self::sync_callback(callback);
- self.download_button.button.set_action(move || {
- let cb = Action::DownloadClick(cb.clone());
- cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(cb);
- });
+ self.download_button.set_callback(callback);
}
fn on_cancel<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
- let cb = Self::sync_callback(callback);
- self.cancel_button.button.set_action(move || {
- let cb = Action::CancelClick(cb.clone());
- cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(cb);
- });
+ self.cancel_button.set_callback(callback);
}
fn on_beta_link<F>(&mut self, callback: F)
where
F: Fn() + Send + 'static,
{
+ self.beta_link.set_callback(callback);
}
fn set_status_text(&mut self, text: &str) {
@@ -76,35 +69,35 @@ impl AppDelegate for AppWindow {
}
fn show_download_button(&mut self) {
- self.download_button.button.set_hidden(false);
+ self.download_button.set_hidden(false);
}
fn hide_download_button(&mut self) {
- self.download_button.button.set_hidden(true);
+ self.download_button.set_hidden(true);
}
fn enable_download_button(&mut self) {
- self.download_button.button.set_enabled(true);
+ self.download_button.set_enabled(true);
}
fn disable_download_button(&mut self) {
- self.download_button.button.set_enabled(false);
+ self.download_button.set_enabled(false);
}
fn show_cancel_button(&mut self) {
- self.cancel_button.button.set_hidden(false);
+ self.cancel_button.set_hidden(false);
}
fn hide_cancel_button(&mut self) {
- self.cancel_button.button.set_hidden(true);
+ self.cancel_button.set_hidden(true);
}
fn enable_cancel_button(&mut self) {
- self.cancel_button.button.set_enabled(true);
+ self.cancel_button.set_enabled(true);
}
fn disable_cancel_button(&mut self) {
- self.cancel_button.button.set_enabled(false);
+ self.cancel_button.set_enabled(false);
}
fn show_beta_text(&mut self) {
@@ -129,7 +122,7 @@ impl AppDelegate for AppWindow {
where
F: Fn() + Send + 'static,
{
- println!("todo. on stable link");
+ self.stable_link.set_callback(callback);
}
fn show_stable_text(&mut self) {
@@ -141,17 +134,19 @@ impl AppDelegate for AppWindow {
}
fn show_error_message(&mut self, message: installer_downloader::delegate::ErrorMessage) {
- let on_cancel = self.error_cancel_callback.clone().map(|cb| {
+ let on_cancel = self.error_cancel_callback.clone().map(|callback| {
move || {
- let cb = Action::ErrorCancel(cb.clone());
- cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(cb);
+ let callback = callback.clone();
+ let callback = Action::ButtonClick { callback };
+ cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(callback);
}
});
- let on_retry = self.error_retry_callback.clone().map(|cb| {
+ let on_retry = self.error_retry_callback.clone().map(|callback| {
move || {
- let cb = Action::ErrorRetry(cb.clone());
- cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(cb);
+ let callback = callback.clone();
+ let callback = Action::ButtonClick { callback };
+ cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(callback);
}
});
diff --git a/installer-downloader/src/cacao_impl/ui.rs b/installer-downloader/src/cacao_impl/ui.rs
index aa58ff4177..5294eabd0c 100644
--- a/installer-downloader/src/cacao_impl/ui.rs
+++ b/installer-downloader/src/cacao_impl/ui.rs
@@ -1,5 +1,5 @@
use std::cell::RefCell;
-use std::ops::Deref;
+use std::ops::{Deref, DerefMut};
use std::sync::{Arc, LazyLock, Mutex, RwLock};
use cacao::appkit::window::{Window, WindowConfig, WindowDelegate};
@@ -79,16 +79,13 @@ impl AppDelegate for AppImpl {
/// Dispatcher actions
pub enum Action {
- /// User clicked the download button
- DownloadClick(Arc<Mutex<Box<dyn Fn() + Send>>>),
- /// User clicked the cancel button
- CancelClick(Arc<Mutex<Box<dyn Fn() + Send>>>),
+ /// User clicked a button.
+ ButtonClick {
+ /// The callback to be invoked in the main thread.
+ callback: Arc<Mutex<Box<dyn Fn() + Send>>>,
+ },
/// Run callback on main thread
QueueMain(Mutex<Option<MainThreadCallback>>),
- /// User clicked the retry button in the error view
- ErrorRetry(Arc<Mutex<Box<dyn Fn() + Send>>>),
- /// User clicked the cancel button in the error view
- ErrorCancel(Arc<Mutex<Box<dyn Fn() + Send>>>),
/// Quit the application.
Quit,
}
@@ -102,13 +99,9 @@ impl Dispatcher for AppImpl {
fn on_ui_message(&self, message: Self::Message) {
let delegate = self.window.delegate.as_ref().unwrap();
match message {
- Action::DownloadClick(cb) => {
- let cb = cb.lock().unwrap();
- cb();
- }
- Action::CancelClick(cb) => {
- let cb = cb.lock().unwrap();
- cb();
+ Action::ButtonClick { callback } => {
+ let callback = callback.lock().unwrap();
+ callback();
}
Action::QueueMain(cb) => {
// NOTE: We assume that this won't panic because they will never run simultaneously
@@ -116,14 +109,6 @@ impl Dispatcher for AppImpl {
let cb = cb.lock().unwrap().take().unwrap();
cb(&mut borrowed);
}
- Action::ErrorRetry(cb) => {
- let cb = cb.lock().unwrap();
- cb();
- }
- Action::ErrorCancel(cb) => {
- let cb = cb.lock().unwrap();
- cb();
- }
Action::Quit => {
self.window.close();
}
@@ -204,6 +189,28 @@ macro_rules! button_wrapper {
&self.button
}
}
+
+ impl DerefMut for $name {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.button
+ }
+ }
+
+ impl $name {
+ /// Register a callback to be execued on the main thread when this button is pressed.
+ pub fn set_callback(&mut self, callback: impl Fn() + Send + 'static) {
+ // Wrap it in an Arc<Mutex> to make it Sync.
+ // We need this because Dispatcher demands sync, but the AppDelegate trait does not
+ // impose that requirement on the callback.
+ let callback = Box::new(callback) as Box<dyn Fn() + Send>;
+ let callback = Arc::new(Mutex::new(callback));
+ self.button.set_action(move || {
+ let callback = callback.clone();
+ let callback = Action::ButtonClick { callback };
+ cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(callback);
+ });
+ }
+ }
};
}