main.cpp 11 KB

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