github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/runtime/ppapi/view_nacl.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ppapi
     6  
     7  import (
     8  	"errors"
     9  )
    10  
    11  var (
    12  	errViewGetRectFailed     = errors.New("View.GetRect operation failed")
    13  	errViewGetClipRectFailed = errors.New("View.GetClipRect operation failed")
    14  )
    15  
    16  // View represents the state of the view of an instance.
    17  //
    18  // You receive new view information through the Instance.DidChangeView callback.
    19  type View struct {
    20  	Resource
    21  }
    22  
    23  func makeView(id pp_Resource) (view View) {
    24  	view.id = id
    25  	return
    26  }
    27  
    28  // IsFullscreen returns true iff the view is currently full screen.
    29  func (v View) IsFullscreen() bool {
    30  	ok := ppb_view_is_fullscreen(v.id)
    31  	return ok != ppFalse
    32  }
    33  
    34  // IsVisible determines whether the module instance might be visible to the
    35  // user.
    36  //
    37  // For example, the Chrome window could be minimized or another window could be
    38  // over it. In both of these cases, the module instance would not be visible to
    39  // the user, but IsVisible() will return true.
    40  //
    41  // Use the result to speed up or stop updates for invisible module instances.
    42  //
    43  // This function performs the duties of GetRect() (determining whether the
    44  // module instance is scrolled into view and the clip rectangle is nonempty) and
    45  // IsPageVisible() (whether the page is visible to the user).
    46  func (v View) IsVisible() bool {
    47  	ok := ppb_view_is_visible(v.id)
    48  	return ok != ppFalse
    49  }
    50  
    51  // IsPageVisible determines if the page that contains the module instance is
    52  // visible.
    53  //
    54  // The most common cause of invisible pages is that the page is in a background
    55  // tab in the browser.
    56  //
    57  // Most applications should use IsVisible() instead of this function since the
    58  // module instance could be scrolled off of a visible page, and this function
    59  // will still return true. However, depending on how your module interacts with
    60  // the page, there may be certain updates that you may want to perform when the
    61  // page is visible even if your specific module instance is not visible.
    62  func (v View) IsPageVisible() bool {
    63  	ok := ppb_view_is_page_visible(v.id)
    64  	return ok != ppFalse
    65  }
    66  
    67  // GetRect retrieves the rectangle of the module instance associated with a view
    68  // changed notification relative to the upper-left of the browser viewport.
    69  //
    70  // This position changes when the page is scrolled.
    71  //
    72  // The returned rectangle may not be inside the visible portion of the viewport
    73  // if the module instance is scrolled off the page. Therefore, the position may
    74  // be negative or larger than the size of the page. The size will always reflect
    75  // the size of the module were it to be scrolled entirely into view.
    76  //
    77  // In general, most modules will not need to worry about the position of the
    78  // module instance in the viewport, and only need to use the size.
    79  func (v View) GetRect() (rect Rect, err error) {
    80  	ok := ppb_view_get_rect(v.id, &rect)
    81  	if ok != ppTrue {
    82  		err = errViewGetRectFailed
    83  	}
    84  	return
    85  }
    86  
    87  // GetClipRect returns the clip rectangle relative to the upper-left corner of
    88  // the module instance.
    89  //
    90  // This rectangle indicates the portions of the module instance that are
    91  // scrolled into view.
    92  //
    93  // If the module instance is scrolled off the view, the return value will be (0,
    94  // 0, 0, 0). This clip rectangle does not take into account page
    95  // visibility. Therefore, if the module instance is scrolled into view, but the
    96  // page itself is on a tab that is not visible, the return rectangle will
    97  // contain the visible rectangle as though the page were visible. Refer to
    98  // IsPageVisible() and IsVisible() if you want to account for page visibility.
    99  //
   100  // Most applications will not need to worry about the clip rectangle. The
   101  // recommended behavior is to do full updates if the module instance is visible,
   102  // as determined by IsVisible(), and do no updates if it is not visible.
   103  //
   104  // However, if the cost for computing pixels is very high for your application,
   105  // or the pages you're targeting frequently have very large module instances
   106  // with small visible portions, you may wish to optimize further. In this case,
   107  // the clip rectangle will tell you which parts of the module to update.
   108  //
   109  // Note that painting of the page and sending of view changed updates happens
   110  // asynchronously. This means when the user scrolls, for example, it is likely
   111  // that the previous backing store of the module instance will be used for the
   112  // first paint, and will be updated later when your application generates new
   113  // content with the new clip. This may cause flickering at the boundaries when
   114  // scrolling. If you do choose to do partial updates, you may want to think
   115  // about what color the invisible portions of your backing store contain (be it
   116  // transparent or some background color) or to paint a certain region outside
   117  // the clip to reduce the visual distraction when this happens.
   118  func (v View) GetClipRect() (rect Rect, err error) {
   119  	ok := ppb_view_get_clip_rect(v.id, &rect)
   120  	if ok != ppTrue {
   121  		err = errViewGetClipRectFailed
   122  	}
   123  	return
   124  }
   125  
   126  // GetCSSScale returns the scale factor between DIPs and CSS pixels.
   127  //
   128  // This allows proper scaling between DIPs - as sent via the Pepper API - and
   129  // CSS pixel coordinates used for Web content.
   130  func (v View) GetCSSScale() float32 {
   131  	return ppb_view_get_css_scale(v.id)
   132  }
   133  
   134  // GetDeviceScale returns the scale factor between device pixels and Density
   135  // Independent Pixels (DIPs, also known as logical pixels or UI pixels on some
   136  // platforms).
   137  //
   138  // This allows the developer to render their contents at device resolution, even
   139  // as coordinates / sizes are given in DIPs through the API.
   140  //
   141  // Note that the coordinate system for Pepper APIs is DIPs. Also note that one
   142  // DIP might not equal one CSS pixel - when page scale/zoom is in effect.
   143  func (v View) GetDeviceScale() float32 {
   144  	return ppb_view_get_device_scale(v.id)
   145  }