TextBox.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/TextEditor.h>
  10. namespace GUI {
  11. class TextBox : public TextEditor {
  12. C_OBJECT(TextBox)
  13. public:
  14. virtual ~TextBox() override = default;
  15. Function<void()> on_up_pressed;
  16. Function<void()> on_down_pressed;
  17. void set_history_enabled(bool enabled) { m_history_enabled = enabled; }
  18. void add_current_text_to_history();
  19. protected:
  20. TextBox();
  21. private:
  22. virtual void keydown_event(GUI::KeyEvent&) override;
  23. bool has_no_history() const { return !m_history_enabled || m_history.is_empty(); }
  24. bool can_go_backwards_in_history() const { return m_history_index > 0; }
  25. bool can_go_forwards_in_history() const { return m_history_index < static_cast<int>(m_history.size()) - 1; }
  26. void add_input_to_history(String);
  27. bool m_history_enabled { false };
  28. Vector<String> m_history;
  29. int m_history_index { -1 };
  30. String m_saved_input;
  31. };
  32. class PasswordBox : public TextBox {
  33. C_OBJECT(PasswordBox)
  34. public:
  35. bool is_showing_reveal_button() const { return m_show_reveal_button; }
  36. void set_show_reveal_button(bool show)
  37. {
  38. m_show_reveal_button = show;
  39. update();
  40. }
  41. private:
  42. PasswordBox();
  43. virtual void paint_event(PaintEvent&) override;
  44. virtual void mousedown_event(GUI::MouseEvent&) override;
  45. Gfx::IntRect reveal_password_button_rect() const;
  46. bool m_show_reveal_button { false };
  47. };
  48. class UrlBox : public TextBox {
  49. C_OBJECT(UrlBox)
  50. public:
  51. virtual ~UrlBox() override = default;
  52. void set_focus_transition(bool focus_transition) { m_focus_transition = focus_transition; }
  53. bool is_focus_transition() const { return m_focus_transition; }
  54. private:
  55. UrlBox();
  56. virtual void mousedown_event(GUI::MouseEvent&) override;
  57. virtual void focusout_event(GUI::FocusEvent&) override;
  58. bool m_focus_transition { true };
  59. };
  60. }