ViewWidget.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2021, Mohsan Ali <mohsan0073@gmail.com>
  5. * Copyright (c) 2022, Mustafa Quraish <mustafa@serenityos.org>
  6. * Copyright (c) 2022, the SerenityOS developers.
  7. * Copyright (c) 2023, Caoimhe Byrne <caoimhebyrne06@gmail.com>
  8. *
  9. * SPDX-License-Identifier: BSD-2-Clause
  10. */
  11. #include "ViewWidget.h"
  12. #include <AK/LexicalPath.h>
  13. #include <AK/StringBuilder.h>
  14. #include <LibCore/DeprecatedFile.h>
  15. #include <LibCore/Directory.h>
  16. #include <LibCore/MappedFile.h>
  17. #include <LibCore/MimeData.h>
  18. #include <LibCore/Timer.h>
  19. #include <LibFileSystemAccessClient/Client.h>
  20. #include <LibGUI/Application.h>
  21. #include <LibGUI/MessageBox.h>
  22. #include <LibGfx/Bitmap.h>
  23. #include <LibGfx/Orientation.h>
  24. #include <LibGfx/Palette.h>
  25. #include <LibImageDecoderClient/Client.h>
  26. namespace ImageViewer {
  27. ViewWidget::ViewWidget()
  28. : m_timer(Core::Timer::try_create().release_value_but_fixme_should_propagate_errors())
  29. {
  30. set_fill_with_background_color(false);
  31. }
  32. void ViewWidget::clear()
  33. {
  34. m_timer->stop();
  35. m_decoded_image.clear();
  36. m_bitmap = nullptr;
  37. if (on_image_change)
  38. on_image_change(m_bitmap);
  39. set_original_rect({});
  40. m_path = {};
  41. reset_view();
  42. update();
  43. }
  44. void ViewWidget::flip(Gfx::Orientation orientation)
  45. {
  46. m_bitmap = m_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
  47. scale_image_for_window();
  48. }
  49. void ViewWidget::rotate(Gfx::RotationDirection rotation_direction)
  50. {
  51. m_bitmap = m_bitmap->rotated(rotation_direction).release_value_but_fixme_should_propagate_errors();
  52. scale_image_for_window();
  53. }
  54. bool ViewWidget::is_next_available() const
  55. {
  56. if (m_current_index.has_value())
  57. return m_current_index.value() + 1 < m_files_in_same_dir.size();
  58. return false;
  59. }
  60. bool ViewWidget::is_previous_available() const
  61. {
  62. if (m_current_index.has_value())
  63. return m_current_index.value() > 0;
  64. return false;
  65. }
  66. // FIXME: Convert to `String` & use LibFileSystemAccessClient + `Core::System::unveil(nullptr, nullptr)`
  67. // - Converting to String is not super-trivial due to the LexicalPath usage, while we can do a bunch of
  68. // String::from_deprecated_string() and String.to_deprecated_string(), it is quite ugly to read and
  69. // probably not the best approach.
  70. //
  71. // - If we go full-unveil (`Core::System::unveil(nullptr, nullptr)`) this functionality does not work,
  72. // we can not access the list of contents of a directory through LibFileSystemAccessClient at the moment.
  73. Vector<DeprecatedString> ViewWidget::load_files_from_directory(DeprecatedString const& path) const
  74. {
  75. Vector<DeprecatedString> files_in_directory;
  76. auto current_dir = LexicalPath(path).parent().string();
  77. // FIXME: Propagate errors
  78. (void)Core::Directory::for_each_entry(current_dir, Core::DirIterator::Flags::SkipDots, [&](auto const& entry, auto const& directory) -> ErrorOr<IterationDecision> {
  79. auto full_path = LexicalPath::join(directory.path().string(), entry.name).string();
  80. if (Gfx::Bitmap::is_path_a_supported_image_format(full_path))
  81. files_in_directory.append(full_path);
  82. return IterationDecision::Continue;
  83. });
  84. return files_in_directory;
  85. }
  86. void ViewWidget::set_path(String const& path)
  87. {
  88. m_path = path;
  89. m_files_in_same_dir = load_files_from_directory(path.to_deprecated_string());
  90. m_current_index = m_files_in_same_dir.find_first_index(path.to_deprecated_string());
  91. }
  92. void ViewWidget::navigate(Directions direction)
  93. {
  94. if (!m_current_index.has_value()) {
  95. return;
  96. }
  97. auto index = m_current_index.value();
  98. if (direction == Directions::Back) {
  99. index--;
  100. } else if (direction == Directions::Forward) {
  101. index++;
  102. } else if (direction == Directions::First) {
  103. index = 0;
  104. } else if (direction == Directions::Last) {
  105. index = m_files_in_same_dir.size() - 1;
  106. }
  107. auto result = FileSystemAccessClient::Client::the().request_file_read_only_approved(window(), m_files_in_same_dir.at(index));
  108. if (result.is_error())
  109. return;
  110. m_current_index = index;
  111. auto value = result.release_value();
  112. open_file(value.filename(), value.stream());
  113. }
  114. void ViewWidget::doubleclick_event(GUI::MouseEvent&)
  115. {
  116. on_doubleclick();
  117. }
  118. void ViewWidget::paint_event(GUI::PaintEvent& event)
  119. {
  120. Frame::paint_event(event);
  121. GUI::Painter painter(*this);
  122. painter.add_clip_rect(event.rect());
  123. painter.add_clip_rect(frame_inner_rect());
  124. Gfx::StylePainter::paint_transparency_grid(painter, frame_inner_rect(), palette());
  125. if (!m_bitmap.is_null())
  126. painter.draw_scaled_bitmap(content_rect(), *m_bitmap, m_bitmap->rect(), 1.0f, m_scaling_mode);
  127. }
  128. void ViewWidget::mousedown_event(GUI::MouseEvent& event)
  129. {
  130. if (event.button() == GUI::MouseButton::Primary)
  131. start_panning(event.position());
  132. GUI::AbstractZoomPanWidget::mousedown_event(event);
  133. }
  134. void ViewWidget::mouseup_event(GUI::MouseEvent& event)
  135. {
  136. if (event.button() == GUI::MouseButton::Primary)
  137. stop_panning();
  138. GUI::AbstractZoomPanWidget::mouseup_event(event);
  139. }
  140. void ViewWidget::open_file(String const& path, Core::File& file)
  141. {
  142. auto open_result = try_open_file(path, file);
  143. if (open_result.is_error()) {
  144. auto error = open_result.release_error();
  145. auto user_error_message = String::formatted("Failed to open the image: {}.", error).release_value_but_fixme_should_propagate_errors();
  146. GUI::MessageBox::show_error(nullptr, user_error_message);
  147. }
  148. }
  149. ErrorOr<void> ViewWidget::try_open_file(String const& path, Core::File& file)
  150. {
  151. // Spawn a new ImageDecoder service process and connect to it.
  152. auto client = TRY(ImageDecoderClient::Client::try_create());
  153. auto mime_type = Core::guess_mime_type_based_on_filename(path);
  154. auto decoded_image_or_none = client->decode_image(TRY(file.read_until_eof()), mime_type);
  155. if (!decoded_image_or_none.has_value()) {
  156. return Error::from_string_literal("Failed to decode image");
  157. }
  158. m_decoded_image = decoded_image_or_none.release_value();
  159. m_bitmap = m_decoded_image->frames[0].bitmap;
  160. if (m_bitmap.is_null()) {
  161. return Error::from_string_literal("Image didn't contain a bitmap");
  162. }
  163. set_original_rect(m_bitmap->rect());
  164. if (m_decoded_image->is_animated && m_decoded_image->frames.size() > 1) {
  165. auto const& first_frame = m_decoded_image->frames[0];
  166. m_timer->set_interval(first_frame.duration);
  167. m_timer->on_timeout = [this] { animate(); };
  168. m_timer->start();
  169. } else {
  170. m_timer->stop();
  171. }
  172. set_path(path);
  173. GUI::Application::the()->set_most_recently_open_file(path);
  174. if (on_image_change)
  175. on_image_change(m_bitmap);
  176. if (scaled_for_first_image())
  177. scale_image_for_window();
  178. else
  179. reset_view();
  180. return {};
  181. }
  182. void ViewWidget::drag_enter_event(GUI::DragEvent& event)
  183. {
  184. auto const& mime_types = event.mime_types();
  185. if (mime_types.contains_slow("text/uri-list"))
  186. event.accept();
  187. }
  188. void ViewWidget::drop_event(GUI::DropEvent& event)
  189. {
  190. event.accept();
  191. if (on_drop)
  192. on_drop(event);
  193. }
  194. void ViewWidget::resize_event(GUI::ResizeEvent& event)
  195. {
  196. event.accept();
  197. scale_image_for_window();
  198. }
  199. void ViewWidget::scale_image_for_window()
  200. {
  201. if (!m_bitmap)
  202. return;
  203. set_original_rect(m_bitmap->rect());
  204. fit_content_to_view(GUI::AbstractZoomPanWidget::FitType::Both);
  205. }
  206. void ViewWidget::resize_window()
  207. {
  208. if (window()->is_fullscreen() || window()->is_maximized())
  209. return;
  210. auto absolute_bitmap_rect = content_rect();
  211. absolute_bitmap_rect.translate_by(window()->rect().top_left());
  212. if (!m_bitmap)
  213. return;
  214. auto new_size = content_rect().size();
  215. if (new_size.width() < 300)
  216. new_size.set_width(300);
  217. if (new_size.height() < 200)
  218. new_size.set_height(200);
  219. if (new_size.width() > 500)
  220. new_size = { 500, 500 * absolute_bitmap_rect.height() / absolute_bitmap_rect.width() };
  221. if (new_size.height() > 500)
  222. new_size = { 500 * absolute_bitmap_rect.width() / absolute_bitmap_rect.height(), 500 };
  223. new_size.set_height(new_size.height() + m_toolbar_height);
  224. window()->resize(new_size);
  225. scale_image_for_window();
  226. }
  227. void ViewWidget::set_bitmap(Gfx::Bitmap const* bitmap)
  228. {
  229. if (m_bitmap == bitmap)
  230. return;
  231. m_bitmap = bitmap;
  232. set_original_rect(m_bitmap->rect());
  233. update();
  234. }
  235. // Same as ImageWidget::animate(), you probably want to keep any changes in sync
  236. void ViewWidget::animate()
  237. {
  238. if (!m_decoded_image.has_value())
  239. return;
  240. m_current_frame_index = (m_current_frame_index + 1) % m_decoded_image->frames.size();
  241. auto const& current_frame = m_decoded_image->frames[m_current_frame_index];
  242. set_bitmap(current_frame.bitmap);
  243. if ((int)current_frame.duration != m_timer->interval()) {
  244. m_timer->restart(current_frame.duration);
  245. }
  246. if (m_current_frame_index == m_decoded_image->frames.size() - 1) {
  247. ++m_loops_completed;
  248. if (m_loops_completed > 0 && m_loops_completed == m_decoded_image->loop_count) {
  249. m_timer->stop();
  250. }
  251. }
  252. }
  253. void ViewWidget::set_scaling_mode(Gfx::Painter::ScalingMode scaling_mode)
  254. {
  255. m_scaling_mode = scaling_mode;
  256. update();
  257. }
  258. }