FontSettingsWidget.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Thomas Keppler <winfr34k@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "FontSettingsWidget.h"
  9. #include <Applications/DisplaySettings/FontSettingsGML.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/ConnectionToWindowServer.h>
  12. #include <LibGUI/FontPicker.h>
  13. #include <LibGfx/Font/FontDatabase.h>
  14. namespace DisplaySettings {
  15. static void update_label_with_font(GUI::Label&, Gfx::Font const&);
  16. FontSettingsWidget::FontSettingsWidget()
  17. {
  18. load_from_gml(font_settings_gml);
  19. auto& default_font = Gfx::FontDatabase::default_font();
  20. m_default_font_label = *find_descendant_of_type_named<GUI::Label>("default_font_label");
  21. update_label_with_font(*m_default_font_label, default_font);
  22. auto& default_font_button = *find_descendant_of_type_named<GUI::Button>("default_font_button");
  23. default_font_button.on_click = [this](auto) {
  24. auto font_picker = GUI::FontPicker::construct(window(), &m_default_font_label->font(), false);
  25. if (font_picker->exec() == GUI::Dialog::ExecResult::OK) {
  26. update_label_with_font(*m_default_font_label, *font_picker->font());
  27. set_modified(true);
  28. }
  29. };
  30. auto& default_fixed_width_font = Gfx::FontDatabase::default_fixed_width_font();
  31. m_fixed_width_font_label = *find_descendant_of_type_named<GUI::Label>("fixed_width_font_label");
  32. update_label_with_font(*m_fixed_width_font_label, default_fixed_width_font);
  33. auto& fixed_width_font_button = *find_descendant_of_type_named<GUI::Button>("fixed_width_font_button");
  34. fixed_width_font_button.on_click = [this](auto) {
  35. auto font_picker = GUI::FontPicker::construct(window(), &m_fixed_width_font_label->font(), true);
  36. if (font_picker->exec() == GUI::Dialog::ExecResult::OK) {
  37. update_label_with_font(*m_fixed_width_font_label, *font_picker->font());
  38. set_modified(true);
  39. }
  40. };
  41. }
  42. static void update_label_with_font(GUI::Label& label, Gfx::Font const& font)
  43. {
  44. label.set_text(font.human_readable_name());
  45. label.set_font(font);
  46. }
  47. void FontSettingsWidget::apply_settings()
  48. {
  49. GUI::ConnectionToWindowServer::the().set_system_fonts(m_default_font_label->font().qualified_name(), m_fixed_width_font_label->font().qualified_name());
  50. }
  51. }