Skip to content

Commit

Permalink
Extract OpaqueNodeMethods to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdanfox committed Mar 3, 2015
1 parent 417a932 commit b424de2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 62 deletions.
3 changes: 2 additions & 1 deletion components/layout/construct.rs
Expand Up @@ -41,7 +41,8 @@ use table_rowgroup::TableRowGroupFlow;
use table_row::TableRowFlow;
use table_cell::TableCellFlow;
use text::TextRunScanner;
use util::{HAS_NEWLY_CONSTRUCTED_FLOW, LayoutDataAccess, OpaqueNodeMethods, LayoutDataWrapper};
use util::{HAS_NEWLY_CONSTRUCTED_FLOW, LayoutDataAccess, LayoutDataWrapper};
use opaque_node::OpaqueNodeMethods;
use wrapper::{PostorderNodeMutTraversal, PseudoElementType, TLayoutNode, ThreadSafeLayoutNode};

use gfx::display_list::OpaqueNode;
Expand Down
3 changes: 2 additions & 1 deletion components/layout/display_list_builder.rs
Expand Up @@ -19,7 +19,8 @@ use fragment::{ScannedTextFragmentInfo, SpecificFragmentInfo};
use inline::InlineFlow;
use list_item::ListItemFlow;
use model;
use util::{OpaqueNodeMethods, ToGfxColor};
use util::ToGfxColor;
use opaque_node::OpaqueNodeMethods;

use geom::{Point2D, Rect, Size2D, SideOffsets2D};
use gfx::color;
Expand Down
2 changes: 1 addition & 1 deletion components/layout/fragment.rs
Expand Up @@ -20,7 +20,7 @@ use layout_debug;
use model::{IntrinsicISizes, IntrinsicISizesContribution, MaybeAuto, specified};
use model;
use text;
use util::OpaqueNodeMethods;
use opaque_node::OpaqueNodeMethods;
use wrapper::{TLayoutNode, ThreadSafeLayoutNode};

use geom::num::Zero;
Expand Down
3 changes: 2 additions & 1 deletion components/layout/layout_task.rs
Expand Up @@ -17,7 +17,8 @@ use incremental::{LayoutDamageComputation, REFLOW, REFLOW_ENTIRE_DOCUMENT, REPAI
use layout_debug;
use parallel::{self, UnsafeFlow};
use sequential;
use util::{LayoutDataAccess, LayoutDataWrapper, OpaqueNodeMethods, ToGfxColor};
use util::{LayoutDataAccess, LayoutDataWrapper, ToGfxColor};
use opaque_node::OpaqueNodeMethods;
use wrapper::{LayoutNode, TLayoutNode, ThreadSafeLayoutNode};

use encoding::EncodingRef;
Expand Down
1 change: 1 addition & 0 deletions components/layout/lib.rs
Expand Up @@ -70,6 +70,7 @@ pub mod layout_task;
pub mod inline;
pub mod list_item;
pub mod model;
pub mod opaque_node;
pub mod parallel;
pub mod sequential;
pub mod table_wrapper;
Expand Down
63 changes: 63 additions & 0 deletions components/layout/opaque_node.rs
@@ -0,0 +1,63 @@
/* 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 http://mozilla.org/MPL/2.0/. */

#![allow(unsafe_blocks)]

use gfx::display_list::OpaqueNode;
use libc::{c_void, uintptr_t};
use script::dom::bindings::js::LayoutJS;
use script::dom::node::Node;
use script::layout_interface::{TrustedNodeAddress};
use script_traits::UntrustedNodeAddress;
use wrapper::{LayoutNode, TLayoutNode, ThreadSafeLayoutNode};

pub trait OpaqueNodeMethods {
/// Converts a DOM node (layout view) to an `OpaqueNode`.
fn from_layout_node(node: &LayoutNode) -> Self;

/// Converts a thread-safe DOM node (layout view) to an `OpaqueNode`.
fn from_thread_safe_layout_node(node: &ThreadSafeLayoutNode) -> Self;

/// Converts a DOM node (script view) to an `OpaqueNode`.
fn from_script_node(node: TrustedNodeAddress) -> Self;

/// Converts a DOM node to an `OpaqueNode'.
fn from_jsmanaged(node: &LayoutJS<Node>) -> Self;

/// Converts this node to an `UntrustedNodeAddress`. An `UntrustedNodeAddress` is just the type
/// of node that script expects to receive in a hit test.
fn to_untrusted_node_address(&self) -> UntrustedNodeAddress;
}

impl OpaqueNodeMethods for OpaqueNode {
fn from_layout_node(node: &LayoutNode) -> OpaqueNode {
unsafe {
OpaqueNodeMethods::from_jsmanaged(node.get_jsmanaged())
}
}

fn from_thread_safe_layout_node(node: &ThreadSafeLayoutNode) -> OpaqueNode {
unsafe {
OpaqueNodeMethods::from_jsmanaged(node.get_jsmanaged())
}
}

fn from_script_node(node: TrustedNodeAddress) -> OpaqueNode {
unsafe {
OpaqueNodeMethods::from_jsmanaged(&LayoutJS::from_trusted_node_address(node))
}
}

fn from_jsmanaged(node: &LayoutJS<Node>) -> OpaqueNode {
unsafe {
let ptr: uintptr_t = node.get_jsobject() as uintptr_t;
OpaqueNode(ptr)
}
}

fn to_untrusted_node_address(&self) -> UntrustedNodeAddress {
let OpaqueNode(addr) = *self;
UntrustedNodeAddress(addr as *const c_void)
}
}
60 changes: 3 additions & 57 deletions components/layout/util.rs
Expand Up @@ -7,16 +7,12 @@
use construct::ConstructionResult;
use incremental::RestyleDamage;
use parallel::DomParallelInfo;
use wrapper::{LayoutNode, TLayoutNode, ThreadSafeLayoutNode};
use wrapper::{LayoutNode, TLayoutNode};

use azure::azure_hl::Color;
use gfx::display_list::OpaqueNode;
use gfx;
use libc::{c_void, uintptr_t};
use script::dom::bindings::js::LayoutJS;
use script::dom::node::{Node, SharedLayoutData};
use script::layout_interface::{LayoutChan, TrustedNodeAddress};
use script_traits::UntrustedNodeAddress;
use script::dom::node::SharedLayoutData;
use script::layout_interface::LayoutChan;
use std::mem;
use std::cell::{Ref, RefMut};
use style::properties::ComputedValues;
Expand Down Expand Up @@ -123,56 +119,6 @@ impl<'ln> LayoutDataAccess for LayoutNode<'ln> {
}
}

pub trait OpaqueNodeMethods {
/// Converts a DOM node (layout view) to an `OpaqueNode`.
fn from_layout_node(node: &LayoutNode) -> Self;

/// Converts a thread-safe DOM node (layout view) to an `OpaqueNode`.
fn from_thread_safe_layout_node(node: &ThreadSafeLayoutNode) -> Self;

/// Converts a DOM node (script view) to an `OpaqueNode`.
fn from_script_node(node: TrustedNodeAddress) -> Self;

/// Converts a DOM node to an `OpaqueNode'.
fn from_jsmanaged(node: &LayoutJS<Node>) -> Self;

/// Converts this node to an `UntrustedNodeAddress`. An `UntrustedNodeAddress` is just the type
/// of node that script expects to receive in a hit test.
fn to_untrusted_node_address(&self) -> UntrustedNodeAddress;
}

impl OpaqueNodeMethods for OpaqueNode {
fn from_layout_node(node: &LayoutNode) -> OpaqueNode {
unsafe {
OpaqueNodeMethods::from_jsmanaged(node.get_jsmanaged())
}
}

fn from_thread_safe_layout_node(node: &ThreadSafeLayoutNode) -> OpaqueNode {
unsafe {
OpaqueNodeMethods::from_jsmanaged(node.get_jsmanaged())
}
}

fn from_script_node(node: TrustedNodeAddress) -> OpaqueNode {
unsafe {
OpaqueNodeMethods::from_jsmanaged(&LayoutJS::from_trusted_node_address(node))
}
}

fn from_jsmanaged(node: &LayoutJS<Node>) -> OpaqueNode {
unsafe {
let ptr: uintptr_t = node.get_jsobject() as uintptr_t;
OpaqueNode(ptr)
}
}

fn to_untrusted_node_address(&self) -> UntrustedNodeAddress {
let OpaqueNode(addr) = *self;
UntrustedNodeAddress(addr as *const c_void)
}
}

/// Allows a CSS color to be converted into a graphics color.
pub trait ToGfxColor {
/// Converts a CSS color to a graphics color.
Expand Down
3 changes: 2 additions & 1 deletion components/layout/wrapper.rs
Expand Up @@ -36,8 +36,9 @@ use canvas::canvas_paint_task::CanvasMsg;
use context::SharedLayoutContext;
use css::node_style::StyledNode;
use incremental::RestyleDamage;
use util::{LayoutDataAccess, LayoutDataFlags, LayoutDataWrapper, OpaqueNodeMethods};
use util::{LayoutDataAccess, LayoutDataFlags, LayoutDataWrapper};
use util::{PrivateLayoutData};
use opaque_node::OpaqueNodeMethods;

use cssparser::RGBA;
use gfx::display_list::OpaqueNode;
Expand Down

0 comments on commit b424de2

Please sign in to comment.