mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
LibGUI: Simplify RadioButton by using AbstractButton exclusive mode
Making an AbstractButton exclusive means that we enforce that only one of the exclusive buttons within the same parent widget can be checked at a time. RadioButton was doing exactly the same thing, except in a custom way. So just remove the custom code and make it exclusive. :^)
This commit is contained in:
parent
f0482a4cab
commit
5e19e72a6a
Notes:
sideshowbarker
2024-07-19 00:17:39 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/5e19e72a6af
3 changed files with 2 additions and 18 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
|
|
@ -36,6 +36,7 @@ namespace GUI {
|
|||
RadioButton::RadioButton(String text)
|
||||
: AbstractButton(move(text))
|
||||
{
|
||||
set_exclusive(true);
|
||||
set_min_width(32);
|
||||
set_fixed_height(22);
|
||||
}
|
||||
|
@ -73,25 +74,10 @@ void RadioButton::paint_event(PaintEvent& event)
|
|||
painter.draw_focus_rect(text_rect.inflated(6, 6), palette().focus_outline());
|
||||
}
|
||||
|
||||
template<typename Callback>
|
||||
void RadioButton::for_each_in_group(Callback callback)
|
||||
{
|
||||
if (!parent())
|
||||
return;
|
||||
parent()->for_each_child_of_type<RadioButton>([&](auto& child) {
|
||||
return callback(downcast<RadioButton>(child));
|
||||
});
|
||||
}
|
||||
|
||||
void RadioButton::click(unsigned)
|
||||
{
|
||||
if (!is_enabled())
|
||||
return;
|
||||
for_each_in_group([this](auto& button) {
|
||||
if (&button != this)
|
||||
button.set_checked(false);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
set_checked(true);
|
||||
}
|
||||
|
||||
|
|
|
@ -49,8 +49,6 @@ private:
|
|||
|
||||
virtual bool is_radio_button() const final { return true; }
|
||||
|
||||
template<typename Callback>
|
||||
void for_each_in_group(Callback);
|
||||
static Gfx::IntSize circle_size();
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue