mirror of
https://github.com/RGBCube/serenity
synced 2025-05-23 21:55:08 +00:00
FileManager: Move "create new directory" action into DirectoryView
This makes it available in both desktop and windowed mode. :^)
This commit is contained in:
parent
0ffd319bda
commit
be48f58d41
3 changed files with 42 additions and 27 deletions
|
@ -25,9 +25,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "DirectoryView.h"
|
#include "DirectoryView.h"
|
||||||
|
#include <AK/LexicalPath.h>
|
||||||
#include <AK/NumberFormat.h>
|
#include <AK/NumberFormat.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/StandardPaths.h>
|
#include <LibCore/StandardPaths.h>
|
||||||
|
#include <LibGUI/InputBox.h>
|
||||||
#include <LibGUI/MessageBox.h>
|
#include <LibGUI/MessageBox.h>
|
||||||
#include <LibGUI/SortingProxyModel.h>
|
#include <LibGUI/SortingProxyModel.h>
|
||||||
#include <serenity.h>
|
#include <serenity.h>
|
||||||
|
@ -127,6 +129,8 @@ DirectoryView::DirectoryView(Mode mode)
|
||||||
set_active_widget(nullptr);
|
set_active_widget(nullptr);
|
||||||
set_content_margins({ 2, 2, 2, 2 });
|
set_content_margins({ 2, 2, 2, 2 });
|
||||||
|
|
||||||
|
setup_actions();
|
||||||
|
|
||||||
setup_model();
|
setup_model();
|
||||||
|
|
||||||
setup_icon_view();
|
setup_icon_view();
|
||||||
|
@ -161,8 +165,12 @@ void DirectoryView::setup_model()
|
||||||
|
|
||||||
add_path_to_history(model().root_path());
|
add_path_to_history(model().root_path());
|
||||||
|
|
||||||
|
bool can_write_in_path = access(model().root_path().characters(), W_OK) == 0;
|
||||||
|
|
||||||
|
m_mkdir_action->set_enabled(can_write_in_path);
|
||||||
|
|
||||||
if (on_path_change)
|
if (on_path_change)
|
||||||
on_path_change(model().root_path());
|
on_path_change(model().root_path(), can_write_in_path);
|
||||||
};
|
};
|
||||||
|
|
||||||
m_model->register_client(*this);
|
m_model->register_client(*this);
|
||||||
|
@ -429,3 +437,21 @@ Vector<String> DirectoryView::selected_file_paths() const
|
||||||
});
|
});
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DirectoryView::setup_actions()
|
||||||
|
{
|
||||||
|
m_mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
|
||||||
|
String value;
|
||||||
|
if (GUI::InputBox::show(value, window(), "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) {
|
||||||
|
auto new_dir_path = LexicalPath::canonicalized_path(
|
||||||
|
String::format("%s/%s",
|
||||||
|
path().characters(),
|
||||||
|
value.characters()));
|
||||||
|
int rc = mkdir(new_dir_path.characters(), 0777);
|
||||||
|
if (rc < 0) {
|
||||||
|
auto saved_errno = errno;
|
||||||
|
GUI::MessageBox::show(window(), String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(saved_errno)), "Error", GUI::MessageBox::Type::Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ public:
|
||||||
|
|
||||||
void launch(const AK::URL&, const LauncherHandler&);
|
void launch(const AK::URL&, const LauncherHandler&);
|
||||||
|
|
||||||
Function<void(const StringView&)> on_path_change;
|
Function<void(const StringView& path, bool can_write_in_path)> on_path_change;
|
||||||
Function<void(GUI::AbstractView&)> on_selection_change;
|
Function<void(GUI::AbstractView&)> on_selection_change;
|
||||||
Function<void(const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request;
|
Function<void(const GUI::ModelIndex&, const GUI::ContextMenuEvent&)> on_context_menu_request;
|
||||||
Function<void(const GUI::ModelIndex&, const GUI::DropEvent&)> on_drop;
|
Function<void(const GUI::ModelIndex&, const GUI::DropEvent&)> on_drop;
|
||||||
|
@ -134,6 +134,8 @@ public:
|
||||||
|
|
||||||
Vector<String> selected_file_paths() const;
|
Vector<String> selected_file_paths() const;
|
||||||
|
|
||||||
|
GUI::Action& mkdir_action() { return *m_mkdir_action; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit DirectoryView(Mode);
|
explicit DirectoryView(Mode);
|
||||||
const GUI::FileSystemModel& model() const { return *m_model; }
|
const GUI::FileSystemModel& model() const { return *m_model; }
|
||||||
|
@ -141,6 +143,7 @@ private:
|
||||||
// ^GUI::ModelClient
|
// ^GUI::ModelClient
|
||||||
virtual void model_did_update(unsigned) override;
|
virtual void model_did_update(unsigned) override;
|
||||||
|
|
||||||
|
void setup_actions();
|
||||||
void setup_model();
|
void setup_model();
|
||||||
void setup_icon_view();
|
void setup_icon_view();
|
||||||
void setup_columns_view();
|
void setup_columns_view();
|
||||||
|
@ -164,4 +167,6 @@ private:
|
||||||
RefPtr<GUI::TableView> m_table_view;
|
RefPtr<GUI::TableView> m_table_view;
|
||||||
RefPtr<GUI::IconView> m_icon_view;
|
RefPtr<GUI::IconView> m_icon_view;
|
||||||
RefPtr<GUI::ColumnsView> m_columns_view;
|
RefPtr<GUI::ColumnsView> m_columns_view;
|
||||||
|
|
||||||
|
RefPtr<GUI::Action> m_mkdir_action;
|
||||||
};
|
};
|
||||||
|
|
|
@ -175,12 +175,14 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config)
|
||||||
Desktop::Launcher::open(URL::create_with_file_protocol("/bin/DisplaySettings"));
|
Desktop::Launcher::open(URL::create_with_file_protocol("/bin/DisplaySettings"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
desktop_view_context_menu->add_action(directory_view.mkdir_action());
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
desktop_view_context_menu->add_action(mkdir_action);
|
|
||||||
desktop_view_context_menu->add_action(touch_action);
|
desktop_view_context_menu->add_action(touch_action);
|
||||||
desktop_view_context_menu->add_separator();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
desktop_view_context_menu->add_separator();
|
||||||
|
|
||||||
desktop_view_context_menu->add_action(file_manager_action);
|
desktop_view_context_menu->add_action(file_manager_action);
|
||||||
desktop_view_context_menu->add_separator();
|
desktop_view_context_menu->add_separator();
|
||||||
desktop_view_context_menu->add_action(display_properties_action);
|
desktop_view_context_menu->add_action(display_properties_action);
|
||||||
|
@ -292,22 +294,6 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
directory_view.open_parent_directory();
|
directory_view.open_parent_directory();
|
||||||
});
|
});
|
||||||
|
|
||||||
auto mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
|
|
||||||
String value;
|
|
||||||
if (GUI::InputBox::show(value, window, "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) {
|
|
||||||
auto new_dir_path = LexicalPath::canonicalized_path(
|
|
||||||
String::format("%s/%s",
|
|
||||||
directory_view.path().characters(),
|
|
||||||
value.characters()));
|
|
||||||
int rc = mkdir(new_dir_path.characters(), 0777);
|
|
||||||
if (rc < 0) {
|
|
||||||
GUI::MessageBox::show(window, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
|
|
||||||
} else {
|
|
||||||
refresh_tree_view();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
auto touch_action = GUI::Action::create("New file...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
|
auto touch_action = GUI::Action::create("New file...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
|
||||||
String value;
|
String value;
|
||||||
if (GUI::InputBox::show(value, window, "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) {
|
if (GUI::InputBox::show(value, window, "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) {
|
||||||
|
@ -600,7 +586,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
auto menubar = GUI::MenuBar::construct();
|
auto menubar = GUI::MenuBar::construct();
|
||||||
|
|
||||||
auto& app_menu = menubar->add_menu("File Manager");
|
auto& app_menu = menubar->add_menu("File Manager");
|
||||||
app_menu.add_action(mkdir_action);
|
app_menu.add_action(directory_view.mkdir_action());
|
||||||
app_menu.add_action(touch_action);
|
app_menu.add_action(touch_action);
|
||||||
app_menu.add_action(copy_action);
|
app_menu.add_action(copy_action);
|
||||||
app_menu.add_action(paste_action);
|
app_menu.add_action(paste_action);
|
||||||
|
@ -646,7 +632,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
main_toolbar.add_action(go_home_action);
|
main_toolbar.add_action(go_home_action);
|
||||||
|
|
||||||
main_toolbar.add_separator();
|
main_toolbar.add_separator();
|
||||||
main_toolbar.add_action(mkdir_action);
|
main_toolbar.add_action(directory_view.mkdir_action());
|
||||||
main_toolbar.add_action(touch_action);
|
main_toolbar.add_action(touch_action);
|
||||||
main_toolbar.add_action(copy_action);
|
main_toolbar.add_action(copy_action);
|
||||||
main_toolbar.add_action(paste_action);
|
main_toolbar.add_action(paste_action);
|
||||||
|
@ -658,7 +644,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
main_toolbar.add_action(*view_as_table_action);
|
main_toolbar.add_action(*view_as_table_action);
|
||||||
main_toolbar.add_action(*view_as_columns_action);
|
main_toolbar.add_action(*view_as_columns_action);
|
||||||
|
|
||||||
directory_view.on_path_change = [&](const String& new_path) {
|
directory_view.on_path_change = [&](const String& new_path, bool can_write_in_path) {
|
||||||
const Gfx::Bitmap* icon = nullptr;
|
const Gfx::Bitmap* icon = nullptr;
|
||||||
if (new_path == Core::StandardPaths::home_directory())
|
if (new_path == Core::StandardPaths::home_directory())
|
||||||
icon = &home_directory_icon();
|
icon = &home_directory_icon();
|
||||||
|
@ -683,8 +669,6 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto can_write_in_path = access(new_path.characters(), W_OK) == 0;
|
|
||||||
mkdir_action->set_enabled(can_write_in_path);
|
|
||||||
touch_action->set_enabled(can_write_in_path);
|
touch_action->set_enabled(can_write_in_path);
|
||||||
paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list");
|
paste_action->set_enabled(can_write_in_path && GUI::Clipboard::the().type() == "text/uri-list");
|
||||||
go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1);
|
go_forward_action->set_enabled(directory_view.path_history_position() < directory_view.path_history_size() - 1);
|
||||||
|
@ -728,7 +712,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
directory_context_menu->add_separator();
|
directory_context_menu->add_separator();
|
||||||
directory_context_menu->add_action(properties_action);
|
directory_context_menu->add_action(properties_action);
|
||||||
|
|
||||||
directory_view_context_menu->add_action(mkdir_action);
|
directory_view_context_menu->add_action(directory_view.mkdir_action());
|
||||||
directory_view_context_menu->add_action(touch_action);
|
directory_view_context_menu->add_action(touch_action);
|
||||||
directory_view_context_menu->add_action(paste_action);
|
directory_view_context_menu->add_action(paste_action);
|
||||||
directory_view_context_menu->add_action(open_terminal_action);
|
directory_view_context_menu->add_action(open_terminal_action);
|
||||||
|
@ -741,7 +725,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
tree_view_directory_context_menu->add_separator();
|
tree_view_directory_context_menu->add_separator();
|
||||||
tree_view_directory_context_menu->add_action(properties_action);
|
tree_view_directory_context_menu->add_action(properties_action);
|
||||||
tree_view_directory_context_menu->add_separator();
|
tree_view_directory_context_menu->add_separator();
|
||||||
tree_view_directory_context_menu->add_action(mkdir_action);
|
tree_view_directory_context_menu->add_action(directory_view.mkdir_action());
|
||||||
tree_view_directory_context_menu->add_action(touch_action);
|
tree_view_directory_context_menu->add_action(touch_action);
|
||||||
|
|
||||||
RefPtr<GUI::Menu> file_context_menu;
|
RefPtr<GUI::Menu> file_context_menu;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue