system-manager.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. SystemManager::SystemManager(View* view) : PanelList(view, Size{~0, ~0}) {
  2. setCollapsible().setVisible(false);
  3. listView.onActivate([&] { onActivate(); });
  4. listView.onChange([&] { onChange(); });
  5. listView.onContext([&] { onContext(); });
  6. listCreate.setText("Create").setIcon(Icon::Action::Add).onActivate([&] { doCreate(); });
  7. listLaunch.setText("Launch").setIcon(Icon::Media::Play).onActivate([&] { onActivate(); });
  8. listRename.setText("Rename").setIcon(Icon::Application::TextEditor).onActivate([&] { doRename(); });
  9. listRemove.setText("Remove").setIcon(Icon::Action::Remove).onActivate([&] { doRemove(); });
  10. }
  11. auto SystemManager::show() -> void {
  12. listView.selectNone().doChange();
  13. refresh();
  14. setVisible(true);
  15. }
  16. auto SystemManager::hide() -> void {
  17. setVisible(false);
  18. actionMenu.rename.setEnabled(false);
  19. actionMenu.remove.setEnabled(false);
  20. }
  21. auto SystemManager::refresh() -> void {
  22. listView.reset();
  23. listView.doChange();
  24. ListViewItem item{&listView};
  25. item.setIcon(Icon::Action::Add);
  26. item.setText("Create New System");
  27. auto location = Path::data;
  28. for(auto& name : directory::folders(location)) {
  29. auto document = BML::unserialize(file::read({location, name, "/", "manifest.bml"}));
  30. if(!document) continue;
  31. auto system = document["system"].text();
  32. ListViewItem item{&listView};
  33. item.setAttribute("location", {location, name});
  34. item.setAttribute("path", location);
  35. item.setAttribute("name", name.trimRight("/", 1L));
  36. item.setAttribute("system", system);
  37. item.setIcon(Icon::Place::Server);
  38. item.setText(name);
  39. //hignlight any systems found that have not been compiled into this binary
  40. if(!interfaces.find([&](auto interface) { return interface->name() == system; })) {
  41. item.setForegroundColor({240, 80, 80});
  42. //only show the missing systems in advanced mode
  43. if(!settings.interface.advancedMode) item.remove();
  44. }
  45. }
  46. }
  47. auto SystemManager::onActivate() -> void {
  48. if(auto item = listView.selected()) {
  49. if(!item.attribute("path")) return;
  50. if(auto system = item.attribute("system")) {
  51. if(auto index = interfaces.find([&](auto interface) { return interface->name() == system; })) {
  52. emulator.create(interfaces[*index], {item.attribute("path"), item.attribute("name"), "/"});
  53. }
  54. }
  55. }
  56. }
  57. auto SystemManager::onChange() -> void {
  58. auto item = listView.selected();
  59. if(item && !item.attribute("location")) {
  60. program.setPanelItem(systemCreation);
  61. actionMenu.create.setEnabled(true);
  62. actionMenu.launch.setEnabled(false);
  63. actionMenu.rename.setEnabled(false);
  64. actionMenu.remove.setEnabled(false);
  65. } else if(item && item.attribute("location")) {
  66. systemOverview.refresh();
  67. program.setPanelItem(systemOverview);
  68. actionMenu.create.setEnabled(false);
  69. actionMenu.launch.setEnabled(true);
  70. actionMenu.rename.setEnabled(true);
  71. actionMenu.remove.setEnabled(true);
  72. } else {
  73. program.setPanelItem(home);
  74. actionMenu.create.setEnabled(true);
  75. actionMenu.launch.setEnabled(false);
  76. actionMenu.rename.setEnabled(false);
  77. actionMenu.remove.setEnabled(false);
  78. }
  79. }
  80. auto SystemManager::onContext() -> void {
  81. if(auto item = listView.selected()) {
  82. if(!item.attribute("path")) return; //no context menu for "Create New System" option
  83. listCreate.setVisible(false);
  84. listLaunch.setVisible(true);
  85. listRename.setVisible(true);
  86. listRemove.setVisible(true);
  87. listMenu.setVisible();
  88. } else {
  89. listCreate.setVisible(true);
  90. listLaunch.setVisible(false);
  91. listRename.setVisible(false);
  92. listRemove.setVisible(false);
  93. listMenu.setVisible();
  94. }
  95. }
  96. auto SystemManager::doCreate() -> void {
  97. //clear the current selection to indicate that creation isn't related to the currently selected system item
  98. listView.selectNone();
  99. program.setPanelItem(systemCreation);
  100. }
  101. auto SystemManager::doRename() -> void {
  102. if(auto item = listView.selected()) {
  103. if(!item.attribute("path")) return;
  104. auto path = item.attribute("path");
  105. auto from = item.attribute("name");
  106. if(auto name = NameDialog()
  107. .setAlignment(program)
  108. .rename(from)
  109. ) {
  110. if(name == from) return;
  111. if(directory::exists({path, name})) return (void)MessageDialog()
  112. .setTitle("Error")
  113. .setText("A directory with this name already exists.\n"
  114. "Please choose a unique name.")
  115. .setAlignment(program)
  116. .error();
  117. if(!directory::rename({path, from}, {path, name})) return (void)MessageDialog()
  118. .setTitle("Error")
  119. .setText("Failed to rename directory.")
  120. .setAlignment(program)
  121. .error();
  122. refresh();
  123. }
  124. }
  125. }
  126. auto SystemManager::doRemove() -> void {
  127. if(auto item = listView.selected()) {
  128. if(!item.attribute("path")) return;
  129. if(MessageDialog()
  130. .setTitle({"Delete ", item.attribute("name")})
  131. .setText("Are you sure you want to permanently delete the selected system?\n"
  132. "All of its contents will be permanently lost!")
  133. .setAlignment(program)
  134. .question() != "Yes") return;
  135. auto location = string{item.attribute("path"), item.attribute("name"), "/"};
  136. if(!directory::remove(location)) return (void)MessageDialog()
  137. .setTitle("Error")
  138. .setText("Failed to delete directory.")
  139. .setAlignment(program)
  140. .error();
  141. program.setPanelItem(home); //hide the system overview for the now-deleted system
  142. refresh();
  143. }
  144. }