mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
Desktop: File creation from the context menu
Kinda hackish, but it does work.
This commit is contained in:
parent
d02c02cebe
commit
5457020d4e
1 changed files with 29 additions and 1 deletions
|
@ -173,7 +173,35 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
auto touch_action = GUI::Action::create("New file...", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
|
||||||
|
auto input_box = GUI::InputBox::construct("Enter name:", "New file", window);
|
||||||
|
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
|
||||||
|
auto new_file_path = canonicalized_path(
|
||||||
|
String::format("%s/%s",
|
||||||
|
model->root_path().characters(),
|
||||||
|
input_box->text_value().characters()));
|
||||||
|
struct stat st;
|
||||||
|
int rc = stat(new_file_path.characters(), &st);
|
||||||
|
if ((rc < 0 && errno != ENOENT)) {
|
||||||
|
GUI::MessageBox::show(String::format("stat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (rc == 0) {
|
||||||
|
GUI::MessageBox::show(String::format("%s: Already exists", new_file_path.characters()), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int fd = creat(new_file_path.characters(), 0666);
|
||||||
|
if (fd < 0) {
|
||||||
|
GUI::MessageBox::show(String::format("creat(\"%s\") failed: %s", new_file_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
rc = close(fd);
|
||||||
|
assert(rc >= 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
desktop_view_context_menu->add_action(mkdir_action);
|
desktop_view_context_menu->add_action(mkdir_action);
|
||||||
|
desktop_view_context_menu->add_action(touch_action);
|
||||||
|
|
||||||
item_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
|
item_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
|
||||||
if (!index.is_valid())
|
if (!index.is_valid())
|
||||||
|
@ -774,4 +802,4 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
||||||
};
|
};
|
||||||
|
|
||||||
return GUI::Application::the().exec();
|
return GUI::Application::the().exec();
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue