mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:47:44 +00:00
ImageViewer: Disable image actions when there is no image
Previously some actions like Rotate/Flip/Set as Desktop Wallpaper would make the application crash if no image was loaded. Now image actions are enabled/disabled based on whether an image has been loaded or not.
This commit is contained in:
parent
400d3ddb08
commit
bce119036a
3 changed files with 29 additions and 2 deletions
|
@ -32,6 +32,8 @@ ViewWidget::~ViewWidget()
|
||||||
void ViewWidget::clear()
|
void ViewWidget::clear()
|
||||||
{
|
{
|
||||||
m_bitmap = nullptr;
|
m_bitmap = nullptr;
|
||||||
|
if (on_image_change)
|
||||||
|
on_image_change(m_bitmap);
|
||||||
m_path = {};
|
m_path = {};
|
||||||
|
|
||||||
reset_view();
|
reset_view();
|
||||||
|
@ -255,6 +257,8 @@ void ViewWidget::load_from_file(const String& path)
|
||||||
|
|
||||||
m_decoded_image = decoded_image_or_error.release_value();
|
m_decoded_image = decoded_image_or_error.release_value();
|
||||||
m_bitmap = m_decoded_image->frames[0].bitmap;
|
m_bitmap = m_decoded_image->frames[0].bitmap;
|
||||||
|
if (on_image_change)
|
||||||
|
on_image_change(m_bitmap);
|
||||||
|
|
||||||
if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
|
if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
|
||||||
const auto& first_frame = m_decoded_image->frames[0];
|
const auto& first_frame = m_decoded_image->frames[0];
|
||||||
|
|
|
@ -42,6 +42,7 @@ public:
|
||||||
Function<void(int, Gfx::IntRect)> on_scale_change;
|
Function<void(int, Gfx::IntRect)> on_scale_change;
|
||||||
Function<void()> on_doubleclick;
|
Function<void()> on_doubleclick;
|
||||||
Function<void(const GUI::DropEvent&)> on_drop;
|
Function<void(const GUI::DropEvent&)> on_drop;
|
||||||
|
Function<void(const Gfx::Bitmap*)> on_image_change;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ViewWidget();
|
ViewWidget();
|
||||||
|
|
|
@ -205,7 +205,7 @@ int main(int argc, char** argv)
|
||||||
widget.navigate(ViewWidget::Directions::Last);
|
widget.navigate(ViewWidget::Directions::Last);
|
||||||
});
|
});
|
||||||
|
|
||||||
auto full_sceen_action = GUI::CommonActions::make_fullscreen_action(
|
auto full_screen_action = GUI::CommonActions::make_fullscreen_action(
|
||||||
[&](auto&) {
|
[&](auto&) {
|
||||||
widget.on_doubleclick();
|
widget.on_doubleclick();
|
||||||
});
|
});
|
||||||
|
@ -238,6 +238,26 @@ int main(int argc, char** argv)
|
||||||
GUI::Clipboard::the().set_bitmap(*widget.bitmap());
|
GUI::Clipboard::the().set_bitmap(*widget.bitmap());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
widget.on_image_change = [&](const Gfx::Bitmap* bitmap) {
|
||||||
|
bool should_enable_image_actions = (bitmap != nullptr);
|
||||||
|
delete_action->set_enabled(should_enable_image_actions);
|
||||||
|
rotate_left_action->set_enabled(should_enable_image_actions);
|
||||||
|
rotate_right_action->set_enabled(should_enable_image_actions);
|
||||||
|
vertical_flip_action->set_enabled(should_enable_image_actions);
|
||||||
|
horizontal_flip_action->set_enabled(should_enable_image_actions);
|
||||||
|
desktop_wallpaper_action->set_enabled(should_enable_image_actions);
|
||||||
|
go_first_action->set_enabled(should_enable_image_actions);
|
||||||
|
go_back_action->set_enabled(should_enable_image_actions);
|
||||||
|
go_forward_action->set_enabled(should_enable_image_actions);
|
||||||
|
go_last_action->set_enabled(should_enable_image_actions);
|
||||||
|
zoom_in_action->set_enabled(should_enable_image_actions);
|
||||||
|
reset_zoom_action->set_enabled(should_enable_image_actions);
|
||||||
|
zoom_out_action->set_enabled(should_enable_image_actions);
|
||||||
|
if (!should_enable_image_actions) {
|
||||||
|
window->set_title("Image Viewer");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
main_toolbar.add_action(open_action);
|
main_toolbar.add_action(open_action);
|
||||||
main_toolbar.add_action(delete_action);
|
main_toolbar.add_action(delete_action);
|
||||||
main_toolbar.add_separator();
|
main_toolbar.add_separator();
|
||||||
|
@ -273,7 +293,7 @@ int main(int argc, char** argv)
|
||||||
navigate_menu.add_action(go_last_action);
|
navigate_menu.add_action(go_last_action);
|
||||||
|
|
||||||
auto& view_menu = menubar->add_menu("&View");
|
auto& view_menu = menubar->add_menu("&View");
|
||||||
view_menu.add_action(full_sceen_action);
|
view_menu.add_action(full_screen_action);
|
||||||
view_menu.add_separator();
|
view_menu.add_separator();
|
||||||
view_menu.add_action(zoom_in_action);
|
view_menu.add_action(zoom_in_action);
|
||||||
view_menu.add_action(reset_zoom_action);
|
view_menu.add_action(reset_zoom_action);
|
||||||
|
@ -291,6 +311,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
if (path != nullptr) {
|
if (path != nullptr) {
|
||||||
widget.load_from_file(path);
|
widget.load_from_file(path);
|
||||||
|
} else {
|
||||||
|
widget.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
window->show();
|
window->show();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue