mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 17:06:31 +00:00
Magnifier: Add the option to save captures
This commit is contained in:
parent
eb0657c23e
commit
69f7a59a34
|
@ -11,4 +11,4 @@ set(SOURCES
|
|||
)
|
||||
|
||||
serenity_app(Magnifier ICON app-magnifier)
|
||||
target_link_libraries(Magnifier LibGfx LibGUI LibMain)
|
||||
target_link_libraries(Magnifier LibGfx LibGUI LibMain LibFileSystemAccessClient)
|
||||
|
|
|
@ -18,6 +18,7 @@ public:
|
|||
virtual ~MagnifierWidget() override = default;
|
||||
void set_scale_factor(int scale_factor);
|
||||
void set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter>);
|
||||
|
||||
void pause_capture(bool pause)
|
||||
{
|
||||
m_pause_capture = pause;
|
||||
|
@ -26,6 +27,7 @@ public:
|
|||
}
|
||||
void display_previous_frame();
|
||||
void display_next_frame();
|
||||
RefPtr<Gfx::Bitmap> current_bitmap() const { return m_grabbed_bitmap; };
|
||||
|
||||
private:
|
||||
MagnifierWidget();
|
||||
|
|
|
@ -5,23 +5,43 @@
|
|||
*/
|
||||
|
||||
#include "MagnifierWidget.h"
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibFileSystemAccessClient/Client.h>
|
||||
#include <LibGUI/ActionGroup.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Menubar.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/BMPWriter.h>
|
||||
#include <LibGfx/Filters/ColorBlindnessFilter.h>
|
||||
#include <LibGfx/PNGWriter.h>
|
||||
#include <LibGfx/QOIWriter.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
static ErrorOr<ByteBuffer> dump_bitmap(RefPtr<Gfx::Bitmap> bitmap, AK::StringView extension)
|
||||
{
|
||||
if (extension == "bmp") {
|
||||
Gfx::BMPWriter dumper;
|
||||
return dumper.dump(bitmap);
|
||||
} else if (extension == "png") {
|
||||
return Gfx::PNGWriter::encode(*bitmap);
|
||||
} else if (extension == "qoi") {
|
||||
return Gfx::QOIWriter::encode(*bitmap);
|
||||
} else {
|
||||
return Error::from_string_literal("invalid image format");
|
||||
}
|
||||
}
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio cpath rpath recvfd sendfd unix"));
|
||||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
|
||||
TRY(Core::System::pledge("stdio cpath rpath recvfd sendfd"));
|
||||
TRY(Core::System::unveil("/res", "r"));
|
||||
TRY(Core::System::unveil("/tmp/user/%uid/portal/filesystemaccess", "rw"));
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
auto app_icon = GUI::Icon::default_icon("app-magnifier"sv);
|
||||
|
@ -40,6 +60,27 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
app->quit();
|
||||
})));
|
||||
|
||||
TRY(file_menu->try_add_action(GUI::CommonActions::make_save_as_action([&](auto&) {
|
||||
AK::String filename = "file for saving";
|
||||
auto do_save = [&]() -> ErrorOr<void> {
|
||||
auto file = TRY(FileSystemAccessClient::Client::the().try_save_file(window, "Capture", "png"));
|
||||
auto path = AK::LexicalPath(file->filename());
|
||||
filename = path.basename();
|
||||
auto encoded = TRY(dump_bitmap(magnifier->current_bitmap(), path.extension()));
|
||||
|
||||
if (!file->write(encoded.data(), encoded.size())) {
|
||||
return Error::from_errno(file->error());
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
auto result = do_save();
|
||||
if (result.is_error()) {
|
||||
GUI::MessageBox::show(window, "Unable to save file.\n"sv, "Error"sv, GUI::MessageBox::Type::Error);
|
||||
warnln("Error saving bitmap to {}: {}", filename, result.error().string_literal());
|
||||
}
|
||||
})));
|
||||
|
||||
auto size_action_group = make<GUI::ActionGroup>();
|
||||
|
||||
auto two_x_action = GUI::Action::create_checkable(
|
||||
|
|
Loading…
Reference in a new issue