KeymapStatusWidget.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "KeymapStatusWidget.h"
  7. #include <LibGUI/Action.h>
  8. #include <LibGUI/ActionGroup.h>
  9. #include <LibGUI/ConnectionToWindowManagerServer.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGUI/Process.h>
  12. #include <LibGfx/Point.h>
  13. #include <LibKeyboard/CharacterMap.h>
  14. void KeymapStatusWidget::mousedown_event(GUI::MouseEvent& event)
  15. {
  16. Gfx::IntPoint point(event.x(), event.y());
  17. MUST(refresh_menu());
  18. m_context_menu->popup(point.translated(this->screen_relative_rect().location()));
  19. }
  20. ErrorOr<void> KeymapStatusWidget::refresh_menu()
  21. {
  22. m_keymaps_group.for_each_action([&](auto& action) {
  23. m_keymaps_group.remove_action(action);
  24. return IterationDecision::Continue;
  25. });
  26. m_context_menu = GUI::Menu::construct();
  27. auto mapper_config = TRY(Core::ConfigFile::open("/etc/Keyboard.ini"));
  28. auto keymaps_string = mapper_config->read_entry("Mapping", "Keymaps", "");
  29. auto keymaps = keymaps_string.split(',');
  30. for (auto& keymap : keymaps) {
  31. auto action = GUI::Action::create_checkable(keymap, [=](auto&) {
  32. GUI::ConnectionToWindowManagerServer::the().async_set_keymap(keymap);
  33. });
  34. action->set_checked(keymap == m_current_keymap);
  35. m_keymaps_group.add_action(action);
  36. m_context_menu->add_action(action);
  37. }
  38. m_keymaps_group.set_exclusive(true);
  39. m_context_menu->add_separator();
  40. auto settings_icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/settings.png"sv));
  41. m_context_menu->add_action(GUI::Action::create("Keyboard &Settings",
  42. settings_icon,
  43. [&](auto&) {
  44. GUI::Process::spawn_or_show_error(window(), "/bin/KeyboardSettings"sv);
  45. }));
  46. return {};
  47. }
  48. void KeymapStatusWidget::set_current_keymap(DeprecatedString const& keymap, ClearBackground clear_background)
  49. {
  50. if (clear_background == ClearBackground::Yes) {
  51. GUI::Painter painter(*this);
  52. painter.clear_rect(rect(), Color::Transparent);
  53. }
  54. m_current_keymap = keymap;
  55. set_tooltip(keymap);
  56. set_text(keymap.substring(0, 2));
  57. }