mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:37:35 +00:00
LibGUI: Add Widget focus policies
Every widget now has a GUI::FocusPolicy that determines how it can receive focus: - NoFocus: The widget is not focusable (default) - TabFocus: The widget can be focused using the tab key. - ClickFocus: The widget can be focused by clicking on it. - StrongFocus: Both of the above. For widgets that have a focus proxy, getting/setting the focus policy will affect the proxy instead.
This commit is contained in:
parent
34014fa838
commit
aef56159a8
32 changed files with 64 additions and 40 deletions
|
@ -109,6 +109,8 @@ ConsoleWidget::ConsoleWidget()
|
|||
print_html(JS::MarkupGenerator::html_from_value(m_interpreter->vm().last_value()));
|
||||
};
|
||||
|
||||
set_focus_proxy(m_input);
|
||||
|
||||
auto& clear_button = bottom_container.add<GUI::Button>();
|
||||
clear_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||
clear_button.set_preferred_size(22, 22);
|
||||
|
@ -165,8 +167,4 @@ void ConsoleWidget::clear_output()
|
|||
m_output_view->update();
|
||||
}
|
||||
|
||||
void ConsoleWidget::focusin_event(GUI::FocusEvent&)
|
||||
{
|
||||
m_input->set_focus(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,9 +47,6 @@ public:
|
|||
private:
|
||||
ConsoleWidget();
|
||||
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
virtual void focusin_event(GUI::FocusEvent&) override;
|
||||
|
||||
RefPtr<GUI::TextBox> m_input;
|
||||
RefPtr<Web::InProcessWebView> m_output_view;
|
||||
RefPtr<Web::DOM::Element> m_output_container;
|
||||
|
|
|
@ -34,6 +34,7 @@ GlyphMapWidget::GlyphMapWidget(Gfx::Font& mutable_font)
|
|||
{
|
||||
m_glyph_count = mutable_font.glyph_count();
|
||||
set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
}
|
||||
|
||||
GlyphMapWidget::~GlyphMapWidget()
|
||||
|
|
|
@ -53,7 +53,6 @@ public:
|
|||
|
||||
private:
|
||||
explicit GlyphMapWidget(Gfx::Font&);
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||
|
|
|
@ -42,6 +42,7 @@
|
|||
|
||||
HexEditor::HexEditor()
|
||||
{
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
set_scrollbars_enabled(true);
|
||||
set_font(Gfx::Font::default_fixed_width_font());
|
||||
set_background_role(ColorRole::Base);
|
||||
|
|
|
@ -74,7 +74,6 @@ protected:
|
|||
virtual void mouseup_event(GUI::MouseEvent&) override;
|
||||
virtual void mousemove_event(GUI::MouseEvent&) override;
|
||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
|
||||
private:
|
||||
bool m_readonly { false };
|
||||
|
|
|
@ -105,7 +105,7 @@ SamplerWidget::SamplerWidget(TrackManager& track_manager)
|
|||
m_open_button = m_open_button_and_recorded_sample_name_container->add<GUI::Button>();
|
||||
m_open_button->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
|
||||
m_open_button->set_preferred_size(24, 24);
|
||||
m_open_button->set_focusable(false);
|
||||
m_open_button->set_focus_policy(GUI::FocusPolicy::TabFocus);
|
||||
m_open_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
|
||||
m_open_button->on_click = [this](auto) {
|
||||
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window());
|
||||
|
|
|
@ -36,6 +36,7 @@ namespace PixelPaint {
|
|||
|
||||
ImageEditor::ImageEditor()
|
||||
{
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
}
|
||||
|
||||
ImageEditor::~ImageEditor()
|
||||
|
|
|
@ -82,8 +82,6 @@ public:
|
|||
private:
|
||||
ImageEditor();
|
||||
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void second_paint_event(GUI::PaintEvent&) override;
|
||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace HackStudio {
|
|||
|
||||
FormWidget::FormWidget()
|
||||
{
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
set_fill_with_background_color(true);
|
||||
set_relative_rect(5, 5, 400, 300);
|
||||
|
||||
|
|
|
@ -46,8 +46,6 @@ public:
|
|||
int grid_size() const { return m_grid_size; }
|
||||
|
||||
private:
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void second_paint_event(GUI::PaintEvent&) override;
|
||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||
|
|
|
@ -35,6 +35,7 @@ namespace GUI {
|
|||
AbstractButton::AbstractButton(const StringView& text)
|
||||
: m_text(text)
|
||||
{
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
set_background_role(Gfx::ColorRole::Button);
|
||||
set_foreground_role(Gfx::ColorRole::ButtonText);
|
||||
|
||||
|
|
|
@ -53,7 +53,6 @@ public:
|
|||
bool is_being_pressed() const { return m_being_pressed; }
|
||||
|
||||
virtual void click(unsigned modifiers = 0) = 0;
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
virtual bool is_uncheckable() const { return true; }
|
||||
|
||||
int auto_repeat_interval() const { return m_auto_repeat_interval; }
|
||||
|
|
|
@ -43,6 +43,7 @@ AbstractView::AbstractView()
|
|||
: m_sort_order(SortOrder::Ascending)
|
||||
, m_selection(*this)
|
||||
{
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
}
|
||||
|
||||
AbstractView::~AbstractView()
|
||||
|
|
|
@ -87,7 +87,6 @@ public:
|
|||
bool is_multi_select() const { return m_multi_select; }
|
||||
void set_multi_select(bool);
|
||||
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
virtual void did_update_model(unsigned flags);
|
||||
virtual void did_update_selection();
|
||||
|
||||
|
|
|
@ -38,6 +38,7 @@ namespace GUI {
|
|||
Button::Button(const StringView& text)
|
||||
: AbstractButton(text)
|
||||
{
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
}
|
||||
|
||||
Button::~Button()
|
||||
|
|
|
@ -58,11 +58,8 @@ public:
|
|||
|
||||
void set_action(Action&);
|
||||
|
||||
virtual bool accepts_focus() const override { return m_focusable; }
|
||||
virtual bool is_uncheckable() const override;
|
||||
|
||||
void set_focusable(bool b) { m_focusable = b; }
|
||||
|
||||
protected:
|
||||
explicit Button(const StringView& text = {});
|
||||
virtual void paint_event(PaintEvent&) override;
|
||||
|
@ -72,7 +69,6 @@ private:
|
|||
Gfx::ButtonStyle m_button_style { Gfx::ButtonStyle::Normal };
|
||||
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
|
||||
WeakPtr<Action> m_action;
|
||||
bool m_focusable { true };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ ComboBox::ComboBox()
|
|||
};
|
||||
|
||||
m_open_button = add<ControlBoxButton>(ControlBoxButton::DownArrow);
|
||||
m_open_button->set_focusable(false);
|
||||
m_open_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
|
||||
m_open_button->on_click = [this](auto) {
|
||||
if (m_list_window->is_visible())
|
||||
close();
|
||||
|
|
|
@ -49,11 +49,11 @@ SpinBox::SpinBox()
|
|||
};
|
||||
|
||||
m_increment_button = add<ControlBoxButton>(ControlBoxButton::UpArrow);
|
||||
m_increment_button->set_focusable(false);
|
||||
m_increment_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
|
||||
m_increment_button->on_click = [this](auto) { set_value(m_value + 1); };
|
||||
m_increment_button->set_auto_repeat_interval(150);
|
||||
m_decrement_button = add<ControlBoxButton>(ControlBoxButton::DownArrow);
|
||||
m_decrement_button->set_focusable(false);
|
||||
m_decrement_button->set_focus_policy(GUI::FocusPolicy::NoFocus);
|
||||
m_decrement_button->on_click = [this](auto) { set_value(m_value - 1); };
|
||||
m_decrement_button->set_auto_repeat_interval(150);
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ TextEditor::TextEditor(Type type)
|
|||
{
|
||||
REGISTER_STRING_PROPERTY("text", text, set_text);
|
||||
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
set_accepts_emoji_input(true);
|
||||
set_override_cursor(Gfx::StandardCursor::IBeam);
|
||||
set_background_role(ColorRole::Base);
|
||||
|
|
|
@ -175,7 +175,6 @@ protected:
|
|||
virtual void focusin_event(FocusEvent&) override;
|
||||
virtual void focusout_event(FocusEvent&) override;
|
||||
virtual void timer_event(Core::TimerEvent&) override;
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
virtual void enter_event(Core::Event&) override;
|
||||
virtual void leave_event(Core::Event&) override;
|
||||
virtual void context_menu_event(ContextMenuEvent&) override;
|
||||
|
|
|
@ -64,6 +64,7 @@ void ToolBar::add_action(Action& action)
|
|||
button.set_exclusive(true);
|
||||
button.set_action(*item->action);
|
||||
button.set_tooltip(item->action->text());
|
||||
button.set_focus_policy(FocusPolicy::TabFocus);
|
||||
if (item->action->icon())
|
||||
button.set_icon(item->action->icon());
|
||||
else
|
||||
|
|
|
@ -326,7 +326,7 @@ void Widget::handle_mouseup_event(MouseEvent& event)
|
|||
|
||||
void Widget::handle_mousedown_event(MouseEvent& event)
|
||||
{
|
||||
if (accepts_focus())
|
||||
if (((unsigned)focus_policy() & (unsigned)FocusPolicy::ClickFocus))
|
||||
set_focus(true, FocusSource::Mouse);
|
||||
mousedown_event(event);
|
||||
if (event.button() == MouseButton::Right) {
|
||||
|
@ -539,6 +539,20 @@ void Widget::set_focus_proxy(Widget* proxy)
|
|||
m_focus_proxy = nullptr;
|
||||
}
|
||||
|
||||
FocusPolicy Widget::focus_policy() const
|
||||
{
|
||||
if (m_focus_proxy)
|
||||
return m_focus_proxy->focus_policy();
|
||||
return m_focus_policy;
|
||||
}
|
||||
|
||||
void Widget::set_focus_policy(FocusPolicy policy)
|
||||
{
|
||||
if (m_focus_proxy)
|
||||
return m_focus_proxy->set_focus_policy(policy);
|
||||
m_focus_policy = policy;
|
||||
}
|
||||
|
||||
bool Widget::is_focused() const
|
||||
{
|
||||
if (m_focus_proxy)
|
||||
|
@ -750,7 +764,7 @@ void Widget::set_updates_enabled(bool enabled)
|
|||
|
||||
void Widget::focus_previous_widget(FocusSource source)
|
||||
{
|
||||
auto focusable_widgets = window()->focusable_widgets();
|
||||
auto focusable_widgets = window()->focusable_widgets(source);
|
||||
for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
|
||||
if (focusable_widgets[i] != this)
|
||||
continue;
|
||||
|
@ -763,7 +777,7 @@ void Widget::focus_previous_widget(FocusSource source)
|
|||
|
||||
void Widget::focus_next_widget(FocusSource source)
|
||||
{
|
||||
auto focusable_widgets = window()->focusable_widgets();
|
||||
auto focusable_widgets = window()->focusable_widgets(source);
|
||||
for (size_t i = 0; i < focusable_widgets.size(); ++i) {
|
||||
if (focusable_widgets[i] != this)
|
||||
continue;
|
||||
|
|
|
@ -88,6 +88,13 @@ private:
|
|||
Function<NonnullRefPtr<Widget>()> m_factory;
|
||||
};
|
||||
|
||||
enum class FocusPolicy {
|
||||
NoFocus = 0,
|
||||
TabFocus = 0x1,
|
||||
ClickFocus = 0x2,
|
||||
StrongFocus = TabFocus | ClickFocus,
|
||||
};
|
||||
|
||||
class Widget : public Core::Object {
|
||||
C_OBJECT(Widget)
|
||||
public:
|
||||
|
@ -155,8 +162,6 @@ public:
|
|||
void update();
|
||||
void update(const Gfx::IntRect&);
|
||||
|
||||
virtual bool accepts_focus() const { return false; }
|
||||
|
||||
bool is_focused() const;
|
||||
void set_focus(bool, FocusSource = FocusSource::Programmatic);
|
||||
|
||||
|
@ -164,6 +169,9 @@ public:
|
|||
const Widget* focus_proxy() const { return m_focus_proxy; }
|
||||
void set_focus_proxy(Widget*);
|
||||
|
||||
void set_focus_policy(FocusPolicy policy);
|
||||
FocusPolicy focus_policy() const;
|
||||
|
||||
enum class ShouldRespectGreediness { No = 0,
|
||||
Yes };
|
||||
struct HitTestResult {
|
||||
|
@ -362,6 +370,7 @@ private:
|
|||
NonnullRefPtr<Gfx::PaletteImpl> m_palette;
|
||||
|
||||
WeakPtr<Widget> m_focus_proxy;
|
||||
FocusPolicy m_focus_policy { FocusPolicy::NoFocus };
|
||||
|
||||
Gfx::StandardCursor m_override_cursor { Gfx::StandardCursor::None };
|
||||
};
|
||||
|
|
|
@ -543,7 +543,7 @@ void Window::set_main_widget(Widget* widget)
|
|||
set_rect(new_window_rect);
|
||||
m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
|
||||
m_main_widget->set_window(this);
|
||||
if (m_main_widget->accepts_focus())
|
||||
if (m_main_widget->focus_policy() != FocusPolicy::NoFocus)
|
||||
m_main_widget->set_focus(true);
|
||||
}
|
||||
update();
|
||||
|
@ -720,7 +720,7 @@ void Window::start_wm_resize()
|
|||
WindowServerConnection::the().post_message(Messages::WindowServer::WM_StartWindowResize(WindowServerConnection::the().my_client_id(), m_window_id));
|
||||
}
|
||||
|
||||
Vector<Widget*> Window::focusable_widgets() const
|
||||
Vector<Widget*> Window::focusable_widgets(FocusSource source) const
|
||||
{
|
||||
if (!m_main_widget)
|
||||
return {};
|
||||
|
@ -728,7 +728,20 @@ Vector<Widget*> Window::focusable_widgets() const
|
|||
Vector<Widget*> collected_widgets;
|
||||
|
||||
Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
|
||||
if (widget.accepts_focus())
|
||||
bool widget_accepts_focus = false;
|
||||
switch (source) {
|
||||
case FocusSource::Keyboard:
|
||||
widget_accepts_focus = ((unsigned)widget.focus_policy() & (unsigned)FocusPolicy::TabFocus);
|
||||
break;
|
||||
case FocusSource::Mouse:
|
||||
widget_accepts_focus = ((unsigned)widget.focus_policy() & (unsigned)FocusPolicy::ClickFocus);
|
||||
break;
|
||||
case FocusSource::Programmatic:
|
||||
widget_accepts_focus = widget.focus_policy() != FocusPolicy::NoFocus;
|
||||
break;
|
||||
}
|
||||
|
||||
if (widget_accepts_focus)
|
||||
collected_widgets.append(&widget);
|
||||
widget.for_each_child_widget([&](auto& child) {
|
||||
if (!child.is_visible())
|
||||
|
@ -853,7 +866,7 @@ void Window::set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio)
|
|||
|
||||
void Window::did_add_widget(Badge<Widget>, Widget& widget)
|
||||
{
|
||||
if (!m_focused_widget && widget.accepts_focus())
|
||||
if (!m_focused_widget && widget.focus_policy() != FocusPolicy::NoFocus)
|
||||
set_focused_widget(&widget);
|
||||
}
|
||||
|
||||
|
|
|
@ -175,7 +175,7 @@ public:
|
|||
void apply_icon();
|
||||
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
|
||||
|
||||
Vector<Widget*> focusable_widgets() const;
|
||||
Vector<Widget*> focusable_widgets(FocusSource) const;
|
||||
|
||||
void schedule_relayout();
|
||||
|
||||
|
|
|
@ -96,7 +96,7 @@ TerminalWidget::TerminalWidget(int ptm_fd, bool automatic_size_policy, RefPtr<Co
|
|||
, m_config(move(config))
|
||||
{
|
||||
set_override_cursor(Gfx::StandardCursor::IBeam);
|
||||
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
set_accepts_emoji_input(true);
|
||||
set_pty_master_fd(ptm_fd);
|
||||
m_cursor_blink_timer = add<Core::Timer>();
|
||||
|
|
|
@ -85,8 +85,6 @@ public:
|
|||
void paste();
|
||||
void clear_including_history();
|
||||
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
|
||||
Function<void(const StringView&)> on_title_change;
|
||||
Function<void()> on_command_exit;
|
||||
|
||||
|
|
|
@ -68,6 +68,7 @@ InProcessWebView::InProcessWebView()
|
|||
{
|
||||
set_should_hide_unnecessary_scrollbars(true);
|
||||
set_background_role(ColorRole::Base);
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
|
||||
m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
|
||||
GUI::Clipboard::the().set_plain_text(selected_text());
|
||||
|
|
|
@ -61,8 +61,6 @@ public:
|
|||
|
||||
void set_should_show_line_box_borders(bool value) { m_should_show_line_box_borders = value; }
|
||||
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
|
||||
GUI::Action& select_all_action() { return *m_select_all_action; }
|
||||
GUI::Action& copy_action() { return *m_copy_action; }
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@ namespace Web {
|
|||
OutOfProcessWebView::OutOfProcessWebView()
|
||||
{
|
||||
set_should_hide_unnecessary_scrollbars(true);
|
||||
set_focus_policy(GUI::FocusPolicy::StrongFocus);
|
||||
m_client = WebContentClient::construct(*this);
|
||||
client().post_message(Messages::WebContentServer::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
|
||||
}
|
||||
|
|
|
@ -68,7 +68,6 @@ private:
|
|||
OutOfProcessWebView();
|
||||
|
||||
// ^Widget
|
||||
virtual bool accepts_focus() const override { return true; }
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void resize_event(GUI::ResizeEvent&) override;
|
||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue