LibGUI: Remove Label icons and replace instances with ImageWidget

These icons are a relic of GLabel and were never implemented to
accomodate both image and text. This convenience can always be added
in the future, but no current instance assumes or needs it, so let's
replace them all with ImageWidget to show clearer intent.
This commit is contained in:
thankyouverycool 2023-04-29 10:42:04 -04:00 committed by Andreas Kling
parent 59483b0654
commit 4c9933bfb7
16 changed files with 69 additions and 102 deletions

View file

@ -17,10 +17,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/home.png"
bitmap: "/res/icons/32x32/home.png"
}
@GUI::Widget {
@ -76,10 +76,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/color-chooser.png"
bitmap: "/res/icons/32x32/color-chooser.png"
}
@GUI::Label {
@ -122,10 +122,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/search-engine.png"
bitmap: "/res/icons/32x32/search-engine.png"
}
@GUI::CheckBox {
@ -191,10 +191,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/downloads.png"
bitmap: "/res/icons/32x32/downloads.png"
}
@GUI::CheckBox {

View file

@ -16,10 +16,10 @@
fixed_width: 32
layout: @GUI::VerticalBoxLayout {}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/app-keyboard-mapper.png"
bitmap: "/res/icons/32x32/app-keyboard-mapper.png"
}
}
@ -69,10 +69,10 @@
fixed_width: 32
layout: @GUI::VerticalBoxLayout {}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/app-keyboard-settings.png"
bitmap: "/res/icons/32x32/app-keyboard-settings.png"
}
}
@ -113,10 +113,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/app-calculator.png"
bitmap: "/res/icons/32x32/app-calculator.png"
}
@GUI::CheckBox {

View file

@ -17,10 +17,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/graphics/mail-server-settings.png"
bitmap: "/res/graphics/mail-server-settings.png"
}
@GUI::Label {
@ -100,10 +100,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/graphics/mail-user-settings.png"
bitmap: "/res/graphics/mail-user-settings.png"
}
@GUI::Label {

View file

@ -17,10 +17,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/graphics/mouse-cursor-speed.png"
bitmap: "/res/graphics/mouse-cursor-speed.png"
}
@GUI::Label {
@ -65,10 +65,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/graphics/scroll-wheel-step-size.png"
bitmap: "/res/graphics/scroll-wheel-step-size.png"
}
@GUI::Label {
@ -175,10 +175,10 @@
spacing: 16
}
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
name: "switch_buttons_image_label"
name: "switch_buttons_image"
}
@GUI::CheckBox {

View file

@ -8,6 +8,7 @@
#include <Applications/MouseSettings/MouseWidgetGML.h>
#include <LibGUI/ConnectionToWindowServer.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Label.h>
#include <LibGUI/Slider.h>
#include <LibGUI/SpinBox.h>
@ -51,7 +52,7 @@ MouseWidget::MouseWidget()
set_modified(true);
};
m_switch_buttons_image_label = *find_descendant_of_type_named<GUI::Label>("switch_buttons_image_label");
m_switch_buttons_image = *find_descendant_of_type_named<GUI::ImageWidget>("switch_buttons_image");
m_switch_buttons_checkbox = *find_descendant_of_type_named<GUI::CheckBox>("switch_buttons_checkbox");
m_switch_buttons_checkbox->set_checked(GUI::ConnectionToWindowServer::the().are_mouse_buttons_switched(), GUI::AllowCallback::No);
m_switch_buttons_checkbox->on_checked = [&](auto) {
@ -103,8 +104,8 @@ void MouseWidget::update_double_click_speed_label()
void MouseWidget::update_switch_buttons_image_label()
{
if (m_switch_buttons_checkbox->is_checked()) {
m_switch_buttons_image_label->set_icon_from_path("/res/graphics/mouse-button-right.png"sv);
m_switch_buttons_image->load_from_file("/res/graphics/mouse-button-right.png"sv);
} else {
m_switch_buttons_image_label->set_icon_from_path("/res/graphics/mouse-button-left.png"sv);
m_switch_buttons_image->load_from_file("/res/graphics/mouse-button-left.png"sv);
}
}

View file

@ -32,7 +32,7 @@ private:
RefPtr<GUI::HorizontalSlider> m_double_click_speed_slider;
RefPtr<GUI::Label> m_double_click_speed_label;
RefPtr<GUI::CheckBox> m_switch_buttons_checkbox;
RefPtr<GUI::Label> m_switch_buttons_image_label;
RefPtr<GUI::ImageWidget> m_switch_buttons_image;
RefPtr<GUI::CheckBox> m_natural_scroll_checkbox;
RefPtr<MouseSettings::DoubleClickArrowWidget> m_double_click_arrow_widget;
};

View file

@ -11,10 +11,10 @@
}
fixed_height: 40
@GUI::Label {
@GUI::ImageWidget {
fixed_width: 32
fixed_height: 32
icon: "/res/icons/32x32/network.png"
bitmap: "/res/icons/32x32/network.png"
}
@GUI::Label {

View file

@ -11,8 +11,8 @@
spacing: 8
}
@GUI::Label {
name: "icon_label"
@GUI::ImageWidget {
name: "process_icon"
fixed_size: [32, 32]
}

View file

@ -31,6 +31,7 @@
#include <LibGUI/FileIconProvider.h>
#include <LibGUI/GroupBox.h>
#include <LibGUI/Icon.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/JsonArrayModel.h>
#include <LibGUI/Label.h>
#include <LibGUI/LazyWidget.h>
@ -539,7 +540,7 @@ ErrorOr<NonnullRefPtr<GUI::Window>> build_process_window(pid_t pid)
VERIFY(process_index.is_valid());
if (auto icon_data = process_index.sibling_at_column(ProcessModel::Column::Icon).data(); icon_data.is_icon()) {
main_widget->find_descendant_of_type_named<GUI::Label>("icon_label")->set_icon(icon_data.as_icon().bitmap_for_size(32));
main_widget->find_descendant_of_type_named<GUI::ImageWidget>("process_icon")->set_bitmap(icon_data.as_icon().bitmap_for_size(32));
}
main_widget->find_descendant_of_type_named<GUI::Label>("process_name")->set_text(String::formatted("{} (PID {})", process_index.sibling_at_column(ProcessModel::Column::Name).data().to_deprecated_string(), pid).release_value_but_fixme_should_propagate_errors());

View file

@ -34,10 +34,10 @@
fixed_width: 60
layout: @GUI::VerticalBoxLayout {}
@GUI::Label {
name: "light_bulb_label"
@GUI::ImageWidget {
fixed_height: 60
icon: "/res/icons/32x32/app-welcome.png"
auto_resize: false
bitmap: "/res/icons/32x32/app-welcome.png"
}
}

View file

@ -15,6 +15,7 @@
#include <LibConfig/Client.h>
#include <LibGUI/Application.h>
#include <LibGUI/Button.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Label.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Palette.h>
@ -46,8 +47,8 @@ private:
}
};
class SquareLabel final : public GUI::Label {
C_OBJECT(SquareLabel);
class SquareImage final : public GUI::ImageWidget {
C_OBJECT(SquareImage);
public:
Function<void()> on_chord_click;
@ -68,7 +69,7 @@ public:
}
});
}
GUI::Label::mousedown_event(event);
GUI::ImageWidget::mousedown_event(event);
}
virtual void mousemove_event(GUI::MouseEvent& event) override
@ -80,7 +81,7 @@ public:
m_square.field->set_chord_preview(m_square, false);
}
}
GUI::Label::mousemove_event(event);
GUI::ImageWidget::mousemove_event(event);
}
virtual void mouseup_event(GUI::MouseEvent& event) override
@ -95,11 +96,11 @@ public:
}
}
m_square.field->set_chord_preview(m_square, m_chord);
GUI::Label::mouseup_event(event);
GUI::ImageWidget::mouseup_event(event);
}
private:
explicit SquareLabel(Square& square)
explicit SquareImage(Square& square)
: m_square(square)
{
}
@ -225,7 +226,7 @@ void Field::reset()
for (int i = rows() * columns(); i < static_cast<int>(m_squares.size()); ++i) {
auto& square = m_squares[i];
square->button->set_visible(false);
square->label->set_visible(false);
square->image->set_visible(false);
}
size_t i = 0;
@ -242,15 +243,15 @@ void Field::reset()
square.has_flag = false;
square.is_considering = false;
square.is_swept = false;
if (!square.label) {
square.label = add<SquareLabel>(square);
square.label->set_palette(m_mine_palette);
square.label->set_background_role(Gfx::ColorRole::Base);
if (!square.image) {
square.image = add<SquareImage>(square);
square.image->set_palette(m_mine_palette);
square.image->set_background_role(Gfx::ColorRole::Base);
}
square.label->set_fill_with_background_color(false);
square.label->set_relative_rect(rect);
square.label->set_visible(false);
square.label->set_icon(nullptr);
square.image->set_fill_with_background_color(false);
square.image->set_relative_rect(rect);
square.image->set_visible(false);
square.image->set_bitmap(nullptr);
if (!square.button) {
square.button = add<SquareButton>();
square.button->on_click = [this, &square](auto) {
@ -262,7 +263,7 @@ void Field::reset()
square.button->on_middle_click = [this, &square] {
on_square_middle_clicked(square);
};
square.label->on_chord_click = [this, &square] {
square.image->on_chord_click = [this, &square] {
on_square_chorded(square);
};
}
@ -325,9 +326,9 @@ void Field::generate_field(size_t start_row, size_t start_column)
});
square.number = number;
if (square.has_mine) {
square.label->set_icon(m_mine_bitmap);
square.image->set_bitmap(m_mine_bitmap);
} else if (square.number) {
square.label->set_icon(m_number_bitmap[square.number - 1]);
square.image->set_bitmap(m_number_bitmap[square.number - 1]);
}
}
}
@ -395,9 +396,9 @@ void Field::on_square_clicked_impl(Square& square, bool should_flood_fill)
update();
square.is_swept = true;
square.button->set_visible(false);
square.label->set_visible(true);
square.image->set_visible(true);
if (square.has_mine) {
square.label->set_fill_with_background_color(true);
square.image->set_fill_with_background_color(true);
game_over();
return;
}
@ -507,12 +508,12 @@ void Field::reveal_mines()
auto& square = this->square(r, c);
if (square.has_mine && !square.has_flag) {
square.button->set_visible(false);
square.label->set_visible(true);
square.image->set_visible(true);
}
if (!square.has_mine && square.has_flag) {
square.button->set_icon(*m_badflag_bitmap);
square.button->set_visible(true);
square.label->set_visible(false);
square.image->set_visible(false);
}
}
}

View file

@ -14,7 +14,7 @@
class Field;
class SquareButton;
class SquareLabel;
class SquareImage;
class Square {
AK_MAKE_NONCOPYABLE(Square);
@ -32,7 +32,7 @@ public:
size_t column { 0 };
size_t number { 0 };
RefPtr<SquareButton> button;
RefPtr<SquareLabel> label;
RefPtr<SquareImage> image;
template<typename Callback>
void for_each_neighbor(Callback);
@ -41,7 +41,7 @@ public:
class Field final : public GUI::Frame {
C_OBJECT(Field)
friend class Square;
friend class SquareLabel;
friend class SquareImage;
public:
static ErrorOr<NonnullRefPtr<Field>> create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button);

View file

@ -33,7 +33,6 @@ Label::Label(String text)
REGISTER_STRING_PROPERTY("text", text, set_text);
REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
REGISTER_WRITE_ONLY_STRING_PROPERTY("icon", set_icon_from_path);
}
void Label::set_autosize(bool autosize, size_t padding)
@ -46,24 +45,6 @@ void Label::set_autosize(bool autosize, size_t padding)
size_to_fit();
}
void Label::set_icon(Gfx::Bitmap const* icon)
{
if (m_icon == icon)
return;
m_icon = icon;
update();
}
void Label::set_icon_from_path(StringView path)
{
auto maybe_bitmap = Gfx::Bitmap::load_from_file(path);
if (maybe_bitmap.is_error()) {
dbgln("Unable to load bitmap `{}` for label icon", path);
return;
}
set_icon(maybe_bitmap.release_value());
}
void Label::set_text(String text)
{
if (text == m_text)
@ -88,15 +69,6 @@ void Label::paint_event(PaintEvent& event)
Painter painter(*this);
painter.add_clip_rect(event.rect());
if (m_icon) {
if (m_should_stretch_icon) {
painter.draw_scaled_bitmap(frame_inner_rect(), *m_icon, m_icon->rect());
} else {
auto icon_location = frame_inner_rect().center().translated(-(m_icon->width() / 2), -(m_icon->height() / 2));
painter.blit(icon_location, *m_icon, m_icon->rect());
}
}
if (text().is_empty())
return;

View file

@ -22,19 +22,12 @@ public:
String const& text() const { return m_text; }
void set_text(String);
void set_icon(Gfx::Bitmap const*);
void set_icon_from_path(StringView);
Gfx::Bitmap const* icon() const { return m_icon.ptr(); }
Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
void set_text_alignment(Gfx::TextAlignment text_alignment) { m_text_alignment = text_alignment; }
Gfx::TextWrapping text_wrapping() const { return m_text_wrapping; }
void set_text_wrapping(Gfx::TextWrapping text_wrapping) { m_text_wrapping = text_wrapping; }
bool should_stretch_icon() const { return m_should_stretch_icon; }
void set_should_stretch_icon(bool b) { m_should_stretch_icon = b; }
bool is_autosize() const { return m_autosize; }
void set_autosize(bool, size_t padding = 0);
@ -56,10 +49,8 @@ private:
void size_to_fit();
String m_text;
RefPtr<Gfx::Bitmap const> m_icon;
Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::Center };
Gfx::TextWrapping m_text_wrapping { Gfx::TextWrapping::Wrap };
bool m_should_stretch_icon { false };
bool m_autosize { false };
size_t m_autosize_padding { 0 };
};

View file

@ -6,6 +6,7 @@
*/
#include <LibGUI/Button.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Label.h>
#include <LibGUI/PasswordInputDialog.h>
#include <LibGUI/PasswordInputDialogGML.h>
@ -25,9 +26,9 @@ PasswordInputDialog::PasswordInputDialog(Window* parent_window, DeprecatedString
auto widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
widget->load_from_gml(password_input_dialog_gml).release_value_but_fixme_should_propagate_errors();
auto& key_icon_label = *widget->find_descendant_of_type_named<GUI::Label>("key_icon_label");
auto& key_icon = *widget->find_descendant_of_type_named<GUI::ImageWidget>("key_icon");
key_icon_label.set_icon(Gfx::Bitmap::load_from_file("/res/icons/32x32/key.png"sv).release_value_but_fixme_should_propagate_errors());
key_icon.set_bitmap(Gfx::Bitmap::load_from_file("/res/icons/32x32/key.png"sv).release_value_but_fixme_should_propagate_errors());
auto& server_label = *widget->find_descendant_of_type_named<GUI::Label>("server_label");
server_label.set_text(String::from_deprecated_string(server).release_value_but_fixme_should_propagate_errors());

View file

@ -9,8 +9,8 @@
shrink_to_fit: true
layout: @GUI::VerticalBoxLayout {}
@GUI::Label {
name: "key_icon_label"
@GUI::ImageWidget {
name: "key_icon"
fixed_height: 32
fixed_width: 32
}