Skip to content

Commit

Permalink
LibGfx+Userland: Make Gfx::SystemTheme propagate errors
Browse files Browse the repository at this point in the history
This patch introduces error propagation to Gfx::SystemTheme to remove
instances of release_value_but_fixme_should_propagate_errors().

Userland applications that have been affected by this change have been
updated to utilise this propagation and as a result 4 such instances of
the aforementioned method have been removed.
  • Loading branch information
CygnixProto authored and linusg committed Dec 14, 2022
1 parent bdd9bc1 commit 806a55e
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 39 deletions.
6 changes: 4 additions & 2 deletions Userland/Applications/DisplaySettings/ThemePreviewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ ThemePreviewWidget::ThemePreviewWidget(Gfx::Palette const& palette)
set_fixed_size(304, 201);
}

void ThemePreviewWidget::set_theme(DeprecatedString path)
ErrorOr<void> ThemePreviewWidget::set_theme(DeprecatedString path)
{
set_theme_from_file(*Core::File::open(path, Core::OpenMode::ReadOnly).release_value_but_fixme_should_propagate_errors());
auto config_file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
TRY(set_theme_from_file(config_file));
return {};
}

void ThemePreviewWidget::paint_preview(GUI::PaintEvent&)
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/DisplaySettings/ThemePreviewWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ThemePreviewWidget final : public GUI::AbstractThemePreview {
C_OBJECT(ThemePreviewWidget);

public:
void set_theme(DeprecatedString path);
ErrorOr<void> set_theme(DeprecatedString path);

private:
explicit ThemePreviewWidget(Gfx::Palette const& palette);
Expand Down
13 changes: 10 additions & 3 deletions Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <LibGUI/Application.h>
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/ItemListModel.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Process.h>

namespace DisplaySettings {
Expand All @@ -21,7 +22,7 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed)
: m_background_settings_changed { background_settings_changed }
{
load_from_gml(themes_settings_gml);
m_themes = Gfx::list_installed_system_themes();
m_themes = MUST(Gfx::list_installed_system_themes());

size_t current_theme_index;
auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();
Expand All @@ -39,7 +40,10 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed)
m_themes_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_theme_names));
m_themes_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
m_selected_theme = &m_themes.at(index.row());
m_theme_preview->set_theme(m_selected_theme->path);
auto set_theme_result = m_theme_preview->set_theme(m_selected_theme->path);
if (set_theme_result.is_error()) {
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("There was an error generating the theme preview: {}", set_theme_result.error()));
}
set_modified(true);
};
m_themes_combo->set_selected_index(current_theme_index, GUI::AllowCallback::No);
Expand All @@ -66,7 +70,10 @@ ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed)
if (current_theme_name == theme_meta.name) {
m_themes_combo->set_selected_index(index, GUI::AllowCallback::No);
m_selected_theme = &m_themes.at(index);
m_theme_preview->set_theme(m_selected_theme->path);
auto set_theme_result = m_theme_preview->set_theme(m_selected_theme->path);
if (set_theme_result.is_error()) {
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("There was an error setting the new theme: {}", set_theme_result.error()));
}
}
++index;
}
Expand Down
13 changes: 9 additions & 4 deletions Userland/Applications/ThemeEditor/MainWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
auto response = FileSystemAccessClient::Client::the().try_open_file(&window, "Select theme file", "/res/themes"sv);
if (response.is_error())
return;
load_from_file(*response.value());
auto load_from_file_result = load_from_file(*response.value());
if (load_from_file_result.is_error()) {
GUI::MessageBox::show_error(&window, DeprecatedString::formatted("Can't open file named {}: {}", response.value()->filename(), load_from_file_result.error()));
return;
}
})));

m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
Expand Down Expand Up @@ -557,10 +561,10 @@ void MainWidget::show_path_picker_dialog(StringView property_display_name, GUI::
path_input.set_text(*result);
}

void MainWidget::load_from_file(Core::File& file)
ErrorOr<void> MainWidget::load_from_file(Core::File& file)
{
auto config_file = Core::ConfigFile::open(file.filename(), file.leak_fd()).release_value_but_fixme_should_propagate_errors();
auto theme = Gfx::load_system_theme(config_file);
auto config_file = TRY(Core::ConfigFile::open(file.filename(), file.leak_fd()));
auto theme = TRY(Gfx::load_system_theme(config_file));
VERIFY(theme.is_valid());

auto new_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
Expand Down Expand Up @@ -599,6 +603,7 @@ void MainWidget::load_from_file(Core::File& file)

m_last_modified_time = Time::now_monotonic();
window()->set_modified(false);
return {};
}

}
2 changes: 1 addition & 1 deletion Userland/Applications/ThemeEditor/MainWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MainWidget final : public GUI::Widget {
ErrorOr<void> initialize_menubar(GUI::Window&);
GUI::Window::CloseRequestDecision request_close();
void update_title();
void load_from_file(Core::File&);
ErrorOr<void> load_from_file(Core::File&);

private:
MainWidget();
Expand Down
5 changes: 4 additions & 1 deletion Userland/Applications/ThemeEditor/PreviewWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ void PreviewWidget::drop_event(GUI::DropEvent& event)
auto response = FileSystemAccessClient::Client::the().try_request_file(window(), urls.first().path(), Core::OpenMode::ReadOnly);
if (response.is_error())
return;
set_theme_from_file(*response.value());

auto set_theme_from_file_result = set_theme_from_file(response.release_value());
if (set_theme_from_file_result.is_error())
GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Setting theme from file has failed: {}", set_theme_from_file_result.error()));
}
}

Expand Down
7 changes: 5 additions & 2 deletions Userland/Applications/ThemeEditor/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto response = FileSystemAccessClient::Client::the().try_request_file_read_only_approved(window, path.value());
if (response.is_error())
GUI::MessageBox::show_error(window, DeprecatedString::formatted("Opening \"{}\" failed: {}", path.value(), response.error()));
else
main_widget->load_from_file(response.release_value());
else {
auto load_from_file_result = main_widget->load_from_file(response.release_value());
if (load_from_file_result.is_error())
GUI::MessageBox::show_error(window, DeprecatedString::formatted("Loading theme from file has failed: {}", load_from_file_result.error()));
}
});
}

Expand Down
8 changes: 4 additions & 4 deletions Userland/Libraries/LibGUI/AbstractThemePreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ void AbstractThemePreview::set_theme(Core::AnonymousBuffer const& theme_buffer)
set_preview_palette(m_preview_palette);
}

void AbstractThemePreview::set_theme_from_file(Core::File& file)
ErrorOr<void> AbstractThemePreview::set_theme_from_file(Core::File& file)
{
auto config_file = Core::ConfigFile::open(file.filename(), file.leak_fd()).release_value_but_fixme_should_propagate_errors();
auto theme = Gfx::load_system_theme(config_file);
VERIFY(theme.is_valid());
auto config_file = TRY(Core::ConfigFile::open(file.filename(), file.leak_fd()));
auto theme = TRY(Gfx::load_system_theme(config_file));

m_preview_palette = Gfx::Palette(Gfx::PaletteImpl::create_with_anonymous_buffer(theme));
set_preview_palette(m_preview_palette);
if (on_theme_load_from_file)
on_theme_load_from_file(file.filename());
return {};
}

void AbstractThemePreview::paint_window(StringView title, Gfx::IntRect const& rect, Gfx::WindowTheme::WindowState state, Gfx::Bitmap const& icon, int button_count)
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibGUI/AbstractThemePreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AbstractThemePreview : public GUI::Frame {

Gfx::Palette const& preview_palette() const { return m_preview_palette; }
void set_preview_palette(Gfx::Palette const&);
void set_theme_from_file(Core::File&);
ErrorOr<void> set_theme_from_file(Core::File&);
void set_theme(Core::AnonymousBuffer const&);

void paint_window(StringView title, Gfx::IntRect const& rect, Gfx::WindowTheme::WindowState, Gfx::Bitmap const& icon, int button_count = 3);
Expand Down
13 changes: 7 additions & 6 deletions Userland/Libraries/LibGfx/SystemTheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void set_system_theme(Core::AnonymousBuffer buffer)
theme_page = theme_buffer.data<SystemTheme>();
}

Core::AnonymousBuffer load_system_theme(Core::ConfigFile const& file)
ErrorOr<Core::AnonymousBuffer> load_system_theme(Core::ConfigFile const& file)
{
auto buffer = Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme)).release_value();
auto buffer = TRY(Core::AnonymousBuffer::create_with_size(sizeof(SystemTheme)));

auto* data = buffer.data<SystemTheme>();

Expand Down Expand Up @@ -148,19 +148,20 @@ Core::AnonymousBuffer load_system_theme(Core::ConfigFile const& file)
return buffer;
}

Core::AnonymousBuffer load_system_theme(DeprecatedString const& path)
ErrorOr<Core::AnonymousBuffer> load_system_theme(DeprecatedString const& path)
{
return load_system_theme(Core::ConfigFile::open(path).release_value_but_fixme_should_propagate_errors());
auto config_file = TRY(Core::ConfigFile::open(path));
return TRY(load_system_theme(config_file));
}

Vector<SystemThemeMetaData> list_installed_system_themes()
ErrorOr<Vector<SystemThemeMetaData>> list_installed_system_themes()
{
Vector<SystemThemeMetaData> system_themes;
Core::DirIterator dt("/res/themes", Core::DirIterator::SkipDots);
while (dt.has_next()) {
auto theme_name = dt.next_path();
auto theme_path = DeprecatedString::formatted("/res/themes/{}", theme_name);
system_themes.append({ LexicalPath::title(theme_name), theme_path });
TRY(system_themes.try_append({ LexicalPath::title(theme_name), theme_path }));
}
quick_sort(system_themes, [](auto& a, auto& b) { return a.name < b.name; });
return system_themes;
Expand Down
6 changes: 3 additions & 3 deletions Userland/Libraries/LibGfx/SystemTheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,15 @@ struct SystemTheme {

Core::AnonymousBuffer& current_system_theme_buffer();
void set_system_theme(Core::AnonymousBuffer);
Core::AnonymousBuffer load_system_theme(Core::ConfigFile const&);
Core::AnonymousBuffer load_system_theme(DeprecatedString const& path);
ErrorOr<Core::AnonymousBuffer> load_system_theme(Core::ConfigFile const&);
ErrorOr<Core::AnonymousBuffer> load_system_theme(DeprecatedString const& path);

struct SystemThemeMetaData {
DeprecatedString name;
DeprecatedString path;
};

Vector<SystemThemeMetaData> list_installed_system_themes();
ErrorOr<Vector<SystemThemeMetaData>> list_installed_system_themes();

}

Expand Down
2 changes: 1 addition & 1 deletion Userland/Services/Taskbar/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ ErrorOr<NonnullRefPtr<GUI::Menu>> build_system_menu(GUI::Window& window)
g_themes_menu = &system_menu->add_submenu("&Themes");
g_themes_menu->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/themes.png"sv).release_value_but_fixme_should_propagate_errors());

g_themes = Gfx::list_installed_system_themes();
g_themes = TRY(Gfx::list_installed_system_themes());
auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme();

{
Expand Down
10 changes: 6 additions & 4 deletions Userland/Services/WindowServer/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,9 +2064,12 @@ void WindowManager::invalidate_after_theme_or_font_change()

bool WindowManager::update_theme(DeprecatedString theme_path, DeprecatedString theme_name, bool keep_desktop_background)
{
auto new_theme = Gfx::load_system_theme(theme_path);
if (!new_theme.is_valid())
auto error_or_new_theme = Gfx::load_system_theme(theme_path);
if (error_or_new_theme.is_error()) {
dbgln("WindowManager: Updating theme failed, error {}", error_or_new_theme.error());
return false;
}
auto new_theme = error_or_new_theme.release_value();
m_theme_overridden = false;
Gfx::set_system_theme(new_theme);
m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(new_theme);
Expand Down Expand Up @@ -2101,8 +2104,7 @@ void WindowManager::clear_theme_override()
{
m_theme_overridden = false;
auto previous_theme_name = m_config->read_entry("Theme", "Name");
auto previous_theme = Gfx::load_system_theme(DeprecatedString::formatted("/res/themes/{}.ini", previous_theme_name));
VERIFY(previous_theme.is_valid());
auto previous_theme = MUST(Gfx::load_system_theme(DeprecatedString::formatted("/res/themes/{}.ini", previous_theme_name)));
Gfx::set_system_theme(previous_theme);
m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(previous_theme);
invalidate_after_theme_or_font_change();
Expand Down
3 changes: 1 addition & 2 deletions Userland/Services/WindowServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
auto wm_config = TRY(Core::ConfigFile::open("/etc/WindowServer.ini"));
auto theme_name = wm_config->read_entry("Theme", "Name", "Default");

auto theme = Gfx::load_system_theme(DeprecatedString::formatted("/res/themes/{}.ini", theme_name));
VERIFY(theme.is_valid());
auto theme = TRY(Gfx::load_system_theme(DeprecatedString::formatted("/res/themes/{}.ini", theme_name)));
Gfx::set_system_theme(theme);
auto palette = Gfx::PaletteImpl::create_with_anonymous_buffer(theme);

Expand Down
11 changes: 7 additions & 4 deletions Userland/Utilities/headless-browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,10 +771,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)

auto page_client = HeadlessBrowserPageClient::create();

if (!resources_folder.is_empty())
page_client->setup_palette(Gfx::load_system_theme(LexicalPath::join(resources_folder, "themes/Default.ini"sv).string()));
else
page_client->setup_palette(Gfx::load_system_theme("/res/themes/Default.ini"));
if (!resources_folder.is_empty()) {
auto system_theme = TRY(Gfx::load_system_theme(LexicalPath::join(resources_folder, "themes/Default.ini"sv).string()));
page_client->setup_palette(system_theme);
} else {
auto system_theme = TRY(Gfx::load_system_theme("/res/themes/Default.ini"));
page_client->setup_palette(system_theme);
}

dbgln("Loading {}", url);
page_client->load(AK::URL(url));
Expand Down

0 comments on commit 806a55e

Please sign in to comment.