confirmDialog.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import Clutter from 'gi://Clutter';
  2. import St from 'gi://St';
  3. import GObject from 'gi://GObject';
  4. import * as ModalDialog from 'resource:///org/gnome/shell/ui/modalDialog.js';
  5. let _openDialog;
  6. export function openConfirmDialog(
  7. title,
  8. message,
  9. sub_message,
  10. ok_label,
  11. cancel_label,
  12. callback,
  13. ) {
  14. if (!_openDialog) {
  15. _openDialog = new ConfirmDialog(
  16. title,
  17. message + '\n' + sub_message,
  18. ok_label,
  19. cancel_label,
  20. callback,
  21. ).open();
  22. }
  23. }
  24. const ConfirmDialog = GObject.registerClass(
  25. class ConfirmDialog extends ModalDialog.ModalDialog {
  26. _init(title, desc, ok_label, cancel_label, callback) {
  27. super._init();
  28. let main_box = new St.BoxLayout({
  29. vertical: false,
  30. });
  31. this.contentLayout.add_child(main_box);
  32. let message_box = new St.BoxLayout({
  33. vertical: true,
  34. });
  35. main_box.add_child(message_box);
  36. let subject_label = new St.Label({
  37. style: 'font-weight: bold',
  38. x_align: Clutter.ActorAlign.CENTER,
  39. text: title,
  40. });
  41. message_box.add_child(subject_label);
  42. let desc_label = new St.Label({
  43. style: 'padding-top: 12px',
  44. x_align: Clutter.ActorAlign.CENTER,
  45. text: desc,
  46. });
  47. message_box.add_child(desc_label);
  48. this.setButtons([
  49. {
  50. label: cancel_label,
  51. action: () => {
  52. this.close();
  53. _openDialog = null;
  54. },
  55. key: Clutter.Escape,
  56. },
  57. {
  58. label: ok_label,
  59. action: () => {
  60. this.close();
  61. callback();
  62. _openDialog = null;
  63. },
  64. },
  65. ]);
  66. }
  67. },
  68. );