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

#include "DesktopStatusWindow.h"
#include <LibGUI/Desktop.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Widget.h>
#include <LibGUI/WindowManagerServerConnection.h>
#include <LibGfx/Palette.h>

class DesktopStatusWidget : public GUI::Widget {
    C_OBJECT(DesktopStatusWidget);

public:
    virtual ~DesktopStatusWidget() override
    {
    }

    Gfx::IntRect rect_for_desktop(unsigned row, unsigned col) const
    {
        auto& desktop = GUI::Desktop::the();

        auto vcols = desktop.workspace_columns();
        auto vrows = desktop.workspace_rows();

        auto desktop_width = (width() - gap() * (vcols - 1)) / vcols;
        auto desktop_height = (height() - gap() * (vrows - 1)) / vrows;

        return {
            col * (desktop_width + gap()), row * (desktop_height + gap()),
            desktop_width, desktop_height
        };
    }

    virtual void paint_event(GUI::PaintEvent& event) override
    {
        GUI::Widget::paint_event(event);

        GUI::Painter painter(*this);
        painter.add_clip_rect(event.rect());
        painter.fill_rect({ 0, 0, width(), height() }, palette().button());

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

        auto active_color = palette().active_window_border1();
        auto inactive_color = palette().inactive_window_border1();

        for (unsigned row = 0; row < desktop.workspace_rows(); ++row) {
            for (unsigned col = 0; col < desktop.workspace_columns(); ++col) {
                painter.fill_rect(rect_for_desktop(row, col),
                    (row == current_row() && col == current_col()) ? active_color : inactive_color);
            }
        }
    }

    virtual void mousedown_event(GUI::MouseEvent& event) override
    {
        auto base_rect = rect_for_desktop(0, 0);
        auto row = event.y() / (base_rect.height() + gap());
        auto col = event.x() / (base_rect.width() + gap());

        // Handle case where divider is clicked.
        if (rect_for_desktop(row, col).contains(event.position()))
            GUI::WindowManagerServerConnection::the().async_set_workspace(row, col);
    }

    virtual void mousewheel_event(GUI::MouseEvent& event) override
    {
        auto& desktop = GUI::Desktop::the();

        auto col = current_col();
        auto row = current_row();

        auto vcols = desktop.workspace_columns();
        auto vrows = desktop.workspace_rows();
        auto direction = event.wheel_delta_y() < 0 ? 1 : -1;

        if (event.modifiers() & Mod_Shift)
            col = abs((int)col + direction) % vcols;
        else
            row = abs((int)row + direction) % vrows;

        GUI::WindowManagerServerConnection::the().async_set_workspace(row, col);
    }

    unsigned current_row() const { return m_current_row; }
    void set_current_row(unsigned row) { m_current_row = row; }
    unsigned current_col() const { return m_current_col; }
    void set_current_col(unsigned col) { m_current_col = col; }

    unsigned gap() const { return m_gap; }

private:
    DesktopStatusWidget()
    {
    }

    unsigned m_gap { 1 };

    unsigned m_current_row;
    unsigned m_current_col;
};

DesktopStatusWindow::DesktopStatusWindow()
{
    GUI::Desktop::the().on_receive_screen_rects([&](GUI::Desktop&) {
        update();
    });
    set_window_type(GUI::WindowType::Applet);
    set_has_alpha_channel(true);
    m_widget = &set_main_widget<DesktopStatusWidget>();
}

DesktopStatusWindow::~DesktopStatusWindow()
{
}

void DesktopStatusWindow::wm_event(GUI::WMEvent& event)
{
    if (event.type() == GUI::Event::WM_WorkspaceChanged) {
        auto& changed_event = static_cast<GUI::WMWorkspaceChangedEvent&>(event);
        m_widget->set_current_row(changed_event.current_row());
        m_widget->set_current_col(changed_event.current_column());
        update();
    }
}