summaryrefslogtreecommitdiff
path: root/Userland/Applets
diff options
context:
space:
mode:
authorTimur Sultanov <tssultanov@outlook.com>2022-04-03 02:06:35 +0300
committerAndreas Kling <kling@serenityos.org>2022-07-21 16:40:47 +0200
commit9906f41e016994b35a05239550fe96e519442f26 (patch)
tree6076cff47e1c67047ef0a7b365facf3f24212430 /Userland/Applets
parentdb11cfa2c573536f1df0c04b5a0a7166f2de26e8 (diff)
downloadserenity-9906f41e016994b35a05239550fe96e519442f26.zip
Keymap+WindowServer: Add context menu to keymap applet
Adding a context menu which lists configured keymaps and allows setting the active keymap
Diffstat (limited to 'Userland/Applets')
-rw-r--r--Userland/Applets/Keymap/CMakeLists.txt1
-rw-r--r--Userland/Applets/Keymap/KeymapStatusWidget.cpp73
-rw-r--r--Userland/Applets/Keymap/KeymapStatusWidget.h34
-rw-r--r--Userland/Applets/Keymap/KeymapStatusWindow.cpp18
-rw-r--r--Userland/Applets/Keymap/KeymapStatusWindow.h6
5 files changed, 111 insertions, 21 deletions
diff --git a/Userland/Applets/Keymap/CMakeLists.txt b/Userland/Applets/Keymap/CMakeLists.txt
index 2092d2de68..ded34f8913 100644
--- a/Userland/Applets/Keymap/CMakeLists.txt
+++ b/Userland/Applets/Keymap/CMakeLists.txt
@@ -6,6 +6,7 @@ serenity_component(
set(SOURCES
KeymapStatusWindow.cpp
+ KeymapStatusWidget.cpp
main.cpp
)
diff --git a/Userland/Applets/Keymap/KeymapStatusWidget.cpp b/Userland/Applets/Keymap/KeymapStatusWidget.cpp
new file mode 100644
index 0000000000..382cd0de83
--- /dev/null
+++ b/Userland/Applets/Keymap/KeymapStatusWidget.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2022, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "KeymapStatusWidget.h"
+#include "LibGUI/ActionGroup.h"
+#include <LibGUI/Action.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("&Settings",
+ settings_icon,
+ [&](auto&) {
+ GUI::Process::spawn_or_show_error(window(), "/bin/KeyboardSettings"sv);
+ }));
+
+ return {};
+}
+
+void KeymapStatusWidget::set_current_keymap(String 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));
+}
diff --git a/Userland/Applets/Keymap/KeymapStatusWidget.h b/Userland/Applets/Keymap/KeymapStatusWidget.h
new file mode 100644
index 0000000000..3e3e0fa54e
--- /dev/null
+++ b/Userland/Applets/Keymap/KeymapStatusWidget.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibCore/FileWatcher.h>
+#include <LibGUI/ActionGroup.h>
+#include <LibGUI/Label.h>
+#include <LibGUI/Menu.h>
+#include <LibGUI/Window.h>
+
+enum class ClearBackground {
+ No,
+ Yes
+};
+
+class KeymapStatusWidget : public GUI::Label {
+ C_OBJECT(KeymapStatusWidget);
+
+ virtual void mousedown_event(GUI::MouseEvent& event) override;
+
+ void set_current_keymap(String const& keymap, ClearBackground clear_background = ClearBackground::Yes);
+
+private:
+ RefPtr<GUI::Menu> m_context_menu;
+ String m_current_keymap;
+
+ ErrorOr<void> refresh_menu();
+
+ GUI::ActionGroup m_keymaps_group;
+};
diff --git a/Userland/Applets/Keymap/KeymapStatusWindow.cpp b/Userland/Applets/Keymap/KeymapStatusWindow.cpp
index a0e42759c9..69248141a1 100644
--- a/Userland/Applets/Keymap/KeymapStatusWindow.cpp
+++ b/Userland/Applets/Keymap/KeymapStatusWindow.cpp
@@ -6,19 +6,10 @@
*/
#include "KeymapStatusWindow.h"
-#include <LibGUI/ConnectionToWindowManagerServer.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Process.h>
#include <LibKeyboard/CharacterMap.h>
-void KeymapStatusWidget::mousedown_event(GUI::MouseEvent& event)
-{
- if (event.button() != GUI::MouseButton::Primary)
- return;
-
- GUI::Process::spawn_or_show_error(window(), "/bin/KeyboardSettings"sv);
-}
-
KeymapStatusWindow::KeymapStatusWindow()
{
set_window_type(GUI::WindowType::Applet);
@@ -27,8 +18,7 @@ KeymapStatusWindow::KeymapStatusWindow()
auto current_keymap = MUST(Keyboard::CharacterMap::fetch_system_map());
auto current_keymap_name = current_keymap.character_map_name();
- m_status_widget->set_tooltip(current_keymap_name);
- m_status_widget->set_text(current_keymap_name.substring(0, 2));
+ m_status_widget->set_current_keymap(current_keymap_name, ClearBackground::No);
}
void KeymapStatusWindow::wm_event(GUI::WMEvent& event)
@@ -42,9 +32,5 @@ void KeymapStatusWindow::wm_event(GUI::WMEvent& event)
void KeymapStatusWindow::set_keymap_text(String const& keymap)
{
- GUI::Painter painter(*m_status_widget);
- painter.clear_rect(m_status_widget->rect(), Color::from_argb(0));
-
- m_status_widget->set_tooltip(keymap);
- m_status_widget->set_text(keymap.substring(0, 2));
+ m_status_widget->set_current_keymap(keymap);
}
diff --git a/Userland/Applets/Keymap/KeymapStatusWindow.h b/Userland/Applets/Keymap/KeymapStatusWindow.h
index 4a2a5b1fdd..a4f4d3a162 100644
--- a/Userland/Applets/Keymap/KeymapStatusWindow.h
+++ b/Userland/Applets/Keymap/KeymapStatusWindow.h
@@ -7,14 +7,10 @@
#pragma once
+#include "KeymapStatusWidget.h"
#include <LibGUI/Label.h>
#include <LibGUI/Window.h>
-class KeymapStatusWidget : public GUI::Label {
- C_OBJECT(KeymapStatusWidget);
-
- virtual void mousedown_event(GUI::MouseEvent& event) override;
-};
class KeymapStatusWindow final : public GUI::Window {
C_OBJECT(KeymapStatusWindow)
public: