| // Copyright 2018 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #![feature(async_await, await_macro, futures_api)] |
| |
| use carnelian::{App, AppAssistant, ViewAssistant, ViewAssistantContext, ViewAssistantPtr}; |
| use failure::{Error, ResultExt}; |
| use fidl::endpoints::{RequestStream, ServiceMarker}; |
| use fidl_fidl_examples_echo::{EchoMarker, EchoRequest, EchoRequestStream}; |
| use fidl_fuchsia_ui_gfx::{self as gfx, ColorRgba}; |
| use fuchsia_async as fasync; |
| use fuchsia_scenic::{Material, Rectangle, SessionPtr, ShapeNode}; |
| use futures::prelude::*; |
| use parking_lot::Mutex; |
| use std::{any::Any, cell::RefCell}; |
| |
| struct EchoAppAssistant {} |
| |
| impl AppAssistant for EchoAppAssistant { |
| fn setup(&mut self) -> Result<(), Error> { |
| Ok(()) |
| } |
| |
| fn create_view_assistant(&mut self, session: &SessionPtr) -> Result<ViewAssistantPtr, Error> { |
| Ok(Mutex::new(RefCell::new(Box::new(EchoViewAssistant { |
| background_node: ShapeNode::new(session.clone()), |
| })))) |
| } |
| |
| fn get_published_services_list(&self) -> Vec<&'static str> { |
| vec![EchoMarker::NAME] |
| } |
| |
| fn handle_service_request( |
| &mut self, service_name: &str, channel: fasync::Channel, |
| ) -> Result<(), Error> { |
| if service_name == EchoMarker::NAME { |
| fasync::spawn( |
| async move { |
| let mut stream = EchoRequestStream::from_channel(channel); |
| while let Some(EchoRequest::EchoString { value, responder }) = |
| await!(stream.try_next()).context("error running echo server")? |
| { |
| // TODO: Also display this in the view |
| responder |
| .send(value.as_ref().map(|s| &**s)) |
| .context("error sending response")?; |
| } |
| Ok(()) |
| } |
| .unwrap_or_else(|e: failure::Error| eprintln!("{:?}", e)), |
| ); |
| } |
| Ok(()) |
| } |
| } |
| |
| struct EchoViewAssistant { |
| background_node: ShapeNode, |
| } |
| |
| impl EchoViewAssistant {} |
| |
| impl ViewAssistant for EchoViewAssistant { |
| fn setup(&mut self, context: &ViewAssistantContext) -> Result<(), Error> { |
| context |
| .import_node |
| .resource() |
| .set_event_mask(gfx::METRICS_EVENT_MASK); |
| context.import_node.add_child(&self.background_node); |
| let material = Material::new(context.session.clone()); |
| material.set_color(ColorRgba { |
| red: 0xb7, |
| green: 0x41, |
| blue: 0x0e, |
| alpha: 0xff, |
| }); |
| self.background_node.set_material(&material); |
| |
| Ok(()) |
| } |
| |
| fn update(&mut self, context: &ViewAssistantContext) -> Result<(), Error> { |
| let center_x = context.width * 0.5; |
| let center_y = context.height * 0.5; |
| self.background_node.set_shape(&Rectangle::new( |
| context.session.clone(), |
| context.width, |
| context.height, |
| )); |
| self.background_node |
| .set_translation(center_x, center_y, 0.0); |
| Ok(()) |
| } |
| |
| fn handle_message(&mut self, _message: &Any) { |
| // If Echo had any custom messages they |
| // would be handled here. |
| } |
| } |
| |
| fn main() -> Result<(), Error> { |
| let assistant = EchoAppAssistant {}; |
| App::run(Box::new(assistant)) |
| } |