Skip to content

Commit

Permalink
[UWP] Basic console panel
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget authored and jdm committed Jun 18, 2020
1 parent 2560e78 commit bd8c7d6
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 33 deletions.
91 changes: 66 additions & 25 deletions support/hololens/ServoApp/BrowserPage.cpp
Expand Up @@ -7,11 +7,7 @@
#include "BrowserPage.h"
#include "BrowserPage.g.cpp"
#include "DefaultUrl.h"

#include "winrt/Microsoft.UI.Xaml.Controls.h"
#include "winrt/Microsoft.UI.Xaml.XamlTypeInfo.h"
#include "winrt/Windows.UI.Text.h"
#include "winrt/Windows.UI.Xaml.Documents.h" // For Run.Text()
#include "Devtools/Client.h"

using namespace std::placeholders;
using namespace winrt::Windows::Foundation;
Expand All @@ -21,7 +17,9 @@ using namespace winrt::Windows::UI::ViewManagement;
using namespace winrt::Windows::ApplicationModel::Core;
using namespace winrt::Windows::ApplicationModel::Resources;
using namespace winrt::Windows::UI::Notifications;
using namespace winrt::Windows::Data::Json;
using namespace winrt::Windows::Data::Xml::Dom;
using namespace winrt::servo;

namespace winrt::ServoApp::implementation {

Expand All @@ -46,6 +44,7 @@ void BrowserPage::BindServoEvents() {
reloadButton().Visibility(Visibility::Collapsed);
stopButton().IsEnabled(true);
stopButton().Visibility(Visibility::Visible);
devtoolsButton().IsEnabled(true);
});
servoControl().OnLoadEnded([=] {
urlbarLoadingIndicator().IsActive(false);
Expand All @@ -64,22 +63,20 @@ void BrowserPage::BindServoEvents() {
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().OnMediaSessionPlaybackStateChange([=](const auto &,
int state) {
if (state == Servo::MediaSessionPlaybackState::None) {
mediaControls().Visibility(Visibility::Collapsed);
return;
}
mediaControls().Visibility(Visibility::Visible);
playButton().Visibility(state == Servo::MediaSessionPlaybackState::Paused
? Visibility::Visible
: Visibility::Collapsed);
pauseButton().Visibility(state == Servo::MediaSessionPlaybackState::Paused
? Visibility::Collapsed
: Visibility::Visible);
});
servoControl().OnDevtoolsStatusChanged(
[=](DevtoolsStatus status, unsigned int port) {
mDevtoolsStatus = status;
Expand Down Expand Up @@ -276,11 +273,49 @@ void BrowserPage::OnPrefererenceSearchboxEdited(
}
}

void BrowserPage::OnDevtoolsMessage(DevtoolsMessageLevel level, hstring source,
hstring body) {
Dispatcher().RunAsync(CoreDispatcherPriority::Low, [=] {
// Temporary text-based logs. Should use gridview.
auto paragraph = Documents::Paragraph();

auto run1 = Documents::Run();
if (level == DevtoolsMessageLevel::Warn) {
run1.Text(L"warn: ");
} else if (level == DevtoolsMessageLevel::Error) {
run1.Text(L"error: ");
} else if (level == DevtoolsMessageLevel::None) {
run1.Text(L"");
}
paragraph.Inlines().Append(run1);

auto run2 = Documents::Run();
run2.Text(body);
paragraph.Inlines().Append(run2);

auto run3 = Documents::Run();
run3.Text(L" " + source);
paragraph.Inlines().Append(run3);

DevtoolsConsoleOutput().Blocks().Append(paragraph);

// Scroll to last message
auto offset = DevtoolsConsoleScrollViewer().ExtentHeight();
DevtoolsConsoleScrollViewer().ChangeView(nullptr, offset, nullptr);
});
}

void BrowserPage::OnDevtoolsDetached() {}

void BrowserPage::OnDevtoolsButtonClicked(IInspectable const &,
RoutedEventArgs const &) {
if (toolbox().Visibility() == Visibility::Visible) {
prefList().Children().Clear();
toolbox().Visibility(Visibility::Collapsed);
DevtoolsConsoleOutput().Blocks().Clear();
if (mDevtoolsClient != nullptr) {
mDevtoolsClient->Stop();
}
return;
}

Expand All @@ -290,10 +325,16 @@ void BrowserPage::OnDevtoolsButtonClicked(IInspectable const &,

auto resourceLoader = ResourceLoader::GetForCurrentView();
if (mDevtoolsStatus == DevtoolsStatus::Running) {
hstring port = to_hstring(mDevtoolsPort);
if (mDevtoolsClient == nullptr) {
DevtoolsDelegate *dd = static_cast<DevtoolsDelegate *>(this);
mDevtoolsClient =
std::make_unique<DevtoolsClient>(L"localhost", port, *dd);
}
mDevtoolsClient->Run();
std::wstring message =
resourceLoader.GetString(L"devtoolsStatus/Running").c_str();
std::wstring formatted =
format(message, std::to_wstring(mDevtoolsPort).c_str());
std::wstring formatted = format(message, port.c_str());
DevtoolsStatusMessage().Text(formatted);
} else if (mDevtoolsStatus == DevtoolsStatus::Failed) {
DevtoolsStatusMessage().Text(
Expand All @@ -317,12 +358,12 @@ void BrowserPage::OnURLEdited(IInspectable const &,
void BrowserPage::OnMediaControlsPlayClicked(IInspectable const &,
RoutedEventArgs const &) {
servoControl().SendMediaSessionAction(
static_cast<int32_t>(servo::Servo::MediaSessionActionType::Play));
static_cast<int32_t>(Servo::MediaSessionActionType::Play));
}
void BrowserPage::OnMediaControlsPauseClicked(IInspectable const &,
RoutedEventArgs const &) {
servoControl().SendMediaSessionAction(
static_cast<int32_t>(servo::Servo::MediaSessionActionType::Pause));
static_cast<int32_t>(Servo::MediaSessionActionType::Pause));
}

} // namespace winrt::ServoApp::implementation
9 changes: 7 additions & 2 deletions support/hololens/ServoApp/BrowserPage.h
Expand Up @@ -5,18 +5,20 @@
#pragma once

#include "BrowserPage.g.h"
#include "ServoControl\ServoControl.h"
#include "ServoControl/ServoControl.h"
#include "Devtools/Client.h"

namespace winrt::ServoApp::implementation {

using namespace winrt::Windows;
using namespace winrt::Windows::Data::Json;
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::UI::Xaml;

static const hstring SERVO_SCHEME = L"fxr";
static const hstring SERVO_SCHEME_SLASH_SLASH = L"fxr://";

struct BrowserPage : BrowserPageT<BrowserPage> {
struct BrowserPage : BrowserPageT<BrowserPage>, public servo::DevtoolsDelegate {
public:
BrowserPage();

Expand All @@ -41,13 +43,16 @@ struct BrowserPage : BrowserPageT<BrowserPage> {
RoutedEventArgs const &);
void OnPrefererenceSearchboxEdited(IInspectable const &,
Input::KeyRoutedEventArgs const &);
void OnDevtoolsMessage(servo::DevtoolsMessageLevel, hstring, hstring);
void OnDevtoolsDetached();

private:
void UpdatePref(ServoApp::Pref, Controls::Control);
void BindServoEvents();
void BuildPrefList();
DevtoolsStatus mDevtoolsStatus = DevtoolsStatus::Stopped;
unsigned int mDevtoolsPort = 0;
std::unique_ptr<servo::DevtoolsClient> mDevtoolsClient;
};
} // namespace winrt::ServoApp::implementation

Expand Down
8 changes: 7 additions & 1 deletion support/hololens/ServoApp/BrowserPage.xaml
Expand Up @@ -130,7 +130,7 @@
</TextBox.KeyboardAccelerators>
</TextBox>
<StackPanel Orientation="Horizontal" Grid.Column="2">
<Button Style="{StaticResource NavigationBarButton}" x:Name="devtoolsButton" x:Uid="devtoolsButton" IsTabStop="true" Click="OnDevtoolsButtonClicked">
<Button Style="{StaticResource NavigationBarButton}" x:Name="devtoolsButton" IsEnabled="false" x:Uid="devtoolsButton" IsTabStop="true" Click="OnDevtoolsButtonClicked">
<Image Source="Assets/UI/devtools.png" Height="18"></Image>
</Button>
<ProgressRing x:Name="urlbarLoadingIndicator" Margin="10,0"/>
Expand All @@ -149,6 +149,12 @@
</Button>
</Grid>
</muxc:TabView.TabStripFooter>
<muxc:TabViewItem x:Uid="devtoolsTabConsole" IsClosable="False">
<ScrollViewer x:Name="DevtoolsConsoleScrollViewer" VerticalScrollMode="Enabled" HorizontalScrollMode="Enabled">
<RichTextBlock x:Name="DevtoolsConsoleOutput" FontSize="10" FontFamily="Consolas" LineHeight="14">
</RichTextBlock>
</ScrollViewer>
</muxc:TabViewItem>
<muxc:TabViewItem x:Uid="devtoolsTabServer" IsClosable="False">
<TextBlock x:Name="DevtoolsStatusMessage" Margin="10"></TextBlock>
</muxc:TabViewItem>
Expand Down

0 comments on commit bd8c7d6

Please sign in to comment.