KeymapStatusWindow.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2021, Timur Sultanov <SultanovTS@yandex.ru>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "KeymapStatusWindow.h"
  7. #include <LibGUI/Painter.h>
  8. #include <LibGUI/WindowManagerServerConnection.h>
  9. #include <LibKeyboard/CharacterMap.h>
  10. KeymapStatusWindow::KeymapStatusWindow()
  11. {
  12. set_window_type(GUI::WindowType::Applet);
  13. set_has_alpha_channel(true);
  14. m_label = &set_main_widget<GUI::Label>();
  15. auto current_keymap = MUST(Keyboard::CharacterMap::fetch_system_map());
  16. auto current_keymap_name = current_keymap.character_map_name();
  17. m_label->set_tooltip(current_keymap_name);
  18. m_label->set_text(current_keymap_name.substring(0, 2));
  19. }
  20. KeymapStatusWindow::~KeymapStatusWindow()
  21. {
  22. }
  23. void KeymapStatusWindow::wm_event(GUI::WMEvent& event)
  24. {
  25. if (event.type() == GUI::WMEvent::WM_KeymapChanged) {
  26. auto& keymap_event = static_cast<GUI::WMKeymapChangedEvent&>(event);
  27. auto keymap = keymap_event.keymap();
  28. set_keymap_text(keymap);
  29. }
  30. }
  31. void KeymapStatusWindow::set_keymap_text(const String& keymap)
  32. {
  33. GUI::Painter painter(*m_label);
  34. painter.clear_rect(m_label->rect(), Color::from_rgba(0));
  35. m_label->set_tooltip(keymap);
  36. m_label->set_text(keymap.substring(0, 2));
  37. }