aboutsummaryrefslogtreecommitdiffstats
path: root/support/hololens/ServoApp/BrowserPage.cpp
blob: e8ba12ba2b30a27b3763370e24f396800ee470ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "pch.h"
#include "logs.h"
#include "BrowserPage.h"
#include "BrowserPage.g.cpp"
#include "DefaultUrl.h"

using namespace std::placeholders;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::UI::ViewManagement;
using namespace winrt::Windows::ApplicationModel::Core;
using namespace winrt::Windows::UI::Notifications;
using namespace winrt::Windows::Data::Xml::Dom;

namespace winrt::ServoApp::implementation {
BrowserPage::BrowserPage() {
  InitializeComponent();
  BindServoEvents();
}

void BrowserPage::BindServoEvents() {
  servoControl().OnURLChanged(
      [=](const auto &, hstring url) { urlTextbox().Text(url); });
  servoControl().OnTitleChanged([=](const auto &, hstring title) {});
  servoControl().OnHistoryChanged([=](bool back, bool forward) {
    backButton().IsEnabled(back);
    forwardButton().IsEnabled(forward);
  });
  servoControl().OnLoadStarted([=] {
    urlbarLoadingIndicator().IsActive(true);
    transientLoadingIndicator().IsIndeterminate(true);

    reloadButton().IsEnabled(false);
    reloadButton().Visibility(Visibility::Collapsed);
    stopButton().IsEnabled(true);
    stopButton().Visibility(Visibility::Visible);
  });
  servoControl().OnLoadEnded([=] {
    urlbarLoadingIndicator().IsActive(false);
    transientLoadingIndicator().IsIndeterminate(false);
    reloadButton().IsEnabled(true);
    reloadButton().Visibility(Visibility::Visible);
    stopButton().IsEnabled(false);
    stopButton().Visibility(Visibility::Collapsed);
  });
  servoControl().OnCaptureGesturesStarted([=] {
    servoControl().Focus(FocusState::Programmatic);
    navigationBar().IsHitTestVisible(false);
  });
  servoControl().OnCaptureGesturesEnded(
      [=] { navigationBar().IsHitTestVisible(true); });
  urlTextbox().GotFocus(std::bind(&BrowserPage::OnURLFocused, this, _1));
  servoControl().OnMediaSessionMetadata(
      [=](hstring title, hstring artist, hstring album) {});
  servoControl().OnMediaSessionPlaybackStateChange(
      [=](const auto &, int state) {
        if (state == servo::Servo::MediaSessionPlaybackState::None) {
          mediaControls().Visibility(Visibility::Collapsed);
          return;
        }
        mediaControls().Visibility(Visibility::Visible);
        playButton().Visibility(
            state == servo::Servo::MediaSessionPlaybackState::Paused
                ? Visibility::Visible
                : Visibility::Collapsed);
        pauseButton().Visibility(
            state == servo::Servo::MediaSessionPlaybackState::Paused
                ? Visibility::Collapsed
                : Visibility::Visible);
      });
  servoControl().OnDevtoolsStatusChanged(
      [=](DevtoolsStatus status, unsigned int port) {
        mDevtoolsStatus = status;
        mDevtoolsPort = port;
      });
  Window::Current().VisibilityChanged(
      [=](const auto &, const VisibilityChangedEventArgs &args) {
        servoControl().ChangeVisibility(args.Visible());
      });
}

void BrowserPage::OnURLFocused(Windows::Foundation::IInspectable const &) {
  urlTextbox().SelectAll();
}

void BrowserPage::OnURLKeyboardAccelerator(
    Windows::Foundation::IInspectable const &,
    Windows::UI::Xaml::Input::KeyboardAcceleratorInvokedEventArgs const &) {
  urlTextbox().Focus(FocusState::Programmatic);
}

void BrowserPage::LoadServoURI(Uri uri) {
  auto scheme = uri.SchemeName();

  if (scheme != SERVO_SCHEME) {
    log("Unexpected URL: ", uri.RawUri().c_str());
    return;
  }
  std::wstring raw{uri.RawUri()};
  auto raw2 = raw.substr(SERVO_SCHEME_SLASH_SLASH.size());
  servoControl().LoadURIOrSearch(raw2);
}

void BrowserPage::SetTransientMode(bool transient) {
  servoControl().SetTransientMode(transient);
  navigationBar().Visibility(transient ? Visibility::Collapsed
                                       : Visibility::Visible);
  transientLoadingIndicator().Visibility(transient ? Visibility::Visible
                                                   : Visibility::Collapsed);
}

void BrowserPage::SetArgs(hstring args) { servoControl().SetArgs(args); }

void BrowserPage::Shutdown() {
  ToastNotificationManager::History().Clear();
  servoControl().Shutdown();
}

/**** USER INTERACTIONS WITH UI ****/

void BrowserPage::OnBackButtonClicked(IInspectable const &,
                                      RoutedEventArgs const &) {
  servoControl().GoBack();
}

void BrowserPage::OnForwardButtonClicked(IInspectable const &,
                                         RoutedEventArgs const &) {
  servoControl().GoForward();
}

void BrowserPage::OnReloadButtonClicked(IInspectable const &,
                                        RoutedEventArgs const &) {
  servoControl().Reload();
}

void BrowserPage::OnStopButtonClicked(IInspectable const &,
                                      RoutedEventArgs const &) {
  servoControl().Stop();
}

void BrowserPage::OnHomeButtonClicked(IInspectable const &,
                                      RoutedEventArgs const &) {
  servoControl().LoadURIOrSearch(DEFAULT_URL);
}

void BrowserPage::OnDevtoolsButtonClicked(IInspectable const &,
                                          RoutedEventArgs const &) {
  auto toastTemplate = ToastTemplateType::ToastText01;
  auto toastXml = ToastNotificationManager::GetTemplateContent(toastTemplate);
  auto toastTextElements = toastXml.GetElementsByTagName(L"text");
  std::wstring message;
  if (mDevtoolsStatus == DevtoolsStatus::Stopped) {
    message = L"Devtools server hasn't started";
  } else if (mDevtoolsStatus == DevtoolsStatus::Running) {
    message = L"DevTools server has started on port " +
              std::to_wstring(mDevtoolsPort);
  } else if (mDevtoolsStatus == DevtoolsStatus::Failed) {
    message = L"Error: could not start DevTools";
  }
  toastTextElements.Item(0).InnerText(message);
  auto toast = ToastNotification(toastXml);
  ToastNotificationManager::CreateToastNotifier().Show(toast);
}

void BrowserPage::OnURLEdited(IInspectable const &,
                              Input::KeyRoutedEventArgs const &e) {
  if (e.Key() == Windows::System::VirtualKey::Enter) {
    servoControl().Focus(FocusState::Programmatic);
    auto input = urlTextbox().Text();
    auto uri = servoControl().LoadURIOrSearch(input);
    urlTextbox().Text(uri);
  }
}

void BrowserPage::OnMediaControlsPlayClicked(
    Windows::Foundation::IInspectable const &,
    Windows::UI::Xaml::RoutedEventArgs const &) {
  servoControl().SendMediaSessionAction(
      static_cast<int32_t>(servo::Servo::MediaSessionActionType::Play));
}
void BrowserPage::OnMediaControlsPauseClicked(
    Windows::Foundation::IInspectable const &,
    Windows::UI::Xaml::RoutedEventArgs const &) {
  servoControl().SendMediaSessionAction(
      static_cast<int32_t>(servo::Servo::MediaSessionActionType::Pause));
}

} // namespace winrt::ServoApp::implementation