summaryrefslogtreecommitdiff
path: root/Userland/Applets/WorkspacePicker/main.cpp
blob: ad658295e364c0fce5f4a64d84b311b35eee471a (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
/*
 * Copyright (c) 2021, Peter Elliott <pelliott@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "DesktopStatusWindow.h"
#include <LibCore/System.h>
#include <LibGUI/Application.h>
#include <LibGUI/ConnectionToWindowManagerServer.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/Painter.h>
#include <LibMain/Main.h>
#include <WindowServer/Window.h>

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    TRY(Core::System::pledge("stdio recvfd sendfd rpath proc exec unix"));

    auto app = TRY(GUI::Application::try_create(arguments));
    app->set_quit_when_last_window_deleted(false);

    // We need to obtain the WM connection here as well before the pledge shortening.
    GUI::ConnectionToWindowManagerServer::the();

    TRY(Core::System::pledge("stdio recvfd sendfd rpath proc exec"));

    TRY(Core::System::unveil("/res", "r"));
    TRY(Core::System::unveil("/bin/DisplaySettings", "x"));

    auto window = TRY(DesktopStatusWindow::try_create());
    window->set_title("WorkspacePicker");
    window->resize(28, 16);

    auto& desktop = GUI::Desktop::the();

    auto hide_tray_icon = [&] {
        window->hide();
    };

    auto show_tray_icon = [&] {
        if (!window->is_visible()) {
            window->show();
            window->make_window_manager(WindowServer::WMEventMask::WorkspaceChanges);
        }
    };

    if (desktop.workspace_rows() != 1 || desktop.workspace_columns() != 1) {
        show_tray_icon();
    }

    desktop.on_receive_screen_rects([&](auto&) {
        if (desktop.workspace_rows() == 1 && desktop.workspace_columns() == 1) {
            hide_tray_icon();
        } else {
            window->update();
            show_tray_icon();
        }
    });

    return app->exec();
}