mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:42:43 +00:00 
			
		
		
		
	FileManager: Start building a file manager.
This commit is contained in:
		
							parent
							
								
									8ae7be611a
								
							
						
					
					
						commit
						4d5fe39494
					
				
					 14 changed files with 270 additions and 2 deletions
				
			
		
							
								
								
									
										
											BIN
										
									
								
								Base/res/icons/file16.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Base/res/icons/file16.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 1.1 KiB | 
							
								
								
									
										
											BIN
										
									
								
								Base/res/icons/file16.rgb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Base/res/icons/file16.rgb
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Base/res/icons/folder16.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Base/res/icons/folder16.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 1.6 KiB | 
							
								
								
									
										
											BIN
										
									
								
								Base/res/icons/folder16.rgb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Base/res/icons/folder16.rgb
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								Base/res/icons/link16.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Base/res/icons/link16.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 1.4 KiB | 
							
								
								
									
										
											BIN
										
									
								
								Base/res/icons/link16.rgb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								Base/res/icons/link16.rgb
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										3
									
								
								FileManager/.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								FileManager/.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | |||
| *.o | ||||
| *.d | ||||
| FileManager | ||||
							
								
								
									
										126
									
								
								FileManager/DirectoryView.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								FileManager/DirectoryView.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,126 @@ | |||
| #include <dirent.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
| #include <sys/stat.h> | ||||
| #include <unistd.h> | ||||
| #include <SharedGraphics/GraphicsBitmap.h> | ||||
| #include <SharedGraphics/Painter.h> | ||||
| #include <AK/FileSystemPath.h> | ||||
| #include "DirectoryView.h" | ||||
| 
 | ||||
| DirectoryView::DirectoryView(GWidget* parent) | ||||
|     : GWidget(parent) | ||||
| { | ||||
|     m_directory_icon = GraphicsBitmap::load_from_file("/res/icons/folder16.rgb", { 16, 16 }); | ||||
|     m_file_icon = GraphicsBitmap::load_from_file("/res/icons/file16.rgb", { 16, 16 }); | ||||
|     m_symlink_icon = GraphicsBitmap::load_from_file("/res/icons/link16.rgb", { 16, 16 }); | ||||
| } | ||||
| 
 | ||||
| DirectoryView::~DirectoryView() | ||||
| { | ||||
| } | ||||
| 
 | ||||
| void DirectoryView::open(const String& path) | ||||
| { | ||||
|     if (m_path == path) | ||||
|         return; | ||||
|     m_path = path; | ||||
|     reload(); | ||||
|     if (on_path_change) | ||||
|         on_path_change(m_path); | ||||
|     update(); | ||||
| } | ||||
| 
 | ||||
| void DirectoryView::reload() | ||||
| { | ||||
|     DIR* dirp = opendir(m_path.characters()); | ||||
|     if (!dirp) { | ||||
|         perror("opendir"); | ||||
|         exit(1); | ||||
|     } | ||||
|     m_directories.clear(); | ||||
|     m_files.clear(); | ||||
|     while (auto* de = readdir(dirp)) { | ||||
|         Entry entry; | ||||
|         entry.name = de->d_name; | ||||
|         struct stat st; | ||||
|         int rc = lstat(String::format("%s/%s", m_path.characters(), de->d_name).characters(), &st); | ||||
|         if (rc < 0) { | ||||
|             perror("lstat"); | ||||
|             continue; | ||||
|         } | ||||
|         entry.size = st.st_size; | ||||
|         entry.mode = st.st_mode; | ||||
|         auto& entries = S_ISDIR(st.st_mode) ? m_directories : m_files; | ||||
|         entries.append(move(entry)); | ||||
|     } | ||||
|     closedir(dirp); | ||||
| } | ||||
| 
 | ||||
| const GraphicsBitmap& DirectoryView::icon_for(const Entry& entry) const | ||||
| { | ||||
|     if (S_ISDIR(entry.mode)) | ||||
|         return *m_directory_icon; | ||||
|     if (S_ISLNK(entry.mode)) | ||||
|         return *m_symlink_icon; | ||||
|     return *m_file_icon; | ||||
| } | ||||
| 
 | ||||
| static String pretty_byte_size(size_t size) | ||||
| { | ||||
|     return String::format("%u", size); | ||||
| } | ||||
| 
 | ||||
| bool DirectoryView::should_show_size_for(const Entry& entry) const | ||||
| { | ||||
|     return S_ISREG(entry.mode); | ||||
| } | ||||
| 
 | ||||
| Rect DirectoryView::row_rect(int item_index) const | ||||
| { | ||||
|     return { 0, item_index * item_height(), width(), item_height() }; | ||||
| } | ||||
| 
 | ||||
| void DirectoryView::mousedown_event(GMouseEvent& event) | ||||
| { | ||||
|     for (int i = 0; i < item_count(); ++i) { | ||||
|         if (row_rect(i).contains(event.position())) { | ||||
|             auto& entry = this->entry(i); | ||||
|             if (entry.is_directory()) { | ||||
|                 FileSystemPath new_path(String::format("%s/%s", m_path.characters(), entry.name.characters())); | ||||
|                 open(new_path.string()); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void DirectoryView::paint_event(GPaintEvent&) | ||||
| { | ||||
|     Painter painter(*this); | ||||
| 
 | ||||
|     int horizontal_padding = 5; | ||||
|     int icon_size = 16; | ||||
|     int painted_item_index = 0; | ||||
| 
 | ||||
|     auto process_entries = [&] (const Vector<Entry>& entries) { | ||||
|         for (size_t i = 0; i < entries.size(); ++i, ++painted_item_index) { | ||||
|             auto& entry = entries[i]; | ||||
|             int y = painted_item_index * item_height(); | ||||
|             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.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)) | ||||
|                 painter.draw_text(size_rect, pretty_byte_size(entry.size), Painter::TextAlignment::CenterRight, Color::Black); | ||||
|         } | ||||
|     }; | ||||
| 
 | ||||
|     process_entries(m_directories); | ||||
|     process_entries(m_files); | ||||
| 
 | ||||
|     Rect unpainted_rect(0, painted_item_index * item_height(), width(), height()); | ||||
|     unpainted_rect.intersect(rect()); | ||||
|     painter.fill_rect(unpainted_rect, Color::White); | ||||
| } | ||||
							
								
								
									
										50
									
								
								FileManager/DirectoryView.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								FileManager/DirectoryView.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,50 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <LibGUI/GWidget.h> | ||||
| #include <AK/Function.h> | ||||
| #include <sys/stat.h> | ||||
| 
 | ||||
| class DirectoryView final : public GWidget { | ||||
| public: | ||||
|     DirectoryView(GWidget* parent); | ||||
|     virtual ~DirectoryView() override; | ||||
| 
 | ||||
|     void open(const String& path); | ||||
|     void reload(); | ||||
| 
 | ||||
|     Function<void(const String&)> on_path_change; | ||||
| 
 | ||||
|     int item_height() const { return 16; } | ||||
|     int item_count() const { return m_directories.size() + m_files.size(); } | ||||
| 
 | ||||
| private: | ||||
|     virtual void paint_event(GPaintEvent&) override; | ||||
|     virtual void mousedown_event(GMouseEvent&) override; | ||||
| 
 | ||||
|     Rect row_rect(int item_index) const; | ||||
| 
 | ||||
|     struct Entry { | ||||
|         String name; | ||||
|         size_t size { 0 }; | ||||
|         mode_t mode { 0 }; | ||||
| 
 | ||||
|         bool is_directory() const { return S_ISDIR(mode); } | ||||
|     }; | ||||
| 
 | ||||
|     const Entry& entry(size_t index) const | ||||
|     { | ||||
|         if (index < m_directories.size()) | ||||
|             return m_directories[index]; | ||||
|         return m_files[index - m_directories.size()]; | ||||
|     } | ||||
|     const GraphicsBitmap& icon_for(const Entry&) const; | ||||
|     bool should_show_size_for(const Entry&) const; | ||||
| 
 | ||||
|     Vector<Entry> m_files; | ||||
|     Vector<Entry> m_directories; | ||||
| 
 | ||||
|     String m_path; | ||||
|     RetainPtr<GraphicsBitmap> m_directory_icon; | ||||
|     RetainPtr<GraphicsBitmap> m_file_icon; | ||||
|     RetainPtr<GraphicsBitmap> m_symlink_icon; | ||||
| }; | ||||
							
								
								
									
										35
									
								
								FileManager/Makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								FileManager/Makefile
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | |||
| OBJS = \
 | ||||
|     DirectoryView.o \
 | ||||
|     main.o | ||||
| 
 | ||||
| APP = FileManager | ||||
| 
 | ||||
| ARCH_FLAGS = | ||||
| STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib -nostdinc | ||||
| USERLAND_FLAGS = -ffreestanding -fno-stack-protector -fno-ident | ||||
| WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings | ||||
| FLAVOR_FLAGS = -march=i386 -m32 -fno-exceptions -fno-rtti -fmerge-all-constants -fno-unroll-loops -fno-pie -fno-pic | ||||
| OPTIMIZATION_FLAGS = -Oz -fno-asynchronous-unwind-tables | ||||
| INCLUDE_FLAGS = -I.. -I. -I../LibC | ||||
| 
 | ||||
| DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND | ||||
| 
 | ||||
| CXXFLAGS = -MMD -MP $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(USERLAND_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES) | ||||
| CXX = clang | ||||
| LD = ld | ||||
| AR = ar | ||||
| 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 | ||||
| 
 | ||||
| .cpp.o: | ||||
| 	@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $< | ||||
| 
 | ||||
| -include $(OBJS:%.o=%.d) | ||||
| 
 | ||||
| clean: | ||||
| 	@echo "CLEAN"; rm -f $(APPS) $(OBJS) *.d | ||||
| 
 | ||||
							
								
								
									
										47
									
								
								FileManager/main.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								FileManager/main.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,47 @@ | |||
| #include <SharedGraphics/GraphicsBitmap.h> | ||||
| #include <LibGUI/GWindow.h> | ||||
| #include <LibGUI/GWidget.h> | ||||
| #include <LibGUI/GButton.h> | ||||
| #include <LibGUI/GListBox.h> | ||||
| #include <LibGUI/GEventLoop.h> | ||||
| #include <sys/wait.h> | ||||
| #include <signal.h> | ||||
| #include <unistd.h> | ||||
| #include <stdio.h> | ||||
| #include <errno.h> | ||||
| #include "DirectoryView.h" | ||||
| 
 | ||||
| static GWindow* make_window(); | ||||
| 
 | ||||
| int main(int, char**) | ||||
| { | ||||
|     GEventLoop loop; | ||||
| 
 | ||||
|     auto* window = make_window(); | ||||
|     window->set_should_exit_app_on_close(true); | ||||
|     window->show(); | ||||
| 
 | ||||
|     return loop.exec(); | ||||
| } | ||||
| 
 | ||||
| GWindow* make_window() | ||||
| { | ||||
|     auto* window = new GWindow; | ||||
|     window->set_title("FileManager"); | ||||
|     window->set_rect(20, 200, 240, 300); | ||||
| 
 | ||||
|     auto* widget = new GWidget; | ||||
|     window->set_main_widget(widget); | ||||
| 
 | ||||
|     auto* directory_view = new DirectoryView(widget); | ||||
|     directory_view->set_relative_rect({ 0, 0, 240, 300 }); | ||||
| 
 | ||||
|     directory_view->on_path_change = [window] (const String& new_path) { | ||||
|         window->set_title(String::format("FileManager: %s", new_path.characters())); | ||||
|     }; | ||||
| 
 | ||||
|     directory_view->open("/"); | ||||
| 
 | ||||
|     return window; | ||||
| } | ||||
| 
 | ||||
|  | @ -29,6 +29,7 @@ | |||
| #define SPAWN_LAUNCHER | ||||
| //#define SPAWN_GUITEST2
 | ||||
| #define SPAWN_CLOCK | ||||
| #define SPAWN_FILE_MANAGER | ||||
| //#define SPAWN_FONTEDITOR
 | ||||
| //#define SPAWN_MULTIPLE_SHELLS
 | ||||
| //#define STRESS_TEST_SPAWNING
 | ||||
|  | @ -120,6 +121,9 @@ static void init_stage2() | |||
| #ifdef SPAWN_CLOCK | ||||
|     Process::create_user_process("/bin/Clock", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, move(environment), tty0); | ||||
| #endif | ||||
| #ifdef SPAWN_FILE_MANAGER | ||||
|     Process::create_user_process("/bin/FileManager", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, move(environment), tty0); | ||||
| #endif | ||||
| #ifdef SPAWN_FONTEDITOR | ||||
|     Process::create_user_process("/bin/FontEditor", (uid_t)100, (gid_t)100, (pid_t)0, error, { }, move(environment), tty0); | ||||
| #endif | ||||
|  |  | |||
|  | @ -16,6 +16,8 @@ make -C ../Clock clean && \ | |||
| make -C ../Clock && \ | ||||
| make -C ../Launcher clean && \ | ||||
| make -C ../Launcher && \ | ||||
| make -C ../FileManager clean && \ | ||||
| make -C ../FileManager && \ | ||||
| make clean &&\ | ||||
| make && \ | ||||
| sudo ./sync.sh | ||||
|  |  | |||
|  | @ -8,6 +8,8 @@ mount -o loop _fs_contents mnt/ | |||
| mkdir -vp mnt/bin | ||||
| mkdir -vp mnt/etc | ||||
| mkdir -vp mnt/proc | ||||
| mkdir -vp mnt/tmp | ||||
| chmod 1777 mnt/tmp | ||||
| mkdir -vp mnt/dev | ||||
| mkdir -vp mnt/dev/pts | ||||
| mknod mnt/dev/tty0 c 4 0 | ||||
|  | @ -54,13 +56,12 @@ cp -v ../Terminal/Terminal mnt/bin/Terminal | |||
| cp -v ../FontEditor/FontEditor mnt/bin/FontEditor | ||||
| cp -v ../Launcher/Launcher mnt/bin/Launcher | ||||
| cp -v ../Clock/Clock mnt/bin/Clock | ||||
| cp -v ../FileManager/FileManager mnt/bin/FileManager | ||||
| cp -v ../Userland/pape mnt/bin/pape | ||||
| cp -v ../Userland/dmesg mnt/bin/dmesg | ||||
| cp -v ../Userland/chmod mnt/bin/chmod | ||||
| cp -v ../Userland/top mnt/bin/top | ||||
| cp -v kernel.map mnt/ | ||||
| ln -s dir_a mnt/dir_cur | ||||
| ln -s nowhere mnt/bad_link | ||||
| sh sync-local.sh | ||||
| umount mnt | ||||
| sync | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling