input_resource.vala 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public class InputResource : InputField, Gtk.Box
  8. {
  9. // Data
  10. public string _type;
  11. // Widgets
  12. public InputString _name;
  13. public Gtk.Button _selector;
  14. public Gtk.Button _revealer;
  15. public ProjectStore _project_store;
  16. public ResourceChooser _chooser;
  17. public Gtk.Dialog _dialog;
  18. public Gtk.EventControllerKey _dialog_controller_key;
  19. public void set_inconsistent(bool inconsistent)
  20. {
  21. }
  22. public bool is_inconsistent()
  23. {
  24. return false;
  25. }
  26. public GLib.Value union_value()
  27. {
  28. return this.value;
  29. }
  30. public void set_union_value(GLib.Value v)
  31. {
  32. this.value = (string)v;
  33. }
  34. public string value
  35. {
  36. get
  37. {
  38. return _name.value;
  39. }
  40. set
  41. {
  42. _name.value = value;
  43. }
  44. }
  45. public InputResource(ProjectStore store, string type)
  46. {
  47. Object(orientation: Gtk.Orientation.HORIZONTAL, spacing: 0);
  48. // Data
  49. _type = type;
  50. // Widgets
  51. _name = new InputString();
  52. _name.set_editable(false);
  53. _name.hexpand = true;
  54. _name.value_changed.connect(on_name_value_changed);
  55. this.pack_start(_name, true, true);
  56. _revealer = new Gtk.Button.from_icon_name("go-jump-symbolic");
  57. _revealer.clicked.connect(on_revealer_clicked);
  58. this.pack_end(_revealer, false);
  59. _selector = new Gtk.Button.from_icon_name("document-open-symbolic");
  60. _selector.clicked.connect(on_selector_clicked);
  61. this.pack_end(_selector, false);
  62. _project_store = store;
  63. _chooser = new ResourceChooser(null, _project_store);
  64. _chooser.set_type_filter(type_filter);
  65. _project_store._project.file_added.connect(on_file_added_or_changed);
  66. _project_store._project.file_changed.connect(on_file_added_or_changed);
  67. _project_store._project.file_removed.connect(on_file_removed);
  68. }
  69. ~InputResource()
  70. {
  71. // Prevents a crash when the parent window gets destroyed.
  72. _chooser.set_type_filter((type, name) => { return false; });
  73. }
  74. private void on_selector_clicked()
  75. {
  76. if (_dialog == null) {
  77. _dialog = new Gtk.Dialog.with_buttons("Select a %s".printf(_type)
  78. , (Gtk.Window)this.get_toplevel()
  79. , Gtk.DialogFlags.MODAL
  80. , null
  81. );
  82. _dialog.delete_event.connect(_dialog.hide_on_delete);
  83. _chooser.resource_selected.connect(() => {
  84. _name.value = _chooser._name;
  85. _dialog.hide();
  86. });
  87. _dialog_controller_key = new Gtk.EventControllerKey(_dialog);
  88. _dialog_controller_key.set_propagation_phase(Gtk.PropagationPhase.CAPTURE);
  89. _dialog_controller_key.key_pressed.connect((keyval) => {
  90. if (keyval == Gdk.Key.Escape) {
  91. _dialog.hide();
  92. return Gdk.EVENT_STOP;
  93. }
  94. return Gdk.EVENT_PROPAGATE;
  95. });
  96. _dialog.skip_taskbar_hint = true;
  97. _dialog.get_content_area().pack_start(_chooser, true, true, 0);
  98. }
  99. _dialog.show_all();
  100. _dialog.present();
  101. }
  102. private void on_revealer_clicked()
  103. {
  104. var tuple = new GLib.Variant.tuple({_type, _name.value});
  105. GLib.Application.get_default().activate_action("reveal-resource", tuple);
  106. }
  107. private bool type_filter(string type, string name)
  108. {
  109. return _type == type;
  110. }
  111. private void on_name_value_changed()
  112. {
  113. value_changed(this);
  114. }
  115. private void on_file_added_or_changed(string type, string name, uint64 size, uint64 mtime)
  116. {
  117. if (type == _type && name == _name.value)
  118. value_changed(this);
  119. }
  120. private void on_file_removed(string type, string name)
  121. {
  122. if (type == _type && name == _name.value)
  123. value_changed(this);
  124. }
  125. }
  126. } /* namespace Crown */