Skip to content

Commit

Permalink
UWP: Add console input
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Jun 19, 2020
1 parent 9de955f commit c722b5f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 29 deletions.
9 changes: 9 additions & 0 deletions support/hololens/ServoApp/BrowserPage.cpp
Expand Up @@ -335,6 +335,15 @@ void BrowserPage::OnDevtoolsButtonClicked(IInspectable const &,
}
}

void BrowserPage::OnJSInputEdited(IInspectable const &,
Input::KeyRoutedEventArgs const &e) {
if (e.Key() == Windows::System::VirtualKey::Enter) {
auto input = JSInput().Text();
JSInput().Text(L"");
mDevtoolsClient->Evaluate(input);
}
}

void BrowserPage::OnURLEdited(IInspectable const &,
Input::KeyRoutedEventArgs const &e) {
if (e.Key() == Windows::System::VirtualKey::Enter) {
Expand Down
1 change: 1 addition & 0 deletions support/hololens/ServoApp/BrowserPage.h
Expand Up @@ -30,6 +30,7 @@ struct BrowserPage : BrowserPageT<BrowserPage>, public servo::DevtoolsDelegate {
void OnStopButtonClicked(IInspectable const &, RoutedEventArgs const &);
void OnHomeButtonClicked(IInspectable const &, RoutedEventArgs const &);
void OnDevtoolsButtonClicked(IInspectable const &, RoutedEventArgs const &);
void OnJSInputEdited(IInspectable const &, Input::KeyRoutedEventArgs const &);
void OnURLEdited(IInspectable const &, Input::KeyRoutedEventArgs const &);
void OnURLFocused(IInspectable const &);
void
Expand Down
63 changes: 35 additions & 28 deletions support/hololens/ServoApp/BrowserPage.xaml
Expand Up @@ -150,34 +150,41 @@
</Grid>
</muxc:TabView.TabStripFooter>
<muxc:TabViewItem x:Uid="devtoolsTabConsole" IsClosable="False">
<ListView ItemsSource="{x:Bind ConsoleLogs}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Vertical" ItemsUpdatingScrollMode="KeepLastItemInView"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:ConsoleLog">
<Grid Background="{x:Bind BgColor}" Padding="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Ellipse Width="8" Height="8" Fill="{x:Bind FgColor}" Grid.Column="0" Margin="10,0"/>
<TextBlock FontSize="12" Text="{x:Bind Body}" Grid.Column="1"/>
<TextBlock FontSize="12" Text="{x:Bind Source}" Foreground="Gray" Grid.Column="2" Margin="10,0"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView ItemsSource="{x:Bind ConsoleLogs}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Vertical" ItemsUpdatingScrollMode="KeepLastItemInView"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:ConsoleLog">
<Grid Background="{x:Bind BgColor}" Padding="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Ellipse Width="8" Height="8" Fill="{x:Bind FgColor}" Grid.Column="0" Margin="10,0"/>
<TextBlock FontFamily="Consolas" FontSize="12" Text="{x:Bind Body}" Grid.Column="1"/>
<TextBlock FontFamily="Consolas" FontSize="12" Text="{x:Bind Source}" Foreground="Gray" Grid.Column="2" Margin="10,0"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
<TextBox MinHeight="12" FontFamily="Consolas" FontSize="12" Grid.Row="1" IsTabStop="true" x:Name="JSInput" VerticalAlignment="Center" KeyUp="OnJSInputEdited" IsSpellCheckEnabled="False"/>
</Grid>
</muxc:TabViewItem>
<muxc:TabViewItem x:Uid="devtoolsTabServer" IsClosable="False">
<TextBlock x:Name="DevtoolsStatusMessage" Margin="10"></TextBlock>
Expand Down
42 changes: 41 additions & 1 deletion support/hololens/ServoApp/Devtools/Client.cpp
Expand Up @@ -42,6 +42,16 @@ void DevtoolsClient::Run() {
});
}

void DevtoolsClient::Evaluate(hstring code) {
if (!code.empty() && mConsoleActor.has_value()) {
JsonObject out;
out.Insert(L"to", *mConsoleActor);
out.Insert(L"type", JsonValue::CreateStringValue(L"evaluateJSAsync"));
out.Insert(L"text", JsonValue::CreateStringValue(code));
Send(out);
}
}

IAsyncAction DevtoolsClient::Loop() {
auto cancellation = co_await winrt::get_cancellation_token();
cancellation.callback([=] {
Expand Down Expand Up @@ -101,11 +111,12 @@ void DevtoolsClient::HandleMessage(JsonObject obj) {
if (tab.HasKey(L"actor")) {
// Attach to tab, and ask for cached messaged
JsonObject msg1;
mConsoleActor = tab.GetNamedValue(L"consoleActor");
msg1.Insert(L"to", tab.GetNamedValue(L"actor"));
msg1.Insert(L"type", JsonValue::CreateStringValue(L"attach"));
Send(msg1);
JsonObject msg2;
msg2.Insert(L"to", tab.GetNamedValue(L"consoleActor"));
msg2.Insert(L"to", *mConsoleActor);
msg2.Insert(L"type",
JsonValue::CreateStringValue(L"getCachedMessages"));
JsonArray types;
Expand All @@ -116,6 +127,12 @@ void DevtoolsClient::HandleMessage(JsonObject obj) {
return;
}
}
} else if (obj.HasKey(L"resultID")) {
// evaluateJSAsync response.
if (obj.GetNamedString(L"type", L"") == L"evaluationResult") {
HandleEvaluationResult(obj);
}
return;
} else if (obj.HasKey(L"type")) { // Not from root
if (obj.GetNamedString(L"type") == L"pageError") {
// Got a page error
Expand Down Expand Up @@ -196,6 +213,29 @@ void DevtoolsClient::HandlePageError(JsonObject message) {
mDelegate.OnDevtoolsMessage(level, source, body);
}

void DevtoolsClient::HandleEvaluationResult(JsonObject message) {
auto level = DevtoolsMessageLevel::None;
hstring body = L"";
if (message.HasKey(L"result")) {
auto value = message.GetNamedValue(L"result");
if (value.ValueType() == JsonValueType::Object) {
auto type = value.GetObject().GetNamedString(L"type");
if (type == L"undefined") {
body = L"undefined";
} else {
body = L"<object>";
}
} else {
body = value.Stringify();
}
} else if (message.GetNamedValue(L"exception").ValueType() !=
JsonValueType::Null) {
level = DevtoolsMessageLevel::Error;
body = message.GetNamedString(L"exceptionMessage", L"");
}
mDelegate.OnDevtoolsMessage(level, L"", body);
}

void DevtoolsClient::HandleConsoleMessage(JsonObject message) {
auto source = ParseSource(message);
auto level = ParseLevel(message);
Expand Down
3 changes: 3 additions & 0 deletions support/hololens/ServoApp/Devtools/Client.h
Expand Up @@ -26,6 +26,7 @@ class DevtoolsClient {
void Run();
void Stop();
void Send(JsonObject);
void Evaluate(hstring);

private:
hstring mPort;
Expand All @@ -46,6 +47,8 @@ class DevtoolsClient {
void HandlePageError(JsonObject);
void HandleConsoleMessage(JsonObject);
void HandleNonHandledMessage(JsonObject);
void HandleEvaluationResult(JsonObject);
std::optional<JsonValue> mConsoleActor;
};

class DevtoolsDelegate {
Expand Down

0 comments on commit c722b5f

Please sign in to comment.