1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:17:34 +00:00

LibGUI: Start adding an automatic widget layout system.

My needs are really quite simple, so I'm just going to add what I need
as I go along. The first thing I needed was a simple box layout with
widgets being able to say whether they prefer fixed or fill for both
their vertical and horizontal sizes.

I also made a simple GStatusBar so FileManager can show how many bytes
worth of files are in the current directory.
This commit is contained in:
Andreas Kling 2019-02-10 11:07:13 +01:00
parent 2cf1dd5b6f
commit 2def3d8d3f
22 changed files with 411 additions and 29 deletions

View file

@ -53,6 +53,8 @@ void DirectoryView::reload()
}
m_directories.clear();
m_files.clear();
size_t bytes_in_files = 0;
while (auto* de = readdir(dirp)) {
Entry entry;
entry.name = de->d_name;
@ -66,10 +68,21 @@ void DirectoryView::reload()
entry.mode = st.st_mode;
auto& entries = S_ISDIR(st.st_mode) ? m_directories : m_files;
entries.append(move(entry));
if (S_ISREG(entry.mode))
bytes_in_files += st.st_size;
}
closedir(dirp);
int excess_height = max(0, (item_count() * item_height()) - height());
m_scrollbar->set_range(0, excess_height);
set_status_message(String::format("%d item%s (%u byte%s)",
item_count(),
item_count() != 1 ? "s" : "",
bytes_in_files,
bytes_in_files != 1 ? "s" : ""));
}
const GraphicsBitmap& DirectoryView::icon_for(const Entry& entry) const
@ -128,7 +141,7 @@ void DirectoryView::paint_event(GPaintEvent&)
Rect icon_rect(horizontal_padding, y, icon_size, item_height());
Rect name_rect(icon_rect.right() + horizontal_padding, y, 100, item_height());
Rect size_rect(name_rect.right() + horizontal_padding, y, 64, item_height());
painter.fill_rect(row_rect(painted_item_index), i % 2 ? Color::LightGray : Color::White);
painter.fill_rect(row_rect(painted_item_index), i % 2 ? Color(210, 210, 210) : Color::White);
painter.blit_with_alpha(icon_rect.location(), icon_for(entry), { 0, 0, icon_size, icon_size });
painter.draw_text(name_rect, entry.name, Painter::TextAlignment::CenterLeft, Color::Black);
if (should_show_size_for(entry))
@ -143,3 +156,9 @@ void DirectoryView::paint_event(GPaintEvent&)
unpainted_rect.intersect(rect());
painter.fill_rect(unpainted_rect, Color::White);
}
void DirectoryView::set_status_message(String&& message)
{
if (on_status_message)
on_status_message(move(message));
}

View file

@ -15,6 +15,7 @@ public:
void reload();
Function<void(const String&)> on_path_change;
Function<void(String)> on_status_message;
int item_height() const { return 16; }
int item_count() const { return m_directories.size() + m_files.size(); }
@ -24,6 +25,8 @@ private:
virtual void resize_event(GResizeEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;
void set_status_message(String&&);
Rect row_rect(int item_index) const;
struct Entry {

View file

@ -23,7 +23,7 @@ LDFLAGS = -static --strip-debug -melf_i386 -e _start --gc-sections
all: $(APP)
$(APP): $(OBJS)
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) ../LibGUI/LibGUI.a ../LibC/LibC.a
$(LD) -o $(APP) $(LDFLAGS) $(OBJS) ../../LibGUI/LibGUI.a ../../LibC/LibC.a
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

View file

@ -1,14 +1,10 @@
#include <SharedGraphics/GraphicsBitmap.h>
#include <LibGUI/GWindow.h>
#include <LibGUI/GWidget.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GListBox.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GEventLoop.h>
#include <sys/wait.h>
#include <signal.h>
#include <LibGUI/GStatusBar.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "DirectoryView.h"
static GWindow* make_window();
@ -33,13 +29,21 @@ GWindow* make_window()
auto* widget = new GWidget;
window->set_main_widget(widget);
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
auto* directory_view = new DirectoryView(widget);
directory_view->set_relative_rect({ 0, 0, 240, 300 });
auto* statusbar = new GStatusBar(widget);
statusbar->set_text("Welcome!");
directory_view->on_path_change = [window] (const String& new_path) {
window->set_title(String::format("FileManager: %s", new_path.characters()));
};
directory_view->on_status_message = [statusbar] (String message) {
statusbar->set_text(move(message));
};
directory_view->open("/");
return window;