summaryrefslogtreecommitdiff
path: root/Userland/Applets/Keymap/KeymapStatusWidget.cpp
blob: 7a1b274347e9978ffa54fbe615ddc60fd9edf873 (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
/*
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "KeymapStatusWidget.h"
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/ConnectionToWindowManagerServer.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Process.h>
#include <LibGfx/Point.h>
#include <LibKeyboard/CharacterMap.h>

void KeymapStatusWidget::mousedown_event(GUI::MouseEvent& event)
{
    Gfx::IntPoint point(event.x(), event.y());
    MUST(refresh_menu());
    m_context_menu->popup(point.translated(this->screen_relative_rect().location()));
}

ErrorOr<void> KeymapStatusWidget::refresh_menu()
{
    m_keymaps_group.for_each_action([&](auto& action) {
        m_keymaps_group.remove_action(action);
        return IterationDecision::Continue;
    });

    m_context_menu = GUI::Menu::construct();

    auto mapper_config = TRY(Core::ConfigFile::open("/etc/Keyboard.ini"));
    auto keymaps_string = mapper_config->read_entry("Mapping", "Keymaps", "");
    auto keymaps = keymaps_string.split(',');

    for (auto& keymap : keymaps) {
        auto action = GUI::Action::create_checkable(keymap, [=](auto&) {
            GUI::ConnectionToWindowManagerServer::the().async_set_keymap(keymap);
        });

        action->set_checked(keymap == m_current_keymap);

        m_keymaps_group.add_action(action);
        m_context_menu->add_action(action);
    }

    m_keymaps_group.set_exclusive(true);

    m_context_menu->add_separator();

    auto settings_icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png"sv));

    m_context_menu->add_action(GUI::Action::create("Keyboard &Settings",
        settings_icon,
        [&](auto&) {
            GUI::Process::spawn_or_show_error(window(), "/bin/KeyboardSettings"sv);
        }));

    return {};
}

void KeymapStatusWidget::set_current_keymap(DeprecatedString const& keymap, ClearBackground clear_background)
{
    if (clear_background == ClearBackground::Yes) {
        GUI::Painter painter(*this);
        painter.clear_rect(rect(), Color::Transparent);
    }

    m_current_keymap = keymap;

    set_tooltip(keymap);
    set_text(keymap.substring(0, 2));
}