caption_bubble_model.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/live_caption/views/caption_bubble_model.h"
  5. #include "base/callback_forward.h"
  6. #include "base/metrics/histogram_functions.h"
  7. #include "components/live_caption/caption_bubble_context.h"
  8. #include "components/live_caption/views/caption_bubble.h"
  9. namespace {
  10. // The caption bubble contains 2 lines of text in its normal size and 8 lines
  11. // in its expanded size, so the maximum number of lines before truncating is 9.
  12. constexpr int kMaxLines = 9;
  13. } // namespace
  14. namespace captions {
  15. CaptionBubbleModel::CaptionBubbleModel(CaptionBubbleContext* context)
  16. : context_(context) {
  17. DCHECK(context_);
  18. }
  19. CaptionBubbleModel::~CaptionBubbleModel() {
  20. if (observer_)
  21. observer_->SetModel(nullptr);
  22. }
  23. void CaptionBubbleModel::SetObserver(CaptionBubble* observer) {
  24. if (observer_)
  25. return;
  26. observer_ = observer;
  27. if (observer_) {
  28. observer_->OnTextChanged();
  29. observer_->OnErrorChanged(
  30. CaptionBubbleErrorType::kGeneric, base::RepeatingClosure(),
  31. base::BindRepeating(
  32. [](CaptionBubbleErrorType error_type, bool checked) {}));
  33. }
  34. }
  35. void CaptionBubbleModel::RemoveObserver() {
  36. observer_ = nullptr;
  37. }
  38. void CaptionBubbleModel::OnTextChanged() {
  39. if (observer_)
  40. observer_->OnTextChanged();
  41. }
  42. void CaptionBubbleModel::SetPartialText(const std::string& partial_text) {
  43. partial_text_ = partial_text;
  44. OnTextChanged();
  45. if (has_error_) {
  46. has_error_ = false;
  47. if (observer_)
  48. observer_->OnErrorChanged(
  49. CaptionBubbleErrorType::kGeneric, base::RepeatingClosure(),
  50. base::BindRepeating(
  51. [](CaptionBubbleErrorType error_type, bool checked) {}));
  52. }
  53. }
  54. void CaptionBubbleModel::Close() {
  55. is_closed_ = true;
  56. ClearText();
  57. }
  58. void CaptionBubbleModel::Open() {
  59. is_closed_ = false;
  60. OnTextChanged();
  61. }
  62. void CaptionBubbleModel::OnError(
  63. CaptionBubbleErrorType error_type,
  64. OnErrorClickedCallback error_clicked_callback,
  65. OnDoNotShowAgainClickedCallback error_silenced_callback) {
  66. has_error_ = true;
  67. error_type_ = error_type;
  68. if (observer_) {
  69. base::UmaHistogramEnumeration(
  70. "Accessibility.LiveCaption.CaptionBubbleError", error_type);
  71. observer_->OnErrorChanged(error_type, std::move(error_clicked_callback),
  72. std::move(error_silenced_callback));
  73. }
  74. }
  75. void CaptionBubbleModel::ClearText() {
  76. partial_text_.clear();
  77. final_text_.clear();
  78. OnTextChanged();
  79. }
  80. void CaptionBubbleModel::CommitPartialText() {
  81. final_text_ += partial_text_;
  82. partial_text_.clear();
  83. if (!observer_)
  84. return;
  85. // Truncate the final text to kMaxLines lines long. This time, alert the
  86. // observer that the text has changed.
  87. const size_t num_lines = observer_->GetNumLinesInLabel();
  88. if (num_lines > kMaxLines) {
  89. const size_t truncate_index =
  90. observer_->GetTextIndexOfLineInLabel(num_lines - kMaxLines);
  91. final_text_.erase(0, truncate_index);
  92. OnTextChanged();
  93. }
  94. }
  95. } // namespace captions