| 1164 | } |
| 1165 | |
| 1166 | void App::updateRecentFiles() { |
| 1167 | UINode* node = nullptr; |
| 1168 | if ( mSettings && mSettings->getSettingsMenu() && |
| 1169 | ( node = mSettings->getSettingsMenu()->getItemId( "menu-recent-files" ) ) ) { |
| 1170 | UIMenuSubMenu* uiMenuSubMenu = static_cast<UIMenuSubMenu*>( node ); |
| 1171 | UIMenu* menu = uiMenuSubMenu->getSubMenu(); |
| 1172 | uiMenuSubMenu->setEnabled( !mRecentFiles.empty() ); |
| 1173 | menu->removeAll(); |
| 1174 | menu->removeEventsOfType( Event::OnItemClicked ); |
| 1175 | if ( mRecentFiles.empty() ) |
| 1176 | return; |
| 1177 | menu->add( i18n( "reopen_closed_document", "Reopen Closed Document" ), nullptr, |
| 1178 | getKeybind( "reopen-closed-tab" ) ) |
| 1179 | ->setId( "reopen-closed-tab" ) |
| 1180 | ->setEnabled( !mRecentClosedFiles.empty() ); |
| 1181 | menu->addSeparator(); |
| 1182 | for ( const auto& file : mRecentFiles ) { |
| 1183 | if ( ( FileSystem::fileExists( file ) && !FileSystem::isDirectory( file ) ) || |
| 1184 | String::startsWith( file, "https://" ) || String::startsWith( file, "http://" ) ) |
| 1185 | menu->add( file ); |
| 1186 | } |
| 1187 | menu->addSeparator(); |
| 1188 | menu->add( i18n( "clear_menu", "Clear Menu" ) )->setId( "clear-menu" ); |
| 1189 | menu->on( Event::OnItemClicked, [this]( const Event* event ) { |
| 1190 | if ( !event->getNode()->isType( UI_TYPE_MENUITEM ) ) |
| 1191 | return; |
| 1192 | const std::string& id = event->getNode()->asType<UIMenuItem>()->getId(); |
| 1193 | if ( id == "reopen-closed-tab" ) { |
| 1194 | reopenClosedTab(); |
| 1195 | } else if ( id == "clear-menu" ) { |
| 1196 | mRecentFiles.clear(); |
| 1197 | updateRecentFiles(); |
| 1198 | updateRecentButtons(); |
| 1199 | } else { |
| 1200 | const String& txt = event->getNode()->asType<UIMenuItem>()->getText(); |
| 1201 | std::string path( txt.toUtf8() ); |
| 1202 | if ( ( FileSystem::fileExists( path ) && !FileSystem::isDirectory( path ) ) || |
| 1203 | String::startsWith( path, "https://" ) || |
| 1204 | String::startsWith( path, "http://" ) ) { |
| 1205 | loadFileFromPathOrFocus( path ); |
| 1206 | } else { |
| 1207 | auto msgBox = UIMessageBox::New( |
| 1208 | UIMessageBox::YES_NO, |
| 1209 | i18n( "file_does_not_exists_anymore_recreate", |
| 1210 | "File does not exists anymore.\nDo you want to recreate it?" ) ); |
| 1211 | msgBox->setTitle( i18n( "file_not_found", "File not found" ) ); |
| 1212 | msgBox->on( Event::OnConfirm, [path, this]( auto ) { |
| 1213 | FileSystem::fileWrite( path, "" ); |
| 1214 | loadFileFromPathOrFocus( path ); |
| 1215 | } ); |
| 1216 | msgBox->center(); |
| 1217 | msgBox->showWhenReady(); |
| 1218 | updateRecentFiles(); |
| 1219 | } |
| 1220 | } |
| 1221 | } ); |
| 1222 | } |
| 1223 | } |
no test coverage detected