Skip to content

Commit aadb35f

Browse files
AtkinsSJawesomekling
authored andcommitted
LibGUI: Add ability to position checkboxes to the right of their text
This uses the new `checkbox_position` property, which can be "Left" or "Right".
1 parent 0ef3c15 commit aadb35f

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Base/usr/share/man/man5/GML-Widget-CheckBox.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ Defines a GUI checkbox widget.
2727

2828
## Registered Properties
2929

30-
| Property | Type | Possible values | Description |
31-
|----------|------|-----------------|--------------------------|
32-
| autosize | bool | true or false | Determines if auto-sized |
30+
| Property | Type | Possible values | Description |
31+
|-------------------|--------|-------------------|--------------------------------------------------------------------|
32+
| autosize | bool | true or false | Determines if auto-sized |
33+
| checkbox_position | String | "Left" or "Right" | Place the checkbox itself on either the left or right of its label |
3334

3435
## See also
3536
- [GML Button](help://man/5/GML-Widget-Button)

Userland/Libraries/LibGUI/CheckBox.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ CheckBox::CheckBox(String text)
2525
{
2626
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
2727

28+
REGISTER_ENUM_PROPERTY(
29+
"checkbox_position", checkbox_position, set_checkbox_position, CheckBoxPosition,
30+
{ CheckBoxPosition::Left, "Left" },
31+
{ CheckBoxPosition::Right, "Right" });
32+
2833
set_min_width(32);
2934
set_fixed_height(22);
3035
}
@@ -35,7 +40,8 @@ void CheckBox::paint_event(PaintEvent& event)
3540
painter.add_clip_rect(event.rect());
3641

3742
auto text_rect = rect();
38-
text_rect.set_left(s_box_width + s_horizontal_padding);
43+
if (m_checkbox_position == CheckBoxPosition::Left)
44+
text_rect.set_left(s_box_width + s_horizontal_padding);
3945
text_rect.set_width(font().width(text()));
4046
text_rect.set_top(height() / 2 - font().glyph_height() / 2);
4147
text_rect.set_height(font().glyph_height());
@@ -50,6 +56,8 @@ void CheckBox::paint_event(PaintEvent& event)
5056
0, height() / 2 - s_box_height / 2 - 1,
5157
s_box_width, s_box_height
5258
};
59+
if (m_checkbox_position == CheckBoxPosition::Right)
60+
box_rect.set_right_without_resize(rect().right());
5361

5462
Gfx::StylePainter::paint_check_box(painter, box_rect, palette(), is_enabled(), is_checked(), is_being_pressed());
5563

Userland/Libraries/LibGUI/CheckBox.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class CheckBox : public AbstractButton {
2222
bool is_autosize() const { return m_autosize; }
2323
void set_autosize(bool);
2424

25+
enum class CheckBoxPosition {
26+
Left,
27+
Right,
28+
};
29+
CheckBoxPosition checkbox_position() const { return m_checkbox_position; }
30+
void set_checkbox_position(CheckBoxPosition value) { m_checkbox_position = value; }
31+
2532
private:
2633
explicit CheckBox(String = {});
2734

@@ -34,6 +41,7 @@ class CheckBox : public AbstractButton {
3441
virtual void paint_event(PaintEvent&) override;
3542

3643
bool m_autosize { false };
44+
CheckBoxPosition m_checkbox_position { CheckBoxPosition::Left };
3745
};
3846

3947
}

0 commit comments

Comments
 (0)