mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 04:37:34 +00:00
Help+LibManual: Move non-UI-specific manual handling to LibManual
This is a first step in deduplicating code within and across Help and man. Because LibManual also doesn't contain any DeprecatedString, some adjustments to Help's string handling is included, just to interoperate with LibManual better. Further work in this area mostly requires String APIs in LibGUI.
This commit is contained in:
parent
78353ec184
commit
ad6a55e1f0
18 changed files with 339 additions and 265 deletions
|
@ -8,7 +8,9 @@
|
|||
*/
|
||||
|
||||
#include "MainWidget.h"
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/URL.h>
|
||||
#include <Applications/Help/HelpWindowGML.h>
|
||||
#include <LibCore/ArgsParser.h>
|
||||
|
@ -30,6 +32,9 @@
|
|||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <LibManual/Node.h>
|
||||
#include <LibManual/PageNode.h>
|
||||
#include <LibManual/SectionNode.h>
|
||||
#include <LibMarkdown/Document.h>
|
||||
|
||||
namespace Help {
|
||||
|
@ -66,25 +71,25 @@ MainWidget::MainWidget()
|
|||
}
|
||||
auto& search_model = *static_cast<GUI::FilteringProxyModel*>(view_model);
|
||||
auto const& mapped_index = search_model.map(index);
|
||||
DeprecatedString path = m_manual_model->page_path(mapped_index);
|
||||
if (path.is_null()) {
|
||||
auto path = m_manual_model->page_path(mapped_index);
|
||||
if (!path.has_value()) {
|
||||
m_web_view->load_empty_document();
|
||||
return;
|
||||
}
|
||||
m_browse_view->selection().clear();
|
||||
m_browse_view->selection().add(mapped_index);
|
||||
m_history.push(path);
|
||||
open_page(path);
|
||||
m_history.push(path.value());
|
||||
open_page(path.value());
|
||||
};
|
||||
|
||||
m_browse_view = find_descendant_of_type_named<GUI::TreeView>("browse_view");
|
||||
m_browse_view->on_selection_change = [this] {
|
||||
DeprecatedString path = m_manual_model->page_path(m_browse_view->selection().first());
|
||||
if (path.is_null())
|
||||
auto path = m_manual_model->page_path(m_browse_view->selection().first());
|
||||
if (!path.has_value())
|
||||
return;
|
||||
|
||||
m_history.push(path);
|
||||
open_page(path);
|
||||
m_history.push(path.value());
|
||||
open_page(path.value());
|
||||
};
|
||||
m_browse_view->on_toggle = [this](GUI::ModelIndex const& index, bool open) {
|
||||
m_manual_model->update_section_node_on_toggle(index, open);
|
||||
|
@ -105,7 +110,7 @@ MainWidget::MainWidget()
|
|||
return;
|
||||
}
|
||||
m_history.push(path);
|
||||
open_page(path);
|
||||
open_page(MUST(String::from_utf8(path)));
|
||||
} else if (url.scheme() == "help") {
|
||||
if (url.host() == "man") {
|
||||
if (url.paths().size() != 2) {
|
||||
|
@ -138,23 +143,17 @@ MainWidget::MainWidget()
|
|||
|
||||
m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) {
|
||||
m_history.go_back();
|
||||
open_page(m_history.current());
|
||||
open_page(MUST(String::from_deprecated_string(m_history.current())));
|
||||
});
|
||||
|
||||
m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) {
|
||||
m_history.go_forward();
|
||||
open_page(m_history.current());
|
||||
open_page(MUST(String::from_deprecated_string(m_history.current())));
|
||||
});
|
||||
|
||||
m_go_back_action->set_enabled(false);
|
||||
m_go_forward_action->set_enabled(false);
|
||||
|
||||
m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) {
|
||||
DeprecatedString path = "/usr/share/man/man7/Help-index.md";
|
||||
m_history.push(path);
|
||||
open_page(path);
|
||||
});
|
||||
|
||||
m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
|
||||
auto selected_text = m_web_view->selected_text();
|
||||
if (!selected_text.is_empty())
|
||||
|
@ -174,37 +173,27 @@ MainWidget::MainWidget()
|
|||
};
|
||||
}
|
||||
|
||||
void MainWidget::set_start_page(StringView start_page, u32 section)
|
||||
ErrorOr<void> MainWidget::set_start_page(StringView start_page, u32 section)
|
||||
{
|
||||
bool set_start_page = false;
|
||||
if (!start_page.is_null()) {
|
||||
if (section != 0) {
|
||||
// > Help [section] [name]
|
||||
DeprecatedString path = DeprecatedString::formatted("/usr/share/man/man{}/{}.md", section, start_page);
|
||||
String path = TRY(String::formatted("/usr/share/man/man{}/{}.md", section, start_page));
|
||||
m_history.push(path);
|
||||
open_page(path);
|
||||
set_start_page = true;
|
||||
} else if (URL url = URL::create_with_url_or_path(start_page); url.is_valid() && url.path().ends_with(".md"sv)) {
|
||||
// > Help [/path/to/documentation/file.md]
|
||||
m_history.push(url.path());
|
||||
open_page(url.path());
|
||||
open_page(TRY(String::from_deprecated_string(url.path())));
|
||||
set_start_page = true;
|
||||
} else {
|
||||
// > Help [query]
|
||||
|
||||
// First, see if we can find the page by name
|
||||
char const* sections[] = {
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
"8"
|
||||
};
|
||||
for (auto s : sections) {
|
||||
DeprecatedString path = DeprecatedString::formatted("/usr/share/man/man{}/{}.md", s, start_page);
|
||||
for (auto s : Manual::section_numbers) {
|
||||
String path = TRY(String::formatted("/usr/share/man/man{}/{}.md", s, start_page));
|
||||
if (Core::File::exists(path)) {
|
||||
m_history.push(path);
|
||||
open_page(path);
|
||||
|
@ -225,10 +214,17 @@ void MainWidget::set_start_page(StringView start_page, u32 section)
|
|||
}
|
||||
if (!set_start_page)
|
||||
m_go_home_action->activate();
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<void> MainWidget::initialize_fallibles(GUI::Window& window)
|
||||
{
|
||||
static String help_index_path = TRY(String::from_utf8("/usr/share/man/man7/Help-index.md"sv));
|
||||
m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) {
|
||||
m_history.push(help_index_path);
|
||||
open_page(help_index_path);
|
||||
});
|
||||
|
||||
(void)TRY(m_toolbar->try_add_action(*m_go_back_action));
|
||||
(void)TRY(m_toolbar->try_add_action(*m_go_forward_action));
|
||||
(void)TRY(m_toolbar->try_add_action(*m_go_home_action));
|
||||
|
@ -244,10 +240,10 @@ ErrorOr<void> MainWidget::initialize_fallibles(GUI::Window& window)
|
|||
TRY(go_menu->try_add_action(*m_go_home_action));
|
||||
|
||||
auto help_menu = TRY(window.try_add_menu("&Help"));
|
||||
static String help_page_path = TRY(String::from_utf8("/usr/share/man/man1/Help.md"sv));
|
||||
TRY(help_menu->try_add_action(GUI::CommonActions::make_command_palette_action(&window)));
|
||||
TRY(help_menu->try_add_action(GUI::Action::create("&Contents", { Key_F1 }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png"sv)), [&](auto&) {
|
||||
DeprecatedString path = "/usr/share/man/man1/Help.md";
|
||||
open_page(path);
|
||||
open_page(help_page_path);
|
||||
})));
|
||||
TRY(help_menu->try_add_action(GUI::CommonActions::make_about_action("Help", TRY(GUI::Icon::try_create_default_icon("app-help"sv)), &window)));
|
||||
|
||||
|
@ -282,8 +278,12 @@ void MainWidget::open_url(URL const& url)
|
|||
if (browse_view_index.has_value()) {
|
||||
m_browse_view->expand_tree(browse_view_index.value().parent());
|
||||
|
||||
DeprecatedString page_and_section = m_manual_model->page_and_section(browse_view_index.value());
|
||||
window()->set_title(DeprecatedString::formatted("{} - Help", page_and_section));
|
||||
auto page_and_section = m_manual_model->page_and_section(browse_view_index.value());
|
||||
if (!page_and_section.has_value())
|
||||
return;
|
||||
auto title = String::formatted("{} - Help", page_and_section.value());
|
||||
if (!title.is_error())
|
||||
window()->set_title(title.release_value().to_deprecated_string());
|
||||
} else {
|
||||
window()->set_title("Help");
|
||||
}
|
||||
|
@ -297,17 +297,17 @@ void MainWidget::open_external(URL const& url)
|
|||
GUI::MessageBox::show(window(), DeprecatedString::formatted("The link to '{}' could not be opened.", url), "Failed to open link"sv, GUI::MessageBox::Type::Error);
|
||||
}
|
||||
|
||||
void MainWidget::open_page(DeprecatedString const& path)
|
||||
void MainWidget::open_page(Optional<String> const& path)
|
||||
{
|
||||
m_go_back_action->set_enabled(m_history.can_go_back());
|
||||
m_go_forward_action->set_enabled(m_history.can_go_forward());
|
||||
|
||||
if (path.is_null()) {
|
||||
if (!path.has_value()) {
|
||||
window()->set_title("Help");
|
||||
m_web_view->load_empty_document();
|
||||
return;
|
||||
}
|
||||
open_url(URL::create_with_url_or_path(path));
|
||||
open_url(URL::create_with_url_or_path(path.value().to_deprecated_string()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue