ViewWidget.cpp 9.2 KB

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