Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 5 | #ifndef UI_ANDROID_MODAL_DIALOG_WRAPPER_H_ |
| 6 | #define UI_ANDROID_MODAL_DIALOG_WRAPPER_H_ |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "base/android/jni_string.h" |
| 11 | #include "base/android/scoped_java_ref.h" |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
| 13 | #include "base/memory/raw_ptr.h" |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 14 | #include "ui/android/ui_android_export.h" |
| 15 | #include "ui/base/models/dialog_model_host.h" |
| 16 | |
| 17 | namespace ui { |
| 18 | class DialogModel; |
| 19 | class WindowAndroid; |
| 20 | } // namespace ui |
| 21 | |
| 22 | namespace ui { |
| 23 | |
| 24 | // Allows dialogs defined by ui::DialogModel() to be shown in android. |
| 25 | // This makes a best effort to map between ui::DialogModel() and Java |
| 26 | // PropertyModel. |
| 27 | // |
| 28 | // Maps title, an optional single paragraph, and ok and cancel buttons. |
| 29 | // Default labels for IDS_APP_OK and IDS_APP_CANCEL will be used for buttons |
| 30 | // if labels are not specified. |
| 31 | // Replacements (if any) are performed in paragraph text, but any emphasis is |
| 32 | // not included since it is not supported in android dialogs. |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 33 | class UI_ANDROID_EXPORT ModalDialogWrapper : public DialogModelHost { |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 34 | public: |
Charles Hager | de93b2e5 | 2025-01-29 23:20:18 | [diff] [blame] | 35 | // Mirrors Java's `ModalDialogProperties#ButtonStyles`. |
| 36 | // TODO(crbug.com/392977703): IntDef that enum in C++. |
| 37 | enum class ModalDialogButtonStyles { |
| 38 | kPrimaryOutlineNegativeOutline = 0, |
| 39 | kPrimaryFilledNegativeOutline = 1, |
| 40 | kPrimaryOutlineNegativeFilled = 2, |
| 41 | kPrimaryFilledNoNegative = 3, |
| 42 | }; |
| 43 | |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 44 | static void ShowTabModal(std::unique_ptr<ui::DialogModel> dialog_model, |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 45 | ui::WindowAndroid* window); |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 46 | |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 47 | static ModalDialogWrapper* GetDialogForTesting(); |
| 48 | |
| 49 | ModalDialogWrapper(const ModalDialogWrapper&) = delete; |
| 50 | ModalDialogWrapper& operator=(const ModalDialogWrapper&) = delete; |
| 51 | virtual ~ModalDialogWrapper(); |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 52 | |
| 53 | // JNI methods. |
| 54 | void PositiveButtonClicked(JNIEnv* env); |
| 55 | void NegativeButtonClicked(JNIEnv* env); |
| 56 | void Dismissed(JNIEnv* env); |
| 57 | void Destroy(JNIEnv* env); |
| 58 | |
| 59 | private: |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 60 | FRIEND_TEST_ALL_PREFIXES(ModalDialogWrapperTest, CloseDialogFromNative); |
| 61 | |
| 62 | ModalDialogWrapper(std::unique_ptr<ui::DialogModel> dialog_model, |
| 63 | ui::WindowAndroid* window_android); |
| 64 | |
| 65 | // `DialogModelHost`: |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 66 | void Close() override; |
| 67 | void OnDialogButtonChanged() override; |
| 68 | |
| 69 | // Build java PropertyModel from ui::DialogModel. |
| 70 | void BuildPropertyModel(); |
| 71 | |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 72 | const std::unique_ptr<ui::DialogModel> dialog_model_; |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 73 | |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 74 | const raw_ptr<WindowAndroid> window_android_; |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 75 | |
Joel Hockey | 18f767a | 2024-08-20 03:23:44 | [diff] [blame] | 76 | base::android::ScopedJavaGlobalRef<jobject> java_obj_; |
| 77 | }; |
| 78 | |
| 79 | } // namespace ui |
| 80 | |
William Liu | 7e940dd | 2024-09-12 13:16:44 | [diff] [blame] | 81 | #endif // UI_ANDROID_MODAL_DIALOG_WRAPPER_H_ |