From 67bcb96ceab4914e6ea9d795a0dbad7e69980856 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 15 Jul 2016 20:23:20 +0530 Subject: [PATCH] Add glue for calc values --- components/style/gecko_conversions.rs | 36 +++++++++++++++++++ components/style/lib.rs | 2 ++ ports/geckolib/gecko_bindings/sugar/mod.rs | 1 + .../gecko_bindings/sugar/ns_style_coord.rs | 34 ++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 components/style/gecko_conversions.rs create mode 100644 ports/geckolib/gecko_bindings/sugar/ns_style_coord.rs diff --git a/components/style/gecko_conversions.rs b/components/style/gecko_conversions.rs new file mode 100644 index 00000000000..26ef5b6e0be --- /dev/null +++ b/components/style/gecko_conversions.rs @@ -0,0 +1,36 @@ +/* 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/. */ + +//! This module contains conversion helpers between Servo and Gecko types +//! Ideally, it would be in geckolib itself, but coherence +//! forces us to keep the traits and implementations here + +use app_units::Au; +use gecko_bindings::structs::nsStyleCoord_CalcValue; +use values::computed::CalcLengthOrPercentage; + +impl From for nsStyleCoord_CalcValue { + fn from(other: CalcLengthOrPercentage) -> nsStyleCoord_CalcValue { + let has_percentage = other.percentage.is_some(); + nsStyleCoord_CalcValue { + mLength: other.length.map(|l| l.0).unwrap_or(0), + mPercent: other.percentage.unwrap_or(0.0), + mHasPercent: has_percentage, + } + } +} + +impl From for CalcLengthOrPercentage { + fn from(other: nsStyleCoord_CalcValue) -> CalcLengthOrPercentage { + let percentage = if other.mHasPercent { + Some(other.mPercent) + } else { + None + }; + CalcLengthOrPercentage { + length: Some(Au(other.mLength)), + percentage: percentage, + } + } +} \ No newline at end of file diff --git a/components/style/lib.rs b/components/style/lib.rs index 813a24af14e..c3c99c7468e 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -80,6 +80,8 @@ pub mod data; pub mod dom; pub mod element_state; pub mod error_reporting; +#[cfg(feature = "gecko")] +pub mod gecko_conversions; pub mod font_face; pub mod keyframes; pub mod logical_geometry; diff --git a/ports/geckolib/gecko_bindings/sugar/mod.rs b/ports/geckolib/gecko_bindings/sugar/mod.rs index 9c1e4637249..fa995c3f963 100644 --- a/ports/geckolib/gecko_bindings/sugar/mod.rs +++ b/ports/geckolib/gecko_bindings/sugar/mod.rs @@ -3,4 +3,5 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ mod ns_style_auto_array; +mod ns_style_coord; mod ns_t_array; diff --git a/ports/geckolib/gecko_bindings/sugar/ns_style_coord.rs b/ports/geckolib/gecko_bindings/sugar/ns_style_coord.rs new file mode 100644 index 00000000000..d24fa10ff45 --- /dev/null +++ b/ports/geckolib/gecko_bindings/sugar/ns_style_coord.rs @@ -0,0 +1,34 @@ +/* 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/. */ + +use structs::{nsStyleCoord_CalcValue, nsStyleCoord_Calc, nsStyleUnit, nsStyleUnion}; +use bindings::{Gecko_ResetStyleCoord, Gecko_SetStyleCoordCalcValue}; + +// Functions here are unsafe because it is possible to use the wrong nsStyleUnit +// FIXME we should be pairing up nsStyleUnion and nsStyleUnit somehow +// nsStyleCoord is one way to do it, but there are other structs using pairs +// of union and unit too + +impl nsStyleUnion { + /// Clean up any resources used by an nsStyleUnit + /// Currently, this only happens if the nsStyleUnit + /// is a Calc + pub unsafe fn reset(&mut self, unit: &mut nsStyleUnit) { + if *unit == nsStyleUnit::eStyleUnit_Calc { + Gecko_ResetStyleCoord(unit, self); + } + } + + /// Set internal value to a calc() value + /// reset() the union before calling this + pub unsafe fn set_calc_value(&mut self, unit: &mut nsStyleUnit, v: nsStyleCoord_CalcValue) { + // Calc should have been cleaned up + assert!(*unit != nsStyleUnit::eStyleUnit_Calc); + Gecko_SetStyleCoordCalcValue(unit, self, v); + } + + pub unsafe fn get_calc(&self) -> nsStyleCoord_CalcValue { + (*(*self.mPointer.as_ref() as *const nsStyleCoord_Calc))._base + } +}