summaryrefslogtreecommitdiff
path: root/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp
blob: 71a3b19363282f54a9b1d0638840559f44da9652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "BackgroundSettingsWidget.h"
#include <AK/LexicalPath.h>
#include <AK/StringBuilder.h>
#include <Applications/DisplaySettings/BackgroundSettingsGML.h>
#include <LibConfig/Client.h>
#include <LibCore/ConfigFile.h>
#include <LibDesktop/Launcher.h>
#include <LibFileSystemAccessClient/Client.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/FileSystemModel.h>
#include <LibGUI/FileTypeFilter.h>
#include <LibGUI/IconView.h>
#include <LibGUI/ItemListModel.h>
#include <LibGUI/MessageBox.h>
#include <LibGfx/Palette.h>
#include <LibGfx/SystemTheme.h>

namespace DisplaySettings {

ErrorOr<NonnullRefPtr<BackgroundSettingsWidget>> BackgroundSettingsWidget::try_create(bool& background_settings_changed)
{
    auto background_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) BackgroundSettingsWidget(background_settings_changed)));

    TRY(background_settings_widget->m_modes.try_append(TRY(String::from_utf8("Tile"sv))));
    TRY(background_settings_widget->m_modes.try_append(TRY(String::from_utf8("Center"sv))));
    TRY(background_settings_widget->m_modes.try_append(TRY(String::from_utf8("Stretch"sv))));

    TRY(background_settings_widget->create_frame());
    TRY(background_settings_widget->load_current_settings());

    return background_settings_widget;
}

BackgroundSettingsWidget::BackgroundSettingsWidget(bool& background_settings_changed)
    : m_background_settings_changed { background_settings_changed }
{
}

ErrorOr<void> BackgroundSettingsWidget::create_frame()
{
    TRY(load_from_gml(background_settings_gml));

    m_monitor_widget = *find_descendant_of_type_named<DisplaySettings::MonitorWidget>("monitor_widget");

    m_wallpaper_view = *find_descendant_of_type_named<GUI::IconView>("wallpaper_view");
    m_wallpaper_view->set_model(GUI::FileSystemModel::create("/res/wallpapers"));
    m_wallpaper_view->set_model_column(GUI::FileSystemModel::Column::Name);
    m_wallpaper_view->on_selection_change = [this] {
        String path = String::from_utf8_short_string(""sv);
        if (!m_wallpaper_view->selection().is_empty()) {
            auto index = m_wallpaper_view->selection().first();
            auto path_or_error = String::from_deprecated_string(static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->full_path(index));
            if (path_or_error.is_error()) {
                GUI::MessageBox::show_error(window(), "Unable to load wallpaper"sv);
                return;
            }
            path = path_or_error.release_value();
        }

        m_monitor_widget->set_wallpaper(path);
        set_modified(true);
    };

    m_context_menu = GUI::Menu::construct();
    auto const file_manager_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-file-manager.png"sv));
    m_show_in_file_manager_action = GUI::Action::create("Show in File Manager", file_manager_icon, [this](GUI::Action const&) {
        LexicalPath path { m_monitor_widget->wallpaper().value().to_deprecated_string() };
        Desktop::Launcher::open(URL::create_with_file_scheme(path.dirname(), path.basename()));
    });
    m_context_menu->add_action(*m_show_in_file_manager_action);

    TRY(m_context_menu->try_add_separator());
    m_copy_action = GUI::CommonActions::make_copy_action(
        [this](auto&) {
            auto wallpaper = m_monitor_widget->wallpaper();
            if (wallpaper.has_value())
                GUI::Clipboard::the().set_data(URL::create_with_file_scheme(wallpaper.value().to_deprecated_string()).to_deprecated_string().bytes(), "text/uri-list");
        },
        this);
    m_context_menu->add_action(*m_copy_action);

    m_wallpaper_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
        if (index.is_valid()) {
            m_context_menu->popup(event.screen_position(), m_show_in_file_manager_action);
        }
    };

    auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button");
    button.on_click = [this](auto) {
        auto response = FileSystemAccessClient::Client::the().open_file(window(), "Select wallpaper"sv, "/res/wallpapers"sv, Core::File::OpenMode::Read, { { GUI::FileTypeFilter::image_files(), GUI::FileTypeFilter::all_files() } });
        if (response.is_error())
            return;
        m_wallpaper_view->selection().clear();
        m_monitor_widget->set_wallpaper(response.release_value().filename());
        m_background_settings_changed = true;
        set_modified(true);
    };

    m_mode_combo = *find_descendant_of_type_named<GUI::ComboBox>("mode_combo");
    m_mode_combo->set_only_allow_values_from_model(true);
    m_mode_combo->set_model(*GUI::ItemListModel<String>::create(m_modes));
    bool first_mode_change = true;
    m_mode_combo->on_change = [this, first_mode_change](auto&, const GUI::ModelIndex& index) mutable {
        m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
        m_background_settings_changed = !first_mode_change;
        first_mode_change = false;
        set_modified(true);
    };

    m_color_input = *find_descendant_of_type_named<GUI::ColorInput>("color_input");
    m_color_input->set_color_has_alpha_channel(false);
    m_color_input->set_color_picker_title("Select color for desktop");
    bool first_color_change = true;
    m_color_input->on_change = [this, first_color_change]() mutable {
        m_monitor_widget->set_background_color(m_color_input->color());
        m_background_settings_changed = !first_color_change;
        first_color_change = false;
        set_modified(true);
    };

    return {};
}

ErrorOr<void> BackgroundSettingsWidget::load_current_settings()
{
    auto ws_config = TRY(Core::ConfigFile::open("/etc/WindowServer.ini"));

    auto selected_wallpaper = TRY(String::from_deprecated_string(Config::read_string("WindowManager"sv, "Background"sv, "Wallpaper"sv, ""sv)));
    if (!selected_wallpaper.is_empty()) {
        auto index = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->index(selected_wallpaper.to_deprecated_string(), m_wallpaper_view->model_column());
        m_wallpaper_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
        m_monitor_widget->set_wallpaper(selected_wallpaper);
    }

    auto mode = TRY(String::from_deprecated_string(ws_config->read_entry("Background", "Mode", "Center")));
    if (!m_modes.contains_slow(mode)) {
        warnln("Invalid background mode '{}' in WindowServer config, falling back to 'Center'", mode);
        mode = TRY(String::from_utf8("Center"sv));
    }
    m_monitor_widget->set_wallpaper_mode(mode);
    m_mode_combo->set_selected_index(m_modes.find_first_index(mode).value_or(0), GUI::AllowCallback::No);

    auto palette_desktop_color = palette().desktop_background();
    auto background_color = ws_config->read_entry("Background", "Color", "");

    if (!background_color.is_empty()) {
        auto opt_color = Color::from_string(background_color);
        if (opt_color.has_value())
            palette_desktop_color = opt_color.value();
    }

    m_color_input->set_color(palette_desktop_color, GUI::AllowCallback::No);
    m_monitor_widget->set_background_color(palette_desktop_color);
    m_background_settings_changed = false;

    return {};
}

void BackgroundSettingsWidget::apply_settings()
{
    auto wallpaper_path_or_empty = m_monitor_widget->wallpaper();

    if (!GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper_bitmap(), wallpaper_path_or_empty)) {
        if (!wallpaper_path_or_empty.has_value()) {
            GUI::MessageBox::show_error(window(), "Unable to load wallpaper"sv);
        } else {
            auto detailed_error_message = String::formatted("Unable to load file {} as wallpaper", wallpaper_path_or_empty.value());
            if (!detailed_error_message.is_error())
                GUI::MessageBox::show_error(window(), detailed_error_message.release_value());
            else
                GUI::MessageBox::show_error(window(), "Unable to load wallpaper"sv);
        }
    }

    GUI::Desktop::the().set_background_color(m_color_input->text());
    GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode());
}

}