main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ViewWidget.h"
  7. #include <AK/URL.h>
  8. #include <LibCore/ArgsParser.h>
  9. #include <LibCore/MimeData.h>
  10. #include <LibDesktop/Launcher.h>
  11. #include <LibGUI/Action.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/Clipboard.h>
  15. #include <LibGUI/Desktop.h>
  16. #include <LibGUI/FilePicker.h>
  17. #include <LibGUI/Label.h>
  18. #include <LibGUI/Menu.h>
  19. #include <LibGUI/Menubar.h>
  20. #include <LibGUI/MessageBox.h>
  21. #include <LibGUI/Toolbar.h>
  22. #include <LibGUI/ToolbarContainer.h>
  23. #include <LibGUI/Window.h>
  24. #include <LibGfx/Bitmap.h>
  25. #include <LibGfx/Palette.h>
  26. #include <LibGfx/Rect.h>
  27. #include <serenity.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. using namespace ImageViewer;
  31. int main(int argc, char** argv)
  32. {
  33. if (pledge("stdio recvfd sendfd rpath wpath cpath unix thread", nullptr) < 0) {
  34. perror("pledge");
  35. return 1;
  36. }
  37. auto app = GUI::Application::construct(argc, argv);
  38. if (!Desktop::Launcher::add_allowed_handler_with_any_url("/bin/ImageViewer")) {
  39. warnln("Failed to set up allowed launch URLs");
  40. return 1;
  41. }
  42. if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
  43. "/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/ImageViewer.md") })) {
  44. warnln("Failed to set up allowed launch URLs");
  45. return 1;
  46. }
  47. if (!Desktop::Launcher::seal_allowlist()) {
  48. warnln("Failed to seal allowed launch URLs");
  49. return 1;
  50. }
  51. auto app_icon = GUI::Icon::default_icon("filetype-image");
  52. const char* path = nullptr;
  53. Core::ArgsParser args_parser;
  54. args_parser.add_positional_argument(path, "The image file to be displayed.", "file", Core::ArgsParser::Required::No);
  55. args_parser.parse(argc, argv);
  56. auto window = GUI::Window::construct();
  57. window->set_double_buffering_enabled(true);
  58. window->resize(300, 200);
  59. window->set_icon(app_icon.bitmap_for_size(16));
  60. window->set_title("Image Viewer");
  61. auto& root_widget = window->set_main_widget<GUI::Widget>();
  62. root_widget.set_fill_with_background_color(true);
  63. root_widget.set_layout<GUI::VerticalBoxLayout>();
  64. root_widget.layout()->set_spacing(2);
  65. auto& toolbar_container = root_widget.add<GUI::ToolbarContainer>();
  66. auto& main_toolbar = toolbar_container.add<GUI::Toolbar>();
  67. auto& widget = root_widget.add<ViewWidget>();
  68. widget.on_scale_change = [&](int scale) {
  69. if (!widget.bitmap()) {
  70. window->set_title("Image Viewer");
  71. return;
  72. }
  73. window->set_title(String::formatted("{} {} {}% - Image Viewer", widget.path(), widget.bitmap()->size().to_string(), scale));
  74. if (scale == 100 && !widget.scaled_for_first_image()) {
  75. widget.set_scaled_for_first_image(true);
  76. widget.resize_window();
  77. }
  78. };
  79. widget.on_drop = [&](auto& event) {
  80. if (!event.mime_data().has_urls())
  81. return;
  82. auto urls = event.mime_data().urls();
  83. if (urls.is_empty())
  84. return;
  85. window->move_to_front();
  86. widget.load_from_file(urls.first().path());
  87. for (size_t i = 1; i < urls.size(); ++i) {
  88. Desktop::Launcher::open(URL::create_with_file_protocol(urls[i].path().characters()), "/bin/ImageViewer");
  89. }
  90. };
  91. widget.on_doubleclick = [&] {
  92. window->set_fullscreen(!window->is_fullscreen());
  93. toolbar_container.set_visible(!window->is_fullscreen());
  94. widget.set_frame_thickness(window->is_fullscreen() ? 0 : 2);
  95. };
  96. // Actions
  97. auto open_action = GUI::CommonActions::make_open_action(
  98. [&](auto&) {
  99. auto path = GUI::FilePicker::get_open_filepath(window, "Open Image");
  100. if (path.has_value()) {
  101. widget.load_from_file(path.value());
  102. }
  103. });
  104. auto delete_action = GUI::CommonActions::make_delete_action(
  105. [&](auto&) {
  106. auto path = widget.path();
  107. if (path.is_empty())
  108. return;
  109. auto msgbox_result = GUI::MessageBox::show(window,
  110. String::formatted("Really delete {}?", path),
  111. "Confirm deletion",
  112. GUI::MessageBox::Type::Warning,
  113. GUI::MessageBox::InputType::OKCancel);
  114. if (msgbox_result == GUI::MessageBox::ExecCancel)
  115. return;
  116. if (unlink(widget.path().characters()) < 0) {
  117. int saved_errno = errno;
  118. GUI::MessageBox::show(window,
  119. String::formatted("unlink({}) failed: {}", path, strerror(saved_errno)),
  120. "Delete failed",
  121. GUI::MessageBox::Type::Error);
  122. return;
  123. }
  124. widget.clear();
  125. });
  126. auto quit_action = GUI::CommonActions::make_quit_action(
  127. [&](auto&) {
  128. app->quit();
  129. });
  130. auto rotate_left_action = GUI::Action::create("Rotate &Left", { Mod_None, Key_L },
  131. [&](auto&) {
  132. widget.rotate(Gfx::RotationDirection::CounterClockwise);
  133. });
  134. auto rotate_right_action = GUI::Action::create("Rotate &Right", { Mod_None, Key_R },
  135. [&](auto&) {
  136. widget.rotate(Gfx::RotationDirection::Clockwise);
  137. });
  138. auto vertical_flip_action = GUI::Action::create("Flip &Vertically", { Mod_None, Key_V },
  139. [&](auto&) {
  140. widget.flip(Gfx::Orientation::Vertical);
  141. });
  142. auto horizontal_flip_action = GUI::Action::create("Flip &Horizontally", { Mod_None, Key_H },
  143. [&](auto&) {
  144. widget.flip(Gfx::Orientation::Horizontal);
  145. });
  146. auto desktop_wallpaper_action = GUI::Action::create("Set as Desktop &Wallpaper",
  147. [&](auto&) {
  148. GUI::Desktop::the().set_wallpaper(widget.path());
  149. });
  150. auto go_first_action = GUI::Action::create("&Go to First", { Mod_None, Key_Home }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-first.png"),
  151. [&](auto&) {
  152. widget.navigate(ViewWidget::Directions::First);
  153. });
  154. auto go_back_action = GUI::Action::create("Go &Back", { Mod_None, Key_Left }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-back.png"),
  155. [&](auto&) {
  156. widget.navigate(ViewWidget::Directions::Back);
  157. });
  158. auto go_forward_action = GUI::Action::create("Go &Forward", { Mod_None, Key_Right }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png"),
  159. [&](auto&) {
  160. widget.navigate(ViewWidget::Directions::Forward);
  161. });
  162. auto go_last_action = GUI::Action::create("Go to &Last", { Mod_None, Key_End }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-last.png"),
  163. [&](auto&) {
  164. widget.navigate(ViewWidget::Directions::Last);
  165. });
  166. auto full_screen_action = GUI::CommonActions::make_fullscreen_action(
  167. [&](auto&) {
  168. widget.on_doubleclick();
  169. });
  170. auto zoom_in_action = GUI::CommonActions::make_zoom_in_action(
  171. [&](auto&) {
  172. widget.set_scale(widget.scale() + 10);
  173. },
  174. window);
  175. auto reset_zoom_action = GUI::CommonActions::make_reset_zoom_action(
  176. [&](auto&) {
  177. widget.set_scale(100);
  178. },
  179. window);
  180. auto zoom_out_action = GUI::CommonActions::make_zoom_out_action(
  181. [&](auto&) {
  182. widget.set_scale(widget.scale() - 10);
  183. },
  184. window);
  185. auto hide_show_toolbar_action = GUI::Action::create("Hide/Show &Toolbar", { Mod_Ctrl, Key_T },
  186. [&](auto&) {
  187. toolbar_container.set_visible(!toolbar_container.is_visible());
  188. });
  189. auto copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
  190. if (widget.bitmap())
  191. GUI::Clipboard::the().set_bitmap(*widget.bitmap());
  192. });
  193. widget.on_image_change = [&](const Gfx::Bitmap* bitmap) {
  194. bool should_enable_image_actions = (bitmap != nullptr);
  195. delete_action->set_enabled(should_enable_image_actions);
  196. rotate_left_action->set_enabled(should_enable_image_actions);
  197. rotate_right_action->set_enabled(should_enable_image_actions);
  198. vertical_flip_action->set_enabled(should_enable_image_actions);
  199. horizontal_flip_action->set_enabled(should_enable_image_actions);
  200. desktop_wallpaper_action->set_enabled(should_enable_image_actions);
  201. go_first_action->set_enabled(should_enable_image_actions);
  202. go_back_action->set_enabled(should_enable_image_actions);
  203. go_forward_action->set_enabled(should_enable_image_actions);
  204. go_last_action->set_enabled(should_enable_image_actions);
  205. zoom_in_action->set_enabled(should_enable_image_actions);
  206. reset_zoom_action->set_enabled(should_enable_image_actions);
  207. zoom_out_action->set_enabled(should_enable_image_actions);
  208. if (!should_enable_image_actions) {
  209. window->set_title("Image Viewer");
  210. }
  211. };
  212. main_toolbar.add_action(open_action);
  213. main_toolbar.add_action(delete_action);
  214. main_toolbar.add_separator();
  215. main_toolbar.add_action(go_first_action);
  216. main_toolbar.add_action(go_back_action);
  217. main_toolbar.add_action(go_forward_action);
  218. main_toolbar.add_action(go_last_action);
  219. main_toolbar.add_separator();
  220. main_toolbar.add_action(zoom_in_action);
  221. main_toolbar.add_action(reset_zoom_action);
  222. main_toolbar.add_action(zoom_out_action);
  223. auto& file_menu = window->add_menu("&File");
  224. file_menu.add_action(open_action);
  225. file_menu.add_action(delete_action);
  226. file_menu.add_separator();
  227. file_menu.add_action(quit_action);
  228. auto& image_menu = window->add_menu("&Image");
  229. image_menu.add_action(rotate_left_action);
  230. image_menu.add_action(rotate_right_action);
  231. image_menu.add_action(vertical_flip_action);
  232. image_menu.add_action(horizontal_flip_action);
  233. image_menu.add_separator();
  234. image_menu.add_action(desktop_wallpaper_action);
  235. auto& navigate_menu = window->add_menu("&Navigate");
  236. navigate_menu.add_action(go_first_action);
  237. navigate_menu.add_action(go_back_action);
  238. navigate_menu.add_action(go_forward_action);
  239. navigate_menu.add_action(go_last_action);
  240. auto& view_menu = window->add_menu("&View");
  241. view_menu.add_action(full_screen_action);
  242. view_menu.add_separator();
  243. view_menu.add_action(zoom_in_action);
  244. view_menu.add_action(reset_zoom_action);
  245. view_menu.add_action(zoom_out_action);
  246. view_menu.add_separator();
  247. view_menu.add_action(hide_show_toolbar_action);
  248. auto& help_menu = window->add_menu("&Help");
  249. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  250. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/ImageViewer.md"), "/bin/Help");
  251. }));
  252. help_menu.add_action(GUI::CommonActions::make_about_action("Image Viewer", app_icon, window));
  253. if (path != nullptr) {
  254. widget.load_from_file(path);
  255. } else {
  256. widget.clear();
  257. }
  258. window->show();
  259. return app->exec();
  260. }